12b7e0694Sopenharmony_ci/*
22b7e0694Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
32b7e0694Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
42b7e0694Sopenharmony_ci * you may not use this file except in compliance with the License.
52b7e0694Sopenharmony_ci * You may obtain a copy of the License at
62b7e0694Sopenharmony_ci *
72b7e0694Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
82b7e0694Sopenharmony_ci *
92b7e0694Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
102b7e0694Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
112b7e0694Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
122b7e0694Sopenharmony_ci * See the License for the specific language governing permissions and
132b7e0694Sopenharmony_ci * limitations under the License.
142b7e0694Sopenharmony_ci */
152b7e0694Sopenharmony_ci
162b7e0694Sopenharmony_ci#include "base_test.h"
172b7e0694Sopenharmony_ci
182b7e0694Sopenharmony_ci#include "js_debugger_config.h"
192b7e0694Sopenharmony_ci
202b7e0694Sopenharmony_cinamespace OHOS {
212b7e0694Sopenharmony_cinamespace ACELite {
222b7e0694Sopenharmony_ciBaseTest::BaseTest() : globObj_(0), attrsObj_(0), styleObj_(0), componentNameId_(0) {}
232b7e0694Sopenharmony_ci
242b7e0694Sopenharmony_civoid BaseTest::SetUp(void)
252b7e0694Sopenharmony_ci{
262b7e0694Sopenharmony_ci    Debugger::GetInstance().SetupJSContext();
272b7e0694Sopenharmony_ci    jerry_init(JERRY_INIT_EMPTY);
282b7e0694Sopenharmony_ci
292b7e0694Sopenharmony_ci    globObj_ = jerry_get_global_object();
302b7e0694Sopenharmony_ci    attrsObj_ = jerry_create_object();
312b7e0694Sopenharmony_ci    JerrySetNamedProperty(globObj_, "attrs", attrsObj_);
322b7e0694Sopenharmony_ci
332b7e0694Sopenharmony_ci    styleObj_ = jerry_create_object();
342b7e0694Sopenharmony_ci    JerrySetNamedProperty(globObj_, "staticStyle", styleObj_);
352b7e0694Sopenharmony_ci    rootComponentMock_.PrepareRootContainer();
362b7e0694Sopenharmony_ci}
372b7e0694Sopenharmony_ci
382b7e0694Sopenharmony_civoid BaseTest::TearDown()
392b7e0694Sopenharmony_ci{
402b7e0694Sopenharmony_ci    JsAppContext::GetInstance()->ReleaseStyles();
412b7e0694Sopenharmony_ci    jerry_release_value(attrsObj_);
422b7e0694Sopenharmony_ci    jerry_release_value(styleObj_);
432b7e0694Sopenharmony_ci    jerry_release_value(globObj_);
442b7e0694Sopenharmony_ci    jerry_cleanup();
452b7e0694Sopenharmony_ci    Debugger::GetInstance().ReleaseJSContext();
462b7e0694Sopenharmony_ci}
472b7e0694Sopenharmony_ci
482b7e0694Sopenharmony_ciComponent *BaseTest::GetRenderedComponent(uint16_t componentKeyId) const
492b7e0694Sopenharmony_ci{
502b7e0694Sopenharmony_ci    jerry_value_t children = jerry_create_null();
512b7e0694Sopenharmony_ci    Component *component = ComponentFactory::CreateComponent(componentKeyId, globObj_, children);
522b7e0694Sopenharmony_ci    rootComponentMock_.RenderComponent(*component);
532b7e0694Sopenharmony_ci    jerry_release_value(children);
542b7e0694Sopenharmony_ci    return component;
552b7e0694Sopenharmony_ci}
562b7e0694Sopenharmony_ci
572b7e0694Sopenharmony_civoid BaseTest::ReleaseComponent(Component *&component) const
582b7e0694Sopenharmony_ci{
592b7e0694Sopenharmony_ci    if (component != nullptr) {
602b7e0694Sopenharmony_ci        component->Release();
612b7e0694Sopenharmony_ci        delete component;
622b7e0694Sopenharmony_ci        component = nullptr;
632b7e0694Sopenharmony_ci    }
642b7e0694Sopenharmony_ci}
652b7e0694Sopenharmony_ci
662b7e0694Sopenharmony_ciColorType BaseTest::GetRGBColor(int32_t colorIntValue) const
672b7e0694Sopenharmony_ci{
682b7e0694Sopenharmony_ci    uint32_t colorValue = colorIntValue;
692b7e0694Sopenharmony_ci    uint8_t red8 = uint8_t((colorValue & TEXT_RED_COLOR_MASK) >> RED_COLOR_START_BIT);
702b7e0694Sopenharmony_ci    uint8_t green8 = uint8_t((colorValue & TEXT_GREEN_COLOR_MASK) >> GREEN_COLOR_START_BIT);
712b7e0694Sopenharmony_ci    uint8_t blue8 = uint8_t((colorValue & TEXT_BLUE_COLOR_MASK));
722b7e0694Sopenharmony_ci    return Color::GetColorFromRGB(red8, green8, blue8);
732b7e0694Sopenharmony_ci}
742b7e0694Sopenharmony_ci
752b7e0694Sopenharmony_civoid BaseTest::UpdateNumAttributeOrStyleValue(Component *component,
762b7e0694Sopenharmony_ci                                              const char *attributeName,
772b7e0694Sopenharmony_ci                                              const int32_t newNumValue,
782b7e0694Sopenharmony_ci                                              const bool isToSetAttribute) const
792b7e0694Sopenharmony_ci{
802b7e0694Sopenharmony_ci    if (component == nullptr) {
812b7e0694Sopenharmony_ci        HILOG_WARN(HILOG_MODULE_ACE, "UpdateNumAttributeOrStyleValue component is null\n");
822b7e0694Sopenharmony_ci        return;
832b7e0694Sopenharmony_ci    }
842b7e0694Sopenharmony_ci    jerry_value_t attrName = jerry_create_string(reinterpret_cast<const jerry_char_t *>(attributeName));
852b7e0694Sopenharmony_ci    jerry_value_t attrValue = jerry_create_number(newNumValue);
862b7e0694Sopenharmony_ci    if (isToSetAttribute) {
872b7e0694Sopenharmony_ci        jerry_set_property(attrsObj_, attrName, attrValue);
882b7e0694Sopenharmony_ci    } else {
892b7e0694Sopenharmony_ci        jerry_set_property(styleObj_, attrName, attrValue);
902b7e0694Sopenharmony_ci    }
912b7e0694Sopenharmony_ci    component->UpdateView(KeyParser::ParseKeyId(attributeName), attrValue);
922b7e0694Sopenharmony_ci    jerry_release_value(attrName);
932b7e0694Sopenharmony_ci    jerry_release_value(attrValue);
942b7e0694Sopenharmony_ci}
952b7e0694Sopenharmony_ci
962b7e0694Sopenharmony_civoid BaseTest::UpdateCharAttributeOrStyleValue(Component *component,
972b7e0694Sopenharmony_ci                                               const char *attributeName,
982b7e0694Sopenharmony_ci                                               const char *newCharValue,
992b7e0694Sopenharmony_ci                                               const bool isToSetAttribute) const
1002b7e0694Sopenharmony_ci{
1012b7e0694Sopenharmony_ci    if (component == nullptr) {
1022b7e0694Sopenharmony_ci        HILOG_WARN(HILOG_MODULE_ACE, "UpdateCharAttributeOrStyleValue component is null\n");
1032b7e0694Sopenharmony_ci        return;
1042b7e0694Sopenharmony_ci    }
1052b7e0694Sopenharmony_ci    jerry_value_t attrName = jerry_create_string(reinterpret_cast<const jerry_char_t *>(attributeName));
1062b7e0694Sopenharmony_ci    jerry_value_t attrValue = jerry_create_string(reinterpret_cast<const jerry_char_t *>(newCharValue));
1072b7e0694Sopenharmony_ci    if (isToSetAttribute) {
1082b7e0694Sopenharmony_ci        jerry_set_property(attrsObj_, attrName, attrValue);
1092b7e0694Sopenharmony_ci    } else {
1102b7e0694Sopenharmony_ci        jerry_set_property(styleObj_, attrName, attrValue);
1112b7e0694Sopenharmony_ci    }
1122b7e0694Sopenharmony_ci    component->UpdateView(KeyParser::ParseKeyId(attributeName), attrValue);
1132b7e0694Sopenharmony_ci    jerry_release_value(attrName);
1142b7e0694Sopenharmony_ci    jerry_release_value(attrValue);
1152b7e0694Sopenharmony_ci}
1162b7e0694Sopenharmony_ci
1172b7e0694Sopenharmony_ciuint16_t BaseTest::SetCompnentNameId(const char *componentName)
1182b7e0694Sopenharmony_ci{
1192b7e0694Sopenharmony_ci    if (componentName == nullptr) {
1202b7e0694Sopenharmony_ci        HILOG_WARN(HILOG_MODULE_ACE, "null component name\n");
1212b7e0694Sopenharmony_ci        return K_UNKNOWN;
1222b7e0694Sopenharmony_ci    }
1232b7e0694Sopenharmony_ci    uint8_t maxLength = 9;
1242b7e0694Sopenharmony_ci    char *tarComponentName = reinterpret_cast<char *>(malloc(maxLength));
1252b7e0694Sopenharmony_ci    if (tarComponentName == nullptr) {
1262b7e0694Sopenharmony_ci        HILOG_WARN(HILOG_MODULE_ACE, "alloc memory fail\n");
1272b7e0694Sopenharmony_ci        return K_UNKNOWN;
1282b7e0694Sopenharmony_ci    }
1292b7e0694Sopenharmony_ci    tarComponentName[0] = '\0';
1302b7e0694Sopenharmony_ci    bool copyRes = false;
1312b7e0694Sopenharmony_ci    if (!strcmp(componentName, "progress")) {
1322b7e0694Sopenharmony_ci        if (strcpy_s(tarComponentName, maxLength, "progress") == 0)
1332b7e0694Sopenharmony_ci            copyRes = true;
1342b7e0694Sopenharmony_ci    } else if (!strcmp(componentName, "chart")) {
1352b7e0694Sopenharmony_ci        if (strcpy_s(tarComponentName, maxLength, "chart") == 0)
1362b7e0694Sopenharmony_ci            copyRes = true;
1372b7e0694Sopenharmony_ci    } else if (!strcmp(componentName, "marquee")) {
1382b7e0694Sopenharmony_ci        if (strcpy_s(tarComponentName, maxLength, "marquee") == 0)
1392b7e0694Sopenharmony_ci            copyRes = true;
1402b7e0694Sopenharmony_ci    }
1412b7e0694Sopenharmony_ci
1422b7e0694Sopenharmony_ci    if (copyRes) {
1432b7e0694Sopenharmony_ci        componentNameId_ = KeyParser::ParseKeyId(tarComponentName, strlen(tarComponentName));
1442b7e0694Sopenharmony_ci    } else {
1452b7e0694Sopenharmony_ci        componentNameId_ = K_UNKNOWN;
1462b7e0694Sopenharmony_ci    }
1472b7e0694Sopenharmony_ci    free(tarComponentName);
1482b7e0694Sopenharmony_ci    tarComponentName = nullptr;
1492b7e0694Sopenharmony_ci    return componentNameId_;
1502b7e0694Sopenharmony_ci}
1512b7e0694Sopenharmony_ci} // namespace ACELite
1522b7e0694Sopenharmony_ci} // namespace OHOS
153