• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 <gtest/gtest.h>
17 
18 #define private public
19 #define protected public
20 #include "resident_process_manager.h"
21 #undef private
22 #undef protected
23 #include "ability_manager_service.h"
24 #include "user_controller.h"
25 
26 using namespace testing;
27 using namespace testing::ext;
28 using namespace OHOS::AppExecFwk;
29 
30 namespace OHOS {
31 namespace AAFwk {
32 
33 class ResidentProcessManagerTest : public testing::Test {
34 public:
35     static void SetUpTestCase(void);
36     static void TearDownTestCase(void);
37     void SetUp();
38     void TearDown();
39 };
40 
SetUpTestCase(void)41 void ResidentProcessManagerTest::SetUpTestCase(void)
42 {}
TearDownTestCase(void)43 void ResidentProcessManagerTest::TearDownTestCase(void)
44 {}
SetUp()45 void ResidentProcessManagerTest::SetUp()
46 {}
TearDown()47 void ResidentProcessManagerTest::TearDown()
48 {}
49 
50 /*
51  * Feature: ResidentProcessManager
52  * Function: StartResidentProcessWithMainElement
53  * SubFunction: NA
54  * FunctionPoints:ResidentProcessManager StartResidentProcessWithMainElement
55  * EnvConditions: NA
56  * CaseDescription: Verify StartResidentProcessWithMainElement
57  */
58 HWTEST_F(ResidentProcessManagerTest, StartResidentProcessWithMainElement_001, TestSize.Level1)
59 {
60     auto manager = std::make_shared<ResidentProcessManager>();
61     std::vector<BundleInfo> bundleInfos;
62     BundleInfo bundleInfo1;
63     BundleInfo bundleInfo2;
64     HapModuleInfo hapModuleInfo1;
65     HapModuleInfo hapModuleInfo2;
66     hapModuleInfo1.isModuleJson = false;
67     hapModuleInfo1.mainAbility = "";
68     hapModuleInfo2.isModuleJson = true;
69     hapModuleInfo2.mainElementName = "mainElementName";
70     hapModuleInfo2.process = "process";
71     bundleInfo1.isKeepAlive = true;
72     bundleInfo1.applicationInfo.process = "";
73     bundleInfo2.isKeepAlive = true;
74     bundleInfo2.applicationInfo.process = "process";
75     bundleInfo2.hapModuleInfos.emplace_back(hapModuleInfo1);
76     bundleInfo2.hapModuleInfos.emplace_back(hapModuleInfo2);
77     bundleInfos.emplace_back(bundleInfo1);
78     bundleInfos.emplace_back(bundleInfo2);
79     manager->StartResidentProcessWithMainElement(bundleInfos, 0);
80     EXPECT_TRUE(manager != nullptr);
81 }
82 
83 /*
84  * Feature: ResidentProcessManager
85  * Function: SetResidentProcessEnabled
86  * SubFunction: NA
87  * FunctionPoints:ResidentProcessManager SetResidentProcessEnabled
88  * EnvConditions: NA
89  * CaseDescription: Verify SetResidentProcessEnabled
90  */
91 HWTEST_F(ResidentProcessManagerTest, SetResidentProcessEnable_001, TestSize.Level1)
92 {
93     auto manager = std::make_shared<ResidentProcessManager>();
94     ASSERT_NE(manager, nullptr);
95     std::string bundleName = "com.example.resident.process";
96     std::string callerName;
97     EXPECT_EQ(manager->SetResidentProcessEnabled(bundleName, callerName, false), INVALID_PARAMETERS_ERR);
98 }
99 
100 /*
101  * Feature: ResidentProcessManager
102  * Function: ResidentProcessManager
103  * SubFunction: NA
104  * FunctionPoints:ResidentProcessManager ResidentProcessManager
105  * EnvConditions: NA
106  * CaseDescription: Verify ResidentProcessManager
107  */
108 HWTEST_F(ResidentProcessManagerTest, SetResidentProcessEnable_002, TestSize.Level1)
109 {
110     auto manager = std::make_shared<ResidentProcessManager>();
111     ASSERT_NE(manager, nullptr);
112 
113     std::string bundleName = "com.example.resident.process";
114     std::string callerName = "resident.process.manager.test";
115     EXPECT_EQ(manager->SetResidentProcessEnabled(bundleName, callerName, false), ERR_NO_RESIDENT_PERMISSION);
116 }
117 
118 /*
119  * Feature: ResidentProcessManager
120  * Function: PutResidentAbility
121  * SubFunction: NA
122  * FunctionPoints:ResidentProcessManager PutResidentAbility
123  * EnvConditions: NA
124  * CaseDescription: Verify PutResidentAbility
125  */
126 HWTEST_F(ResidentProcessManagerTest, PutResidentAbility_001, TestSize.Level1)
127 {
128     auto manager = std::make_shared<ResidentProcessManager>();
129     ASSERT_NE(manager, nullptr);
130 
131     std::string bundleName = "com.example.resident.process";
132     std::string callerName = "resident.process.manager.test";
133     auto residentId = manager->PutResidentAbility(bundleName, callerName, 0);
134     EXPECT_GE(residentId, 0);
135     EXPECT_TRUE(manager->IsResidentAbility(bundleName, callerName, 0));
136     manager->RemoveResidentAbility(residentId);
137     EXPECT_TRUE(manager->residentAbilityInfos_.empty());
138 }
139 
140 /*
141  * Feature: ResidentProcessManager
142  * Function: AddFailedResidentAbility
143  * SubFunction: NA
144  * FunctionPoints:ResidentProcessManager AddFailedResidentAbility
145  * EnvConditions: NA
146  * CaseDescription: Verify AddFailedResidentAbility
147  */
148 HWTEST_F(ResidentProcessManagerTest, AddFailedResidentAbility_001, TestSize.Level1)
149 {
150     auto manager = std::make_shared<ResidentProcessManager>();
151     ASSERT_NE(manager, nullptr);
152 
153     std::string bundleName = "com.example.resident.process";
154     std::string callerName = "resident.process.manager.test";
155     manager->unlockedAfterBoot_ = true;
156     manager->AddFailedResidentAbility(bundleName, callerName, 0);
157     EXPECT_TRUE(manager->failedResidentAbilityInfos_.empty());
158     manager->unlockedAfterBoot_ = false;
159     manager->AddFailedResidentAbility(bundleName, callerName, 0);
160     EXPECT_EQ(manager->failedResidentAbilityInfos_.size(), 1);
161 
162     manager->StartFailedResidentAbilities();
163     EXPECT_TRUE(manager->unlockedAfterBoot_);
164     EXPECT_TRUE(manager->failedResidentAbilityInfos_.empty());
165 }
166 }  // namespace AAFwk
167 }  // namespace OHOS
168