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 "base_test.h"
17
18 #include "js_debugger_config.h"
19
20 namespace OHOS {
21 namespace ACELite {
BaseTest()22 BaseTest::BaseTest() : globObj_(0), attrsObj_(0), styleObj_(0), componentNameId_(0) {}
23
SetUp(void)24 void BaseTest::SetUp(void)
25 {
26 Debugger::GetInstance().SetupJSContext();
27 jerry_init(JERRY_INIT_EMPTY);
28
29 globObj_ = jerry_get_global_object();
30 attrsObj_ = jerry_create_object();
31 JerrySetNamedProperty(globObj_, "attrs", attrsObj_);
32
33 styleObj_ = jerry_create_object();
34 JerrySetNamedProperty(globObj_, "staticStyle", styleObj_);
35 rootComponentMock_.PrepareRootContainer();
36 }
37
TearDown()38 void BaseTest::TearDown()
39 {
40 JsAppContext::GetInstance()->ReleaseStyles();
41 jerry_release_value(attrsObj_);
42 jerry_release_value(styleObj_);
43 jerry_release_value(globObj_);
44 jerry_cleanup();
45 Debugger::GetInstance().ReleaseJSContext();
46 }
47
GetRenderedComponent(uint16_t componentKeyId) const48 Component *BaseTest::GetRenderedComponent(uint16_t componentKeyId) const
49 {
50 jerry_value_t children = jerry_create_null();
51 Component *component = ComponentFactory::CreateComponent(componentKeyId, globObj_, children);
52 rootComponentMock_.RenderComponent(*component);
53 jerry_release_value(children);
54 return component;
55 }
56
GetRGBColor(int32_t colorIntValue) const57 ColorType BaseTest::GetRGBColor(int32_t colorIntValue) const
58 {
59 uint32_t colorValue = colorIntValue;
60 uint8_t red8 = uint8_t((colorValue & TEXT_RED_COLOR_MASK) >> RED_COLOR_START_BIT);
61 uint8_t green8 = uint8_t((colorValue & TEXT_GREEN_COLOR_MASK) >> GREEN_COLOR_START_BIT);
62 uint8_t blue8 = uint8_t((colorValue & TEXT_BLUE_COLOR_MASK));
63 return Color::GetColorFromRGB(red8, green8, blue8);
64 }
65
UpdateNumAttributeOrStyleValue(Component * component,const char * attributeName,const int32_t newNumValue,const bool isToSetAttribute) const66 void BaseTest::UpdateNumAttributeOrStyleValue(Component *component,
67 const char *attributeName,
68 const int32_t newNumValue,
69 const bool isToSetAttribute) const
70 {
71 if (component == nullptr) {
72 HILOG_WARN(HILOG_MODULE_ACE, "UpdateNumAttributeOrStyleValue component is null\n");
73 return;
74 }
75 jerry_value_t attrName = jerry_create_string(reinterpret_cast<const jerry_char_t *>(attributeName));
76 jerry_value_t attrValue = jerry_create_number(newNumValue);
77 if (isToSetAttribute) {
78 jerry_set_property(attrsObj_, attrName, attrValue);
79 } else {
80 jerry_set_property(styleObj_, attrName, attrValue);
81 }
82 component->UpdateView(KeyParser::ParseKeyId(attributeName), attrValue);
83 jerry_release_value(attrName);
84 jerry_release_value(attrValue);
85 }
86
UpdateCharAttributeOrStyleValue(Component * component,const char * attributeName,const char * newCharValue,const bool isToSetAttribute) const87 void BaseTest::UpdateCharAttributeOrStyleValue(Component *component,
88 const char *attributeName,
89 const char *newCharValue,
90 const bool isToSetAttribute) const
91 {
92 if (component == nullptr) {
93 HILOG_WARN(HILOG_MODULE_ACE, "UpdateCharAttributeOrStyleValue component is null\n");
94 return;
95 }
96 jerry_value_t attrName = jerry_create_string(reinterpret_cast<const jerry_char_t *>(attributeName));
97 jerry_value_t attrValue = jerry_create_string(reinterpret_cast<const jerry_char_t *>(newCharValue));
98 if (isToSetAttribute) {
99 jerry_set_property(attrsObj_, attrName, attrValue);
100 } else {
101 jerry_set_property(styleObj_, attrName, attrValue);
102 }
103 component->UpdateView(KeyParser::ParseKeyId(attributeName), attrValue);
104 jerry_release_value(attrName);
105 jerry_release_value(attrValue);
106 }
107
SetCompnentNameId(const char * componentName)108 uint16_t BaseTest::SetCompnentNameId(const char *componentName)
109 {
110 if (componentName == nullptr) {
111 HILOG_WARN(HILOG_MODULE_ACE, "null component name\n");
112 return K_UNKNOWN;
113 }
114 uint8_t maxLength = 9;
115 char *tarComponentName = reinterpret_cast<char *>(malloc(maxLength));
116 if (tarComponentName == nullptr) {
117 HILOG_WARN(HILOG_MODULE_ACE, "alloc memory fail\n");
118 return K_UNKNOWN;
119 }
120 tarComponentName[0] = '\0';
121 bool copyRes = false;
122 if (!strcmp(componentName, "progress")) {
123 if (strcpy_s(tarComponentName, maxLength, "progress") == 0)
124 copyRes = true;
125 } else if (!strcmp(componentName, "chart")) {
126 if (strcpy_s(tarComponentName, maxLength, "chart") == 0)
127 copyRes = true;
128 } else if (!strcmp(componentName, "marquee")) {
129 if (strcpy_s(tarComponentName, maxLength, "marquee") == 0)
130 copyRes = true;
131 }
132
133 if (copyRes) {
134 componentNameId_ = KeyParser::ParseKeyId(tarComponentName, strlen(tarComponentName));
135 } else {
136 componentNameId_ = K_UNKNOWN;
137 }
138 free(tarComponentName);
139 tarComponentName = nullptr;
140 return componentNameId_;
141 }
142 } // namespace ACELite
143 } // namespace OHOS
144