2019-06-06 19:36:56 -07:00
|
|
|
/*
|
2018-09-11 15:27:47 -07:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2018-01-10 09:45:11 -08:00
|
|
|
*
|
2019-10-15 10:30:08 -07:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2018-01-10 09:45:11 -08:00
|
|
|
*/
|
2019-10-15 10:30:08 -07:00
|
|
|
|
2018-01-10 09:45:11 -08:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <yoga/YGNode.h>
|
|
|
|
|
|
|
|
static void _dirtied(YGNodeRef node) {
|
2019-03-25 05:37:36 -07:00
|
|
|
int* dirtiedCount = (int*) node->getContext();
|
2018-01-10 09:45:11 -08:00
|
|
|
(*dirtiedCount)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(YogaTest, dirtied) {
|
|
|
|
const YGNodeRef root = YGNodeNew();
|
|
|
|
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
|
|
|
|
YGNodeStyleSetWidth(root, 100);
|
|
|
|
YGNodeStyleSetHeight(root, 100);
|
|
|
|
|
|
|
|
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
|
|
|
|
|
|
|
|
int dirtiedCount = 0;
|
|
|
|
root->setContext(&dirtiedCount);
|
|
|
|
root->setDirtiedFunc(_dirtied);
|
|
|
|
|
|
|
|
ASSERT_EQ(0, dirtiedCount);
|
|
|
|
|
|
|
|
// `_dirtied` MUST be called in case of explicit dirtying.
|
|
|
|
root->setDirty(true);
|
|
|
|
ASSERT_EQ(1, dirtiedCount);
|
|
|
|
|
|
|
|
// `_dirtied` MUST be called ONCE.
|
|
|
|
root->setDirty(true);
|
|
|
|
ASSERT_EQ(1, dirtiedCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(YogaTest, dirtied_propagation) {
|
|
|
|
const YGNodeRef root = YGNodeNew();
|
|
|
|
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
|
|
|
|
YGNodeStyleSetWidth(root, 100);
|
|
|
|
YGNodeStyleSetHeight(root, 100);
|
|
|
|
|
|
|
|
const YGNodeRef root_child0 = YGNodeNew();
|
|
|
|
YGNodeStyleSetWidth(root_child0, 50);
|
|
|
|
YGNodeStyleSetHeight(root_child0, 20);
|
|
|
|
YGNodeInsertChild(root, root_child0, 0);
|
|
|
|
|
|
|
|
const 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;
|
|
|
|
root->setContext(&dirtiedCount);
|
|
|
|
root->setDirtiedFunc(_dirtied);
|
|
|
|
|
|
|
|
ASSERT_EQ(0, dirtiedCount);
|
|
|
|
|
|
|
|
// `_dirtied` MUST be called for the first time.
|
|
|
|
root_child0->markDirtyAndPropogate();
|
|
|
|
ASSERT_EQ(1, dirtiedCount);
|
|
|
|
|
|
|
|
// `_dirtied` must NOT be called for the second time.
|
|
|
|
root_child0->markDirtyAndPropogate();
|
|
|
|
ASSERT_EQ(1, dirtiedCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(YogaTest, dirtied_hierarchy) {
|
|
|
|
const YGNodeRef root = YGNodeNew();
|
|
|
|
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
|
|
|
|
YGNodeStyleSetWidth(root, 100);
|
|
|
|
YGNodeStyleSetHeight(root, 100);
|
|
|
|
|
|
|
|
const YGNodeRef root_child0 = YGNodeNew();
|
|
|
|
YGNodeStyleSetWidth(root_child0, 50);
|
|
|
|
YGNodeStyleSetHeight(root_child0, 20);
|
|
|
|
YGNodeInsertChild(root, root_child0, 0);
|
|
|
|
|
|
|
|
const 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;
|
|
|
|
root_child0->setContext(&dirtiedCount);
|
|
|
|
root_child0->setDirtiedFunc(_dirtied);
|
|
|
|
|
|
|
|
ASSERT_EQ(0, dirtiedCount);
|
|
|
|
|
|
|
|
// `_dirtied` must NOT be called for descendants.
|
|
|
|
root->markDirtyAndPropogate();
|
|
|
|
ASSERT_EQ(0, dirtiedCount);
|
|
|
|
|
|
|
|
// `_dirtied` must NOT be called for the sibling node.
|
|
|
|
root_child1->markDirtyAndPropogate();
|
|
|
|
ASSERT_EQ(0, dirtiedCount);
|
|
|
|
|
|
|
|
// `_dirtied` MUST be called in case of explicit dirtying.
|
|
|
|
root_child0->markDirtyAndPropogate();
|
|
|
|
ASSERT_EQ(1, dirtiedCount);
|
|
|
|
}
|