Rebase of facebook flexbox to yoga
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-third-party / yoga / YGRoundingMeasureFuncTest.cpp
1 /**
2  * Copyright (c) 2014-present, Facebook, Inc.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7
8 #include <gtest/gtest.h>
9 #include <yoga/YGNode.h>
10 #include <yoga/Yoga.h>
11
12 static YGSize _measureFloor(YGNodeRef node,
13                             float width,
14                             YGMeasureMode widthMode,
15                             float height,
16                             YGMeasureMode heightMode) {
17   return YGSize{
18       width = 10.2f, height = 10.2f,
19   };
20 }
21
22 static YGSize _measureCeil(YGNodeRef node,
23                            float width,
24                            YGMeasureMode widthMode,
25                            float height,
26                            YGMeasureMode heightMode) {
27   return YGSize{
28       width = 10.5f, height = 10.5f,
29   };
30 }
31
32 static YGSize _measureFractial(YGNodeRef node,
33   float width,
34   YGMeasureMode widthMode,
35   float height,
36   YGMeasureMode heightMode) {
37   return YGSize{
38     width = 0.5f, height = 0.5f,
39   };
40 }
41
42 TEST(YogaTest, rounding_feature_with_custom_measure_func_floor) {
43   const YGConfigRef config = YGConfigNew();
44   const YGNodeRef root = YGNodeNewWithConfig(config);
45
46   const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
47   root_child0->setMeasureFunc(_measureFloor);
48   YGNodeInsertChild(root, root_child0, 0);
49
50   YGConfigSetPointScaleFactor(config, 0.0f);
51
52   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionRTL);
53
54   ASSERT_FLOAT_EQ(10.2, YGNodeLayoutGetWidth(root_child0));
55   ASSERT_FLOAT_EQ(10.2, YGNodeLayoutGetHeight(root_child0));
56
57   YGConfigSetPointScaleFactor(config, 1.0f);
58
59   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
60
61   ASSERT_FLOAT_EQ(11, YGNodeLayoutGetWidth(root_child0));
62   ASSERT_FLOAT_EQ(11, YGNodeLayoutGetHeight(root_child0));
63
64   YGConfigSetPointScaleFactor(config, 2.0f);
65
66   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionRTL);
67
68   ASSERT_FLOAT_EQ(10.5, YGNodeLayoutGetWidth(root_child0));
69   ASSERT_FLOAT_EQ(10.5, YGNodeLayoutGetHeight(root_child0));
70
71   YGConfigSetPointScaleFactor(config, 4.0f);
72
73   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
74
75   ASSERT_FLOAT_EQ(10.25, YGNodeLayoutGetWidth(root_child0));
76   ASSERT_FLOAT_EQ(10.25, YGNodeLayoutGetHeight(root_child0));
77
78   YGConfigSetPointScaleFactor(config, 1.0f / 3.0f);
79
80   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionRTL);
81
82   ASSERT_FLOAT_EQ(12.0, YGNodeLayoutGetWidth(root_child0));
83   ASSERT_FLOAT_EQ(12.0, YGNodeLayoutGetHeight(root_child0));
84
85   YGNodeFreeRecursive(root);
86
87   YGConfigFree(config);
88 }
89
90 TEST(YogaTest, rounding_feature_with_custom_measure_func_ceil) {
91   const YGConfigRef config = YGConfigNew();
92   const YGNodeRef root = YGNodeNewWithConfig(config);
93
94   const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
95   root_child0->setMeasureFunc(_measureCeil);
96   YGNodeInsertChild(root, root_child0, 0);
97
98   YGConfigSetPointScaleFactor(config, 1.0f);
99
100   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
101
102   ASSERT_FLOAT_EQ(11, YGNodeLayoutGetWidth(root_child0));
103   ASSERT_FLOAT_EQ(11, YGNodeLayoutGetHeight(root_child0));
104
105   YGNodeFreeRecursive(root);
106
107   YGConfigFree(config);
108 }
109
110 TEST(YogaTest, rounding_feature_with_custom_measure_and_fractial_matching_scale) {
111   const YGConfigRef config = YGConfigNew();
112   const YGNodeRef root = YGNodeNewWithConfig(config);
113
114   const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
115   YGNodeStyleSetPosition(root_child0, YGEdgeLeft, 73.625);
116   root_child0->setMeasureFunc(_measureFractial);
117   YGNodeInsertChild(root, root_child0, 0);
118
119   YGConfigSetPointScaleFactor(config, 2.0f);
120
121   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
122
123   ASSERT_FLOAT_EQ(0.5, YGNodeLayoutGetWidth(root_child0));
124   ASSERT_FLOAT_EQ(0.5, YGNodeLayoutGetHeight(root_child0));
125   ASSERT_FLOAT_EQ(73.5, YGNodeLayoutGetLeft(root_child0));
126
127   YGNodeFreeRecursive(root);
128
129   YGConfigFree(config);
130 }