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 "gtest/gtest.h"
17
18 #include "utils.h"
19
20 #define private public
21 #define protected public
22 #include "reclaim_priority_constants.h"
23 #include "default_multi_account_strategy.h"
24 #include "multi_account_manager.h"
25 #undef private
26 #undef protected
27
28 namespace OHOS {
29 namespace Memory {
30 using namespace testing;
31 using namespace testing::ext;
32
33 class MultiAccountManagerTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37 void SetUp();
38 void TearDown();
39 };
40
SetUpTestCase()41 void MultiAccountManagerTest::SetUpTestCase()
42 {
43 }
44
TearDownTestCase()45 void MultiAccountManagerTest::TearDownTestCase()
46 {
47 }
48
SetUp()49 void MultiAccountManagerTest::SetUp()
50 {
51 }
52
TearDown()53 void MultiAccountManagerTest::TearDown()
54 {
55 }
56
57 HWTEST_F(MultiAccountManagerTest, InitTest, TestSize.Level1)
58 {
59 MultiAccountManager::GetInstance().Init();
60 EXPECT_EQ(MultiAccountManager::GetInstance().initialized_, true);
61 }
62
63 HWTEST_F(MultiAccountManagerTest, SetAccountPrority, TestSize.Level1)
64 {
65 int accountId = 2;
66 std::string accountName = "admin";
67 AccountSA::OsAccountType accountType = AccountSA::OsAccountType::ADMIN;
68 bool isActived = true;
69
70 MultiAccountManager::GetInstance().SetAccountPriority(accountId, accountName, accountType, isActived);
71 std::shared_ptr<AccountPriorityInfo> info = MultiAccountManager::GetInstance().GetAccountPriorityInfo(accountId);
72
73 EXPECT_EQ(info->GetId(), accountId);
74 EXPECT_STREQ(info->GetName().c_str(), accountName.c_str());
75 EXPECT_EQ(info->GetType(), accountType);
76 EXPECT_EQ(info->GetIsActived(), isActived);
77 EXPECT_EQ(info->GetPriority(), static_cast<int>(DefaultMultiAccountPriority::HIGH_PRIORITY));
78 }
79
80 HWTEST_F(MultiAccountManagerTest, RecalcBundlePriortiy, TestSize.Level1)
81 {
82 int accountId = 2;
83 std::string accountName = "admin";
84 AccountSA::OsAccountType accountType = AccountSA::OsAccountType::ADMIN;
85 bool isActived = false;
86 int bundlePriority = RECLAIM_PRIORITY_FOREGROUND;
87
88 MultiAccountManager::GetInstance().SetAccountPriority(accountId, accountName, accountType, isActived);
89 int recalcPriority = MultiAccountManager::GetInstance().RecalcBundlePriority(accountId, bundlePriority);
90
91 EXPECT_EQ(recalcPriority, RECLAIM_PRIORITY_FOREGROUND + 50);
92 }
93
94 HWTEST_F(MultiAccountManagerTest, AccountColdSwitch, TestSize.Level1)
95 {
96 int accountId = 100;
97 std::shared_ptr<AccountBundleInfo> account = std::make_shared<AccountBundleInfo>(accountId);
98 std::shared_ptr<BundlePriorityInfo> bundle = std::make_shared<BundlePriorityInfo>("app",
99 accountId * USER_ID_SHIFT + 1, 100);
100 ProcessPriorityInfo proc1(1001, bundle->uid_, bundle->priority_);
101 ProcessPriorityInfo proc2(1002, bundle->uid_, bundle->priority_);
102 ProcessPriorityInfo proc3(1003, bundle->uid_, bundle->priority_);
103 ProcessPriorityInfo proc4(1004, bundle->uid_, bundle->priority_);
104
105 std::map<int, std::shared_ptr<AccountBundleInfo>> osAccountsInfoMap;
106 bundle->AddProc(proc1);
107 bundle->AddProc(proc2);
108 bundle->AddProc(proc3);
109 bundle->AddProc(proc4);
110 account->AddBundleToOsAccount(bundle);
111 osAccountsInfoMap.insert(std::make_pair(account->id_, account));
112
113 std::vector<int> switchedIds { accountId };
114 EXPECT_EQ(MultiAccountManager::GetInstance().
115 HandleAccountColdSwitch(switchedIds, osAccountsInfoMap), true);
116 }
117
118 HWTEST_F(MultiAccountManagerTest, AccountHotSwitch, TestSize.Level1)
119 {
120 int accountId = 100;
121 std::shared_ptr<AccountBundleInfo> account = std::make_shared<AccountBundleInfo>(accountId);
122 std::shared_ptr<BundlePriorityInfo> bundle = std::make_shared<BundlePriorityInfo>("app",
123 accountId * USER_ID_SHIFT + 1, 100);
124 ProcessPriorityInfo proc1(1001, bundle->uid_, bundle->priority_);
125 ProcessPriorityInfo proc2(1002, bundle->uid_, bundle->priority_);
126 ProcessPriorityInfo proc3(1003, bundle->uid_, bundle->priority_);
127 ProcessPriorityInfo proc4(1004, bundle->uid_, bundle->priority_);
128
129 std::map<int, std::shared_ptr<AccountBundleInfo>> osAccountsInfoMap;
130 bundle->AddProc(proc1);
131 bundle->AddProc(proc2);
132 bundle->AddProc(proc3);
133 bundle->AddProc(proc4);
134 account->AddBundleToOsAccount(bundle);
135 osAccountsInfoMap.insert(std::make_pair(account->id_, account));
136
137 std::string accountName = "admin";
138 AccountSA::OsAccountType accountType = AccountSA::OsAccountType::ADMIN;
139 bool isActived = false;
140 MultiAccountManager::GetInstance().SetAccountPriority(accountId, accountName, accountType, isActived);
141
142 std::vector<int> switchedIds { accountId };
143 MultiAccountManager::GetInstance().HandleAccountHotSwitch(switchedIds, osAccountsInfoMap);
144
145 EXPECT_EQ(bundle->priority_, 150);
146 }
147
148 /**
149 * @tc.name: AddAccountPriorityInfo
150 * @tc.desc: Test add value include id_ name_ type_ isActived_
151 * @tc.type: FUNC
152 */
153 HWTEST_F(MultiAccountManagerTest, AddAccountPriorityInfoTest, TestSize.Level1)
154 {
155 int accountId = 3;
156 std::string accountName = "admin";
157 AccountSA::OsAccountType accountType = AccountSA::OsAccountType::ADMIN;
158 bool isActived = true;
159
160 MultiAccountManager::GetInstance().SetAccountPriority(accountId, accountName, accountType, isActived);
161 std::shared_ptr<AccountPriorityInfo> accountInfo =
162 MultiAccountManager::GetInstance().GetAccountPriorityInfo(accountId);
163 MultiAccountManager::GetInstance().AddAccountPriorityInfo(accountInfo);
164 EXPECT_EQ(accountInfo->GetId(), accountId);
165 EXPECT_STREQ(accountInfo->GetName().c_str(), accountName.c_str());
166 EXPECT_EQ(accountInfo->GetType(), accountType);
167 EXPECT_EQ(accountInfo->GetIsActived(), isActived);
168 }
169
170 /**
171 * @tc.name: SetMultiAccountStrategy and GetMultiAccountStratgy
172 * @tc.desc: Test set value of strategy_ equals to nullptr
173 * @tc.desc: Test get value of strategy_
174 * @tc.type: FUNC
175 */
176 HWTEST_F(MultiAccountManagerTest, SetMultiAccountStrategyTest, TestSize.Level1)
177 {
178 // strategy_ equals to nullptr
179 std::shared_ptr<MultiAccountStrategy> strategy = nullptr;
180 bool retMul = MultiAccountManager::GetInstance().SetMultiAccountStrategy(strategy);
181 EXPECT_EQ(retMul, false);
182
183 // set and get value of strategy_
184 strategy = MultiAccountManager::GetInstance().GetMultiAccountStratgy();
185 retMul = MultiAccountManager::GetInstance().SetMultiAccountStrategy(strategy);
186 EXPECT_EQ(retMul, true);
187 }
188
189 /**
190 * @tc.name: GetSwitchedAccountIds
191 * @tc.desc: test GetSwitchedAccountIds into for and if branch
192 * @tc.type: FUNC
193 */
194 HWTEST_F(MultiAccountManagerTest, GetSwitchedAccountIdsTest, TestSize.Level1)
195 {
196 MultiAccountManager oldAct;
197 oldAct.oldActiveAccountIds_ = {1, 3, 5, 7, 9};
198 std::vector<int> accountIds;
199 std::vector<int> accountIds1 = {1, 3, 5, 7, 9};
200 oldAct.GetSwitchedAccountIds(accountIds);
201 EXPECT_EQ(accountIds, accountIds1);
202 }
203
204 /**
205 * @tc.name: UpdateAccountPriorityInfo
206 * @tc.desc: test UpdateAccountPriorityInfoTest into for and if branch
207 * @tc.type: FUNC
208 */
209 HWTEST_F(MultiAccountManagerTest, UpdateAccountPriorityInfoTest, TestSize.Level1)
210 {
211 std::vector<int> accountIds;
212 EXPECT_EQ(MultiAccountManager::GetInstance().UpdateAccountPriorityInfo(accountIds), true);
213 std::vector<int> accountIds1 = {1, 3, 5, 7, 9};
214 EXPECT_EQ(MultiAccountManager::GetInstance().UpdateAccountPriorityInfo(accountIds1), false);
215 }
216
217 /**
218 * @tc.name: HandleOsAccountsChanged
219 * @tc.desc: test initialized_ == false
220 * @tc.desc: test the branch of UpdateAccountPriorityInfo(updatedAccountIds) equals to false
221 * @tc.desc: test the branch of switchMod equals to AccountSA::COLD_SWITCH
222 * @tc.desc: test the branch of switchMod equals to AccountSA::HOT_SWITCH
223 * @tc.type: FUNC
224 */
225 HWTEST_F(MultiAccountManagerTest, HandleOsAccountsChangedTest, TestSize.Level1)
226 {
227 MultiAccountManager mulAcc;
228 mulAcc.initialized_ = false;
229 int accountId = 100;
230 AccountSA::OS_ACCOUNT_SWITCH_MOD switchMod1 = AccountSA::OsAccountManager::GetOsAccountSwitchMod();
231 AccountSA::OS_ACCOUNT_SWITCH_MOD switchMod2 = AccountSA::COLD_SWITCH;
232 AccountSA::OS_ACCOUNT_SWITCH_MOD switchMod3 = AccountSA::HOT_SWITCH;
233 std::map<int, std::shared_ptr<AccountBundleInfo>> osAccountsInfoMap;
234
235 // the branch of test initialized_ equals to false
236 bool ret = mulAcc.HandleOsAccountsChanged(accountId, switchMod1, osAccountsInfoMap);
237 EXPECT_EQ(ret, false);
238
239 // the branch of UpdateAccountPriorityInfo(updatedAccountIds) equals to false
240 ret = mulAcc.HandleOsAccountsChanged(accountId, switchMod1, osAccountsInfoMap);
241 EXPECT_EQ(ret, false);
242
243 // the branch of switchMod equals to AccountSA::COLD_SWITCH
244 ret = MultiAccountManager::GetInstance().HandleOsAccountsChanged(accountId, switchMod2, osAccountsInfoMap);
245 EXPECT_EQ(ret, true);
246
247 // the branch of switchMod equals to AccountSA::HOT_SWITCH
248 ret = MultiAccountManager::GetInstance().HandleOsAccountsChanged(accountId, switchMod3, osAccountsInfoMap);
249 EXPECT_EQ(ret, true);
250 }
251
252 }
253 }
254