1 /*
2 * Copyright (c) 2025 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 "edm_errors.h"
17 #include "hilog_wrapper.h"
18 #include "locale_config.h"
19 #include "locale_matcher.h"
20 #include "notification_locale.h"
21 #include <gtest/gtest.h>
22
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace ExternalDeviceManager {
27 static std::string g_localJson = R"(
28 {
29 "string": [
30 {
31 "name": "usb_transmission_error_title",
32 "value": "USB Transmission Error"
33 },
34 {
35 "name": "usb_troubleshoot_message",
36 "value": "Click to view error details and solutions"
37 }
38 ]
39 })";
40
41 const std::string localJsonFilePath = "./local_string.json";
42
43 class NotificationLocaleTest : public testing::Test {
44 public:
45 static void SetUpTestCase(void);
46 static void TearDownTestCase(void);
47 void SetUp();
48 void TearDown();
49 };
50
SetUpTestCase(void)51 void NotificationLocaleTest::SetUpTestCase(void)
52 {
53 std::ofstream ofs(localJsonFilePath);
54 if (ofs.is_open()) {
55 ofs << g_localJson;
56 ofs.close();
57 }
58 }
TearDownTestCase(void)59 void NotificationLocaleTest::TearDownTestCase(void)
60 {
61 if (access(localJsonFilePath.c_str(), F_OK) == 0) {
62 if (remove(localJsonFilePath.c_str()) != 0) {
63 EDM_LOGE(MODULE_SERVICE, "Failed to remove file: %{public}s", localJsonFilePath.c_str());
64 }
65 }
66 }
SetUp(void)67 void NotificationLocaleTest::SetUp(void) {}
TearDown(void)68 void NotificationLocaleTest::TearDown(void) {}
69
70 /**
71 * @tc.name: ParseLocaleCfg001
72 * @tc.desc: Test ParseLocaleCfg
73 * @tc.type: FUNC
74 */
75 HWTEST_F(NotificationLocaleTest, ParseLocaleCfg001, TestSize.Level1)
76 {
77 EDM_LOGI(MODULE_SERVICE, "ParseLocaleCfg001 begin");
78 auto ¬ificationLocale = NotificationLocale::GetInstance();
79 EXPECT_NE(¬ificationLocale, nullptr);
80 EXPECT_TRUE(notificationLocale.languageMap_.size() == 0);
81
82 std::unordered_map<std::string, std::string> originalLanguageMap;
83 for (const auto &pair : notificationLocale.languageMap_) {
84 originalLanguageMap[pair.first] = pair.second;
85 }
86
87 notificationLocale.islanguageMapInit_ = true;
88 notificationLocale.ParseLocaleCfg();
89 EXPECT_TRUE(notificationLocale.languageMap_.size() == 0);
90 notificationLocale.islanguageMapInit_ = false;
91 notificationLocale.ParseLocaleCfg();
92 EXPECT_TRUE(notificationLocale.islanguageMapInit_);
93 EXPECT_GT(notificationLocale.languageMap_.size(), 0);
94 EDM_LOGI(MODULE_SERVICE, "ParseLocaleCfg001 end");
95 }
96
97 /**
98 * @tc.name: GetStringByKey001
99 * @tc.desc: Test GetValueByKey
100 * @tc.type: FUNC
101 */
102 HWTEST_F(NotificationLocaleTest, GetStringByKey001, TestSize.Level1)
103 {
104 EDM_LOGI(MODULE_SERVICE, "GetStringByKey001 begin");
105 auto ¬ificationLocale = NotificationLocale::GetInstance();
106 EXPECT_NE(¬ificationLocale, nullptr);
107 notificationLocale.islanguageMapInit_ = false;
108 notificationLocale.ParseLocaleCfg();
109 notificationLocale.UpdateStringMap();
110 EXPECT_GT(notificationLocale.languageMap_.size(), 0);
111 EXPECT_GT(notificationLocale.stringMap_.size(), 0);
112 EXPECT_TRUE(notificationLocale.GetValueByKey("key").empty());
113 EXPECT_FALSE(notificationLocale.GetValueByKey("usb_transmission_error_title").empty());
114 EDM_LOGI(MODULE_SERVICE, "GetStringByKey001 end");
115 }
116
117 /**
118 * @tc.name: UpdateStringMap001
119 * @tc.desc: Test UpdateStringMap
120 * @tc.type: FUNC
121 */
122 HWTEST_F(NotificationLocaleTest, UpdateStringMap001, TestSize.Level1)
123 {
124 EDM_LOGI(MODULE_SERVICE, "UpdateStringMap001 begin");
125 auto ¬ificationLocale = NotificationLocale::GetInstance();
126 EXPECT_NE(¬ificationLocale, nullptr);
127 OHOS::Global::I18n::LocaleInfo locale(Global::I18n::LocaleConfig::GetSystemLocale());
128 std::string curBaseName = locale.GetBaseName();
129 std::string localeBaseNameBak = curBaseName;
130 std::unordered_map<std::string, std::string> languageMap;
131 notificationLocale.ParseLocaleCfg();
132 EXPECT_TRUE(notificationLocale.islanguageMapInit_);
133 EXPECT_GT(notificationLocale.languageMap_.size(), 0);
134
135 notificationLocale.stringMap_.clear();
136 // same langauge when the size of stringMap_ is 0. stringMap_ should be empty
137 notificationLocale.UpdateStringMap();
138 EXPECT_EQ(notificationLocale.stringMap_.size(), 0);
139
140 // change langauge to en-Latn-US or zh-Hans-CN when the size of stringMap_ is 0.
141 // after UpdateStringMap, stringMap_ should not be empty
142 std::string language1 = "en-Latn-US";
143 std::string language2 = "zh-Hans-CN";
144 std::string language = language1;
145 if (curBaseName == language1) {
146 language = language2;
147 }
148 notificationLocale.localeBaseName_ = language;
149 notificationLocale.UpdateStringMap();
150 EXPECT_GT(notificationLocale.stringMap_.size(), 0);
151
152 std::unordered_map<std::string, std::string> stringMapBak;
153 for (const auto &pair : notificationLocale.stringMap_) {
154 stringMapBak[pair.first] = pair.second;
155 }
156
157 // change language between en-Latn-US and zh-Hans-CN. stringMap_ should have same size and same content
158 if (notificationLocale.localeBaseName_ == language1) {
159 notificationLocale.localeBaseName_ = language2;
160 } else {
161 notificationLocale.localeBaseName_ = language1;
162 }
163
164 notificationLocale.UpdateStringMap();
165 EXPECT_GT(notificationLocale.stringMap_.size(), 0);
166 EXPECT_EQ(notificationLocale.stringMap_.size(), stringMapBak.size());
167 std::string key = "usb_transmission_error_title";
168 std::string value = notificationLocale.GetValueByKey(key);
169 EXPECT_EQ(value, stringMapBak[key]);
170
171 // restore
172 if (notificationLocale.localeBaseName_ != localeBaseNameBak) {
173 notificationLocale.localeBaseName_ = localeBaseNameBak;
174 notificationLocale.UpdateStringMap();
175 }
176 EDM_LOGI(MODULE_SERVICE, "UpdateStringMap001 end");
177 }
178
179 /**
180 * @tc.name: ParseJsonfile001
181 * @tc.desc: Test ParseJsonfile
182 * @tc.type: FUNC
183 */
184 HWTEST_F(NotificationLocaleTest, ParseJsonfile001, TestSize.Level1)
185 {
186 EDM_LOGI(MODULE_SERVICE, "ParseJsonfile001 begin");
187 auto ¬ificationLocale = NotificationLocale::GetInstance();
188 EXPECT_NE(¬ificationLocale, nullptr);
189 std::unordered_map<std::string, std::string> languageMap;
190 std::string path = "";
191 bool bRet = notificationLocale.ParseJsonfile(path, languageMap);
192 EXPECT_FALSE(bRet);
193
194 bRet = notificationLocale.ParseJsonfile(localJsonFilePath, languageMap);
195 EXPECT_TRUE(bRet);
196 EXPECT_GT(languageMap.size(), 0);
197 EDM_LOGI(MODULE_SERVICE, "ParseJsonfile001 end");
198 }
199 }
200 }
201