1 /*
2 * Copyright (c) 2024-2025 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 #include <mutex>
18
19 #define private public
20 #include "exit_resident_process_manager.h"
21 #undef private
22
23 #include "ability_manager_errors.h"
24 #include "hilog_tag_wrapper.h"
25 #include "in_process_call_wrapper.h"
26 #include "remote_client_manager.h"
27
28 using namespace testing;
29 using namespace testing::ext;
30
31 namespace OHOS {
32 namespace AppExecFwk {
33 class ExitResidentProcessManagerTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37 void SetUp() override;
38 void TearDown() override;
39 };
40
SetUpTestCase()41 void ExitResidentProcessManagerTest::SetUpTestCase()
42 {}
43
TearDownTestCase()44 void ExitResidentProcessManagerTest::TearDownTestCase()
45 {}
46
SetUp()47 void ExitResidentProcessManagerTest::SetUp()
48 {}
49
TearDown()50 void ExitResidentProcessManagerTest::TearDown()
51 {}
52
53 /**
54 * @tc.name: IsMemorySizeSufficent_001
55 * @tc.desc: Verify that the IsMemorySizeSufficient interface calls normally
56 * @tc.type: FUNC
57 */
58 HWTEST_F(ExitResidentProcessManagerTest, IsMemorySizeSufficent_001, TestSize.Level1)
59 {
60 auto exitResidentProcessManager = std::make_shared<ExitResidentProcessManager>();
61 EXPECT_EQ(exitResidentProcessManager->IsMemorySizeSufficient(), true);
62 }
63
64 /**
65 * @tc.name: RecordExitResidentBundleName_001
66 * @tc.desc: Verify that the RecordExitResidentBundleName interface calls normally
67 * @tc.type: FUNC
68 */
69 HWTEST_F(ExitResidentProcessManagerTest, RecordExitResidentBundleName_001, TestSize.Level1)
70 {
71 std::string bundleName = "bundleName";
72 auto exitResidentProcessManager = std::make_shared<ExitResidentProcessManager>();
73 int32_t uid = 0;
74 EXPECT_EQ(exitResidentProcessManager->RecordExitResidentBundleName(bundleName, uid), false);
75 }
76
77 /**
78 * @tc.name: RecordExitResidentBundleName_002
79 * @tc.desc: Verify that the RecordExitResidentBundleName interface calls normally
80 * @tc.type: FUNC
81 */
82 HWTEST_F(ExitResidentProcessManagerTest, RecordExitResidentBundleName_002, TestSize.Level1)
83 {
84 auto exitResidentProcessManager = std::make_shared<ExitResidentProcessManager>();
85 exitResidentProcessManager->currentMemorySizeState_ = MemoryState::LOW_MEMORY;
86 EXPECT_EQ(exitResidentProcessManager->RecordExitResidentBundleName("", 0), true);
87 }
88
89 /**
90 * @tc.name: RecordExitResidentBundleDependedOnWeb_001
91 * @tc.desc: Verify that the RecordExitResidentBundleDependedOnWeb interface calls normally
92 * @tc.type: FUNC
93 */
94 HWTEST_F(ExitResidentProcessManagerTest, RecordExitResidentBundleDependedOnWeb_001, TestSize.Level1)
95 {
96 std::string bundleName = "bundleName";
97 auto exitResidentProcessManager = std::make_shared<ExitResidentProcessManager>();
98 int32_t uid = 0;
99 exitResidentProcessManager->RecordExitResidentBundleDependedOnWeb(bundleName, uid);
100 auto flag = exitResidentProcessManager->exitResidentBundlesDependedOnWeb_;
101 EXPECT_EQ(flag.empty(), false);
102 }
103
104 /**
105 * @tc.name: HandleMemorySizeInSufficent_001
106 * @tc.desc: Verify that the HandleMemorySizeInSufficent interface calls normally
107 * @tc.type: FUNC
108 */
109 HWTEST_F(ExitResidentProcessManagerTest, HandleMemorySizeInSufficent_001, TestSize.Level1)
110 {
111 auto exitResidentProcessManager = std::make_shared<ExitResidentProcessManager>();
112 exitResidentProcessManager->HandleMemorySizeInSufficent();
113 EXPECT_EQ(exitResidentProcessManager->HandleMemorySizeInSufficent(),
114 AAFwk::ERR_NATIVE_MEMORY_SIZE_STATE_UNCHANGED);
115 }
116
117 /**
118 * @tc.name: HandleMemorySizeInSufficent_002
119 * @tc.desc: Verify that the HandleMemorySizeInSufficent interface calls normally
120 * @tc.type: FUNC
121 */
122 HWTEST_F(ExitResidentProcessManagerTest, HandleMemorySizeInSufficent_002, TestSize.Level1)
123 {
124 auto exitResidentProcessManager = std::make_shared<ExitResidentProcessManager>();
125 EXPECT_EQ(exitResidentProcessManager->HandleMemorySizeInSufficent(), ERR_OK);
126 }
127
128 /**
129 * @tc.name: HandleExitResidentBundleDependedOnWeb_001
130 * @tc.desc: Verify that the HandleExitResidentBundleDependedOnWeb interface calls normally
131 * @tc.type: FUNC
132 */
133 HWTEST_F(ExitResidentProcessManagerTest, HandleExitResidentBundleDependedOnWeb_001, TestSize.Level1)
134 {
135 std::vector<ExitResidentProcessInfo> bundleNames;
136 auto exitResidentProcessManager = std::make_shared<ExitResidentProcessManager>();
137 exitResidentProcessManager->HandleExitResidentBundleDependedOnWeb(bundleNames);
138 auto flag = exitResidentProcessManager->exitResidentBundlesDependedOnWeb_;
139 EXPECT_EQ(flag.empty(), true);
140 }
141
142 /**
143 * @tc.name: QueryExitBundleInfos_001
144 * @tc.desc: Verify that the QueryExitBundleInfos interface calls normally
145 * @tc.type: FUNC
146 */
147 HWTEST_F(ExitResidentProcessManagerTest, QueryExitBundleInfos_001, TestSize.Level1)
148 {
149 std::vector<ExitResidentProcessInfo> exitBundleNames;
150 std::vector<AppExecFwk::BundleInfo> exitBundleInfos;
151 auto exitResidentProcessManager = std::make_shared<ExitResidentProcessManager>();
152 std::shared_ptr<RemoteClientManager> remoteClientManager = std::make_shared<RemoteClientManager>();
153 auto bundleMgrHelper = remoteClientManager->GetBundleManagerHelper();
154 exitResidentProcessManager->QueryExitBundleInfos(exitBundleNames, exitBundleInfos);
155 EXPECT_NE(remoteClientManager, nullptr);
156 EXPECT_NE(bundleMgrHelper, nullptr);
157 }
158
159 /**
160 * @tc.name: IsKilledForUpgradeWeb_001
161 * @tc.desc: Verify that the IsKilledForUpgradeWeb interface calls normally
162 * @tc.type: FUNC
163 */
164 HWTEST_F(ExitResidentProcessManagerTest, IsKilledForUpgradeWeb_001, TestSize.Level1)
165 {
166 std::string bundleName = "bundleName";
167 auto exitResidentProcessManager = std::make_shared<ExitResidentProcessManager>();
168 // exitResidentProcessManager->exitResidentBundlesDependedOnWeb_;
169 EXPECT_EQ(exitResidentProcessManager->IsKilledForUpgradeWeb(bundleName), false);
170 }
171
172 /**
173 * @tc.name: IsMemorySizeSufficient_001
174 * @tc.desc: Verify that the IsMemorySizeSufficient interface calls normally
175 * @tc.type: FUNC
176 */
177 HWTEST_F(ExitResidentProcessManagerTest, IsMemorySizeSufficient_001, TestSize.Level1)
178 {
179 auto exitResidentProcessManager = std::make_shared<ExitResidentProcessManager>();
180 exitResidentProcessManager->currentBigMemoryState_ = MemoryState::MEMORY_RECOVERY;
181 EXPECT_EQ(exitResidentProcessManager->IsMemorySizeSufficient(), true);
182 }
183
184 /**
185 * @tc.name: RecordExitResidentBundleNameOnRequireBigMemory_001
186 * @tc.desc: Verify that the RecordExitResidentBundleNameOnRequireBigMemory interface calls normally
187 * @tc.type: FUNC
188 */
189 HWTEST_F(ExitResidentProcessManagerTest, RecordExitResidentBundleNameOnRequireBigMemory_001, TestSize.Level1)
190 {
191 auto exitResidentProcessManager = std::make_shared<ExitResidentProcessManager>();
192 exitResidentProcessManager->currentBigMemoryState_ = MemoryState::NO_REQUIRE_BIG_MEMORY;
193 EXPECT_EQ(exitResidentProcessManager->RecordExitResidentBundleNameOnRequireBigMemory("", 0), false);
194
195 auto exitResidentProcessManager2 = std::make_shared<ExitResidentProcessManager>();
196 exitResidentProcessManager2->currentBigMemoryState_ = MemoryState::MEMORY_RECOVERY;
197 EXPECT_EQ(exitResidentProcessManager2->RecordExitResidentBundleNameOnRequireBigMemory("", 0), true);
198 }
199
200 /**
201 * @tc.name: HandleRequireBigMemoryOptimization_001
202 * @tc.desc: Verify that the HandleRequireBigMemoryOptimization interface calls normally
203 * @tc.type: FUNC
204 */
205 HWTEST_F(ExitResidentProcessManagerTest, HandleRequireBigMemoryOptimization_001, TestSize.Level1)
206 {
207 auto exitResidentProcessManager = std::make_shared<ExitResidentProcessManager>();
208 exitResidentProcessManager->currentBigMemoryState_ = MemoryState::NO_REQUIRE_BIG_MEMORY;
209 EXPECT_EQ(exitResidentProcessManager->HandleRequireBigMemoryOptimization(), ERR_OK);
210
211 auto exitResidentProcessManager2 = std::make_shared<ExitResidentProcessManager>();
212 exitResidentProcessManager2->currentBigMemoryState_ = MemoryState::MEMORY_RECOVERY;
213 EXPECT_NE(exitResidentProcessManager2->HandleRequireBigMemoryOptimization(), ERR_OK);
214 }
215
216 /**
217 * @tc.name: HandleNoRequireBigMemoryOptimization_001
218 * @tc.desc: Verify that the HandleNoRequireBigMemoryOptimization interface calls normally
219 * @tc.type: FUNC
220 */
221 HWTEST_F(ExitResidentProcessManagerTest, HandleNoRequireBigMemoryOptimization_001, TestSize.Level1)
222 {
223 auto exitResidentProcessManager = std::make_shared<ExitResidentProcessManager>();
224 std::vector<ExitResidentProcessInfo> vecInfo;
225 exitResidentProcessManager->currentBigMemoryState_ = MemoryState::LOW_MEMORY;
226 EXPECT_EQ(exitResidentProcessManager->HandleNoRequireBigMemoryOptimization(vecInfo), ERR_OK);
227
228 auto exitResidentProcessManager2 = std::make_shared<ExitResidentProcessManager>();
229 exitResidentProcessManager2->currentBigMemoryState_ = MemoryState::NO_REQUIRE_BIG_MEMORY;
230 EXPECT_NE(exitResidentProcessManager2->HandleNoRequireBigMemoryOptimization(vecInfo), ERR_OK);
231 }
232
233 /**
234 * @tc.name: IsNoRequireBigMemory_001
235 * @tc.desc: Verify IsNoRequireBigMemory when state is NO_REQUIRE_BIG_MEMORY
236 * @tc.type: FUNC
237 */
238 HWTEST_F(ExitResidentProcessManagerTest, IsNoRequireBigMemory_001, TestSize.Level1)
239 {
240 auto exitResidentProcessManager = std::make_shared<ExitResidentProcessManager>();
241 exitResidentProcessManager->currentBigMemoryState_ = MemoryState::NO_REQUIRE_BIG_MEMORY;
242 EXPECT_EQ(exitResidentProcessManager->IsNoRequireBigMemory(), true);
243 }
244 /**
245 * @tc.name: RecordExitResidentBundleNameOnRequireBigMemory_002
246 * @tc.desc: Verify RecordExitResidentBundleNameOnRequireBigMemory when state is REQUIRE_BIG_MEMORY
247 * @tc.type: FUNC
248 */
249 HWTEST_F(ExitResidentProcessManagerTest, RecordExitResidentBundleNameOnRequireBigMemory_002, TestSize.Level1)
250 {
251 auto exitResidentProcessManager = std::make_shared<ExitResidentProcessManager>();
252 exitResidentProcessManager->currentBigMemoryState_ = MemoryState::REQUIRE_BIG_MEMORY;
253 std::string bundleName = "testBundle";
254 int32_t uid = 1000;
255 EXPECT_EQ(exitResidentProcessManager->RecordExitResidentBundleNameOnRequireBigMemory(bundleName, uid), true);
256 }
257
258 /**
259 * @tc.name: IsKilledForUpgradeWeb_002
260 * @tc.desc: Verify IsKilledForUpgradeWeb when bundle is in the list
261 * @tc.type: FUNC
262 */
263 HWTEST_F(ExitResidentProcessManagerTest, IsKilledForUpgradeWeb_002, TestSize.Level1)
264 {
265 auto exitResidentProcessManager = std::make_shared<ExitResidentProcessManager>();
266 std::string bundleName = "testBundle";
267 exitResidentProcessManager->exitResidentBundlesDependedOnWeb_.emplace_back(bundleName, 1000);
268 EXPECT_EQ(exitResidentProcessManager->IsKilledForUpgradeWeb(bundleName), true);
269 }
270 } // namespace AppExecFwk
271 } // namespace OHOS
272