2016-08-31 04:58:35 -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.
|
|
|
|
*/
|
|
|
|
|
2016-08-31 06:10:06 -07:00
|
|
|
#include <CSSLayout/CSSLayout.h>
|
2016-08-31 04:58:35 -07:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2016-10-27 10:52:11 -07:00
|
|
|
static CSSSize _measureMax(CSSNodeRef node,
|
2016-08-31 04:58:35 -07:00
|
|
|
float width,
|
2016-12-02 05:47:43 -08:00
|
|
|
YGMeasureMode widthMode,
|
2016-08-31 04:58:35 -07:00
|
|
|
float height,
|
2016-12-02 05:47:43 -08:00
|
|
|
YGMeasureMode heightMode) {
|
2016-10-25 07:33:58 -07:00
|
|
|
|
2016-10-27 10:52:11 -07:00
|
|
|
int *measureCount = (int *)CSSNodeGetContext(node);
|
2016-11-09 11:23:21 -08:00
|
|
|
(*measureCount)++;
|
|
|
|
|
2016-08-31 04:58:35 -07:00
|
|
|
return CSSSize {
|
2016-12-02 05:47:43 -08:00
|
|
|
.width = widthMode == YGMeasureModeUndefined ? 10 : width,
|
|
|
|
.height = heightMode == YGMeasureModeUndefined ? 10 : height,
|
2016-10-25 07:33:58 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-10-27 10:52:11 -07:00
|
|
|
static CSSSize _measureMin(CSSNodeRef node,
|
2016-10-25 07:33:58 -07:00
|
|
|
float width,
|
2016-12-02 05:47:43 -08:00
|
|
|
YGMeasureMode widthMode,
|
2016-10-25 07:33:58 -07:00
|
|
|
float height,
|
2016-12-02 05:47:43 -08:00
|
|
|
YGMeasureMode heightMode) {
|
2016-10-25 07:33:58 -07:00
|
|
|
|
2016-10-27 10:52:11 -07:00
|
|
|
int *measureCount = (int *)CSSNodeGetContext(node);
|
2016-10-25 07:33:58 -07:00
|
|
|
*measureCount = *measureCount + 1;
|
|
|
|
return CSSSize {
|
2016-12-02 05:47:43 -08:00
|
|
|
.width = widthMode == YGMeasureModeUndefined || (widthMode == YGMeasureModeAtMost && width > 10) ? 10 : width,
|
|
|
|
.height = heightMode == YGMeasureModeUndefined || (heightMode == YGMeasureModeAtMost && height > 10) ? 10 : height,
|
2016-08-31 04:58:35 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CSSLayoutTest, measure_once_single_flexible_child) {
|
|
|
|
const CSSNodeRef root = CSSNodeNew();
|
2016-12-02 05:47:43 -08:00
|
|
|
CSSNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
|
|
|
|
CSSNodeStyleSetAlignItems(root, YGAlignFlexStart);
|
2016-08-31 04:58:35 -07:00
|
|
|
CSSNodeStyleSetWidth(root, 100);
|
|
|
|
CSSNodeStyleSetHeight(root, 100);
|
|
|
|
|
|
|
|
const CSSNodeRef root_child0 = CSSNodeNew();
|
2016-10-25 07:33:58 -07:00
|
|
|
int measureCount = 0;
|
|
|
|
CSSNodeSetContext(root_child0, &measureCount);
|
|
|
|
CSSNodeSetMeasureFunc(root_child0, _measureMax);
|
|
|
|
CSSNodeStyleSetFlexGrow(root_child0, 1);
|
2016-08-31 04:58:35 -07:00
|
|
|
CSSNodeInsertChild(root, root_child0, 0);
|
|
|
|
|
2016-12-02 05:47:43 -08:00
|
|
|
CSSNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
|
2016-10-25 07:33:58 -07:00
|
|
|
|
|
|
|
ASSERT_EQ(1, measureCount);
|
|
|
|
|
|
|
|
CSSNodeFreeRecursive(root);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CSSLayoutTest, remeasure_with_same_exact_width_larger_than_needed_height) {
|
|
|
|
const CSSNodeRef root = CSSNodeNew();
|
|
|
|
|
|
|
|
const CSSNodeRef root_child0 = CSSNodeNew();
|
|
|
|
int measureCount = 0;
|
|
|
|
CSSNodeSetContext(root_child0, &measureCount);
|
|
|
|
CSSNodeSetMeasureFunc(root_child0, _measureMin);
|
|
|
|
CSSNodeInsertChild(root, root_child0, 0);
|
|
|
|
|
2016-12-02 05:47:43 -08:00
|
|
|
CSSNodeCalculateLayout(root, 100, 100, YGDirectionLTR);
|
|
|
|
CSSNodeCalculateLayout(root, 100, 50, YGDirectionLTR);
|
2016-08-31 04:58:35 -07:00
|
|
|
|
|
|
|
ASSERT_EQ(1, measureCount);
|
2016-09-29 04:09:56 -07:00
|
|
|
|
|
|
|
CSSNodeFreeRecursive(root);
|
2016-08-31 04:58:35 -07:00
|
|
|
}
|
|
|
|
|
2016-10-25 07:33:58 -07:00
|
|
|
TEST(CSSLayoutTest, remeasure_with_same_atmost_width_larger_than_needed_height) {
|
2016-08-31 04:58:35 -07:00
|
|
|
const CSSNodeRef root = CSSNodeNew();
|
2016-12-02 05:47:43 -08:00
|
|
|
CSSNodeStyleSetAlignItems(root, YGAlignFlexStart);
|
2016-08-31 04:58:35 -07:00
|
|
|
|
|
|
|
const CSSNodeRef root_child0 = CSSNodeNew();
|
2016-10-25 07:33:58 -07:00
|
|
|
int measureCount = 0;
|
|
|
|
CSSNodeSetContext(root_child0, &measureCount);
|
|
|
|
CSSNodeSetMeasureFunc(root_child0, _measureMin);
|
2016-08-31 04:58:35 -07:00
|
|
|
CSSNodeInsertChild(root, root_child0, 0);
|
|
|
|
|
2016-12-02 05:47:43 -08:00
|
|
|
CSSNodeCalculateLayout(root, 100, 100, YGDirectionLTR);
|
|
|
|
CSSNodeCalculateLayout(root, 100, 50, YGDirectionLTR);
|
2016-10-25 07:33:58 -07:00
|
|
|
|
|
|
|
ASSERT_EQ(1, measureCount);
|
|
|
|
|
|
|
|
CSSNodeFreeRecursive(root);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CSSLayoutTest, remeasure_with_computed_width_larger_than_needed_height) {
|
|
|
|
const CSSNodeRef root = CSSNodeNew();
|
2016-12-02 05:47:43 -08:00
|
|
|
CSSNodeStyleSetAlignItems(root, YGAlignFlexStart);
|
2016-10-25 07:33:58 -07:00
|
|
|
|
|
|
|
const CSSNodeRef root_child0 = CSSNodeNew();
|
2016-08-31 04:58:35 -07:00
|
|
|
int measureCount = 0;
|
2016-10-25 07:33:58 -07:00
|
|
|
CSSNodeSetContext(root_child0, &measureCount);
|
|
|
|
CSSNodeSetMeasureFunc(root_child0, _measureMin);
|
|
|
|
CSSNodeInsertChild(root, root_child0, 0);
|
2016-08-31 04:58:35 -07:00
|
|
|
|
2016-12-02 05:47:43 -08:00
|
|
|
CSSNodeCalculateLayout(root, 100, 100, YGDirectionLTR);
|
|
|
|
CSSNodeStyleSetAlignItems(root, YGAlignStretch);
|
|
|
|
CSSNodeCalculateLayout(root, 10, 50, YGDirectionLTR);
|
2016-08-31 04:58:35 -07:00
|
|
|
|
2016-10-25 07:33:58 -07:00
|
|
|
ASSERT_EQ(1, measureCount);
|
|
|
|
|
|
|
|
CSSNodeFreeRecursive(root);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CSSLayoutTest, remeasure_with_atmost_computed_width_undefined_height) {
|
|
|
|
const CSSNodeRef root = CSSNodeNew();
|
2016-12-02 05:47:43 -08:00
|
|
|
CSSNodeStyleSetAlignItems(root, YGAlignFlexStart);
|
2016-10-25 07:33:58 -07:00
|
|
|
|
|
|
|
const CSSNodeRef root_child0 = CSSNodeNew();
|
|
|
|
int measureCount = 0;
|
|
|
|
CSSNodeSetContext(root_child0, &measureCount);
|
|
|
|
CSSNodeSetMeasureFunc(root_child0, _measureMin);
|
|
|
|
CSSNodeInsertChild(root, root_child0, 0);
|
|
|
|
|
2016-12-02 05:47:43 -08:00
|
|
|
CSSNodeCalculateLayout(root, 100, YGUndefined, YGDirectionLTR);
|
|
|
|
CSSNodeCalculateLayout(root, 10, YGUndefined, YGDirectionLTR);
|
2016-08-31 04:58:35 -07:00
|
|
|
|
|
|
|
ASSERT_EQ(1, measureCount);
|
2016-09-29 04:09:56 -07:00
|
|
|
|
|
|
|
CSSNodeFreeRecursive(root);
|
2016-08-31 04:58:35 -07:00
|
|
|
}
|