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