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 
ReleaseComponent(Component * & component) const57 void BaseTest::ReleaseComponent(Component *&component) const
58 {
59     if (component != nullptr) {
60         component->Release();
61         delete component;
62         component = nullptr;
63     }
64 }
65 
GetRGBColor(int32_t colorIntValue) const66 ColorType BaseTest::GetRGBColor(int32_t colorIntValue) const
67 {
68     uint32_t colorValue = colorIntValue;
69     uint8_t red8 = uint8_t((colorValue & TEXT_RED_COLOR_MASK) >> RED_COLOR_START_BIT);
70     uint8_t green8 = uint8_t((colorValue & TEXT_GREEN_COLOR_MASK) >> GREEN_COLOR_START_BIT);
71     uint8_t blue8 = uint8_t((colorValue & TEXT_BLUE_COLOR_MASK));
72     return Color::GetColorFromRGB(red8, green8, blue8);
73 }
74 
UpdateNumAttributeOrStyleValue(Component * component,const char * attributeName,const int32_t newNumValue,const bool isToSetAttribute) const75 void BaseTest::UpdateNumAttributeOrStyleValue(Component *component,
76                                               const char *attributeName,
77                                               const int32_t newNumValue,
78                                               const bool isToSetAttribute) const
79 {
80     if (component == nullptr) {
81         HILOG_WARN(HILOG_MODULE_ACE, "UpdateNumAttributeOrStyleValue component is null\n");
82         return;
83     }
84     jerry_value_t attrName = jerry_create_string(reinterpret_cast<const jerry_char_t *>(attributeName));
85     jerry_value_t attrValue = jerry_create_number(newNumValue);
86     if (isToSetAttribute) {
87         jerry_set_property(attrsObj_, attrName, attrValue);
88     } else {
89         jerry_set_property(styleObj_, attrName, attrValue);
90     }
91     component->UpdateView(KeyParser::ParseKeyId(attributeName), attrValue);
92     jerry_release_value(attrName);
93     jerry_release_value(attrValue);
94 }
95 
UpdateCharAttributeOrStyleValue(Component * component,const char * attributeName,const char * newCharValue,const bool isToSetAttribute) const96 void BaseTest::UpdateCharAttributeOrStyleValue(Component *component,
97                                                const char *attributeName,
98                                                const char *newCharValue,
99                                                const bool isToSetAttribute) const
100 {
101     if (component == nullptr) {
102         HILOG_WARN(HILOG_MODULE_ACE, "UpdateCharAttributeOrStyleValue component is null\n");
103         return;
104     }
105     jerry_value_t attrName = jerry_create_string(reinterpret_cast<const jerry_char_t *>(attributeName));
106     jerry_value_t attrValue = jerry_create_string(reinterpret_cast<const jerry_char_t *>(newCharValue));
107     if (isToSetAttribute) {
108         jerry_set_property(attrsObj_, attrName, attrValue);
109     } else {
110         jerry_set_property(styleObj_, attrName, attrValue);
111     }
112     component->UpdateView(KeyParser::ParseKeyId(attributeName), attrValue);
113     jerry_release_value(attrName);
114     jerry_release_value(attrValue);
115 }
116 
SetCompnentNameId(const char * componentName)117 uint16_t BaseTest::SetCompnentNameId(const char *componentName)
118 {
119     if (componentName == nullptr) {
120         HILOG_WARN(HILOG_MODULE_ACE, "null component name\n");
121         return K_UNKNOWN;
122     }
123     uint8_t maxLength = 9;
124     char *tarComponentName = reinterpret_cast<char *>(malloc(maxLength));
125     if (tarComponentName == nullptr) {
126         HILOG_WARN(HILOG_MODULE_ACE, "alloc memory fail\n");
127         return K_UNKNOWN;
128     }
129     tarComponentName[0] = '\0';
130     bool copyRes = false;
131     if (!strcmp(componentName, "progress")) {
132         if (strcpy_s(tarComponentName, maxLength, "progress") == 0)
133             copyRes = true;
134     } else if (!strcmp(componentName, "chart")) {
135         if (strcpy_s(tarComponentName, maxLength, "chart") == 0)
136             copyRes = true;
137     } else if (!strcmp(componentName, "marquee")) {
138         if (strcpy_s(tarComponentName, maxLength, "marquee") == 0)
139             copyRes = true;
140     }
141 
142     if (copyRes) {
143         componentNameId_ = KeyParser::ParseKeyId(tarComponentName, strlen(tarComponentName));
144     } else {
145         componentNameId_ = K_UNKNOWN;
146     }
147     free(tarComponentName);
148     tarComponentName = nullptr;
149     return componentNameId_;
150 }
151 } // namespace ACELite
152 } // namespace OHOS
153