1 /*
2 * Copyright (c) 2021-2022 Huawei Device 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 "file_ex.h"
17 #include "gtest/gtest.h"
18
19 #include "base/json/json_util.h"
20 #include "frameworks/bridge/common/dom/dom_document.h"
21 #include "frameworks/bridge/test/unittest/cardfrontend/card_test_factory.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS::Ace::Framework {
27 namespace {
28
29 const std::string CORRECT_PATH = "/data/test/resource/carddomclock/correct.json";
30 const std::string ERROR_PATH = "/data/test/resource/carddomclock/error.json";
31 constexpr int32_t DOM_CLOCK_INDEX = 2;
32
33 } // namespace
34
35 class DomCardClockTest : public testing::Test {
36 public:
37 static void SetUpTestCase();
38 static void TearDownTestCase();
39 void SetUp() override;
40 void TearDown() override;
41 };
42
SetUpTestCase()43 void DomCardClockTest::SetUpTestCase() {}
TearDownTestCase()44 void DomCardClockTest::TearDownTestCase() {}
SetUp()45 void DomCardClockTest::SetUp() {}
TearDown()46 void DomCardClockTest::TearDown() {}
47
48 /**
49 * @tc.name: DomClockDataParse001
50 * @tc.desc: Verify that dom clock can be created successfully and the attr is correct.
51 * @tc.type: FUNC
52 */
53 HWTEST_F(DomCardClockTest, DomClockDataParse001, TestSize.Level1)
54 {
55 /**
56 * @tc.steps: step1. read the correct card template file.
57 * @tc.expected: step1. read file successfully.
58 */
59 std::string index;
60 bool isSuccess = LoadStringFromFile(CORRECT_PATH, index);
61 ASSERT_TRUE(isSuccess);
62
63 /**
64 * @tc.steps: step2. use factory to parse template file.
65 * @tc.expected: step2. parse successfully and can get the dom clock.
66 */
67 auto document = AceType::MakeRefPtr<DOMDocument>(0);
68 auto page = AceType::MakeRefPtr<JsAcePage>(0, document, "");
69 CardTestFactory::GetInstance().ParseCardDsl(page, index);
70 auto domClock = AceType::DynamicCast<DOMClock>(document->GetDOMNodeById(DOM_CLOCK_INDEX));
71 ASSERT_TRUE(domClock != nullptr);
72 ASSERT_TRUE(domClock->GetTag() == "clock");
73
74 /**
75 * @tc.steps: step3. read clock component attr.
76 * @tc.expected: step3. clock component attr is correct.
77 */
78 auto clockComponent = AceType::DynamicCast<ClockComponent>(domClock->GetSpecializedComponent());
79 ASSERT_TRUE(clockComponent != nullptr);
80 auto hourswest = clockComponent->GetHoursWest();
81 ASSERT_EQ(hourswest, 8);
82 auto digitRadiusRatio = clockComponent->GetDigitRadiusRatio();
83 ASSERT_DOUBLE_EQ(digitRadiusRatio, 0.7);
84 auto digitSizeRatio = clockComponent->GetDigitSizeRatio();
85 ASSERT_DOUBLE_EQ(digitSizeRatio, 0.08);
86 auto face = clockComponent->GetClockFaceSrc();
87 ASSERT_EQ(face, "common/clock_widget.png");
88 auto hourHand = clockComponent->GetHourHandSrc();
89 ASSERT_EQ(hourHand, "common/clock_widget_hour.png");
90 auto minuteHand = clockComponent->GetMinuteHandSrc();
91 ASSERT_EQ(minuteHand, "common/clock_widget_minute.png");
92 auto secondHand = clockComponent->GetSecondHandSrc();
93 ASSERT_EQ(secondHand, "common/clock_widget_second.png");
94 auto faceNight = clockComponent->GetClockFaceNightSrc();
95 ASSERT_EQ(faceNight, "common/black_clock_widget.png");
96 auto hourHandNight = clockComponent->GetHourHandNightSrc();
97 ASSERT_EQ(hourHandNight, "common/black_clock_widget_hour.png");
98 auto minuteHandNight = clockComponent->GetMinuteHandNightSrc();
99 ASSERT_EQ(minuteHandNight, "common/black_clock_widget_minute.png");
100 auto digitColor = clockComponent->GetDigitColor();
101 ASSERT_EQ(digitColor, Color::FromString("#000000"));
102 auto digitColorNight = clockComponent->GetDigitColorNight();
103 ASSERT_EQ(digitColorNight, Color::FromString("#FFFFFF"));
104 }
105
106 /**
107 * @tc.name: DomClockDataParse002
108 * @tc.desc: Verify that dom clock can be created successfully and the style is correct.
109 * @tc.type: FUNC
110 */
111 HWTEST_F(DomCardClockTest, DomClockDataParse002, TestSize.Level1)
112 {
113 /**
114 * @tc.steps: step1. read the correct card template file.
115 * @tc.expected: step1. read file successfully.
116 */
117 std::string index;
118 bool isSuccess = LoadStringFromFile(CORRECT_PATH, index);
119 ASSERT_TRUE(isSuccess);
120
121 /**
122 * @tc.steps: step2. use factory to parse template file.
123 * @tc.expected: step2. parse successfully and can get the dom clock.
124 */
125 auto document = AceType::MakeRefPtr<DOMDocument>(0);
126 auto page = AceType::MakeRefPtr<JsAcePage>(0, document, "");
127 CardTestFactory::GetInstance().ParseCardDsl(page, index);
128 auto domClock = AceType::DynamicCast<DOMClock>(document->GetDOMNodeById(DOM_CLOCK_INDEX));
129 ASSERT_TRUE(domClock != nullptr);
130 ASSERT_TRUE(domClock->GetTag() == "clock");
131
132 /**
133 * @tc.steps: step3. read clock component style.
134 * @tc.expected: step3. clock component style is correct.
135 */
136 auto clockComponent = AceType::DynamicCast<ClockComponent>(domClock->GetSpecializedComponent());
137 ASSERT_TRUE(clockComponent != nullptr);
138 auto fontFamilies = clockComponent->GetFontFamilies();
139 ASSERT_EQ(fontFamilies.size(), 1UL);
140 ASSERT_EQ(fontFamilies[0], "Courier");
141 }
142
143 /**
144 * @tc.name: DomClockDataParse003
145 * @tc.desc: Verify that dom clock can be created successfully and the style is incorrect.
146 * @tc.type: FUNC
147 */
148 HWTEST_F(DomCardClockTest, DomClockDataParse003, TestSize.Level1)
149 {
150 /**
151 * @tc.steps: step1. read the Error card template file.
152 * @tc.expected: step1. read file successfully.
153 */
154 std::string index;
155 bool isSuccess = LoadStringFromFile(ERROR_PATH, index);
156 ASSERT_TRUE(isSuccess);
157
158 /**
159 * @tc.steps: step2. use factory to parse template file.
160 * @tc.expected: step2. parse successfully and can get the dom clock.
161 */
162 auto document = AceType::MakeRefPtr<DOMDocument>(0);
163 auto page = AceType::MakeRefPtr<JsAcePage>(0, document, "");
164 CardTestFactory::GetInstance().ParseCardDsl(page, index);
165 auto domClock = AceType::DynamicCast<DOMClock>(document->GetDOMNodeById(DOM_CLOCK_INDEX));
166 ASSERT_TRUE(domClock != nullptr);
167 ASSERT_TRUE(domClock->GetTag() == "clock");
168
169 /**
170 * @tc.steps: step3. read clock component style.
171 * @tc.expected: step3. clock component style is incorrect.
172 */
173 auto clockComponent = AceType::DynamicCast<ClockComponent>(domClock->GetSpecializedComponent());
174 ASSERT_TRUE(clockComponent != nullptr);
175 auto hoursWest = clockComponent->GetHoursWest();
176 ASSERT_EQ(hoursWest, DBL_MAX);
177 }
178
179 /**
180 * @tc.name: DomClockDataUpdate001
181 * @tc.desc: Verify that dom clock can be created and updated successfully.
182 * @tc.type: FUNC
183 */
184 HWTEST_F(DomCardClockTest, DomClockDataUpdate001, TestSize.Level1)
185 {
186 /**
187 * @tc.steps: step1. read the correct card template file.
188 * @tc.expected: step1. read file successfully.
189 */
190 std::string index;
191 bool isSuccess = LoadStringFromFile(CORRECT_PATH, index);
192 ASSERT_TRUE(isSuccess);
193
194 /**
195 * @tc.steps: step2. use factory to parse template file.
196 * @tc.expected: step2. parse successfully and can get the dom clock.
197 */
198 auto document = AceType::MakeRefPtr<DOMDocument>(0);
199 auto page = AceType::MakeRefPtr<JsAcePage>(0, document, "");
200 // update config value, the data key in json file is clockconfig.
201 // digitSizeRatio: 0.08 -> 1.2.
202 auto params = "{\n"
203 " \"timeRegion\": \"8\",\n"
204 " \"fontFamily\": \"Courier\",\n"
205 " \"clockconfig\": {\n"
206 " \"digitRadiusRatio\": 1.2,\n"
207 " \"digitSizeRatio\": 1.2,\n"
208 " \"face\": \"common/clock_widget.png\",\n"
209 " \"hourHand\": \"common/clock_widget_hour.png\",\n"
210 " \"minuteHand\": \"common/clock_widget_minute.png\",\n"
211 " \"secondHand\": \"common/clock_widget_second.png\",\n"
212 " \"faceNight\": \"common/black_clock_widget.png\",\n"
213 " \"hourHandNight\": \"common/black_clock_widget_hour.png\",\n"
214 " \"minuteHandNight\": \"common/black_clock_widget_minute.png\",\n"
215 " \"digitColor\": \"#000000\",\n"
216 " \"digitColorNight\": \"#FFFFFF\"\n"
217 " }\n"
218 " }";
219 CardTestFactory::GetInstance().ParseCardDslWithParams(page, index, params);
220 auto domClock = AceType::DynamicCast<DOMClock>(document->GetDOMNodeById(DOM_CLOCK_INDEX));
221 ASSERT_TRUE(domClock != nullptr);
222 ASSERT_TRUE(domClock->GetTag() == "clock");
223
224 /**
225 * @tc.steps: step3. read clock component style.
226 * @tc.expected: step3. clock component config is updated.
227 */
228 auto clockComponent = AceType::DynamicCast<ClockComponent>(domClock->GetSpecializedComponent());
229 ASSERT_TRUE(clockComponent != nullptr);
230 auto digitSizeRatio = clockComponent->GetDigitSizeRatio();
231 ASSERT_EQ(digitSizeRatio, 1.2);
232 }
233
234 /**
235 * @tc.name: DomClockDataUpdate002
236 * @tc.desc: Verify that dom clock can be created and updated successfully.
237 * @tc.type: FUNC
238 */
239 HWTEST_F(DomCardClockTest, DomClockDataUpdate002, TestSize.Level1)
240 {
241 /**
242 * @tc.steps: step1. read the correct card template file.
243 * @tc.expected: step1. read file successfully.
244 */
245 std::string index;
246 bool isSuccess = LoadStringFromFile(CORRECT_PATH, index);
247 ASSERT_TRUE(isSuccess);
248
249 /**
250 * @tc.steps: step2. use factory to parse template file.
251 * @tc.expected: step2. parse successfully and can get the dom clock.
252 */
253 auto document = AceType::MakeRefPtr<DOMDocument>(0);
254 auto page = AceType::MakeRefPtr<JsAcePage>(0, document, "");
255 // update config value, the data key in json file is clockconfig.
256 auto params = "{\n"
257 " \"timeRegion\": \"8\",\n"
258 " \"fontFamily\": \"Couriery\",\n"
259 " \"clockconfig\": {\n"
260 " \"digitRadiusRatio\": 1,\n"
261 " \"digitSizeRatio\": 2,\n"
262 " \"face\": \"common/clockwidget.png\",\n"
263 " \"hourHand\": \"common/clockwidget_hour.png\",\n"
264 " \"minuteHand\": \"common/clockwidget_minute.png\",\n"
265 " \"secondHand\": \"common/clockwidget_second.png\",\n"
266 " \"faceNight\": \"common/blackclock_widget.png\",\n"
267 " \"hourHandNight\": \"common/blackclock_widget_hour.png\",\n"
268 " \"minuteHandNight\": \"common/blackclock_widget_minute.png\",\n"
269 " \"digitColor\": \"#000001\",\n"
270 " \"digitColorNight\": \"#FFFFFE\"\n"
271 " }\n"
272 " }";
273 CardTestFactory::GetInstance().ParseCardDslWithParams(page, index, params);
274 auto domClock = AceType::DynamicCast<DOMClock>(document->GetDOMNodeById(DOM_CLOCK_INDEX));
275 ASSERT_TRUE(domClock != nullptr);
276 ASSERT_TRUE(domClock->GetTag() == "clock");
277
278 /**
279 * @tc.steps: step3. read clock component style.
280 * @tc.expected: step3. clock component config is updated.
281 */
282 auto clockComponent = AceType::DynamicCast<ClockComponent>(domClock->GetSpecializedComponent());
283 ASSERT_TRUE(clockComponent != nullptr);
284 auto hourswest = clockComponent->GetHoursWest();
285 ASSERT_EQ(hourswest, 8);
286 auto fontFamilies = clockComponent->GetFontFamilies();
287 ASSERT_EQ(fontFamilies[0], "Couriery");
288 auto digitRadiusRatio = clockComponent->GetDigitRadiusRatio();
289 ASSERT_DOUBLE_EQ(digitRadiusRatio, 1);
290 auto digitSizeRatio = clockComponent->GetDigitSizeRatio();
291 ASSERT_DOUBLE_EQ(digitSizeRatio, 2);
292 auto face = clockComponent->GetClockFaceSrc();
293 ASSERT_EQ(face, "common/clockwidget.png");
294 auto hourHand = clockComponent->GetHourHandSrc();
295 ASSERT_EQ(hourHand, "common/clockwidget_hour.png");
296 auto minuteHand = clockComponent->GetMinuteHandSrc();
297 ASSERT_EQ(minuteHand, "common/clockwidget_minute.png");
298 auto secondHand = clockComponent->GetSecondHandSrc();
299 ASSERT_EQ(secondHand, "common/clockwidget_second.png");
300 auto faceNight = clockComponent->GetClockFaceNightSrc();
301 ASSERT_EQ(faceNight, "common/blackclock_widget.png");
302 auto hourHandNight = clockComponent->GetHourHandNightSrc();
303 ASSERT_EQ(hourHandNight, "common/blackclock_widget_hour.png");
304 auto minuteHandNight = clockComponent->GetMinuteHandNightSrc();
305 ASSERT_EQ(minuteHandNight, "common/blackclock_widget_minute.png");
306 auto digitColor = clockComponent->GetDigitColor();
307 ASSERT_EQ(digitColor, Color::FromString("#000001"));
308 auto digitColorNight = clockComponent->GetDigitColorNight();
309 ASSERT_EQ(digitColorNight, Color::FromString("#FFFFFE"));
310 }
311
312 } // namespace OHOS::Ace::Framework
313