• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include <sstream>
17 #include <string>
18 #include "file_ex.h"
19 #include "gtest/gtest.h"
20 #include "language/language_ui.h"
21 
22 using namespace testing::ext;
23 using namespace Updater;
24 using namespace Lang;
25 using namespace std::string_literals;
26 
27 namespace UpdaterUt {
28 constexpr auto LOCALE_FILE = "/data/updater/ui/locale";
29 constexpr std::array STR_FILES {"/data/updater/ui/l0.json", "/data/updater/ui/l1.json", "/data/updater/ui/l2.json"};
30 constexpr auto KEY_EMPTY_JSON = "/data/updater/ui/keyEmpty.json";
31 constexpr auto STR_NO_EXIST_JSON = "/data/updater/ui/strNotExist.json";
32 constexpr auto ALL_CORRECT_JSON = "/data/updater/ui/correct_all.json";
33 // only correct for one language
34 constexpr std::array CORRECT_JSONS {
35     std::make_tuple(Language::CHINESE, "zh", "/data/updater/ui/correct_zh.json"),
36     std::make_tuple(Language::SPANISH, "es", "/data/updater/ui/correct_es.json"),
37     std::make_tuple(Language::ENGLISH, "en", "/data/updater/ui/correct_en.json")
38 };
39 constexpr std::array LANG_ARRAY { Language::CHINESE, Language::ENGLISH, Language::SPANISH };
40 constexpr std::array LANG_MAP {
41     std::pair {Language::CHINESE, "zh"},
42     std::pair {Language::ENGLISH, "en"},
43     std::pair {Language::SPANISH, "es"}
44 };
GetStrForLang(Language language)45 constexpr auto GetStrForLang(Language language)
46 {
47     for (auto [lang, localeStr] : LANG_MAP) {
48         if (lang == language) {
49             return localeStr;
50         }
51     }
52     return "";
53 }
54 static_assert(GetStrForLang(Language::CHINESE), "zh");
55 static_assert(GetStrForLang(Language::ENGLISH), "en");
56 static_assert(GetStrForLang(Language::SPANISH), "es");
57 
58 class UpdaterUiLangUnitTest : public testing::Test {
59 public:
SetUpTestCase(void)60     static void SetUpTestCase(void) {}
TearDownTestCase(void)61     static void TearDownTestCase(void) {}
SetUp()62     void SetUp() override {}
TearDown()63     void TearDown() override
64     {
65         unlink(LOCALE_FILE);
66     }
67 };
68 
GetTestJson(LangResource & langRes)69 std::string GetTestJson(LangResource &langRes)
70 {
71     std::stringstream ss;
72     ss << R"({"locale":{"localeFile" : ")"s;
73     ss << langRes.localeFile << R"(", "res" : [)";
74     for (auto &res : langRes.res) {
75         ss << R"({"path" : ")";
76         ss << res.path;
77         ss << R"(", "level" :)";
78         ss << res.level;
79         ss << R"(},)";
80     }
81     if (!langRes.res.empty()) {
82         ss.seekp(-1, std::ios_base::end);
83     }
84     ss << "]}}";
85     LOG(INFO) << ss.str();
86     return ss.str();
87 }
88 
89 HWTEST_F(UpdaterUiLangUnitTest, testLoadLangRes, TestSize.Level0)
90 {
91     EXPECT_EQ(LanguageUI::GetInstance().LoadLangRes(JsonNode {}), false);
92     LangResource langRes {};
93     langRes.localeFile = LOCALE_FILE;
94     langRes.res = std::vector<Res> {{"invalidPath", -1}};
95 
96     // level invalid, too small
97     EXPECT_EQ(LanguageUI::GetInstance().LoadLangRes(JsonNode {GetTestJson(langRes)}), false);
98 
99     langRes.res = std::vector<Res> {{"invalidPath", 3}};
100     // level invalid, too big
101     EXPECT_EQ(LanguageUI::GetInstance().LoadLangRes(JsonNode {GetTestJson(langRes)}), false);
102 
103     langRes.res = std::vector<Res> {{"invalidPath", 0}};
104     // locale file not existed
105     EXPECT_EQ(LanguageUI::GetInstance().LoadLangRes(JsonNode {GetTestJson(langRes)}), false);
106 }
107 
108 HWTEST_F(UpdaterUiLangUnitTest, testParseLanguage, TestSize.Level0)
109 {
110     LangResource langRes {};
111     langRes.localeFile = LOCALE_FILE;
112     langRes.res = std::vector<Res> {{"invalidPath", 0}};
113     // set langRes.localeFile
114     EXPECT_EQ(LanguageUI::GetInstance().LoadLangRes(JsonNode {GetTestJson(langRes)}), false);
115     EXPECT_EQ(LanguageUI::GetInstance().ParseLanguage(), Language::CHINESE);
116 
117     EXPECT_EQ(OHOS::SaveStringToFile(LOCALE_FILE, "en"), true);
118     EXPECT_EQ(LanguageUI::GetInstance().ParseLanguage(), Language::ENGLISH);
119 
120     EXPECT_EQ(OHOS::SaveStringToFile(LOCALE_FILE, "zh"), true);
121     EXPECT_EQ(LanguageUI::GetInstance().ParseLanguage(), Language::CHINESE);
122 
123     EXPECT_EQ(OHOS::SaveStringToFile(LOCALE_FILE, "es"), true);
124     EXPECT_EQ(LanguageUI::GetInstance().ParseLanguage(), Language::SPANISH);
125 
126     EXPECT_EQ(OHOS::SaveStringToFile(LOCALE_FILE, "invalid"), true);
127     EXPECT_EQ(LanguageUI::GetInstance().ParseLanguage(), Language::CHINESE);
128 }
129 
130 HWTEST_F(UpdaterUiLangUnitTest, testInit01, TestSize.Level0)
131 {
132     LangResource langRes {};
133     langRes.localeFile = LOCALE_FILE;
134     langRes.res = std::vector<Res> {{"invalidPath", 0}};
135     EXPECT_EQ(LanguageUI::GetInstance().LoadLangRes(JsonNode {GetTestJson(langRes)}), false);
136     EXPECT_EQ(LanguageUI::GetInstance().Init(Language::CHINESE), false);
137     EXPECT_EQ(LanguageUI::GetInstance().Init(Language::ENGLISH), false);
138     EXPECT_EQ(LanguageUI::GetInstance().Init(Language::SPANISH), false);
139 }
140 
141 HWTEST_F(UpdaterUiLangUnitTest, testInit02, TestSize.Level0)
142 {
143     LangResource langRes {};
144     langRes.localeFile = LOCALE_FILE;
145     langRes.res = std::vector<Res> {{KEY_EMPTY_JSON, 0}};
146     EXPECT_EQ(LanguageUI::GetInstance().LoadLangRes(JsonNode {GetTestJson(langRes)}), false);
147     EXPECT_EQ(OHOS::FileExists(KEY_EMPTY_JSON), true) << KEY_EMPTY_JSON << " not exist";
148     EXPECT_EQ(LanguageUI::GetInstance().Init(Language::CHINESE), false);
149     EXPECT_EQ(LanguageUI::GetInstance().Init(Language::ENGLISH), false);
150     EXPECT_EQ(LanguageUI::GetInstance().Init(Language::SPANISH), false);
151 }
152 
153 HWTEST_F(UpdaterUiLangUnitTest, testInit03, TestSize.Level0)
154 {
155     LangResource langRes {};
156     langRes.localeFile = LOCALE_FILE;
157     langRes.res = std::vector<Res> {{STR_NO_EXIST_JSON, 0}};
158     EXPECT_EQ(LanguageUI::GetInstance().LoadLangRes(JsonNode {GetTestJson(langRes)}), false);
159     EXPECT_EQ(OHOS::FileExists(STR_NO_EXIST_JSON), true) << STR_NO_EXIST_JSON << " not exist";
160     EXPECT_EQ(LanguageUI::GetInstance().Init(Language::CHINESE), false);
161     EXPECT_EQ(LanguageUI::GetInstance().Init(Language::ENGLISH), false);
162     EXPECT_EQ(LanguageUI::GetInstance().Init(Language::SPANISH), false);
163 }
164 
165 HWTEST_F(UpdaterUiLangUnitTest, testInit04, TestSize.Level0)
166 {
167     LangResource langRes {};
168     langRes.localeFile = LOCALE_FILE;
169     langRes.res = std::vector<Res> {{ALL_CORRECT_JSON, 0}};
170     EXPECT_EQ(LanguageUI::GetInstance().LoadLangRes(JsonNode {GetTestJson(langRes)}), true);
171     EXPECT_EQ(OHOS::FileExists(ALL_CORRECT_JSON), true) << ALL_CORRECT_JSON << " not exist";
172     EXPECT_EQ(LanguageUI::GetInstance().Init(Language::CHINESE), true);
173     EXPECT_EQ(LanguageUI::GetInstance().Init(Language::ENGLISH), true);
174     EXPECT_EQ(LanguageUI::GetInstance().Init(Language::SPANISH), true);
175 
176     for (auto [langType, locale, filePath] : CORRECT_JSONS) {
177         langRes.res = std::vector<Res> {{filePath, 0}};
178         EXPECT_EQ(OHOS::SaveStringToFile(LOCALE_FILE, GetStrForLang(langType)), true);
179         EXPECT_EQ(LanguageUI::GetInstance().LoadLangRes(JsonNode {GetTestJson(langRes)}), true);
180         EXPECT_EQ(OHOS::FileExists(filePath), true) << filePath << " not exist";
181         for (auto lang : LANG_ARRAY) {
182             EXPECT_EQ(LanguageUI::GetInstance().Init(lang), lang == langType);
183         }
184     }
185 }
186 
187 HWTEST_F(UpdaterUiLangUnitTest, testTranslate01, TestSize.Level0)
188 {
189     LangResource langRes {};
190     langRes.localeFile = LOCALE_FILE;
191     langRes.res = std::vector<Res> {{STR_FILES[0], 0}};
192 
193     EXPECT_EQ(OHOS::SaveStringToFile(LOCALE_FILE, "en"), true);
194     EXPECT_EQ(LanguageUI::GetInstance().LoadLangRes(JsonNode {GetTestJson(langRes)}), true);
195     EXPECT_EQ(LanguageUI::GetInstance().Translate("REBOOT_DEVICE"), "reboot devices");
196     EXPECT_EQ(LanguageUI::GetInstance().Translate("SHUTDOWN_DEVICE"), "shutdown device");
197     EXPECT_EQ(LanguageUI::GetInstance().Translate("FACTORY_RESET"), "factory reset");
198     EXPECT_EQ(LanguageUI::GetInstance().Translate("NOT_EXIST_KEY"), "");
199 
200     EXPECT_EQ(OHOS::SaveStringToFile(LOCALE_FILE, "zh"), true);
201     EXPECT_EQ(LanguageUI::GetInstance().LoadLangRes(JsonNode {GetTestJson(langRes)}), true);
202     EXPECT_EQ(LanguageUI::GetInstance().Translate("REBOOT_DEVICE"), "重启设备");
203     EXPECT_EQ(LanguageUI::GetInstance().Translate("SHUTDOWN_DEVICE"), "关闭设备");
204     EXPECT_EQ(LanguageUI::GetInstance().Translate("FACTORY_RESET"), "清除用户数据");
205     EXPECT_EQ(LanguageUI::GetInstance().Translate("NOT_EXIST_KEY"), "");
206 }
207 
208 HWTEST_F(UpdaterUiLangUnitTest, testTranslate02, TestSize.Level1)
209 {
210     LangResource langRes {};
211     langRes.localeFile = LOCALE_FILE;
212     langRes.res = std::vector<Res> {{STR_FILES[0], 0}, {STR_FILES[1], 1}};
213 
214     EXPECT_EQ(OHOS::SaveStringToFile(LOCALE_FILE, "en"), true);
215     EXPECT_EQ(LanguageUI::GetInstance().LoadLangRes(JsonNode {GetTestJson(langRes)}), true);
216     EXPECT_EQ(LanguageUI::GetInstance().Translate("REBOOT_DEVICE"), "reboot devices");
217     EXPECT_EQ(LanguageUI::GetInstance().Translate("SHUTDOWN_DEVICE"), "shutdown device");
218     EXPECT_EQ(LanguageUI::GetInstance().Translate("FACTORY_RESET"), "factory reset");
219     EXPECT_EQ(LanguageUI::GetInstance().Translate("SDCARD_UPDATE"), "update from sdcard");
220     EXPECT_EQ(LanguageUI::GetInstance().Translate("NOT_EXIST_KEY"), "default text");
221 
222     EXPECT_EQ(OHOS::SaveStringToFile(LOCALE_FILE, "zh"), true);
223     EXPECT_EQ(LanguageUI::GetInstance().LoadLangRes(JsonNode {GetTestJson(langRes)}), true);
224     EXPECT_EQ(LanguageUI::GetInstance().Translate("REBOOT_DEVICE"), "重启设备");
225     EXPECT_EQ(LanguageUI::GetInstance().Translate("SHUTDOWN_DEVICE"), "关闭设备");
226     EXPECT_EQ(LanguageUI::GetInstance().Translate("FACTORY_RESET"), "清除用户数据");
227     EXPECT_EQ(LanguageUI::GetInstance().Translate("SDCARD_UPDATE"), "SD卡升级");
228     EXPECT_EQ(LanguageUI::GetInstance().Translate("NOT_EXIST_KEY"), "默认文字");
229 }
230 
231 HWTEST_F(UpdaterUiLangUnitTest, testTranslate03, TestSize.Level1)
232 {
233     LangResource langRes {};
234     langRes.localeFile = LOCALE_FILE;
235     langRes.res = std::vector<Res> {{STR_FILES[0], 0}, {STR_FILES[1], 1}, {STR_FILES[2], 2}};
236 
237     EXPECT_EQ(OHOS::SaveStringToFile(LOCALE_FILE, "en"), true);
238     EXPECT_EQ(LanguageUI::GetInstance().LoadLangRes(JsonNode {GetTestJson(langRes)}), true);
239     EXPECT_EQ(LanguageUI::GetInstance().Translate("REBOOT_DEVICE"), "reboot devices");
240     EXPECT_EQ(LanguageUI::GetInstance().Translate("SHUTDOWN_DEVICE"), "shutdown");
241     EXPECT_EQ(LanguageUI::GetInstance().Translate("FACTORY_RESET"), "factory reset");
242     EXPECT_EQ(LanguageUI::GetInstance().Translate("SDCARD_UPDATE"), "sdcard update");
243     EXPECT_EQ(LanguageUI::GetInstance().Translate("NOT_EXIST_KEY"), "default");
244 
245     EXPECT_EQ(OHOS::SaveStringToFile(LOCALE_FILE, "zh"), true);
246     EXPECT_EQ(LanguageUI::GetInstance().LoadLangRes(JsonNode {GetTestJson(langRes)}), true);
247     EXPECT_EQ(LanguageUI::GetInstance().Translate("REBOOT_DEVICE"), "重启设备");
248     EXPECT_EQ(LanguageUI::GetInstance().Translate("SHUTDOWN_DEVICE"), "关机");
249     EXPECT_EQ(LanguageUI::GetInstance().Translate("FACTORY_RESET"), "恢复出厂设置");
250     EXPECT_EQ(LanguageUI::GetInstance().Translate("SDCARD_UPDATE"), "SD卡升级");
251     EXPECT_EQ(LanguageUI::GetInstance().Translate("NOT_EXIST_KEY"), "默认");
252 }
253 }
254