2019-01-29 10:30:11 -08:00
|
|
|
/**
|
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the LICENSE
|
|
|
|
* file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <yoga/Yoga.h>
|
|
|
|
#include <yoga/YGMarker.h>
|
|
|
|
#include <yoga/instrumentation.h>
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <tuple>
|
|
|
|
|
2019-01-29 10:30:14 -08:00
|
|
|
bool operator==(const YGMarkerLayoutData&, const YGMarkerLayoutData&);
|
|
|
|
void PrintTo(const YGMarkerLayoutData, std::ostream*);
|
|
|
|
|
2019-01-29 10:30:11 -08:00
|
|
|
namespace facebook {
|
|
|
|
namespace yoga {
|
|
|
|
namespace marker {
|
|
|
|
namespace test {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using uniquePtr = std::unique_ptr<T, std::function<void(T*)>>;
|
|
|
|
|
|
|
|
struct MarkerTest : public ::testing::Test {
|
|
|
|
struct Data {
|
|
|
|
YGMarker marker;
|
|
|
|
YGNodeRef node;
|
|
|
|
YGMarkerData markerData;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EndData {
|
|
|
|
Data data;
|
|
|
|
void* cookie;
|
2019-01-29 10:30:11 -08:00
|
|
|
union {
|
|
|
|
YGMarkerLayoutData layout;
|
|
|
|
} markerData;
|
2019-01-29 10:30:11 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
Data start;
|
|
|
|
EndData end;
|
|
|
|
} markerCookie;
|
|
|
|
|
|
|
|
static void* startMarker(YGMarker, YGNodeRef, YGMarkerData);
|
|
|
|
static void endMarker(YGMarker, YGNodeRef, YGMarkerData, void*);
|
2019-01-29 10:30:14 -08:00
|
|
|
uniquePtr<YGNode> makeNode();
|
|
|
|
uniquePtr<YGNode> addChild(uniquePtr<YGNode>& owner);
|
|
|
|
static void calculateLayout(
|
|
|
|
uniquePtr<YGNode>& node,
|
|
|
|
float width = YGUndefined,
|
|
|
|
float height = YGUndefined);
|
2019-01-29 10:30:11 -08:00
|
|
|
|
|
|
|
void SetUp() override;
|
2019-01-29 10:30:14 -08:00
|
|
|
uniquePtr<YGConfig> config;
|
2019-01-29 10:30:11 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(MarkerTest, marker_start_works) {
|
2019-01-29 10:30:14 -08:00
|
|
|
auto root = makeNode();
|
2019-01-29 10:30:11 -08:00
|
|
|
|
|
|
|
decltype(MarkerSection<YGMarkerLayout>::data)* dataAddress;
|
|
|
|
{
|
|
|
|
MarkerSection<YGMarkerLayout> marker{root.get()};
|
|
|
|
dataAddress = &marker.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_EQ(markerCookie.start.marker, YGMarkerLayout)
|
|
|
|
<< "wrong marker type passed to `startMarker`";
|
|
|
|
ASSERT_EQ(markerCookie.start.node, root.get())
|
|
|
|
<< "wrong node pointer passed to `startMarker`";
|
|
|
|
ASSERT_EQ(markerCookie.start.markerData.layout, dataAddress)
|
|
|
|
<< "wrong pointer to marker data passed to `startMarker`";
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MarkerTest, marker_end_works) {
|
2019-01-29 10:30:14 -08:00
|
|
|
auto root = makeNode();
|
2019-01-29 10:30:11 -08:00
|
|
|
|
|
|
|
{ MarkerSection<YGMarkerLayout> marker{root.get()}; }
|
|
|
|
|
|
|
|
ASSERT_EQ(markerCookie.end.data.marker, markerCookie.start.marker)
|
|
|
|
<< "marker type passed to `endMarker` differs from type passed to "
|
|
|
|
"`startMarker`";
|
|
|
|
ASSERT_EQ(markerCookie.end.data.node, markerCookie.start.node)
|
|
|
|
<< "node passed to `endMarker` differs from node passed to `startMarker`";
|
|
|
|
ASSERT_EQ(
|
|
|
|
markerCookie.end.data.markerData.layout,
|
|
|
|
markerCookie.start.markerData.layout)
|
|
|
|
<< "marker data pointer passed to `endMarker` differs from pointer "
|
|
|
|
"passed to `startMarker`";
|
|
|
|
ASSERT_EQ(markerCookie.end.cookie, &markerCookie)
|
|
|
|
<< "pointer returned by `startMarker` was not passed to `endMarker`";
|
|
|
|
}
|
|
|
|
|
2019-01-29 10:30:11 -08:00
|
|
|
TEST_F(MarkerTest, layout_marker) {
|
2019-01-29 10:30:14 -08:00
|
|
|
auto root = makeNode();
|
2019-01-29 10:30:11 -08:00
|
|
|
|
2019-01-29 10:30:14 -08:00
|
|
|
calculateLayout(root);
|
2019-01-29 10:30:11 -08:00
|
|
|
|
|
|
|
ASSERT_EQ(markerCookie.start.marker, YGMarkerLayout);
|
|
|
|
ASSERT_EQ(markerCookie.start.node, root.get());
|
|
|
|
}
|
|
|
|
|
2019-01-29 10:30:11 -08:00
|
|
|
TEST_F(MarkerTest, layout_marker_counts_single_node_layout) {
|
2019-01-29 10:30:14 -08:00
|
|
|
auto root = makeNode();
|
2019-01-29 10:30:11 -08:00
|
|
|
|
2019-01-29 10:30:14 -08:00
|
|
|
calculateLayout(root);
|
2019-01-29 10:30:11 -08:00
|
|
|
|
2019-01-29 10:30:14 -08:00
|
|
|
ASSERT_EQ(
|
|
|
|
markerCookie.end.markerData.layout,
|
|
|
|
(YGMarkerLayoutData{.layouts = 1, .measures = 0}));
|
2019-01-29 10:30:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MarkerTest, layout_marker_counts_multi_node_layout) {
|
2019-01-29 10:30:14 -08:00
|
|
|
auto root = makeNode();
|
|
|
|
auto childA = addChild(root);
|
|
|
|
auto childB = addChild(root);
|
2019-01-29 10:30:11 -08:00
|
|
|
|
2019-01-29 10:30:14 -08:00
|
|
|
calculateLayout(root);
|
2019-01-29 10:30:11 -08:00
|
|
|
|
2019-01-29 10:30:14 -08:00
|
|
|
ASSERT_EQ(
|
|
|
|
markerCookie.end.markerData.layout,
|
|
|
|
(YGMarkerLayoutData{.layouts = 3, .measures = 4}));
|
2019-01-29 10:30:11 -08:00
|
|
|
}
|
|
|
|
|
2019-01-29 10:30:11 -08:00
|
|
|
void* MarkerTest::startMarker(
|
|
|
|
YGMarker marker,
|
|
|
|
YGNodeRef node,
|
|
|
|
YGMarkerData data) {
|
|
|
|
markerCookie.start = {marker, node, data};
|
|
|
|
return &markerCookie;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MarkerTest::endMarker(
|
|
|
|
YGMarker marker,
|
|
|
|
YGNodeRef node,
|
|
|
|
YGMarkerData data,
|
|
|
|
void* id) {
|
2019-01-29 10:30:11 -08:00
|
|
|
markerCookie.end = {{marker, node, data}, id, {}};
|
|
|
|
switch (marker) {
|
|
|
|
case YGMarkerLayout:
|
|
|
|
markerCookie.end.markerData.layout = *marker::data<YGMarkerLayout>(data);
|
|
|
|
break;
|
|
|
|
};
|
2019-01-29 10:30:11 -08:00
|
|
|
}
|
|
|
|
|
2019-01-29 10:30:14 -08:00
|
|
|
uniquePtr<YGNode> MarkerTest::makeNode() {
|
2019-01-29 10:30:11 -08:00
|
|
|
auto n = uniquePtr<YGNode>{YGNodeNewWithConfig(config.get()), &YGNodeFree};
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2019-01-29 10:30:14 -08:00
|
|
|
uniquePtr<YGNode> MarkerTest::addChild(uniquePtr<YGNode>& owner) {
|
|
|
|
auto n = makeNode();
|
2019-01-29 10:30:11 -08:00
|
|
|
YGNodeInsertChild(owner.get(), n.get(), YGNodeGetChildCount(owner.get()));
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2019-01-29 10:30:14 -08:00
|
|
|
void MarkerTest::calculateLayout(
|
|
|
|
uniquePtr<YGNode>& node,
|
|
|
|
float width,
|
|
|
|
float height) {
|
|
|
|
YGNodeCalculateLayout(node.get(), width, height, YGDirectionLTR);
|
|
|
|
}
|
|
|
|
|
2019-01-29 10:30:11 -08:00
|
|
|
void MarkerTest::SetUp() {
|
|
|
|
markerCookie = {};
|
2019-01-29 10:30:14 -08:00
|
|
|
|
|
|
|
config = uniquePtr<YGConfig>{YGConfigNew(), &YGConfigFree};
|
|
|
|
YGConfigSetMarkerCallbacks(config.get(), {startMarker, endMarker});
|
2019-01-29 10:30:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
decltype(MarkerTest::markerCookie) MarkerTest::markerCookie = {};
|
|
|
|
|
|
|
|
} // namespace test
|
|
|
|
} // namespace marker
|
|
|
|
} // namespace yoga
|
|
|
|
} // namespace facebook
|
2019-01-29 10:30:14 -08:00
|
|
|
|
|
|
|
bool operator==(const YGMarkerLayoutData& lhs, const YGMarkerLayoutData& rhs) {
|
|
|
|
return lhs.layouts == rhs.layouts && lhs.measures == rhs.measures;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintTo(const YGMarkerLayoutData data, std::ostream* os) {
|
|
|
|
*os << "YGMarkerLayoutData{ layouts = " << data.layouts
|
|
|
|
<< ", measures = " << data.measures << " }";
|
|
|
|
}
|