• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 #define private public
17 #include "app_mgr_service.h"
18 #include "app_mgr_service_inner.h"
19 #undef private
20 
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 
24 #include "app_scheduler_host.h"
25 #include "app_scheduler_proxy.h"
26 #include "mock_app_mgr_service_inner.h"
27 #include "mock_app_spawn_socket.h"
28 #include "mock_native_token.h"
29 #include "semaphore_ex.h"
30 
31 using namespace testing::ext;
32 using testing::_;
33 using testing::Invoke;
34 using testing::InvokeWithoutArgs;
35 using testing::Return;
36 
37 namespace {
38 constexpr int COUNT = 1;
39 }
40 
41 namespace OHOS {
42 namespace AppExecFwk {
43 class TestAppSchedulerImpl : public AppSchedulerHost {
44 public:
ScheduleForegroundApplication()45     void ScheduleForegroundApplication() override
46     {}
ScheduleBackgroundApplication()47     void ScheduleBackgroundApplication() override
48     {}
ScheduleTerminateApplication()49     void ScheduleTerminateApplication() override
50     {}
ScheduleShrinkMemory(const int)51     void ScheduleShrinkMemory(const int) override
52     {}
ScheduleLowMemory()53     void ScheduleLowMemory() override
54     {}
ScheduleMemoryLevel(const int)55     void ScheduleMemoryLevel(const int) override
56     {}
ScheduleHeapMemory(const int,OHOS::AppExecFwk::MallocInfo &)57     void ScheduleHeapMemory(const int, OHOS::AppExecFwk::MallocInfo&) override
58     {}
ScheduleLaunchApplication(const AppLaunchData &,const Configuration &)59     void ScheduleLaunchApplication(const AppLaunchData&, const Configuration&) override
60     {}
ScheduleLaunchAbility(const AbilityInfo &,const sptr<IRemoteObject> &,const std::shared_ptr<AAFwk::Want> &)61     void ScheduleLaunchAbility(const AbilityInfo&, const sptr<IRemoteObject>&,
62         const std::shared_ptr<AAFwk::Want>&) override
63     {}
ScheduleCleanAbility(const sptr<IRemoteObject> &)64     void ScheduleCleanAbility(const sptr<IRemoteObject>&) override
65     {}
ScheduleProfileChanged(const Profile &)66     void ScheduleProfileChanged(const Profile&) override
67     {}
ScheduleConfigurationUpdated(const Configuration &)68     void ScheduleConfigurationUpdated(const Configuration&) override
69     {}
ScheduleProcessSecurityExit()70     void ScheduleProcessSecurityExit() override
71     {}
ScheduleAbilityStage(const HapModuleInfo &)72     void ScheduleAbilityStage(const HapModuleInfo&) override
73     {}
ScheduleUpdateApplicationInfoInstalled(const ApplicationInfo &)74     void ScheduleUpdateApplicationInfoInstalled(const ApplicationInfo&) override
75     {}
ScheduleAcceptWant(const AAFwk::Want & want,const std::string & moduleName)76     void ScheduleAcceptWant(const AAFwk::Want& want, const std::string& moduleName) override
77     {}
ScheduleNotifyLoadRepairPatch(const std::string & bundleName,const sptr<IQuickFixCallback> & callback,const int32_t recordId)78     int32_t ScheduleNotifyLoadRepairPatch(const std::string& bundleName,
79         const sptr<IQuickFixCallback>& callback, const int32_t recordId) override
80     {
81         return 0;
82     }
ScheduleNotifyHotReloadPage(const sptr<IQuickFixCallback> & callback,const int32_t recordId)83     int32_t ScheduleNotifyHotReloadPage(const sptr<IQuickFixCallback>& callback, const int32_t recordId) override
84     {
85         return 0;
86     }
ScheduleNotifyUnLoadRepairPatch(const std::string & bundleName,const sptr<IQuickFixCallback> & callback,const int32_t recordId)87     int32_t ScheduleNotifyUnLoadRepairPatch(const std::string& bundleName,
88         const sptr<IQuickFixCallback>& callback, const int32_t recordId) override
89     {
90         return 0;
91     }
ScheduleNotifyAppFault(const FaultData & faultData)92     int32_t ScheduleNotifyAppFault(const FaultData &faultData) override
93     {
94         return 0;
95     }
96 };
97 class AppMgrServiceModuleTest : public testing::Test {
98 public:
99     static void SetUpTestCase();
100     static void TearDownTestCase();
101     void SetUp();
102     void TearDown();
103 
104 protected:
105     inline static std::shared_ptr<MockAppMgrServiceInner> mockAppMgrServiceInner_{ nullptr };
106     inline static std::shared_ptr<AppMgrService> appMgrService_{ nullptr };
107     inline static sptr<IRemoteObject> testRemoteObject_{ nullptr };
108 };
109 
SetUpTestCase()110 void AppMgrServiceModuleTest::SetUpTestCase()
111 {
112     MockNativeToken::SetNativeToken();
113     if (!appMgrService_) {
114         appMgrService_ = std::make_shared<AppMgrService>();
115     }
116 
117     if (!mockAppMgrServiceInner_) {
118         mockAppMgrServiceInner_ = std::make_shared<MockAppMgrServiceInner>();
119     }
120 
121     if (appMgrService_ && mockAppMgrServiceInner_) {
122         appMgrService_->appMgrServiceInner_ = mockAppMgrServiceInner_;
123         appMgrService_->OnStart();
124     }
125 
126     if (!testRemoteObject_) {
127         testRemoteObject_ = static_cast<IRemoteObject*>(new TestAppSchedulerImpl);
128     }
129 }
130 
TearDownTestCase()131 void AppMgrServiceModuleTest::TearDownTestCase()
132 {
133     if (testRemoteObject_) {
134         testRemoteObject_.clear();
135     }
136 
137     if (mockAppMgrServiceInner_) {
138         mockAppMgrServiceInner_.reset();
139     }
140 
141     if (appMgrService_) {
142         appMgrService_->OnStop();
143         int sleepTime = 1;
144         sleep(sleepTime);  // Waiting for appMgrService_'s event loop backend thread exited.
145         if (appMgrService_->appMgrServiceInner_) {
146             appMgrService_->appMgrServiceInner_.reset();
147         }
148         if (appMgrService_->amsMgrScheduler_) {
149             appMgrService_->amsMgrScheduler_.clear();
150         }
151         appMgrService_.reset();
152     }
153 }
154 
SetUp()155 void AppMgrServiceModuleTest::SetUp()
156 {}
157 
TearDown()158 void AppMgrServiceModuleTest::TearDown()
159 {}
160 
161 /*
162  * Feature: AppMgrService
163  * Function: AttachApplication
164  * SubFunction: NA
165  * FunctionPoints: AppMgrService => AppMgrServiceInner: AttachApplication
166  * CaseDescription: Check event loop AttachApplication task post from AppMgrService to AppMgrServiceInner.
167  */
168 HWTEST_F(AppMgrServiceModuleTest, AttachApplication_001, TestSize.Level1)
169 {
170     EXPECT_TRUE(appMgrService_);
171     EXPECT_TRUE(mockAppMgrServiceInner_);
172     EXPECT_TRUE(testRemoteObject_);
173 
174     for (int i = 0; i < COUNT; ++i) {
175         EXPECT_CALL(*mockAppMgrServiceInner_, AddAppDeathRecipient(_, _))
176             .WillOnce(InvokeWithoutArgs(mockAppMgrServiceInner_.get(), &MockAppMgrServiceInner::Post));
177         EXPECT_CALL(*mockAppMgrServiceInner_, AttachApplication(_, _))
178             .WillOnce(InvokeWithoutArgs(mockAppMgrServiceInner_.get(), &MockAppMgrServiceInner::Post));
179 
180         sptr<IRemoteObject> client;
181         appMgrService_->AttachApplication(client);
182         mockAppMgrServiceInner_->Wait();
183     }
184 }
185 
186 /*
187  * Feature: AppMgrService
188  * Function: ApplicationForegrounded
189  * SubFunction: NA
190  * FunctionPoints: AppMgrService => AppMgrServiceInner: ApplicationForegrounded
191  * CaseDescription: Check event loop ApplicationForegrounded task post from AppMgrService to AppMgrServiceInner.
192  */
193 HWTEST_F(AppMgrServiceModuleTest, ApplicationForegrounded_001, TestSize.Level1)
194 {
195     EXPECT_TRUE(appMgrService_);
196     EXPECT_TRUE(mockAppMgrServiceInner_);
197 
198     int32_t testRecordId = 123;
199     bool testResult = false;
200     Semaphore sem(0);
201 
__anon7c1189070202(const int32_t recordId) 202     auto mockHandler = [&](const int32_t recordId) {
203         testResult = (recordId == testRecordId);
204         sem.Post();
205     };
206 
207     for (int i = 0; i < COUNT; ++i) {
208         testResult = false;
209 
210         EXPECT_CALL(*mockAppMgrServiceInner_, ApplicationForegrounded(_)).Times(1).WillOnce(Invoke(mockHandler));
211 
212         appMgrService_->ApplicationForegrounded(testRecordId);
213 
214         sem.Wait();
215 
216         EXPECT_TRUE(testResult);
217     }
218 }
219 
220 /*
221  * Feature: AppMgrService
222  * Function: ApplicationBackgrounded
223  * SubFunction: NA
224  * FunctionPoints: AppMgrService => AppMgrServiceInner: ApplicationBackgrounded
225  * CaseDescription: Check event loop ApplicationBackgrounded task post from AppMgrService to AppMgrServiceInner.
226  */
227 HWTEST_F(AppMgrServiceModuleTest, ApplicationBackgrounded_001, TestSize.Level1)
228 {
229     EXPECT_TRUE(appMgrService_);
230     EXPECT_TRUE(mockAppMgrServiceInner_);
231 
232     int32_t testRecordId = 123;
233     bool testResult = false;
234     Semaphore sem(0);
235 
__anon7c1189070302(const int32_t recordId) 236     auto mockHandler = [&](const int32_t recordId) {
237         testResult = (recordId == testRecordId);
238         sem.Post();
239     };
240 
241     for (int i = 0; i < COUNT; ++i) {
242         testResult = false;
243 
244         EXPECT_CALL(*mockAppMgrServiceInner_, ApplicationBackgrounded(_)).Times(1).WillOnce(Invoke(mockHandler));
245 
246         appMgrService_->ApplicationBackgrounded(testRecordId);
247 
248         sem.Wait();
249 
250         EXPECT_TRUE(testResult);
251     }
252 }
253 
254 /*
255  * Feature: AppMgrService
256  * Function: ApplicationTerminated
257  * SubFunction: NA
258  * FunctionPoints: AppMgrService => AppMgrServiceInner: ApplicationTerminated
259  * CaseDescription: Check event loop ApplicationTerminated task post from AppMgrService to AppMgrServiceInner.
260  */
261 HWTEST_F(AppMgrServiceModuleTest, ApplicationTerminated_001, TestSize.Level1)
262 {
263     EXPECT_TRUE(appMgrService_);
264     EXPECT_TRUE(mockAppMgrServiceInner_);
265 
266     int32_t testRecordId = 123;
267     bool testResult = false;
268     Semaphore sem(0);
269 
__anon7c1189070402(const int32_t recordId) 270     auto mockHandler = [&testResult, testRecordId, &sem](const int32_t recordId) {
271         testResult = (recordId == testRecordId);
272         sem.Post();
273     };
274 
275     for (int i = 0; i < COUNT; ++i) {
276         testResult = false;
277 
278         EXPECT_CALL(*mockAppMgrServiceInner_, ApplicationTerminated(_)).Times(1).WillOnce(Invoke(mockHandler));
279 
280         appMgrService_->ApplicationTerminated(testRecordId);
281 
282         sem.Wait();
283 
284         EXPECT_TRUE(testResult);
285     }
286 }
287 
288 /*
289  * Feature: AppMgrService
290  * Function: ClearUpApplicationData
291  * SubFunction: NA
292  * FunctionPoints: AppMgrService => AppMgrServiceInner: ClearUpApplicationData
293  * CaseDescription: Check event loop ClearUpApplicationData task post from AppMgrService to AppMgrServiceInner.
294  */
295 HWTEST_F(AppMgrServiceModuleTest, ClearUpApplicationData_001, TestSize.Level1)
296 {
297     EXPECT_TRUE(appMgrService_);
298     EXPECT_TRUE(mockAppMgrServiceInner_);
299 
300     std::string testAppName("testApp");
301     bool testResult = false;
302     Semaphore sem(0);
303 
__anon7c1189070502(const std::string& appName, const int32_t, const pid_t) 304     auto mockHandler = [&testResult, testAppName, &sem](const std::string& appName, const int32_t, const pid_t) {
305         testResult = (appName == testAppName);
306         sem.Post();
307     };
308 
309     for (int i = 0; i < COUNT; ++i) {
310         testResult = false;
311 
312         EXPECT_CALL(*mockAppMgrServiceInner_, ClearUpApplicationData(_, _, _)).Times(1).WillOnce(Invoke(mockHandler));
313 
314         appMgrService_->ClearUpApplicationData(testAppName);
315 
316         sem.Wait();
317 
318         EXPECT_TRUE(testResult);
319     }
320 }
321 
322 /*
323  * Feature: AppMgrService
324  * Function: GetAllRunningProcesses
325  * SubFunction: NA
326  * FunctionPoints: AppMgrService => AppMgrServiceInner: GetAllRunningProcesses
327  * CaseDescription: Check GetAllRunningProcesses.
328  */
329 HWTEST_F(AppMgrServiceModuleTest, GetAllRunningProcesses_001, TestSize.Level1)
330 {
331     EXPECT_TRUE(appMgrService_);
332     EXPECT_TRUE(mockAppMgrServiceInner_);
333 
334     std::vector<RunningProcessInfo> testRunningProcessInfo;
335 
336     int32_t testPid = 456;
337     std::string testProcessName = "testProcess";
338 
339     Semaphore sem(0);
__anon7c1189070602(std::vector<RunningProcessInfo>& runningProcessInfo) 340     auto mockHandler = [testProcessName, testPid, &sem](std::vector<RunningProcessInfo>& runningProcessInfo) {
341         auto& it = runningProcessInfo.emplace_back();
342         it.processName_ = testProcessName;
343         it.pid_ = testPid;
344         sem.Post();
345         return ERR_OK;
346     };
347 
348     for (int i = 0; i < COUNT; ++i) {
349         testRunningProcessInfo.clear();
350         EXPECT_CALL(*mockAppMgrServiceInner_, GetAllRunningProcesses(_)).Times(1).WillOnce(Invoke(mockHandler));
351 
352         auto result = appMgrService_->GetAllRunningProcesses(testRunningProcessInfo);
353         sem.Wait();
354 
355         EXPECT_EQ(testRunningProcessInfo.size(), size_t(1));
356         EXPECT_EQ(testRunningProcessInfo[0].processName_, testProcessName);
357         EXPECT_EQ(testRunningProcessInfo[0].pid_, testPid);
358         EXPECT_EQ(result, ERR_OK);
359     }
360 }
361 
362 /*
363  * Feature: AppMgrService
364  * Function: KillApplication
365  * SubFunction: NA
366  * FunctionPoints: AppMgrService => AppMgrServiceInner: KillApplication
367  * CaseDescription: Check KillApplication task post from AppMgrService to AppMgrServiceInner.
368  */
369 HWTEST_F(AppMgrServiceModuleTest, KillApplication_001, TestSize.Level1)
370 {
371     EXPECT_TRUE(appMgrService_);
372     EXPECT_TRUE(mockAppMgrServiceInner_);
373 
374     std::string testBundleName("testApp");
375     bool testResult = false;
376     Semaphore sem(0);
377 
__anon7c1189070702(const std::string& bundleName) 378     auto mockHandler = [&testResult, testBundleName, &sem](const std::string& bundleName) {
379         testResult = (bundleName == testBundleName);
380         sem.Post();
381         return 0;
382     };
383 
384     for (int i = 0; i < COUNT; ++i) {
385         testResult = false;
386 
387         EXPECT_CALL(*mockAppMgrServiceInner_, KillApplication(_)).Times(1).WillOnce(Invoke(mockHandler));
388 
389         int ret = appMgrService_->GetAmsMgr()->KillApplication(testBundleName);
390 
391         sem.Wait();
392 
393         EXPECT_TRUE(testResult);
394         EXPECT_EQ(ret, 0);
395     }
396 }
397 
398 /*
399  * Feature: AppMgrService
400  * Function: QueryServiceState
401  * SubFunction: AppMgrService => AppMgrServiceInner: QueryAppSpawnConnectionState
402  * FunctionPoints: NA
403  * CaseDescription: Check QueryServiceState.
404  */
405 HWTEST_F(AppMgrServiceModuleTest, QueryServiceState_001, TestSize.Level1)
406 {
407     EXPECT_TRUE(appMgrService_);
408     EXPECT_TRUE(mockAppMgrServiceInner_);
409 
410     SpawnConnectionState testSpawnConnectionState = SpawnConnectionState::STATE_CONNECTED;
411     Semaphore sem(0);
412 
__anon7c1189070802() 413     auto mockHandler = [&]() {
414         sem.Post();
415         return testSpawnConnectionState;
416     };
417 
418     for (int i = 0; i < COUNT; ++i) {
419         EXPECT_CALL(*mockAppMgrServiceInner_, QueryAppSpawnConnectionState()).Times(1).WillOnce(Invoke(mockHandler));
420 
421         auto serviceState = appMgrService_->QueryServiceState();
422 
423         sem.Wait();
424 
425         EXPECT_EQ(serviceState.connectionState, testSpawnConnectionState);
426     }
427 }
428 
429 /*
430  * Feature: AppMgrService
431  * Function: GetAmsMgr
432  * SubFunction: NA
433  * FunctionPoints: NA
434  * CaseDescription: Check GetAmsMgr.
435  */
436 HWTEST_F(AppMgrServiceModuleTest, GetAmsMgr_001, TestSize.Level1)
437 {
438     EXPECT_TRUE(appMgrService_);
439 
440     auto amsMgr = appMgrService_->GetAmsMgr();
441 
442     EXPECT_TRUE(amsMgr);
443 }
444 }  // namespace AppExecFwk
445 }  // namespace OHOS
446