1 /* 2 * Copyright (c) 2022 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 #include "common/text.h" 16 #include "file_ex.h" 17 #include "gtest/gtest.h" 18 #include "json_node.h" 19 #include "language_ui.h" 20 #include "log.h" 21 #include "view_api.h" 22 23 namespace { 24 using namespace Updater; 25 using namespace Lang; 26 using namespace testing::ext; 27 28 class UpdaterUiViewApiTest : public testing::Test { 29 public: SetUpTestCase(void)30 static void SetUpTestCase(void) {} TearDownTestCase(void)31 static void TearDownTestCase(void) {} SetUp()32 void SetUp() override {} TearDown()33 void TearDown() override {} 34 }; 35 36 HWTEST_F(UpdaterUiViewApiTest, test_get_color_from_str, TestSize.Level0) 37 { 38 static const std::vector<std::pair<const char *, std::pair<OHOS::ColorType, bool>>> colorDatas { 39 {"#00ffffff", {OHOS::Color::GetColorFromRGBA(0, 0xff, 0xff, 0xff), true}}, 40 {"#aabbccdd", {OHOS::Color::GetColorFromRGBA(0xaa, 0xbb, 0xcc, 0xdd), true}}, 41 {"#AABBCCDD", {OHOS::Color::GetColorFromRGBA(0xaa, 0xbb, 0xcc, 0xdd), true}}, 42 {"#FFFFFFGF", {OHOS::Color::GetColorFromRGBA(0, 0, 0, 0xff), false}}, 43 {"#0011223", {OHOS::Color::GetColorFromRGBA(0, 0, 0, 0xff), false}}, 44 {"#001122333", {OHOS::Color::GetColorFromRGBA(0, 0, 0, 0xff), false}}, 45 {"A1B2C3D4", {OHOS::Color::GetColorFromRGBA(0, 0, 0, 0xff), false}}, 46 }; 47 bool isValid {}; 48 for (auto &colorData : colorDatas) { 49 isValid = Updater::CheckColor(colorData.first); 50 ASSERT_EQ(isValid, colorData.second.second); 51 if (isValid) { 52 EXPECT_EQ(Updater::StrToColor(colorData.first).full, colorData.second.first.full); 53 } 54 } 55 } 56 57 HWTEST_F(UpdaterUiViewApiTest, test_translate_text, TestSize.Level0) 58 { 59 constexpr auto localeFile = "/data/updater/locale"; 60 EXPECT_EQ(OHOS::SaveStringToFile(localeFile, "en"), true); 61 string jsonStr = R"({"locale":{ 62 "res" : [ 63 { 64 "path" : "/data/updater/ui/l0.json", 65 "level" : 0 66 } 67 ], 68 "localeFile" : "/data/updater/locale"}})"; 69 EXPECT_EQ(LanguageUI::GetInstance().LoadLangRes(JsonNode {jsonStr}), true); 70 EXPECT_EQ(TranslateText("[]"), "[]"); 71 EXPECT_EQ(TranslateText("{REBOOT_DEVICE]"), "{REBOOT_DEVICE]"); 72 EXPECT_EQ(TranslateText("[REBOOT_DEVICE}"), "[REBOOT_DEVICE}"); 73 EXPECT_EQ(TranslateText("[REBOOT_DEVICE]"), "reboot devices"); 74 EXPECT_EQ(TranslateText("reboot devices"), "reboot devices"); 75 unlink(localeFile); 76 } 77 78 HWTEST_F(UpdaterUiViewApiTest, test_get_align, TestSize.Level0) 79 { 80 EXPECT_EQ(GetAlign("left"), OHOS::TEXT_ALIGNMENT_LEFT); 81 EXPECT_EQ(GetAlign("right"), OHOS::TEXT_ALIGNMENT_RIGHT); 82 EXPECT_EQ(GetAlign("center"), OHOS::TEXT_ALIGNMENT_CENTER); 83 EXPECT_EQ(GetAlign("top"), OHOS::TEXT_ALIGNMENT_TOP); 84 EXPECT_EQ(GetAlign("bottom"), OHOS::TEXT_ALIGNMENT_BOTTOM); 85 EXPECT_EQ(GetAlign("unknow"), OHOS::TEXT_ALIGNMENT_CENTER); 86 } 87 }