2016-10-14 04:35:46 -07:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2014-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <CSSLayout/CSSLayout.h>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2016-10-27 10:52:11 -07:00
|
|
|
static CSSSize _measure(CSSNodeRef node,
|
2016-10-14 04:35:46 -07:00
|
|
|
float width,
|
|
|
|
CSSMeasureMode widthMode,
|
|
|
|
float height,
|
|
|
|
CSSMeasureMode heightMode) {
|
2016-11-09 11:23:21 -08:00
|
|
|
int *measureCount = (int*) CSSNodeGetContext(node);
|
|
|
|
if (measureCount) {
|
|
|
|
(*measureCount)++;
|
|
|
|
}
|
|
|
|
|
2016-10-14 04:35:46 -07:00
|
|
|
return CSSSize {
|
2016-11-09 11:23:21 -08:00
|
|
|
.width = 10,
|
|
|
|
.height = 10,
|
2016-10-14 04:35:46 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-11-09 11:23:21 -08:00
|
|
|
TEST(CSSLayoutTest, dont_measure_single_grow_shrink_child) {
|
|
|
|
const CSSNodeRef root = CSSNodeNew();
|
|
|
|
CSSNodeStyleSetWidth(root, 100);
|
|
|
|
CSSNodeStyleSetHeight(root, 100);
|
|
|
|
|
|
|
|
int measureCount = 0;
|
|
|
|
|
|
|
|
const CSSNodeRef root_child0 = CSSNodeNew();
|
|
|
|
CSSNodeSetContext(root_child0, &measureCount);
|
|
|
|
CSSNodeSetMeasureFunc(root_child0, _measure);
|
|
|
|
CSSNodeStyleSetFlexGrow(root_child0, 1);
|
|
|
|
CSSNodeStyleSetFlexShrink(root_child0, 1);
|
|
|
|
CSSNodeInsertChild(root, root_child0, 0);
|
|
|
|
|
|
|
|
CSSNodeCalculateLayout(root, CSSUndefined, CSSUndefined, CSSDirectionLTR);
|
|
|
|
|
|
|
|
ASSERT_EQ(0, measureCount);
|
2016-11-15 20:20:09 -08:00
|
|
|
|
|
|
|
CSSNodeFreeRecursive(root);
|
2016-11-09 11:23:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#if GTEST_HAS_DEATH_TEST
|
2016-11-07 05:28:22 -08:00
|
|
|
TEST(CSSLayoutTest, cannot_add_child_to_node_with_measure_func) {
|
2016-10-14 04:35:46 -07:00
|
|
|
const CSSNodeRef root = CSSNodeNew();
|
|
|
|
CSSNodeSetMeasureFunc(root, _measure);
|
|
|
|
|
|
|
|
const CSSNodeRef root_child0 = CSSNodeNew();
|
2016-11-07 05:28:22 -08:00
|
|
|
ASSERT_DEATH(CSSNodeInsertChild(root, root_child0, 0), "Cannot add child.*");
|
2016-11-15 20:20:09 -08:00
|
|
|
CSSNodeFree(root_child0);
|
2016-11-09 08:42:45 -08:00
|
|
|
CSSNodeFreeRecursive(root);
|
2016-11-07 05:28:22 -08:00
|
|
|
}
|
2016-10-14 04:35:46 -07:00
|
|
|
|
2016-11-09 08:42:45 -08:00
|
|
|
TEST(CSSLayoutTest, cannot_add_nonnull_measure_func_to_non_leaf_node) {
|
2016-11-07 05:28:22 -08:00
|
|
|
const CSSNodeRef root = CSSNodeNew();
|
|
|
|
const CSSNodeRef root_child0 = CSSNodeNew();
|
|
|
|
CSSNodeInsertChild(root, root_child0, 0);
|
2016-10-14 04:35:46 -07:00
|
|
|
|
2016-11-07 05:28:22 -08:00
|
|
|
ASSERT_DEATH(CSSNodeSetMeasureFunc(root, _measure), "Cannot set measure function.*");
|
2016-11-09 08:42:45 -08:00
|
|
|
CSSNodeFreeRecursive(root);
|
2016-10-14 04:35:46 -07:00
|
|
|
}
|
2016-11-09 08:42:45 -08:00
|
|
|
|
|
|
|
TEST(CSSLayoutTest, can_nullify_measure_func_on_any_node) {
|
|
|
|
const CSSNodeRef root = CSSNodeNew();
|
|
|
|
CSSNodeInsertChild(root, CSSNodeNew(), 0);
|
|
|
|
|
|
|
|
CSSNodeSetMeasureFunc(root, NULL);
|
|
|
|
ASSERT_TRUE(CSSNodeGetMeasureFunc(root) == NULL);
|
|
|
|
CSSNodeFreeRecursive(root);
|
|
|
|
}
|
|
|
|
|
2016-11-07 13:55:52 -08:00
|
|
|
#endif
|