Files
Hippy/layout/gtest/tests/HPDirtiedTest.cpp
2021-03-12 16:00:28 +08:00

117 lines
3.4 KiB
C++
Executable File

/* Tencent is pleased to support the open source community by making Hippy available.
* Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <Hippy.h>
#include <gtest.h>
static void _dirtied(HPNodeRef node) {
int* dirtiedCount = (int*)node->getContext();
(*dirtiedCount)++;
}
TEST(HippyTest, dirtied) {
const HPNodeRef root = HPNodeNew();
HPNodeStyleSetAlignItems(root, FlexAlignStart);
HPNodeStyleSetWidth(root, 100);
HPNodeStyleSetHeight(root, 100);
HPNodeDoLayout(root, VALUE_UNDEFINED, VALUE_UNDEFINED);
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(HippyTest, dirtied_propagation) {
const HPNodeRef root = HPNodeNew();
HPNodeStyleSetAlignItems(root, FlexAlignStart);
HPNodeStyleSetWidth(root, 100);
HPNodeStyleSetHeight(root, 100);
const HPNodeRef root_child0 = HPNodeNew();
HPNodeStyleSetWidth(root_child0, 50);
HPNodeStyleSetHeight(root_child0, 20);
HPNodeInsertChild(root, root_child0, 0);
const HPNodeRef root_child1 = HPNodeNew();
HPNodeStyleSetWidth(root_child1, 50);
HPNodeStyleSetHeight(root_child1, 20);
HPNodeInsertChild(root, root_child1, 1);
HPNodeDoLayout(root, VALUE_UNDEFINED, VALUE_UNDEFINED);
int dirtiedCount = 0;
root->setContext(&dirtiedCount);
root->setDirtiedFunc(_dirtied);
ASSERT_EQ(0, dirtiedCount);
// `_dirtied` MUST be called for the first time.
root_child0->markAsDirty();
ASSERT_EQ(1, dirtiedCount);
// `_dirtied` must NOT be called for the second time.
root_child0->markAsDirty();
ASSERT_EQ(1, dirtiedCount);
}
TEST(HippyTest, dirtied_hierarchy) {
const HPNodeRef root = HPNodeNew();
HPNodeStyleSetAlignItems(root, FlexAlignStart);
HPNodeStyleSetWidth(root, 100);
HPNodeStyleSetHeight(root, 100);
const HPNodeRef root_child0 = HPNodeNew();
HPNodeStyleSetWidth(root_child0, 50);
HPNodeStyleSetHeight(root_child0, 20);
HPNodeInsertChild(root, root_child0, 0);
const HPNodeRef root_child1 = HPNodeNew();
HPNodeStyleSetWidth(root_child1, 50);
HPNodeStyleSetHeight(root_child1, 20);
HPNodeInsertChild(root, root_child1, 1);
HPNodeDoLayout(root, VALUE_UNDEFINED, VALUE_UNDEFINED);
int dirtiedCount = 0;
root_child0->setContext(&dirtiedCount);
root_child0->setDirtiedFunc(_dirtied);
ASSERT_EQ(0, dirtiedCount);
// `_dirtied` must NOT be called for descendants.
root->markAsDirty();
ASSERT_EQ(0, dirtiedCount);
// `_dirtied` must NOT be called for the sibling node.
root_child1->markAsDirty();
ASSERT_EQ(0, dirtiedCount);
// `_dirtied` MUST be called in case of explicit dirtying.
root_child0->markAsDirty();
ASSERT_EQ(1, dirtiedCount);
}