#include <gtest/gtest.h>
#include <yoga/Yoga.h>
#include <yoga/node/Node.h>
using namespace facebook;
static void _dirtied(YGNodeConstRef node) {
int* dirtiedCount = (int*)YGNodeGetContext(node);
(*dirtiedCount)++;
}
TEST(YogaTest, dirtied) {
YGNodeRef root = YGNodeNew();
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
int dirtiedCount = 0;
YGNodeSetContext(root, &dirtiedCount);
YGNodeSetDirtiedFunc(root, _dirtied);
ASSERT_EQ(0, dirtiedCount);
static_cast<yoga::Node*>(root)->setDirty(true);
ASSERT_EQ(1, dirtiedCount);
static_cast<yoga::Node*>(root)->setDirty(true);
ASSERT_EQ(1, dirtiedCount);
}
TEST(YogaTest, dirtied_propagation) {
YGNodeRef root = YGNodeNew();
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);
YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetWidth(root_child0, 50);
YGNodeStyleSetHeight(root_child0, 20);
YGNodeInsertChild(root, root_child0, 0);
YGNodeRef root_child1 = YGNodeNew();
YGNodeStyleSetWidth(root_child1, 50);
YGNodeStyleSetHeight(root_child1, 20);
YGNodeInsertChild(root, root_child1, 1);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
int dirtiedCount = 0;
YGNodeSetContext(root, &dirtiedCount);
YGNodeSetDirtiedFunc(root, _dirtied);
ASSERT_EQ(0, dirtiedCount);
static_cast<yoga::Node*>(root_child0)->markDirtyAndPropagate();
ASSERT_EQ(1, dirtiedCount);
static_cast<yoga::Node*>(root_child0)->markDirtyAndPropagate();
ASSERT_EQ(1, dirtiedCount);
}
TEST(YogaTest, dirtied_hierarchy) {
YGNodeRef root = YGNodeNew();
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);
YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetWidth(root_child0, 50);
YGNodeStyleSetHeight(root_child0, 20);
YGNodeInsertChild(root, root_child0, 0);
YGNodeRef root_child1 = YGNodeNew();
YGNodeStyleSetWidth(root_child1, 50);
YGNodeStyleSetHeight(root_child1, 20);
YGNodeInsertChild(root, root_child1, 1);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
int dirtiedCount = 0;
YGNodeSetContext(root_child0, &dirtiedCount);
YGNodeSetDirtiedFunc(root_child0, _dirtied);
ASSERT_EQ(0, dirtiedCount);
static_cast<yoga::Node*>(root)->markDirtyAndPropagate();
ASSERT_EQ(0, dirtiedCount);
static_cast<yoga::Node*>(root_child1)->markDirtyAndPropagate();
ASSERT_EQ(0, dirtiedCount);
static_cast<yoga::Node*>(root_child0)->markDirtyAndPropagate();
ASSERT_EQ(1, dirtiedCount);
}