Publish events for node allocation and deallocation

Summary:
@public

Publish two events, `NodeAllocation` and `NodeDeallocation`, in the same places where the global node counter is changed.

Reviewed By: SidharthGuglani

Differential Revision: D15174858

fbshipit-source-id: 6e4e9add88513b9e987189ca5035d76da2a1de55
This commit is contained in:
David Aurelio
2019-05-09 04:14:08 -07:00
committed by Facebook Github Bot
parent 018916403e
commit 88b23ebb3d
5 changed files with 142 additions and 1 deletions

1
BUCK
View File

@@ -16,6 +16,7 @@ COMPILER_FLAGS = LIBRARY_COMPILER_FLAGS + [
TEST_COMPILER_FLAGS = BASE_COMPILER_FLAGS + GMOCK_OVERRIDE_FLAGS + [
"-std=c++1y",
"-DDEBUG",
"-DYG_ENABLE_EVENTS",
]
yoga_cxx_library(

118
tests/EventsTest.cpp Normal file
View File

@@ -0,0 +1,118 @@
/**
* 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/events.h>
#include <yoga/YGNode.h>
#include <functional>
#include <memory>
namespace facebook {
namespace yoga {
namespace test {
struct EventArgs {
const YGNode* node;
Event::Type type;
std::unique_ptr<void, std::function<void(void*)>> dataPtr;
template <Event::Type E>
const Event::TypedData<E>& data() {
return *static_cast<Event::TypedData<E>*>(dataPtr.get());
}
};
struct EventTest : public ::testing::Test {
static EventArgs lastEvent;
static void SetUpTestCase() noexcept;
};
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);
YGNodeFree(n);
}
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);
YGNodeFree(n);
YGConfigFree(c);
}
TEST_F(EventTest, clone_node_event) {
auto c = YGConfigNew();
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);
YGNodeFree(n);
YGNodeFree(clone);
YGConfigFree(c);
}
TEST_F(EventTest, free_node_event) {
auto c = YGConfigNew();
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);
YGConfigFree(c);
}
namespace {
template <Event::Type E>
EventArgs createArgs(const YGNode& node, const Event::Data& data) {
using Data = Event::TypedData<E>;
auto deleteData = [](void* x) { delete static_cast<Data*>(x); };
return {&node, E, {new Data{data.get<E>()}, deleteData}};
}
} // namespace
void EventTest::SetUpTestCase() noexcept {
static bool isSetup = false;
if (isSetup) {
return;
}
isSetup = true;
Event::subscribe([](const YGNode& node, Event::Type type, Event::Data data) {
switch (type) {
case Event::NodeAllocation:
lastEvent = createArgs<Event::NodeAllocation>(node, data);
break;
case Event::NodeDeallocation:
lastEvent = createArgs<Event::NodeDeallocation>(node, data);
break;
}
});
}
EventArgs EventTest::lastEvent{};
} // namespace test
} // namespace yoga
} // namespace facebook

View File

@@ -13,6 +13,7 @@
#include "YGNode.h"
#include "YGNodePrint.h"
#include "Yoga-internal.h"
#include "events.h"
#include "instrumentation.h"
#ifdef _MSC_VER
#include <float.h>
@@ -213,6 +214,9 @@ WIN_EXPORT YGNodeRef YGNodeNewWithConfig(const YGConfigRef config) {
YGAssertWithConfig(
config, node != nullptr, "Could not allocate memory for node");
gNodeInstanceCount++;
#ifdef YG_ENABLE_EVENTS
Event::publish<Event::NodeAllocation>(node, {config});
#endif
if (config->useWebDefaults) {
node->getStyle().flexDirection() = YGFlexDirectionRow;
@@ -238,6 +242,9 @@ YGNodeRef YGNodeClone(YGNodeRef oldNode) {
node != nullptr,
"Could not allocate memory for node");
gNodeInstanceCount++;
#ifdef YG_ENABLE_EVENTS
Event::publish<Event::NodeAllocation>(node, {node->getConfig()});
#endif
node->setOwner(nullptr);
return node;
}
@@ -284,6 +291,9 @@ void YGNodeFree(const YGNodeRef node) {
}
node->clearChildren();
#ifdef YG_ENABLE_EVENTS
Event::publish<Event::NodeDeallocation>(node, {node->getConfig()});
#endif
delete node;
gNodeInstanceCount--;
}

View File

@@ -8,6 +8,8 @@
#include <memory>
#include <stdexcept>
#include <iostream>
namespace facebook {
namespace yoga {

View File

@@ -15,7 +15,7 @@ namespace facebook {
namespace yoga {
struct Event {
enum Type {};
enum Type { NodeAllocation, NodeDeallocation };
class Data;
using Subscriber = void(const YGNode&, Type, Data);
@@ -51,5 +51,15 @@ private:
static void publish(const YGNode&, Type, const Data&);
};
template <>
struct Event::TypedData<Event::NodeAllocation> {
YGConfig* config;
};
template <>
struct Event::TypedData<Event::NodeDeallocation> {
YGConfig* config;
};
} // namespace yoga
} // namespace facebook