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, SetAccountPrority, TestSize.Level1)
58 {
59 int accountId = 2;
60 std::string accountName = "admin";
61 AccountSA::OsAccountType accountType = AccountSA::OsAccountType::ADMIN;
62 bool isActived = true;
63
64 MultiAccountManager::GetInstance().SetAccountPriority(accountId, accountName, accountType, isActived);
65 std::shared_ptr<AccountPriorityInfo> info = MultiAccountManager::GetInstance().GetAccountPriorityInfo(accountId);
66
67 EXPECT_EQ(info->GetId(), accountId);
68 EXPECT_STREQ(info->GetName().c_str(), accountName.c_str());
69 EXPECT_EQ(info->GetType(), accountType);
70 EXPECT_EQ(info->GetIsActived(), isActived);
71 EXPECT_EQ(info->GetPriority(), static_cast<int>(DefaultMultiAccountPriority::HIGH_PRIORITY));
72 }
73
74 HWTEST_F(MultiAccountManagerTest, RecalcBundlePriortiy, TestSize.Level1)
75 {
76 int accountId = 2;
77 std::string accountName = "admin";
78 AccountSA::OsAccountType accountType = AccountSA::OsAccountType::ADMIN;
79 bool isActived = false;
80 int bundlePriority = RECLAIM_PRIORITY_FOREGROUND;
81
82 MultiAccountManager::GetInstance().SetAccountPriority(accountId, accountName, accountType, isActived);
83 int recalcPriority = MultiAccountManager::GetInstance().RecalcBundlePriority(accountId, bundlePriority);
84
85 EXPECT_EQ(recalcPriority, RECLAIM_PRIORITY_FOREGROUND + 50);
86 }
87
88 HWTEST_F(MultiAccountManagerTest, AccountColdSwitch, TestSize.Level1)
89 {
90 int accountId = 100;
91 std::shared_ptr<AccountBundleInfo> account = std::make_shared<AccountBundleInfo>(accountId);
92 std::shared_ptr<BundlePriorityInfo> bundle = std::make_shared<BundlePriorityInfo>("app",
93 accountId * USER_ID_SHIFT + 1, 100);
94 ProcessPriorityInfo proc1(1001, bundle->uid_, bundle->priority_);
95 ProcessPriorityInfo proc2(1002, bundle->uid_, bundle->priority_);
96 ProcessPriorityInfo proc3(1003, bundle->uid_, bundle->priority_);
97 ProcessPriorityInfo proc4(1004, bundle->uid_, bundle->priority_);
98
99 std::map<int, std::shared_ptr<AccountBundleInfo>> osAccountsInfoMap;
100 bundle->AddProc(proc1);
101 bundle->AddProc(proc2);
102 bundle->AddProc(proc3);
103 bundle->AddProc(proc4);
104 account->AddBundleToOsAccount(bundle);
105 osAccountsInfoMap.insert(std::make_pair(account->id_, account));
106
107 std::vector<int> switchedIds { accountId };
108 MultiAccountManager::GetInstance().HandleAccountColdSwitch(switchedIds, osAccountsInfoMap);
109 }
110
111 HWTEST_F(MultiAccountManagerTest, AccountHotSwitch, TestSize.Level1)
112 {
113 int accountId = 100;
114 std::shared_ptr<AccountBundleInfo> account = std::make_shared<AccountBundleInfo>(accountId);
115 std::shared_ptr<BundlePriorityInfo> bundle = std::make_shared<BundlePriorityInfo>("app",
116 accountId * USER_ID_SHIFT + 1, 100);
117 ProcessPriorityInfo proc1(1001, bundle->uid_, bundle->priority_);
118 ProcessPriorityInfo proc2(1002, bundle->uid_, bundle->priority_);
119 ProcessPriorityInfo proc3(1003, bundle->uid_, bundle->priority_);
120 ProcessPriorityInfo proc4(1004, bundle->uid_, bundle->priority_);
121
122 std::map<int, std::shared_ptr<AccountBundleInfo>> osAccountsInfoMap;
123 bundle->AddProc(proc1);
124 bundle->AddProc(proc2);
125 bundle->AddProc(proc3);
126 bundle->AddProc(proc4);
127 account->AddBundleToOsAccount(bundle);
128 osAccountsInfoMap.insert(std::make_pair(account->id_, account));
129
130 std::string accountName = "admin";
131 AccountSA::OsAccountType accountType = AccountSA::OsAccountType::ADMIN;
132 bool isActived = false;
133 MultiAccountManager::GetInstance().SetAccountPriority(accountId, accountName, accountType, isActived);
134
135 std::vector<int> switchedIds { accountId };
136 MultiAccountManager::GetInstance().HandleAccountHotSwitch(switchedIds, osAccountsInfoMap);
137
138 EXPECT_EQ(bundle->priority_, 150);
139 }
140 }
141 }
142