1 /* 2 * Copyright (c) 2023 iSoftStone Information Technology (Group) Co.,Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <cstdint> 17 #include <functional> 18 #include <string> 19 #include <tuple> 20 #include <vector> 21 22 #include "gtest/gtest.h" 23 24 #include "base/geometry/dimension.h" 25 26 #define protected public 27 #define private public 28 29 #include "core/components_ng/property/templates_parser.h" 30 31 using namespace testing; 32 using namespace testing::ext; 33 34 namespace OHOS::Ace::NG { 35 namespace {} 36 37 class TemplatesParserTestNg : public testing::Test { 38 public: SetUpTestSuite()39 static void SetUpTestSuite() {}; TearDownTestSuite()40 static void TearDownTestSuite() {}; 41 }; 42 43 /** 44 * @tc.name: TemplatesParserTestNg001 45 * @tc.desc: Test ParseTemplateArgs. 46 * @tc.type: FUNC 47 */ 48 HWTEST_F(TemplatesParserTestNg, TemplatesParserTestNg001, TestSize.Level1) 49 { 50 /** 51 * @tc.steps: step1 init args of ParseTemplateArgs and 52 call ParseTemplateArgs. 53 * @tc.expected: the return value is same as the forth val of tuple. 54 */ 55 double size = 0; 56 double gap = 0; 57 int32_t childrenCount = 0; 58 std::vector<std::tuple<std::function<std::string(void)>, double, double, int32_t, std::vector<double>>> parms = { __anon25ab16310202() 59 { [&]() { 60 size = 200; 61 gap = 10; 62 childrenCount = 5; 63 return ""; 64 }, 65 size, gap, childrenCount, {} }, __anon25ab16310402() 66 { [&]() { 67 size = 200; 68 gap = 10; 69 childrenCount = 5; 70 return "abc"; 71 }, 72 size, gap, childrenCount, {} }, __anon25ab16310602() 73 { [&]() { 74 size = 200; 75 gap = 10; 76 childrenCount = 5; 77 return "1fr 2px 3%"; 78 }, 79 size, gap, childrenCount, { -2, 0, 0 } }, __anon25ab16310802() 80 { [&]() { 81 size = 200; 82 gap = 10; 83 childrenCount = 5; 84 return "1fr 2px 900%"; 85 }, 86 size, gap, childrenCount, { -2, 0, 0 } }, __anon25ab16310a02() 87 { [&]() { 88 size = 200; 89 gap = 10; 90 childrenCount = 5; 91 return "auto-fill 1fr 3fr"; 92 }, 93 size, gap, childrenCount, {} }, __anon25ab16310c02() 94 { [&]() { 95 size = 200; 96 gap = 100; 97 childrenCount = 5; 98 return "0.1fr 20px 100px"; 99 }, 100 size, gap, childrenCount, { -12, 0, 0 } }, __anon25ab16310e02() 101 { [&]() { 102 size = 200; 103 gap = 10; 104 childrenCount = 5; 105 return "repeat"; 106 }, 107 size, gap, childrenCount, {} }, __anon25ab16311002() 108 { [&]() { 109 size = 200; 110 gap = 10; 111 childrenCount = 5; 112 return "auto-fill"; 113 }, 114 size, gap, childrenCount, {} }, __anon25ab16311202() 115 { [&]() { 116 size = 200; 117 gap = 10; 118 childrenCount = 5; 119 return "repeat(1, 100px 2px 3px 4px 5px 6px 7px 8px)"; 120 }, 121 size, gap, childrenCount, { 0, 0, 0, 0, 0, 0, 0, 0 } }, __anon25ab16311402() 122 { [&]() { 123 size = 200; 124 gap = 10; 125 childrenCount = 5; 126 return "repeatabcdefghi"; 127 }, 128 size, gap, childrenCount, {} }, __anon25ab16311602() 129 { [&]() { 130 size = 200; 131 gap = 10; 132 childrenCount = 5; 133 return "repeat(3, 100px a b c d e f g)"; 134 }, 135 size, gap, childrenCount, {} }, __anon25ab16311802() 136 { [&]() { 137 size = 200; 138 gap = 10; 139 childrenCount = 5; 140 return "repeat auto-fill 1fr 2px 900%"; 141 }, 142 size, gap, childrenCount, { 0 } }, __anon25ab16311a02() 143 { [&]() { 144 size = 200; 145 gap = 10; 146 childrenCount = 5; 147 return "auto-fill arepeatpx10vp10 10)2"; 148 }, 149 size, gap, childrenCount, { 0 } }, __anon25ab16311c02() 150 { [&]() { 151 size = 200; 152 gap = 10; 153 childrenCount = 5; 154 return "repeat(auto-fill, 2px )repeat(auto-fill, 2px )repeat(auto-fill, 2vp)"; 155 }, 156 size, gap, childrenCount, { 0 } }, __anon25ab16311e02() 157 { [&]() { 158 size = 200; 159 gap = 10; 160 childrenCount = 5; 161 return "repeat(auto-fill, 2px 2% 2vp 2 )"; 162 }, 163 size, gap, childrenCount, { 2, 0, 2, 2 } }, __anon25ab16312002() 164 { [&]() { 165 size = 200; 166 gap = 10; 167 childrenCount = 5; 168 return "2px 2px repeat(auto-fill, 2px 2% 2 ) 2px"; 169 }, 170 size, gap, childrenCount, { 2, 2, 2, 0, 2, 2 } }, __anon25ab16312202() 171 { [&]() { 172 size = 200; 173 gap = 10; 174 childrenCount = 5; 175 return "(auto-fit, 2px 2)"; 176 }, 177 size, gap, childrenCount, {} }, __anon25ab16312402() 178 { [&]() { 179 size = 200; 180 gap = 10; 181 childrenCount = 5; 182 return "repeat(auto-fit, ) 2px"; 183 }, 184 size, gap, childrenCount, { 0 } } 185 }; 186 for (auto [args, size, gap, childrenCount, rt] : parms) { 187 auto result = ParseTemplateArgs(args(), size, gap, childrenCount); 188 EXPECT_EQ(result.first.size(), rt.size()); 189 for (int i = 0; i < rt.size() && i < result.first.size(); i++) { 190 EXPECT_EQ(rt[i], result.first[i]); 191 } 192 } 193 } 194 195 HWTEST_F(TemplatesParserTestNg, TemplatesParserTestNg002, TestSize.Level1) 196 { 197 /** 198 * @tc.steps: step1. Test ParseArgsWithAutoFit. 199 * @tc.expected: retVal.first is empty. 200 */ 201 std::string args = "repeat(auto-fit,4px) 2px 2px 2px"; 202 double size = 100; 203 double gap = -10; 204 int32_t childrenCount = 6; 205 206 auto retVal = ParseTemplateArgs(args, size, gap, childrenCount); 207 EXPECT_EQ(retVal.first.size(), 4); 208 209 /** 210 * @tc.steps: step2. Test ParseArgsWithAutoFill. 211 * @tc.expected: retVal.first.size() is 100. 212 */ 213 args = "repeat(auto-fill,4px) 2px 2px 2px"; 214 size = 6; 215 gap = 100; 216 childrenCount = 6; 217 vector<int> gt { 4, 2, 2, 2 }; 218 219 retVal = ParseTemplateArgs(args, size, gap, childrenCount); 220 EXPECT_EQ(retVal.first.size(), 4); 221 for (int i = 0; i < retVal.first.size(); i++) { 222 EXPECT_EQ(retVal.first[i], gt[i]); 223 } 224 225 /** 226 * @tc.steps: step2. Test ParseAutoFill. 227 * @tc.expected: retVal.first.size() is empty. 228 */ 229 args = "auto-fill,4px 2px 2px 2px"; 230 size = 6; 231 gap = -10; 232 childrenCount = 6; 233 234 retVal = ParseTemplateArgs(args, size, gap, childrenCount); 235 EXPECT_TRUE(retVal.first.empty()); 236 } 237 } // namespace OHOS::Ace::NG 238