Publish event when visiting nodes during layout

Summary:
@public

Publish an event when visiting nodes during layouts.

Reviewed By: SidharthGuglani

Differential Revision: D15206965

fbshipit-source-id: c201f084b1d4186bc64560b8033be965f2549236
This commit is contained in:
David Aurelio
2019-05-09 04:14:08 -07:00
committed by Facebook Github Bot
parent e96a09e5ff
commit 5824dbda66
3 changed files with 47 additions and 17 deletions

View File

@@ -10,8 +10,10 @@
#include <yoga/YGNode.h>
#include <yoga/testutil/testutil.h>
#include <algorithm>
#include <functional>
#include <memory>
#include <vector>
namespace facebook {
namespace yoga {
@@ -33,16 +35,18 @@ class EventTest : public ::testing::Test {
static void listen(const YGNode&, Event::Type, Event::Data);
public:
static EventArgs lastEvent;
static std::vector<EventArgs> events;
static EventArgs& lastEvent() { return events.back(); }
void TearDown() override;
};
TEST_F(EventTest, new_node_has_event) {
auto c = YGConfigGetDefault();
auto n = YGNodeNew();
ASSERT_EQ(lastEvent.node, n);
ASSERT_EQ(lastEvent.type, Event::NodeAllocation);
ASSERT_EQ(lastEvent.data<Event::NodeAllocation>().config, c);
ASSERT_EQ(lastEvent().node, n);
ASSERT_EQ(lastEvent().type, Event::NodeAllocation);
ASSERT_EQ(lastEvent().data<Event::NodeAllocation>().config, c);
YGNodeFree(n);
}
@@ -51,9 +55,9 @@ TEST_F(EventTest, new_node_with_config_event) {
auto c = YGConfigNew();
auto n = YGNodeNewWithConfig(c);
ASSERT_EQ(lastEvent.node, n);
ASSERT_EQ(lastEvent.type, Event::NodeAllocation);
ASSERT_EQ(lastEvent.data<Event::NodeAllocation>().config, c);
ASSERT_EQ(lastEvent().node, n);
ASSERT_EQ(lastEvent().type, Event::NodeAllocation);
ASSERT_EQ(lastEvent().data<Event::NodeAllocation>().config, c);
YGNodeFree(n);
YGConfigFree(c);
@@ -64,9 +68,9 @@ TEST_F(EventTest, clone_node_event) {
auto n = YGNodeNewWithConfig(c);
auto clone = YGNodeClone(n);
ASSERT_EQ(lastEvent.node, clone);
ASSERT_EQ(lastEvent.type, Event::NodeAllocation);
ASSERT_EQ(lastEvent.data<Event::NodeAllocation>().config, c);
ASSERT_EQ(lastEvent().node, clone);
ASSERT_EQ(lastEvent().type, Event::NodeAllocation);
ASSERT_EQ(lastEvent().data<Event::NodeAllocation>().config, c);
YGNodeFree(n);
YGNodeFree(clone);
@@ -78,13 +82,29 @@ TEST_F(EventTest, free_node_event) {
auto n = YGNodeNewWithConfig(c);
YGNodeFree(n);
ASSERT_EQ(lastEvent.node, n);
ASSERT_EQ(lastEvent.type, Event::NodeDeallocation);
ASSERT_EQ(lastEvent.data<Event::NodeDeallocation>().config, c);
ASSERT_EQ(lastEvent().node, n);
ASSERT_EQ(lastEvent().type, Event::NodeDeallocation);
ASSERT_EQ(lastEvent().data<Event::NodeDeallocation>().config, c);
YGConfigFree(c);
}
TEST_F(EventTest, layout_events) {
auto root = YGNodeNew();
auto child = YGNodeNew();
YGNodeInsertChild(root, child, 0);
YGNodeCalculateLayout(root, 123, 456, YGDirectionLTR);
ASSERT_EQ(events[2].node, root);
ASSERT_EQ(events[2].type, Event::NodeLayout);
ASSERT_EQ(events[3].node, child);
ASSERT_EQ(events[3].type, Event::NodeLayout);
YGNodeFreeRecursive(root);
}
namespace {
template <Event::Type E>
@@ -99,15 +119,22 @@ EventArgs createArgs(const YGNode& node, const Event::Data& data) {
void EventTest::listen(const YGNode& node, Event::Type type, Event::Data data) {
switch (type) {
case Event::NodeAllocation:
lastEvent = createArgs<Event::NodeAllocation>(node, data);
events.push_back(createArgs<Event::NodeAllocation>(node, data));
break;
case Event::NodeDeallocation:
lastEvent = createArgs<Event::NodeDeallocation>(node, data);
events.push_back(createArgs<Event::NodeDeallocation>(node, data));
break;
case Event::NodeLayout:
events.push_back(createArgs<Event::NodeLayout>(node, data));
break;
}
}
EventArgs EventTest::lastEvent{};
void EventTest::TearDown() {
events.clear();
}
std::vector<EventArgs> EventTest::events{};
} // namespace test
} // namespace yoga