1 /*
2  * Copyright (c) 2024 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 <cstdio>
17 #include <gtest/gtest.h>
18 #include <securec.h>
19 
20 #include "account_manager.h"
21 #include "key_event.h"
22 #include "mmi_log.h"
23 
24 #undef MMI_LOG_TAG
25 #define MMI_LOG_TAG "AccountManagerTest"
26 
27 namespace OHOS {
28 namespace MMI {
29 namespace {
30 using namespace testing::ext;
31 constexpr int32_t MAIN_ACCOUNT_ID { 100 };
32 constexpr size_t DEFAULT_BUFFER_LENGTH { 512 };
33 const std::string SECURE_SETTING_URI_PROXY {""};
34 } // namespace
35 
36 class AccountManagerTest : public testing::Test {
37 public:
SetUpTestCase(void)38     static void SetUpTestCase(void) {}
TearDownTestCase(void)39     static void TearDownTestCase(void) {}
40 };
41 
42 /**
43  * @tc.name: AccountManagerTest_GetInstance_01
44  * @tc.desc: Test the funcation GetInstance
45  * @tc.type: FUNC
46  * @tc.require:
47  */
48 HWTEST_F(AccountManagerTest, AccountManagerTest_GetInstance_01, TestSize.Level1)
49 {
50     CALL_TEST_DEBUG;
51     AccountManager accountManager;
52     accountManager.instance_ = nullptr;
53     ASSERT_NO_FATAL_FAILURE(accountManager.GetInstance());
54 }
55 
56 /**
57  * @tc.name: AccountManagerTest_SubscribeCommonEvent_01
58  * @tc.desc: Test the funcation SubscribeCommonEvent
59  * @tc.type: FUNC
60  * @tc.require:
61  */
62 HWTEST_F(AccountManagerTest, AccountManagerTest_SubscribeCommonEvent_01, TestSize.Level1)
63 {
64     CALL_TEST_DEBUG;
65     AccountManager accountManager;
66     accountManager.subscriber_ = nullptr;
67     accountManager.timerId_ = -1;
68     ASSERT_NO_FATAL_FAILURE(accountManager.SubscribeCommonEvent());
69 }
70 
71 /**
72  * @tc.name: AccountManagerTest_UnsubscribeCommonEvent_01
73  * @tc.desc: Test the funcation UnsubscribeCommonEvent
74  * @tc.type: FUNC
75  * @tc.require:
76  */
77 HWTEST_F(AccountManagerTest, AccountManagerTest_UnsubscribeCommonEvent_01, TestSize.Level1)
78 {
79     CALL_TEST_DEBUG;
80     AccountManager accountManager;
81     ASSERT_NO_FATAL_FAILURE(accountManager.SubscribeCommonEvent());
82     accountManager.subscriber_ = nullptr;
83     ASSERT_NO_FATAL_FAILURE(accountManager.UnsubscribeCommonEvent());
84 }
85 
86 /**
87  * @tc.name: AccountManagerTest_SubscribeCommonEvent_02
88  * @tc.desc: Test the funcation SubscribeCommonEvent
89  * @tc.type: FUNC
90  * @tc.require:
91  */
92 HWTEST_F(AccountManagerTest, AccountManagerTest_SubscribeCommonEvent_02, TestSize.Level1)
93 {
94     CALL_TEST_DEBUG;
95     AccountManager accountManager;
96     accountManager.subscriber_ = nullptr;
97     accountManager.timerId_ = 1;
98     ASSERT_NO_FATAL_FAILURE(accountManager.SubscribeCommonEvent());
99 }
100 
101 /**
102  * @tc.name: AccountManagerTest_SetupMainAccount_01
103  * @tc.desc: Test the funcation SetupMainAccount
104  * @tc.type: FUNC
105  * @tc.require:
106  */
107 HWTEST_F(AccountManagerTest, AccountManagerTest_SetupMainAccount_01, TestSize.Level1)
108 {
109     CALL_TEST_DEBUG;
110     AccountManager accountManager;
111     accountManager.currentAccountId_ = MAIN_ACCOUNT_ID;
112     auto [_, isNew] = accountManager.accounts_.emplace(MAIN_ACCOUNT_ID,
113         std::make_unique<AccountManager::AccountSetting>(MAIN_ACCOUNT_ID));
114     EXPECT_TRUE(isNew);
115     ASSERT_NO_FATAL_FAILURE(accountManager.SetupMainAccount());
116 }
117 
118 /**
119  * @tc.name: AccountManagerTest_OnAddUser_01
120  * @tc.desc: Test the funcation OnAddUser
121  * @tc.type: FUNC
122  * @tc.require:
123  */
124 HWTEST_F(AccountManagerTest, AccountManagerTest_OnAddUser_01, TestSize.Level1)
125 {
126     CALL_TEST_DEBUG;
127     AccountManager accountManager;
128     EventFwk::CommonEventData data;
129     int32_t accountId = data.GetCode();
130     accountId = 3;
131     auto [_, isNew] = accountManager.accounts_.emplace(accountId,
132         std::make_unique<AccountManager::AccountSetting>(accountId));
133     EXPECT_TRUE(isNew);
134     ASSERT_NO_FATAL_FAILURE(accountManager.OnAddUser(data));
135 }
136 
137 /**
138  * @tc.name: AccountManagerTest_OnRemoveUser_01
139  * @tc.desc: Test the funcation OnRemoveUser
140  * @tc.type: FUNC
141  * @tc.require:
142  */
143 HWTEST_F(AccountManagerTest, AccountManagerTest_OnRemoveUser_01, TestSize.Level1)
144 {
145     CALL_TEST_DEBUG;
146     AccountManager accountManager;
147     EventFwk::CommonEventData data;
148     int32_t accountId = data.GetCode();
149     accountId = 5;
150     ASSERT_NO_FATAL_FAILURE(accountManager.OnAddUser(data));
151     ASSERT_NO_FATAL_FAILURE(accountManager.OnRemoveUser(data));
152 }
153 
154 /**
155  * @tc.name: AccountManagerTest_OnCommonEvent_01
156  * @tc.desc: Test the funcation OnCommonEvent
157  * @tc.type: FUNC
158  * @tc.require:
159  */
160 HWTEST_F(AccountManagerTest, AccountManagerTest_OnCommonEvent_01, TestSize.Level1)
161 {
162     CALL_TEST_DEBUG;
163     AccountManager accountManager;
164     EventFwk::CommonEventData data;
165     ASSERT_NO_FATAL_FAILURE(accountManager.OnCommonEvent(data));
166 }
167 
168 /**
169  * @tc.name: AccountManagerTest_OnSwitchUser_01
170  * @tc.desc: Test the funcation OnSwitchUser
171  * @tc.type: FUNC
172  * @tc.require:
173  */
174 HWTEST_F(AccountManagerTest, AccountManagerTest_OnSwitchUser_01, TestSize.Level1)
175 {
176     CALL_TEST_DEBUG;
177     AccountManager accountManager;
178     EventFwk::CommonEventData data;
179     int32_t accountId = data.GetCode();
180     accountId = 1;
181     accountManager.currentAccountId_ = 1;
182     ASSERT_NO_FATAL_FAILURE(accountManager.OnSwitchUser(data));
183 }
184 
185 /**
186  * @tc.name: AccountManagerTest_OnSwitchUser_02
187  * @tc.desc: Test the funcation OnSwitchUser
188  * @tc.type: FUNC
189  * @tc.require:
190  */
191 HWTEST_F(AccountManagerTest, AccountManagerTest_OnSwitchUser_02, TestSize.Level1)
192 {
193     CALL_TEST_DEBUG;
194     AccountManager accountManager;
195     EventFwk::CommonEventData data;
196     int32_t accountId = data.GetCode();
197     accountId = 2;
198     accountManager.currentAccountId_ = -1;
199     ASSERT_NO_FATAL_FAILURE(accountManager.OnSwitchUser(data));
200 }
201 
202 /**
203  * @tc.name: AccountManagerTest_InitializeSetting_01
204  * @tc.desc: Test the funcation InitializeSetting
205  * @tc.type: FUNC
206  * @tc.require:
207  */
208 HWTEST_F(AccountManagerTest, AccountManagerTest_InitializeSetting_01, TestSize.Level1)
209 {
210     CALL_TEST_DEBUG;
211     int32_t accountId = 1;
212     AccountManager::AccountSetting accountSetting(accountId);
213     accountSetting.switchObserver_ = nullptr;
214     ASSERT_NO_FATAL_FAILURE(accountSetting.InitializeSetting());
215 }
216 
217 /**
218  * @tc.name: AccountManagerTest_InitializeSetting_02
219  * @tc.desc: Test the funcation InitializeSetting
220  * @tc.type: FUNC
221  * @tc.require:
222  */
223 HWTEST_F(AccountManagerTest, AccountManagerTest_InitializeSetting_02, TestSize.Level1)
224 {
225     CALL_TEST_DEBUG;
226     int32_t accountId = 2;
227     AccountManager::AccountSetting accountSetting(accountId);
228     accountSetting.onScreenLockedSwitchObserver_ = nullptr;
229     ASSERT_NO_FATAL_FAILURE(accountSetting.InitializeSetting());
230 }
231 
232 /**
233  * @tc.name: AccountManagerTest_InitializeSetting_03
234  * @tc.desc: Test the funcation InitializeSetting
235  * @tc.type: FUNC
236  * @tc.require:
237  */
238 HWTEST_F(AccountManagerTest, AccountManagerTest_InitializeSetting_03, TestSize.Level1)
239 {
240     CALL_TEST_DEBUG;
241     int32_t accountId = 3;
242     AccountManager::AccountSetting accountSetting(accountId);
243     accountSetting.configObserver_ = nullptr;
244     ASSERT_NO_FATAL_FAILURE(accountSetting.InitializeSetting());
245 }
246 
247 /**
248  * @tc.name: AccountManagerTest_ReadSwitchStatus_01
249  * @tc.desc: Test the funcation ReadSwitchStatus
250  * @tc.type: FUNC
251  * @tc.require:
252  */
253 HWTEST_F(AccountManagerTest, AccountManagerTest_ReadSwitchStatus_01, TestSize.Level1)
254 {
255     CALL_TEST_DEBUG;
256     int32_t accountId = 3;
257     AccountManager::AccountSetting accountSetting(accountId);
258     accountSetting.accountId_ = -1;
259     std::string key = "down";
260     bool currentSwitchStatus = true;
261     bool ret = accountSetting.ReadSwitchStatus(key, currentSwitchStatus);
262     EXPECT_FALSE(ret);
263 }
264 
265 /**
266  * @tc.name: AccountManagerTest_ReadSwitchStatus_02
267  * @tc.desc: Test the funcation ReadSwitchStatus
268  * @tc.type: FUNC
269  * @tc.require:
270  */
271 HWTEST_F(AccountManagerTest, AccountManagerTest_ReadSwitchStatus_02, TestSize.Level1)
272 {
273     CALL_TEST_DEBUG;
274     int32_t accountId = 5;
275     AccountManager::AccountSetting accountSetting(accountId);
276     accountSetting.accountId_ = 2;
277     std::string key = "down";
278     bool currentSwitchStatus = false;
279 
280     char buf[DEFAULT_BUFFER_LENGTH] {};
281     EXPECT_FALSE(sprintf_s(buf, sizeof(buf), SECURE_SETTING_URI_PROXY.c_str(), accountSetting.accountId_) > 0);
282     bool ret = accountSetting.ReadSwitchStatus(key, currentSwitchStatus);
283     EXPECT_FALSE(ret);
284 }
285 
286 /**
287  * @tc.name: AccountManagerTest_ReadLongPressTime_01
288  * @tc.desc: Test the funcation ReadLongPressTime
289  * @tc.type: FUNC
290  * @tc.require:
291  */
292 HWTEST_F(AccountManagerTest, AccountManagerTest_ReadLongPressTime_01, TestSize.Level1)
293 {
294     CALL_TEST_DEBUG;
295     int32_t accountId = 3;
296     AccountManager::AccountSetting accountSetting(accountId);
297     accountSetting.accountId_ = -1;
298     ASSERT_NO_FATAL_FAILURE(accountSetting.ReadLongPressTime());
299 }
300 
301 /**
302  * @tc.name: AccountManagerTest_ReadLongPressTime_02
303  * @tc.desc: Test the funcation ReadLongPressTime
304  * @tc.type: FUNC
305  * @tc.require:
306  */
307 HWTEST_F(AccountManagerTest, AccountManagerTest_ReadLongPressTime_02, TestSize.Level1)
308 {
309     CALL_TEST_DEBUG;
310     int32_t accountId = 3;
311     AccountManager::AccountSetting accountSetting(accountId);
312     accountSetting.accountId_ = 2;
313 
314     char buf[DEFAULT_BUFFER_LENGTH] {};
315     EXPECT_FALSE(sprintf_s(buf, sizeof(buf), SECURE_SETTING_URI_PROXY.c_str(), accountSetting.accountId_) > 0);
316     ASSERT_NO_FATAL_FAILURE(accountSetting.ReadLongPressTime());
317 }
318 } // namespace MMI
319 } // namespace OHOS