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 <gtest/gtest.h>
17
18 #define private public
19 #include "multi_user_config_mgr.h"
20 #undef private
21 using namespace testing;
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace AAFwk {
26
27 class MultiUserConfigMgrTest : public testing::Test {
28 public:
29 static void SetUpTestCase();
30 static void TearDownTestCase();
31 void SetUp();
32 void TearDown();
33 };
34
SetUpTestCase()35 void MultiUserConfigMgrTest::SetUpTestCase() {}
TearDownTestCase()36 void MultiUserConfigMgrTest::TearDownTestCase() {}
SetUp()37 void MultiUserConfigMgrTest::SetUp() {}
TearDown()38 void MultiUserConfigMgrTest::TearDown() {}
39
40 /**
41 * @tc.name: SetOrUpdateConfigByUserId_0100
42 * @tc.desc: SetOrUpdateConfigByUserId.
43 * @tc.type: FUNC
44 */
45 HWTEST_F(MultiUserConfigMgrTest, SetOrUpdateConfigByUserId_0100, TestSize.Level1)
46 {
47 int32_t userId = 1;
48
49 AppExecFwk::Configuration config1;
50 int displayId = 1001;
51 std::string val{ "中文" };
52 config1.AddItem(displayId,
53 AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, val);
54
55 AppExecFwk::Configuration config2;
56 int displayId2 = 1002;
57 std::string English{ "英文" };
58 config2.AddItem(displayId2,
59 AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, English);
60
61 std::vector<std::string> changeKeyV;
62
63 auto multiUserConfigurationMgr =
64 std::make_shared<AppExecFwk::MultiUserConfigurationMgr>();
65
66 multiUserConfigurationMgr->multiUserConfiguration_.emplace(
67 std::make_pair(userId, config2));
68 multiUserConfigurationMgr->SetOrUpdateConfigByUserId(
69 userId, config1, changeKeyV);
70 EXPECT_FALSE(changeKeyV.empty());
71 auto item = config2.GetItem(displayId2,
72 AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE);
73 EXPECT_EQ(item, English);
74
75 multiUserConfigurationMgr->multiUserConfiguration_.clear();
76 changeKeyV.clear();
77 multiUserConfigurationMgr->globalConfiguration_ = nullptr;
78 multiUserConfigurationMgr->SetOrUpdateConfigByUserId(
79 userId, config1, changeKeyV);
80
81 multiUserConfigurationMgr->globalConfiguration_ =
82 std::make_shared<AppExecFwk::Configuration>();
83 multiUserConfigurationMgr->SetOrUpdateConfigByUserId(
84 userId, config1, changeKeyV);
85 EXPECT_FALSE(changeKeyV.empty());
86 EXPECT_FALSE(multiUserConfigurationMgr->multiUserConfiguration_.empty());
87 }
88
89 /**
90 * @tc.name: UpdateMultiUserConfiguration_0100
91 * @tc.desc: UpdateMultiUserConfiguration.
92 * @tc.type: FUNC
93 */
94 HWTEST_F(MultiUserConfigMgrTest, UpdateMultiUserConfiguration_0100, TestSize.Level1)
95 {
96 AppExecFwk::Configuration config1;
97 int displayId = 1001;
98 std::string val{ "中文" };
99 config1.AddItem(displayId,
100 AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, val);
101
102 AppExecFwk::Configuration config2;
103 int displayId2 = 1002;
104 std::string English{ "英文" };
105 config2.AddItem(displayId2,
106 AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, English);
107
108 auto multiUserConfigurationMgr =
109 std::make_shared<AppExecFwk::MultiUserConfigurationMgr>();
110 multiUserConfigurationMgr->multiUserConfiguration_.emplace(
111 std::make_pair(1, config2));
112 multiUserConfigurationMgr->UpdateMultiUserConfiguration(config1);
113 for (auto& item : multiUserConfigurationMgr->multiUserConfiguration_) {
114 auto result = item.second.GetItem(displayId2,
115 AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE);
116 EXPECT_EQ(result, English);
117 }
118 }
119
120 /**
121 * @tc.name: UpdateMultiUserConfigurationForGlobal_0100
122 * @tc.desc: UpdateMultiUserConfigurationForGlobal.
123 * @tc.type: FUNC
124 */
125 HWTEST_F(MultiUserConfigMgrTest, UpdateMultiUserConfigurationForGlobal_0100, TestSize.Level1)
126 {
127 AppExecFwk::Configuration globalConfig;
128 int displayId = 1001;
129 std::string val{ "中文" };
130 globalConfig.AddItem(displayId,
131 AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, val);
132
133 AppExecFwk::Configuration config;
134 int displayId2 = 1002;
135 std::string English{ "英文" };
136 config.AddItem(displayId2,
137 AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, English);
138
139 auto multiUserConfigurationMgr =
140 std::make_shared<AppExecFwk::MultiUserConfigurationMgr>();
141
142 multiUserConfigurationMgr->multiUserConfiguration_.emplace(
143 std::make_pair(1, config));
144 multiUserConfigurationMgr->UpdateMultiUserConfiguration(globalConfig);
145
146 for (auto& item : multiUserConfigurationMgr->multiUserConfiguration_) {
147 auto result = item.second.GetItem(displayId,
148 AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE);
149 EXPECT_EQ(result, val);
150 }
151 }
152
153 /**
154 * @tc.name: GetForegroundOsAccountLocalId_0100
155 * @tc.desc: GetForegroundOsAccountLocalId.
156 * @tc.type: FUNC
157 */
158 HWTEST_F(MultiUserConfigMgrTest, GetForegroundOsAccountLocalId_0100, TestSize.Level1)
159 {
160 EXPECT_EQ(AppExecFwk::MultiUserConfigurationMgr::
161 GetForegroundOsAccountLocalId(), 100);
162 }
163
164 /**
165 * @tc.name: HandleConfiguration_0100
166 * @tc.desc: HandleConfiguration.
167 * @tc.type: FUNC
168 */
169 HWTEST_F(MultiUserConfigMgrTest, HandleConfiguration_0100, TestSize.Level1)
170 {
171 AppExecFwk::Configuration config1;
172 int displayId = 1001;
173 std::string val{ "中文" };
174 config1.AddItem(displayId,
175 AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, val);
176
177 int32_t userId = -1;
178 std::vector<std::string> changeKeyV;
179 bool isNotifyUser0 = true;
180
181 auto multiUserConfigurationMgr =
182 std::make_shared<AppExecFwk::MultiUserConfigurationMgr>();
183 multiUserConfigurationMgr->globalConfiguration_ = nullptr;
184 multiUserConfigurationMgr->HandleConfiguration(userId,
185 config1, changeKeyV, isNotifyUser0);
186
187 multiUserConfigurationMgr->globalConfiguration_ =
188 std::make_shared<AppExecFwk::Configuration>();
189 int displayId2 = 1002;
190 std::string English{ "英文" };
191 multiUserConfigurationMgr->globalConfiguration_->AddItem(displayId2,
192 AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, English);
193
194 multiUserConfigurationMgr->HandleConfiguration(userId,
195 config1, changeKeyV, isNotifyUser0);
196 EXPECT_FALSE(changeKeyV.empty());
197 EXPECT_EQ(multiUserConfigurationMgr->globalConfiguration_->GetItem(displayId2,
198 AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE), English);
199 }
200
201 /**
202 * @tc.name: HandleConfiguration_0200
203 * @tc.desc: HandleConfiguration.
204 * @tc.type: FUNC
205 */
206 HWTEST_F(MultiUserConfigMgrTest, HandleConfiguration_0200, TestSize.Level1)
207 {
208 int32_t userId =
209 AppExecFwk::MultiUserConfigurationMgr::GetForegroundOsAccountLocalId();
210 AppExecFwk::Configuration config1;
211 int displayId = 1001;
212 std::string val{ "中文" };
213 config1.AddItem(displayId,
214 AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, val);
215
216 AppExecFwk::Configuration config2;
217 int displayId2 = 1002;
218 std::string English{ "英文" };
219 config2.AddItem(displayId2,
220 AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, English);
221
222 std::vector<std::string> changeKeyV;
223 bool isNotifyUser0 = false;
224 auto multiUserConfigurationMgr =
225 std::make_shared<AppExecFwk::MultiUserConfigurationMgr>();
226 multiUserConfigurationMgr->multiUserConfiguration_.emplace(
227 std::make_pair(userId, config2));
228 multiUserConfigurationMgr->HandleConfiguration(userId,
229 config1, changeKeyV, isNotifyUser0);
230 EXPECT_FALSE(changeKeyV.empty());
231 EXPECT_EQ(isNotifyUser0, true);
232 }
233
234 /**
235 * @tc.name: HandleConfiguration_0300
236 * @tc.desc: HandleConfiguration.
237 * @tc.type: FUNC
238 */
239 HWTEST_F(MultiUserConfigMgrTest, HandleConfiguration_0300, TestSize.Level1)
240 {
241 AppExecFwk::Configuration configLanguage;
242 configLanguage.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, "chinese");
243
244 std::vector<std::string> changeKeyV;
245 bool isNotifyUser0 = true;
246 auto multiUserConfigurationMgr = std::make_shared<AppExecFwk::MultiUserConfigurationMgr>();
247 multiUserConfigurationMgr->HandleConfiguration(100, configLanguage, changeKeyV, isNotifyUser0);
248
249 auto configColor = std::make_shared<AppExecFwk::Configuration>();
250 configColor->AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE, "dark");
251 multiUserConfigurationMgr->InitConfiguration(configColor);
252
253 auto resultConfig = multiUserConfigurationMgr->GetConfigurationByUserId(100);
254 EXPECT_EQ(resultConfig->GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE), "chinese");
255 EXPECT_EQ(resultConfig->GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE), "dark");
256 }
257
258 /**
259 * @tc.name: InitConfiguration_0200
260 * @tc.desc: InitConfiguration.
261 * @tc.type: FUNC
262 */
263 HWTEST_F(MultiUserConfigMgrTest, InitConfiguration_0100, TestSize.Level1)
264 {
265 auto config = std::make_shared<AppExecFwk::Configuration>();
266 int displayId = 1001;
267 std::string val{ "中文" };
268 config->AddItem(displayId, AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, val);
269 auto multiUserConfigurationMgr =
270 std::make_shared<AppExecFwk::MultiUserConfigurationMgr>();
271 multiUserConfigurationMgr->globalConfiguration_ = nullptr;
272 multiUserConfigurationMgr->InitConfiguration(config);
273
274 multiUserConfigurationMgr->globalConfiguration_ =
275 std::make_shared<AppExecFwk::Configuration>();
276 int displayId2 = 1002;
277 std::string English{ "英文" };
278 multiUserConfigurationMgr->globalConfiguration_->AddItem(displayId2,
279 AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, English);
280 multiUserConfigurationMgr->InitConfiguration(config);
281 EXPECT_EQ(config, multiUserConfigurationMgr->globalConfiguration_);
282 }
283
284 /**
285 * @tc.name: GetConfigurationByUserId_0200
286 * @tc.desc: GetConfigurationByUserId.
287 * @tc.type: FUNC
288 */
289 HWTEST_F(MultiUserConfigMgrTest, GetConfigurationByUserId_0100, TestSize.Level1)
290 {
291 int32_t userId = 1;
292 auto multiUserConfigurationMgr =
293 std::make_shared<AppExecFwk::MultiUserConfigurationMgr>();
294
295 AppExecFwk::Configuration config;
296 int displayId = 1001;
297 std::string val{ "中文" };
298 config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, val);
299 multiUserConfigurationMgr->multiUserConfiguration_.emplace(std::make_pair(userId, config));
300 EXPECT_NE(multiUserConfigurationMgr->GetConfigurationByUserId(userId), nullptr);
301
302 multiUserConfigurationMgr->multiUserConfiguration_.clear();
303 multiUserConfigurationMgr->globalConfiguration_ = nullptr;
304 EXPECT_EQ(multiUserConfigurationMgr->GetConfigurationByUserId(userId), nullptr);
305
306 multiUserConfigurationMgr->globalConfiguration_ =
307 std::make_shared<AppExecFwk::Configuration>();
308 EXPECT_NE(multiUserConfigurationMgr->GetConfigurationByUserId(userId), nullptr);
309 }
310
311 } // namespace AppExecFwk
312 } // namespace OHOS
313