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