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 DefaultMultiAccountStrategyTest : public testing::Test {
34 public:
35     static void SetUpTestCase();
36     static void TearDownTestCase();
37     void SetUp();
38     void TearDown();
39 };
40 
SetUpTestCase()41 void DefaultMultiAccountStrategyTest::SetUpTestCase()
42 {
43 }
44 
TearDownTestCase()45 void DefaultMultiAccountStrategyTest::TearDownTestCase()
46 {
47 }
48 
SetUp()49 void DefaultMultiAccountStrategyTest::SetUp()
50 {
51 }
52 
TearDown()53 void DefaultMultiAccountStrategyTest::TearDown()
54 {
55 }
56 
57 /**
58  * @tc.name: SetAccountPriority
59  * @tc.desc: Test the value of accountInfo->GetIsActived() equals to true
60  * @tc.desc: Test the value of accountInfo->priority_ equals to HIGH_PRIORITY
61  * @tc.type: FUNC
62  */
63 HWTEST_F(DefaultMultiAccountStrategyTest, SetAccountProrityTest, TestSize.Level1)
64 {
65     std::shared_ptr<AccountPriorityInfo> accountInfo;
66     bool isActived = true;
67     int accountId = 0;
68     std::string accountName = "admin";
69     AccountSA::OsAccountType accountType = AccountSA::OsAccountType::ADMIN;
70     DefaultMultiAccountStrategy mulAcc;
71 
72     // the branch of GetIsActived() equals to true
73     // the branch of priority_ equals to HIGH_PRIORITY
74     AccountPriorityInfo accPri1(accountId, accountName, accountType, isActived);
75     MultiAccountManager::GetInstance().SetAccountPriority(accountId, accountName, accountType, isActived);
76     accountInfo = MultiAccountManager::GetInstance().GetAccountPriorityInfo(accountId);
77     mulAcc.SetAccountPriority(accountInfo);
78     EXPECT_EQ(accountInfo->priority_, static_cast<int>(DefaultMultiAccountPriority::HIGH_PRIORITY));
79 
80     // the branch of priority_ equals to LOW_PRIORITY
81     isActived = false;
82     AccountPriorityInfo accPri2(accountId, accountName, accountType, isActived);
83     MultiAccountManager::GetInstance().SetAccountPriority(accountId, accountName, accountType, isActived);
84     accountInfo = MultiAccountManager::GetInstance().GetAccountPriorityInfo(accountId);
85     mulAcc.SetAccountPriority(accountInfo);
86     EXPECT_EQ(accountInfo->priority_, static_cast<int>(DefaultMultiAccountPriority::LOW_PRIORITY));
87 
88     // the branch of GetIsActived() equals to false
89     accountInfo = nullptr;
90     bool setAcc = mulAcc.SetAccountPriority(accountInfo);
91     EXPECT_EQ(setAcc, false);
92 }
93 
94 /**
95  * @tc.name: RecalcBundlePriority
96  * @tc.desc: Test the return value that not equals to RECLAIM_PRIORITY_VISIBLE
97  * @tc.desc: Test the value of accountInfo equals to nullptr
98  * @tc.type: FUNC
99  */
100 HWTEST_F(DefaultMultiAccountStrategyTest, RecalcBundlePriortiyTest, TestSize.Level1)
101 {
102     std::shared_ptr<AccountPriorityInfo> accountInfo;
103     bool isActived = true;
104     int accountId = 0;
105     std::string accountName = "admin";
106     AccountSA::OsAccountType accountType = AccountSA::OsAccountType::ADMIN;
107     DefaultMultiAccountStrategy mulAcc;
108     int bundlePriority = RECLAIM_PRIORITY_VISIBLE;
109 
110     // return value not equals to RECLAIM_PRIORITY_VISIBLE
111     MultiAccountManager::GetInstance().SetAccountPriority(accountId, accountName, accountType, isActived);
112     accountInfo = MultiAccountManager::GetInstance().GetAccountPriorityInfo(accountId);
113     mulAcc.SetAccountPriority(accountInfo);
114     int recalcPriority = mulAcc.RecalcBundlePriority(accountInfo, bundlePriority);
115     EXPECT_NE(recalcPriority, RECLAIM_PRIORITY_VISIBLE);
116 
117     // the branch of accountInfo equals to nullptr
118     accountInfo = nullptr;
119     mulAcc.SetAccountPriority(accountInfo);
120     recalcPriority = mulAcc.RecalcBundlePriority(accountInfo, bundlePriority);
121     EXPECT_EQ(recalcPriority, RECLAIM_PRIORITY_MAX);
122 }
123 
124 }
125 }
126