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/YGNode.h>
#include <yoga/testutil/testutil.h> #include <yoga/testutil/testutil.h>
#include <algorithm>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <vector>
namespace facebook { namespace facebook {
namespace yoga { namespace yoga {
@@ -33,16 +35,18 @@ class EventTest : public ::testing::Test {
static void listen(const YGNode&, Event::Type, Event::Data); static void listen(const YGNode&, Event::Type, Event::Data);
public: 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) { TEST_F(EventTest, new_node_has_event) {
auto c = YGConfigGetDefault(); auto c = YGConfigGetDefault();
auto n = YGNodeNew(); auto n = YGNodeNew();
ASSERT_EQ(lastEvent.node, n); ASSERT_EQ(lastEvent().node, n);
ASSERT_EQ(lastEvent.type, Event::NodeAllocation); ASSERT_EQ(lastEvent().type, Event::NodeAllocation);
ASSERT_EQ(lastEvent.data<Event::NodeAllocation>().config, c); ASSERT_EQ(lastEvent().data<Event::NodeAllocation>().config, c);
YGNodeFree(n); YGNodeFree(n);
} }
@@ -51,9 +55,9 @@ TEST_F(EventTest, new_node_with_config_event) {
auto c = YGConfigNew(); auto c = YGConfigNew();
auto n = YGNodeNewWithConfig(c); auto n = YGNodeNewWithConfig(c);
ASSERT_EQ(lastEvent.node, n); ASSERT_EQ(lastEvent().node, n);
ASSERT_EQ(lastEvent.type, Event::NodeAllocation); ASSERT_EQ(lastEvent().type, Event::NodeAllocation);
ASSERT_EQ(lastEvent.data<Event::NodeAllocation>().config, c); ASSERT_EQ(lastEvent().data<Event::NodeAllocation>().config, c);
YGNodeFree(n); YGNodeFree(n);
YGConfigFree(c); YGConfigFree(c);
@@ -64,9 +68,9 @@ TEST_F(EventTest, clone_node_event) {
auto n = YGNodeNewWithConfig(c); auto n = YGNodeNewWithConfig(c);
auto clone = YGNodeClone(n); auto clone = YGNodeClone(n);
ASSERT_EQ(lastEvent.node, clone); ASSERT_EQ(lastEvent().node, clone);
ASSERT_EQ(lastEvent.type, Event::NodeAllocation); ASSERT_EQ(lastEvent().type, Event::NodeAllocation);
ASSERT_EQ(lastEvent.data<Event::NodeAllocation>().config, c); ASSERT_EQ(lastEvent().data<Event::NodeAllocation>().config, c);
YGNodeFree(n); YGNodeFree(n);
YGNodeFree(clone); YGNodeFree(clone);
@@ -78,13 +82,29 @@ TEST_F(EventTest, free_node_event) {
auto n = YGNodeNewWithConfig(c); auto n = YGNodeNewWithConfig(c);
YGNodeFree(n); YGNodeFree(n);
ASSERT_EQ(lastEvent.node, n); ASSERT_EQ(lastEvent().node, n);
ASSERT_EQ(lastEvent.type, Event::NodeDeallocation); ASSERT_EQ(lastEvent().type, Event::NodeDeallocation);
ASSERT_EQ(lastEvent.data<Event::NodeDeallocation>().config, c); ASSERT_EQ(lastEvent().data<Event::NodeDeallocation>().config, c);
YGConfigFree(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 { namespace {
template <Event::Type E> 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) { void EventTest::listen(const YGNode& node, Event::Type type, Event::Data data) {
switch (type) { switch (type) {
case Event::NodeAllocation: case Event::NodeAllocation:
lastEvent = createArgs<Event::NodeAllocation>(node, data); events.push_back(createArgs<Event::NodeAllocation>(node, data));
break; break;
case Event::NodeDeallocation: 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; break;
} }
} }
EventArgs EventTest::lastEvent{}; void EventTest::TearDown() {
events.clear();
}
std::vector<EventArgs> EventTest::events{};
} // namespace test } // namespace test
} // namespace yoga } // namespace yoga

View File

@@ -3667,6 +3667,9 @@ bool YGLayoutNodeInternal(
const YGConfigRef config, const YGConfigRef config,
YGMarkerLayoutData& layoutMarkerData, YGMarkerLayoutData& layoutMarkerData,
void* const layoutContext) { void* const layoutContext) {
#ifdef YG_ENABLE_EVENTS
Event::publish<Event::NodeLayout>(node);
#endif
YGLayout* layout = &node->getLayout(); YGLayout* layout = &node->getLayout();
gDepth++; gDepth++;

View File

@@ -15,7 +15,7 @@ namespace facebook {
namespace yoga { namespace yoga {
struct Event { struct Event {
enum Type { NodeAllocation, NodeDeallocation }; enum Type { NodeAllocation, NodeDeallocation, NodeLayout };
class Data; class Data;
using Subscriber = void(const YGNode&, Type, Data); using Subscriber = void(const YGNode&, Type, Data);