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 "stylemgr_tdd_test.h"
17 #include <cstdio>
18 #include "component.h"
19 #include "div_component.h"
20 #include "js_ability.h"
21 #include "js_debugger_config.h"
22 #include "stylemgr/app_style_manager.h"
23 #include "stylemgr/app_style_sheet.h"
24
25 namespace OHOS {
26 namespace ACELite {
StyleMgrTddTest()27 StyleMgrTddTest::StyleMgrTddTest()
28 {}
29
SetUp()30 void StyleMgrTddTest::SetUp()
31 {
32 Debugger::GetInstance().SetupJSContext();
33 jerry_init(JERRY_INIT_EMPTY);
34 if (g_testStyleMgr == nullptr) {
35 g_testStyleMgr = new AppStyleManager();
36 g_testStyleMgr->Prepare();
37 }
38 rootComponentMock_.PrepareRootContainer();
39 }
40
TearDown()41 void StyleMgrTddTest::TearDown()
42 {
43 if (g_testStyleMgr != nullptr) {
44 delete g_testStyleMgr;
45 g_testStyleMgr = nullptr;
46 }
47 jerry_cleanup();
48 Debugger::GetInstance().ReleaseJSContext();
49 }
50
TestCaseB(const char * name) const51 void StyleMgrTddTest::TestCaseB(const char* name) const
52 {
53 g_assertRecord = 0;
54 g_currentTestCaseName = const_cast<char *>(name);
55 }
56
JudeTestCaseResult() const57 void StyleMgrTddTest::JudeTestCaseResult() const
58 {
59 if (g_assertRecord > 0) {
60 printf("[Test Case] [%s] FAILED \n", g_currentTestCaseName);
61 } else {
62 printf("[Test Case] [%s] PASSED \n", g_currentTestCaseName);
63 }
64 }
65
ReleaseTestResources(const jerry_value_t * values,uint8_t valueCount,Component ** components,uint8_t componentCount) const66 void StyleMgrTddTest::ReleaseTestResources(const jerry_value_t* values,
67 uint8_t valueCount,
68 Component** components,
69 uint8_t componentCount) const
70 {
71 // release JS values
72 if ((values != nullptr) && (valueCount != 0)) {
73 for (int i = 0; i < valueCount; i++) {
74 jerry_release_value(values[i]);
75 }
76 }
77
78 // release component instances
79 if ((components != nullptr) && (componentCount != 0)) {
80 for (int i = 0; ((i < componentCount) && (components[i] != nullptr)); i++) {
81 components[i]->Release();
82 delete components[i];
83 components[i] = nullptr;
84 }
85 }
86 }
87
88 /**
89 * prepare valid style sheet with given selectors and key value paire
90 */
PrepareStyleSheet(const char * selectors,const char * selectorName,const char * propName,uint16_t propValue) const91 JSValue StyleMgrTddTest::PrepareStyleSheet(const char* selectors,
92 const char* selectorName,
93 const char* propName,
94 uint16_t propValue) const
95 {
96 if ((selectors == nullptr) || (strlen(selectors) == 0) || (selectorName == nullptr) ||
97 (strlen(selectorName) == 0) || (propName == nullptr) || (strlen(propName) == 0)) {
98 return jerry_create_undefined();
99 }
100
101 /**
102 * create one style sheet
103 */
104 JSValue styleSheet = jerry_create_object();
105
106 /**
107 * create id selector object and set it to style sheet
108 */
109 JSValue idSelectorKey = jerry_create_string(reinterpret_cast<const jerry_char_t *>(selectors));
110 JSValue idSelectors = jerry_create_object();
111 jerry_release_value(jerry_set_property(styleSheet, idSelectorKey, idSelectors));
112 jerry_release_value(idSelectorKey);
113
114 /**
115 * create one id selector
116 */
117 JSValue sampleSelectorKey = jerry_create_string(reinterpret_cast<const jerry_char_t *>(selectorName));
118 JSValue sampleSelector = jerry_create_object();
119 jerry_release_value(jerry_set_property(idSelectors, sampleSelectorKey, sampleSelector));
120 jerry_release_value(sampleSelectorKey);
121
122 /**
123 * add height key value into sample
124 */
125 JSValue heightKey = jerry_create_string(reinterpret_cast<const jerry_char_t *>(propName));
126 JSValue heightValue = jerry_create_number(propValue);
127 jerry_release_value(jerry_set_property(sampleSelector, heightKey, heightValue));
128 jerry_release_value(heightKey);
129
130 return styleSheet;
131 }
132
PrepareStyleSheetWithKeyValues(const char * selector,JSValue selectorObj,const char * selectorName,const uint8_t itemCount,char * keys[],const uint16_t values[]) const133 JSValue StyleMgrTddTest::PrepareStyleSheetWithKeyValues(const char* selector,
134 JSValue selectorObj,
135 const char* selectorName,
136 const uint8_t itemCount,
137 char* keys[],
138 const uint16_t values[]) const
139 {
140 if ((itemCount == 0) || (selectorName == nullptr) || (strlen(selectorName) == 0) ||
141 (keys == nullptr) || (values == nullptr)) {
142 return jerry_create_undefined();
143 }
144
145 /**
146 * create one style sheet
147 */
148 JSValue styleSheet = jerry_create_object();
149
150 /**
151 * create selector object and set it to style sheet
152 */
153 JSValue selectorObjectTarget = jerry_create_object();
154 JSValue selectorKey = jerry_create_string(reinterpret_cast<const jerry_char_t *>(selector));
155 jerry_release_value(jerry_set_property(styleSheet, selectorKey, selectorObjectTarget));
156 jerry_release_value(selectorKey);
157
158 /**
159 * set selector into style sheet object
160 */
161 JSValue selectorNameValue = jerry_create_string(reinterpret_cast<const jerry_char_t *>(selectorName));
162 jerry_release_value(jerry_set_property(selectorObjectTarget, selectorNameValue, selectorObj));
163 jerry_release_value(selectorNameValue);
164
165 /**
166 * add value into selector
167 */
168 for (uint8_t i = 0; i < itemCount; i++) {
169 JSValue widthValue = jerry_create_number(values[i]);
170 JSValue keyNameValue = jerry_create_string(reinterpret_cast<const jerry_char_t *>(keys[i]));
171 jerry_release_value(jerry_set_property(selectorObj, keyNameValue, widthValue));
172 jerry_release_value(keyNameValue);
173 }
174
175 return styleSheet;
176 }
177
178 /**
179 * prepare valid style option
180 */
PrepareStyleOption(const char * styleType,const char * propName,uint16_t propValue) const181 JSValue StyleMgrTddTest::PrepareStyleOption(const char* styleType, const char* propName, uint16_t propValue) const
182 {
183 if ((styleType == nullptr) || (strlen(styleType) == 0) || (propName == nullptr) || (strlen(propName) == 0)) {
184 return jerry_create_undefined();
185 }
186
187 /**
188 * create one style sheet
189 */
190 JSValue styleOption = jerry_create_object();
191
192 /**
193 * create style object
194 */
195 JSValue styleTypeKey = jerry_create_string(reinterpret_cast<const jerry_char_t *>(styleType));
196 JSValue styleObj = jerry_create_object();
197 jerry_release_value(jerry_set_property(styleOption, styleTypeKey, styleObj));
198 jerry_release_value(styleTypeKey);
199
200 /**
201 * add height key value into style
202 */
203 JSValue propKey = jerry_create_string(reinterpret_cast<const jerry_char_t *>(propName));
204 JSValue propValueObj = jerry_create_number(propValue);
205 jerry_release_value(jerry_set_property(styleObj, propKey, propValueObj));
206 jerry_release_value(propKey);
207
208 return styleOption;
209 }
210
211 /**
212 * prepare valid attr option
213 */
PrepareAttrOption(const char * propName,const char * propValue) const214 JSValue StyleMgrTddTest::PrepareAttrOption(const char* propName, const char* propValue) const
215 {
216 if ((propName == nullptr) || (strlen(propName) == 0)) {
217 return jerry_create_undefined();
218 }
219
220 /**
221 * create one style sheet
222 */
223 JSValue option = jerry_create_object();
224
225 /**
226 * create style object
227 */
228 JSValue attrsKey = jerry_create_string(reinterpret_cast<const jerry_char_t *>("attrs"));
229 JSValue attrsObj = jerry_create_object();
230 jerry_release_value(jerry_set_property(option, attrsKey, attrsObj));
231 jerry_release_value(attrsKey);
232
233 /**
234 * add height key value into style
235 */
236 JSValue propKey = jerry_create_string(reinterpret_cast<const jerry_char_t *>(propName));
237 JSValue propValueObj = jerry_create_string(reinterpret_cast<const jerry_char_t *>(propValue));
238 jerry_release_value(jerry_set_property(attrsObj, propKey, propValueObj));
239 jerry_release_value(propKey);
240
241 return option;
242 }
243
AddValueToAttrOption(jerry_value_t attrOption,const char * propName,const char * propValue) const244 void StyleMgrTddTest::AddValueToAttrOption(jerry_value_t attrOption, const char* propName, const char* propValue) const
245 {
246 if ((propName == nullptr) || (strlen(propName) == 0)) {
247 return;
248 }
249
250 JSValue attrsKey = jerry_create_string(reinterpret_cast<const jerry_char_t *>("attrs"));
251 JSValue attrsObj = jerry_create_object();
252 jerry_release_value(jerry_set_property(attrOption, attrsKey, attrsObj));
253 jerry_release_value(attrsKey);
254 /**
255 * add height key value into style
256 */
257 JSValue propKey = jerry_create_string(reinterpret_cast<const jerry_char_t *>(propName));
258 JSValue propValueObj = jerry_create_string(reinterpret_cast<const jerry_char_t *>(propValue));
259 jerry_release_value(jerry_set_property(attrsObj, propKey, propValueObj));
260 jerry_release_value(propKey);
261 }
262
AddStrValueToOption(jerry_value_t option,const char * propName,const char * propValue) const263 void StyleMgrTddTest::AddStrValueToOption(jerry_value_t option, const char* propName, const char* propValue) const
264 {
265 if ((propName == nullptr) || (strlen(propName) == 0)) {
266 return;
267 }
268
269 /**
270 * add height key value into style
271 */
272 JSValue propKey = jerry_create_string(reinterpret_cast<const jerry_char_t *>(propName));
273 JSValue propValueObj = jerry_create_string(reinterpret_cast<const jerry_char_t *>(propValue));
274 jerry_release_value(jerry_set_property(option, propKey, propValueObj));
275 jerry_release_value(propKey);
276 }
277
ACELiteStyleManagerInlineStyle001()278 void StyleMgrTddTest::ACELiteStyleManagerInlineStyle001()
279 {
280 TestCaseB("ACELiteStyleManagerInlineStyle001");
281 /**
282 * @tc.steps: step1. init test environment
283 */
284 TDD_CASE_BEGIN();
285
286 /**
287 * @tc.steps: step2. create undefine for style sheet object
288 */
289 JSValue undefined = jerry_create_undefined();
290
291 /**
292 * @tc.steps: step3. trigger style sheet initialization
293 */
294 g_testStyleMgr->InitStyleSheet(undefined);
295
296 /**
297 * @tc.steps: step4. check if the style sheet object is generated
298 */
299 EXPECT_TRUE(g_testStyleMgr->GetStyleSheet() == nullptr);
300
301 /**
302 * @tc.steps: step4. clean up test environment
303 */
304 TDD_CASE_END();
305 }
306
ACELiteStyleManagerInlineStyle002()307 void StyleMgrTddTest::ACELiteStyleManagerInlineStyle002()
308 {
309 TestCaseB("ACELiteStyleManagerInlineStyle002");
310 /**
311 * @tc.steps: step1. init test environment
312 */
313 TDD_CASE_BEGIN();
314
315 /**
316 * @tc.steps: step2. create undefine for style sheet object
317 */
318 JSValue styleSheet = jerry_create_object();
319
320 /**
321 * @tc.steps: step3. trigger style sheet initialization
322 */
323 g_testStyleMgr->InitStyleSheet(styleSheet);
324
325 /**
326 * @tc.steps: step4. check if the style sheet object is generated, style sheet instance will be created out
327 */
328 EXPECT_FALSE(g_testStyleMgr->GetStyleSheet() == nullptr);
329
330 /**
331 * @tc.steps: step5. release style sheet object
332 */
333 jerry_release_value(styleSheet);
334
335 /**
336 * @tc.steps: step6. clean up test environment
337 */
338 TDD_CASE_END();
339 }
340
ACELiteStyleManagerInlineStyle003()341 void StyleMgrTddTest::ACELiteStyleManagerInlineStyle003()
342 {
343 TestCaseB("ACELiteStyleManagerInlineStyle003");
344 /**
345 * @tc.steps: step1. init test environment
346 */
347 TDD_CASE_BEGIN();
348
349 /**
350 * @tc.steps: step2. prepare valid style sheet
351 */
352 constexpr char id[] = "idSelectors";
353 const char *sample = "sample";
354 constexpr char heightKey[] = "height";
355 constexpr int32_t heightValue = 50;
356 JSValue styleSheetObj = PrepareStyleSheet(id, sample, heightKey, heightValue);
357
358 /**
359 * @tc.steps: step3. trigger style sheet initialization
360 */
361 g_testStyleMgr->InitStyleSheet(styleSheetObj);
362
363 /**
364 * @tc.steps: step4. check the result
365 */
366 AppStyleSheet* styleSheet = const_cast<AppStyleSheet *>(g_testStyleMgr->GetStyleSheet());
367 EXPECT_FALSE(styleSheet == nullptr);
368 if (styleSheet == nullptr) {
369 jerry_release_value(styleSheetObj);
370 TDD_CASE_END();
371 return;
372 }
373
374 AppStyle* sampleSelector = styleSheet->GetStyleFromIDSelectors(sample);
375 EXPECT_FALSE(sampleSelector == nullptr);
376 if (sampleSelector == nullptr) {
377 jerry_release_value(styleSheetObj);
378 TDD_CASE_END();
379 return;
380 }
381
382 AppStyleItem* sampleSelectorItem = const_cast<AppStyleItem *>(sampleSelector->GetFirst());
383 EXPECT_FALSE(sampleSelectorItem == nullptr);
384 if (sampleSelectorItem == nullptr) {
385 jerry_release_value(styleSheetObj);
386 TDD_CASE_END();
387 return;
388 }
389
390 EXPECT_EQ(sampleSelectorItem->GetNumValue(), heightValue);
391
392 /**
393 * @tc.steps: step5. release the style sheet JS object
394 */
395 jerry_release_value(styleSheetObj);
396
397 /**
398 * @tc.steps: step6. clean up test environment
399 */
400 TDD_CASE_END();
401 }
402
ACELiteStyleManagerInlineStyle004()403 void StyleMgrTddTest::ACELiteStyleManagerInlineStyle004()
404 {
405 TestCaseB("ACELiteStyleManagerInlineStyle004");
406 /**
407 * @tc.steps: step1. init test environment
408 */
409 TDD_CASE_BEGIN();
410
411 /**
412 * @tc.steps: step2. prepare valid style sheet with class selector
413 */
414 constexpr char classSelector[] = "classSelectors";
415 const char *sample = "sampleClass";
416 constexpr char widthKey[] = "width";
417 constexpr int32_t widthValue = 200;
418 JSValue styleSheetObj = PrepareStyleSheet(classSelector, sample, widthKey, widthValue);
419
420 /**
421 * @tc.steps: step3. trigger style sheet initialization
422 */
423 g_testStyleMgr->InitStyleSheet(styleSheetObj);
424
425 /**
426 * @tc.steps: step4. check the result
427 */
428 AppStyleSheet* styleSheet = const_cast<AppStyleSheet *>(g_testStyleMgr->GetStyleSheet());
429 EXPECT_FALSE(styleSheet == nullptr);
430 if (styleSheet == nullptr) {
431 jerry_release_value(styleSheetObj);
432 TDD_CASE_END();
433 return;
434 }
435
436 AppStyle* sampleSelector = styleSheet->GetStyleFromClassSelectors(sample);
437 EXPECT_FALSE(sampleSelector == nullptr);
438 if (sampleSelector == nullptr) {
439 jerry_release_value(styleSheetObj);
440 TDD_CASE_END();
441 return;
442 }
443
444 AppStyleItem* sampleSelectorItem = const_cast<AppStyleItem *>(sampleSelector->GetFirst());
445 EXPECT_FALSE(sampleSelectorItem == nullptr);
446 if (sampleSelectorItem == nullptr) {
447 jerry_release_value(styleSheetObj);
448 TDD_CASE_END();
449 return;
450 }
451
452 EXPECT_EQ(sampleSelectorItem->GetNumValue(), widthValue);
453
454 /**
455 * @tc.steps: step5. release the style sheet JS object
456 */
457 jerry_release_value(styleSheetObj);
458
459 /**
460 * @tc.steps: step6. clean up test environment
461 */
462 TDD_CASE_END();
463 }
464
ACELiteStyleManagerInlineStyle005()465 void StyleMgrTddTest::ACELiteStyleManagerInlineStyle005()
466 {
467 TestCaseB("ACELiteStyleManagerInlineStyle005");
468 /**
469 * @tc.steps: step1. init test environment
470 */
471 TDD_CASE_BEGIN();
472
473 /**
474 * @tc.steps: step2. prepare valid style sheet with class selector
475 */
476 constexpr char classSelector[] = "classSelectors";
477 // give an very long selector name
478 constexpr char sample[] = "sampleClassSelectorWithVeryLongNameOverSixtyFourCharatersWhichIsTooLong";
479 constexpr char widthKey[] = "width";
480 constexpr int32_t widthValue = 200;
481 JSValue styleSheetObj = PrepareStyleSheet(classSelector, sample, widthKey, widthValue);
482
483 /**
484 * @tc.steps: step3. trigger style sheet initialization
485 */
486 g_testStyleMgr->InitStyleSheet(styleSheetObj);
487
488 /**
489 * @tc.steps: step4. check the result
490 */
491 AppStyleSheet* styleSheet = const_cast<AppStyleSheet *>(g_testStyleMgr->GetStyleSheet());
492 EXPECT_FALSE(styleSheet == nullptr);
493 if (styleSheet == nullptr) {
494 jerry_release_value(styleSheetObj);
495 TDD_CASE_END();
496 return;
497 }
498
499 /**
500 * @tc.steps: step5. release the style sheet JS object
501 */
502 jerry_release_value(styleSheetObj);
503
504 /**
505 * @tc.steps: step6. clean up test environment
506 */
507 TDD_CASE_END();
508 }
509
ACELiteStyleManagerIDSelector001()510 void StyleMgrTddTest::ACELiteStyleManagerIDSelector001()
511 {
512 TestCaseB("ACELiteStyleManagerIDSelector001");
513 /**
514 * @tc.steps: step1. init test environment
515 */
516 TDD_CASE_BEGIN();
517
518 /**
519 * @tc.steps: step2. prepare valid style sheet with class selector
520 */
521 DivComponent* divComponent = new DivComponent(UNDEFINED, UNDEFINED, g_testStyleMgr);
522
523 /**
524 * @tc.steps: step3. check the result
525 */
526 EXPECT_TRUE(g_testStyleMgr->GetStyleSheet() == nullptr);
527
528 divComponent->Release();
529 delete divComponent;
530 divComponent = nullptr;
531
532 /**
533 * @tc.steps: step4. clean up test environment
534 */
535 TDD_CASE_END();
536 }
537
ACELiteStyleManagerIDSelector002()538 void StyleMgrTddTest::ACELiteStyleManagerIDSelector002()
539 {
540 TestCaseB("ACELiteStyleManagerIDSelector002");
541 /**
542 * @tc.steps: step1. init test environment
543 */
544 TDD_CASE_BEGIN();
545
546 /**
547 * @tc.steps: step2. prepare valid style sheet with class selector
548 */
549 constexpr char styleType[] = "staticStyle";
550 constexpr char widthKey[] = "width";
551 constexpr int32_t widthValue = 200;
552 jerry_value_t styleOption = PrepareStyleOption(styleType, widthKey, widthValue);
553 DivComponent* divComponent = new DivComponent(styleOption, UNDEFINED, g_testStyleMgr);
554 divComponent->Render();
555 /**
556 * @tc.steps: step3. check the result
557 */
558 EXPECT_TRUE(g_testStyleMgr->GetStyleSheet() == nullptr);
559
560 /**
561 * @tc.steps: step4. check the result
562 */
563 jerry_release_value(styleOption);
564 divComponent->Release();
565 delete divComponent;
566 divComponent = nullptr;
567
568 /**
569 * @tc.steps: step5. clean up test environment
570 */
571 TDD_CASE_END();
572 }
573
ACELiteStyleManagerIDSelector003()574 void StyleMgrTddTest::ACELiteStyleManagerIDSelector003()
575 {
576 TestCaseB("ACELiteStyleManagerIDSelector003");
577 /**
578 * @tc.steps: step1. init test environment
579 */
580 TDD_CASE_BEGIN();
581
582 /**
583 * @tc.steps: step2. prepare valid style sheet with class selector
584 */
585 constexpr char idKey[] = "id";
586 constexpr char idValue[] = "test";
587 jerry_value_t styleOption = PrepareAttrOption(idKey, idValue);
588 DivComponent* divComponent = new DivComponent(styleOption, UNDEFINED, g_testStyleMgr);
589 divComponent->Render();
590
591 /**
592 * @tc.steps: step3. check the result, no style sheet, so no one will be added into combined style
593 */
594 EXPECT_TRUE(g_testStyleMgr->GetStyleSheet() == nullptr);
595
596 /**
597 * @tc.steps: step4. check the result
598 */
599 jerry_release_value(styleOption);
600 divComponent->Release();
601 delete divComponent;
602 divComponent = nullptr;
603
604 /**
605 * @tc.steps: step5. clean up test environment
606 */
607 TDD_CASE_END();
608 }
609
ACELiteStyleManagerIDSelector004()610 void StyleMgrTddTest::ACELiteStyleManagerIDSelector004()
611 {
612 TestCaseB("ACELiteStyleManagerIDSelector004");
613 /**
614 * @tc.steps: step1. init test environment
615 */
616 TDD_CASE_BEGIN();
617
618 /**
619 * @tc.steps: step2. prepare valid style sheet with class selector
620 */
621 constexpr char classSelector[] = "classSelectors";
622 constexpr char sample[] = "sample";
623 constexpr char widthKey[] = "width";
624 constexpr int32_t widthValue = 200;
625 JSValue styleSheetObj = PrepareStyleSheet(classSelector, sample, widthKey, widthValue);
626 /**
627 * @tc.steps: step3. trigger style sheet initialization
628 */
629 g_testStyleMgr->InitStyleSheet(styleSheetObj);
630
631 /**
632 * @tc.steps: step4. prepare valid style sheet with class selector
633 */
634 constexpr char styleType[] = "staticStyle";
635 constexpr char heightKey[] = "height";
636 constexpr int32_t heightValue = 100;
637 jerry_value_t option = PrepareStyleOption(styleType, heightKey, heightValue);
638
639 AddValueToAttrOption(option, "staticClass", "sample");
640 DivComponent* divComponent = new DivComponent(option, UNDEFINED, g_testStyleMgr);
641
642 const uint8_t valueCount = 2;
643 const jerry_value_t jsValues[valueCount] = {option, styleSheetObj};
644 const uint8_t componentCount = 1;
645 Component* components[1] = {divComponent};
646
647 /**
648 * @tc.steps: step5. check the result, no style sheet, so no one will be added into combined style
649 */
650 EXPECT_FALSE(g_testStyleMgr->GetStyleSheet() == nullptr);
651 if (g_testStyleMgr->GetStyleSheet() == nullptr) {
652 ReleaseTestResources(jsValues, valueCount, components, componentCount);
653 TDD_CASE_END();
654 return;
655 }
656
657 /**
658 * @tc.steps: step6. release test resources
659 */
660 ReleaseTestResources(jsValues, valueCount, components, componentCount);
661
662 /**
663 * @tc.steps: step7. clean up test environment
664 */
665 TDD_CASE_END();
666 }
667
ACELiteStyleManagerIDSelector005()668 void StyleMgrTddTest::ACELiteStyleManagerIDSelector005()
669 {
670 TestCaseB("ACELiteStyleManagerIDSelector005");
671 /**
672 * @tc.steps: step1. init test environment
673 */
674 TDD_CASE_BEGIN();
675
676 /**
677 * @tc.steps: step2. prepare valid style sheet with class selector
678 */
679 constexpr char styleType[] = "staticStyle";
680 constexpr char widthKey2[] = "width";
681 constexpr int32_t widthValue2 = 100;
682 jerry_value_t styleOption = PrepareStyleOption(styleType, widthKey2, widthValue2);
683 DivComponent* divComponent = new DivComponent(styleOption, UNDEFINED, g_testStyleMgr);
684
685 constexpr char classSelector[] = "classSelectors";
686 constexpr char sample[] = "sample";
687 constexpr char widthKey[] = "width";
688 constexpr int32_t widthValue = 500;
689 JSValue styleSheetObj = PrepareStyleSheet(classSelector, sample, widthKey, widthValue);
690 /**
691 * @tc.steps: step3. trigger style sheet initialization
692 */
693 g_testStyleMgr->InitStyleSheet(styleSheetObj);
694 rootComponentMock_.RenderComponent(*divComponent);
695
696 /**
697 * @tc.steps: step4. prepare valid style sheet with class selector
698 */
699 Component* component = reinterpret_cast<Component *>(divComponent);
700 UIViewGroup* uiView = reinterpret_cast<UIViewGroup *>(component->GetComponentRootView());
701
702 const uint8_t jsValueCount = 2;
703 const jerry_value_t jerryValues[jsValueCount] = {styleOption, styleSheetObj};
704 const uint8_t componentNumber = 1;
705 Component* componentInstances[1] = {divComponent};
706
707 /**
708 * @tc.steps: step5. check the result, no style sheet, so no one will be added into combined style
709 */
710 AppStyleSheet* appStyleSheet = const_cast<AppStyleSheet *>(g_testStyleMgr->GetStyleSheet());
711 EXPECT_FALSE(appStyleSheet == nullptr);
712 if (appStyleSheet == nullptr) {
713 ReleaseTestResources(jerryValues, jsValueCount, componentInstances, componentNumber);
714 TDD_CASE_END();
715 return;
716 }
717
718 uint16_t width = uiView->GetWidth();
719 EXPECT_EQ(width, widthValue2);
720
721 /**
722 * @tc.steps: step6. release test resources
723 */
724 ReleaseTestResources(jerryValues, jsValueCount, componentInstances, componentNumber);
725
726 /**
727 * @tc.steps: step7. clean up test environment
728 */
729 TDD_CASE_END();
730 }
731
ACELiteStyleManagerClassSelector001()732 void StyleMgrTddTest::ACELiteStyleManagerClassSelector001()
733 {
734 TestCaseB("ACELiteStyleManagerClassSelector001");
735 /**
736 * @tc.steps: step1. init test environment
737 */
738 TDD_CASE_BEGIN();
739
740 /**
741 * @tc.steps: step2. prepare valid style sheet with class selector
742 */
743 DivComponent* divComponent = new DivComponent(UNDEFINED, UNDEFINED, g_testStyleMgr);
744 divComponent->Render();
745
746 /**
747 * @tc.steps: step5. check the result, no style sheet, so no one will be added into combined style
748 */
749 EXPECT_TRUE(const_cast<AppStyleSheet *>(g_testStyleMgr->GetStyleSheet()) == nullptr);
750
751 divComponent->Release();
752 delete divComponent;
753 divComponent = nullptr;
754
755 /**
756 * @tc.steps: step4. clean up test environment
757 */
758 TDD_CASE_END();
759 }
760
ACELiteStyleManagerClassSelector002()761 void StyleMgrTddTest::ACELiteStyleManagerClassSelector002()
762 {
763 TestCaseB("ACELiteStyleManagerClassSelector002");
764 /**
765 * @tc.steps: step1. init test environment
766 */
767 TDD_CASE_BEGIN();
768
769 /**
770 * @tc.steps: step2. prepare valid style
771 */
772 constexpr int32_t heightValue = 100;
773 constexpr char heightKey[] = "height";
774 constexpr char styleType[] = "staticStyle";
775 jerry_value_t styleOption = PrepareStyleOption(styleType, heightKey, heightValue);
776 DivComponent* divComponent = new DivComponent(styleOption, UNDEFINED, g_testStyleMgr);
777 divComponent->Render();
778
779 /**
780 * @tc.steps: step3. check the result
781 */
782 const AppStyleSheet* appStyleSheet = const_cast<AppStyleSheet *>(g_testStyleMgr->GetStyleSheet());
783 EXPECT_TRUE(appStyleSheet == nullptr);
784
785 /**
786 * @tc.steps: step4. clean up test environment
787 */
788 jerry_release_value(styleOption);
789 divComponent->Release();
790 delete divComponent;
791 divComponent = nullptr;
792 TDD_CASE_END();
793 }
794
ACELiteStyleManagerClassSelector003()795 void StyleMgrTddTest::ACELiteStyleManagerClassSelector003()
796 {
797 TestCaseB("ACELiteStyleManagerClassSelector003");
798 /**
799 * @tc.steps: step1. init test environment
800 */
801 TDD_CASE_BEGIN();
802
803 /**
804 * @tc.steps: step2. prepare valid style sheet with class selector
805 */
806 constexpr char classSelector[] = "classSelectors";
807 constexpr char sample[] = "sample";
808 constexpr char widthKey[] = "width";
809 constexpr int32_t widthValue = 200;
810 JSValue styleSheetObj = PrepareStyleSheet(classSelector, sample, widthKey, widthValue);
811 /**
812 * @tc.steps: step3. trigger style sheet initialization
813 */
814 g_testStyleMgr->InitStyleSheet(styleSheetObj);
815
816 /**
817 * @tc.steps: step4. prepare valid style sheet with class selector
818 */
819 constexpr char styleType[] = "staticStyle";
820 constexpr char widthKey2[] = "width";
821 constexpr int32_t widthValue2 = 100;
822 jerry_value_t styleOption = PrepareStyleOption(styleType, widthKey2, widthValue2);
823 DivComponent* divComponent = new DivComponent(styleOption, UNDEFINED, g_testStyleMgr);
824 rootComponentMock_.RenderComponent(*divComponent);
825
826 const uint8_t jsCount = 2;
827 const jerry_value_t jsObjects[jsCount] = {styleOption, styleSheetObj};
828 const uint8_t divNumber = 1;
829 Component* divComponents[1] = {divComponent};
830
831 /**
832 * @tc.steps: step5. check the result, no style sheet, so no one will be added into combined style
833 */
834 Component* component = reinterpret_cast<Component *>(divComponent);
835 UIViewGroup* uiView = reinterpret_cast<UIViewGroup *>(component->GetComponentRootView());
836 AppStyleSheet* appStyleSheet = const_cast<AppStyleSheet *>(g_testStyleMgr->GetStyleSheet());
837 EXPECT_FALSE(appStyleSheet == nullptr);
838 if (appStyleSheet == nullptr) {
839 ReleaseTestResources(jsObjects, jsCount, divComponents, divNumber);
840 TDD_CASE_END();
841 return;
842 }
843
844 uint16_t width = uiView->GetWidth();
845 EXPECT_EQ(width, widthValue2);
846
847 /**
848 * @tc.steps: step6. release test resources
849 */
850 ReleaseTestResources(jsObjects, jsCount, divComponents, divNumber);
851
852 /**
853 * @tc.steps: step7. clean up test environment
854 */
855 TDD_CASE_END();
856 }
857
PreparePesudoObjects(uint16_t value) const858 JSValue StyleMgrTddTest::PreparePesudoObjects(uint16_t value) const
859 {
860 constexpr char classSelector[] = "classSelectors";
861 constexpr char sample[] = "sample";
862 constexpr int32_t activeWidthValue = 300;
863 JSValue classSelectorObj = jerry_create_object();
864 const uint8_t itemCount = 2;
865 char* keys[itemCount];
866 uint16_t const values[itemCount] = {value, activeWidthValue};
867 keys[0] = const_cast<char *>("width");
868 keys[1] = const_cast<char *>("width:active");
869 JSValue styleSheetObj =
870 PrepareStyleSheetWithKeyValues(classSelector, classSelectorObj, sample, itemCount, keys, values);
871 return styleSheetObj;
872 }
873
ACELiteStyleManagerCSSPesudo001()874 void StyleMgrTddTest::ACELiteStyleManagerCSSPesudo001()
875 {
876 TDD_CASE_BEGIN();
877
878 /**
879 * @tc.steps: step1. prepare pesudo class items
880 */
881 constexpr uint16_t widthValue = 200;
882 JSValue styleSheetObj = PreparePesudoObjects(widthValue);
883
884 /**
885 * @tc.steps: step2. trigger style sheet initialization
886 */
887 g_testStyleMgr->InitStyleSheet(styleSheetObj);
888
889 /**
890 * @tc.steps: step3. prepare valid style sheet with class selector
891 */
892 constexpr uint16_t heightValue = 50;
893 jerry_value_t option = PrepareStyleOption("staticStyle", "height", heightValue);
894 AddStrValueToOption(option, "staticClass", "sample");
895 DivComponent* divComponent = new DivComponent(option, UNDEFINED, g_testStyleMgr);
896 rootComponentMock_.RenderComponent(*divComponent);
897 const uint8_t jsObjCount = 2;
898 const jerry_value_t jsObjs[jsObjCount] = {option, styleSheetObj};
899 const uint8_t divComponentNumber = 1;
900 Component* divComponentInstances[1] = {divComponent};
901
902 /**
903 * @tc.steps: step4. check the result, no style sheet, so no one will be added into combined style
904 */
905 uint16_t index = KeyParser::ParseKeyId("width");
906 EXPECT_TRUE(g_testStyleMgr->isPseudoExist(index, 0x01));
907 Component* component = reinterpret_cast<Component *>(divComponent);
908 UIViewGroup* uiView = reinterpret_cast<UIViewGroup *>(component->GetComponentRootView());
909 if (uiView != nullptr) {
910 EXPECT_EQ(uiView->GetWidth(), widthValue);
911 EXPECT_EQ(uiView->GetHeight(), heightValue);
912 }
913
914 ReleaseTestResources(jsObjs, jsObjCount, divComponentInstances, divComponentNumber);
915 TDD_CASE_END();
916 }
917
RunTests()918 void StyleMgrTddTest::RunTests()
919 {
920 ACELiteStyleManagerInlineStyle001();
921 ACELiteStyleManagerInlineStyle002();
922 ACELiteStyleManagerInlineStyle003();
923 ACELiteStyleManagerInlineStyle004();
924 ACELiteStyleManagerInlineStyle005();
925 ACELiteStyleManagerIDSelector001();
926 ACELiteStyleManagerIDSelector002();
927 ACELiteStyleManagerIDSelector003();
928 ACELiteStyleManagerIDSelector004();
929 ACELiteStyleManagerIDSelector005();
930 ACELiteStyleManagerClassSelector001();
931 ACELiteStyleManagerClassSelector002();
932 ACELiteStyleManagerClassSelector003();
933 ACELiteStyleManagerCSSPesudo001();
934 }
935
936 #ifdef TDD_ASSERTIONS
937 /**
938 * @tc.name: ACELiteStyleManagerInlineStyle001
939 * @tc.desc: Verify the style sheet initialization.
940 */
941 HWTEST_F(StyleMgrTddTest, InlineStyle001, TestSize.Level1)
942 {
943 StyleMgrTddTest::ACELiteStyleManagerInlineStyle001();
944 }
945
946 /**
947 * @tc.name: ACELiteStyleManagerInlineStyle002
948 * @tc.desc: Verify the style sheet initialization.
949 */
950 HWTEST_F(StyleMgrTddTest, InlineStyle002, TestSize.Level1)
951 {
952 StyleMgrTddTest::ACELiteStyleManagerInlineStyle002();
953 }
954
955 /**
956 * @tc.name: ACELiteStyleManagerInlineStyle003
957 * @tc.desc: Verify the style sheet initialization.
958 */
959 HWTEST_F(StyleMgrTddTest, InlineStyle003, TestSize.Level1)
960 {
961 StyleMgrTddTest::ACELiteStyleManagerInlineStyle003();
962 }
963
964 /**
965 * @tc.name: ACELiteStyleManagerInlineStyle004
966 * @tc.desc: Verify the style sheet initialization.
967 */
968 HWTEST_F(StyleMgrTddTest, InlineStyle004, TestSize.Level1)
969 {
970 StyleMgrTddTest::ACELiteStyleManagerInlineStyle004();
971 }
972
973 /**
974 * @tc.name: ACELiteStyleManagerInlineStyle005
975 * @tc.desc: Verify the style sheet initialization.
976 */
977 HWTEST_F(StyleMgrTddTest, InlineStyle005, TestSize.Level0)
978 {
979 StyleMgrTddTest::ACELiteStyleManagerInlineStyle005();
980 }
981
982 /**
983 * @tc.name: ACELiteStyleManagerIDSelector001
984 * @tc.desc: Verify the style sheet initialization.
985 */
986 HWTEST_F(StyleMgrTddTest, IDSelector001, TestSize.Level1)
987 {
988 StyleMgrTddTest::ACELiteStyleManagerIDSelector001();
989 }
990
991 /**
992 * @tc.name: ACELiteStyleManagerIDSelector002
993 * @tc.desc: Verify the style sheet initialization.
994 */
995 HWTEST_F(StyleMgrTddTest, IDSelector002, TestSize.Level1)
996 {
997 StyleMgrTddTest::ACELiteStyleManagerIDSelector002();
998 }
999
1000 /**
1001 * @tc.name: ACELiteStyleManagerIDSelector003
1002 * @tc.desc: Verify the style sheet initialization.
1003 */
1004 HWTEST_F(StyleMgrTddTest, IDSelector003, TestSize.Level1)
1005 {
1006 StyleMgrTddTest::ACELiteStyleManagerIDSelector003();
1007 }
1008
1009 /**
1010 * @tc.name: ACELiteStyleManagerIDSelector004
1011 * @tc.desc: Verify the style sheet initialization.
1012 */
1013 HWTEST_F(StyleMgrTddTest, IDSelector004, TestSize.Level1)
1014 {
1015 StyleMgrTddTest::ACELiteStyleManagerIDSelector004();
1016 }
1017
1018 /**
1019 * @tc.name: ACELiteStyleManagerIDSelector005
1020 * @tc.desc: Verify the style sheet initialization.
1021 */
1022 HWTEST_F(StyleMgrTddTest, IDSelector005, TestSize.Level0)
1023 {
1024 StyleMgrTddTest::ACELiteStyleManagerIDSelector005();
1025 }
1026
1027 /**
1028 * @tc.name: ACELiteStyleManagerClassSelector001
1029 * @tc.desc: Verify the style sheet initialization.
1030 */
1031 HWTEST_F(StyleMgrTddTest, ClassSelector001, TestSize.Level1)
1032 {
1033 StyleMgrTddTest::ACELiteStyleManagerClassSelector001();
1034 }
1035
1036 /**
1037 * @tc.name: ACELiteStyleManagerClassSelector002
1038 * @tc.desc: Verify the style sheet initialization.
1039 */
1040 HWTEST_F(StyleMgrTddTest, ClassSelector002, TestSize.Level1)
1041 {
1042 StyleMgrTddTest::ACELiteStyleManagerClassSelector002();
1043 }
1044
1045 /**
1046 * @tc.name: ACELiteStyleManagerClassSelector003
1047 * @tc.desc: Verify the style sheet initialization.
1048 */
1049 HWTEST_F(StyleMgrTddTest, ClassSelector003, TestSize.Level1)
1050 {
1051 StyleMgrTddTest::ACELiteStyleManagerClassSelector003();
1052 }
1053
1054 /**
1055 * @tc.name: ACELiteStyleManagerCSSPesudo001
1056 * @tc.desc: Verify the style sheet initialization.
1057 */
1058 HWTEST_F(StyleMgrTddTest, CSSPesudo001, TestSize.Level0)
1059 {
1060 StyleMgrTddTest::ACELiteStyleManagerCSSPesudo001();
1061 }
1062 #endif
1063 } // ACELite
1064 } // OHOS
1065
1066