• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define private public
17 #include "app_mgr_service_inner.h"
18 #undef private
19 
20 #include <limits>
21 #include <gtest/gtest.h>
22 #include "iremote_object.h"
23 #include "refbase.h"
24 #include "application_info.h"
25 #include "app_record_id.h"
26 #include "app_scheduler_host.h"
27 #include "ability_info.h"
28 #include "ability_running_record.h"
29 #include "hilog_wrapper.h"
30 #include "mock_app_scheduler.h"
31 #include "mock_ability_token.h"
32 #include "mock_app_spawn_client.h"
33 #include "mock_app_mgr_service_inner.h"
34 #include "mock_iapp_state_callback.h"
35 #include "mock_bundle_manager.h"
36 #include "mock_application.h"
37 #include "mock_native_token.h"
38 
39 using namespace testing::ext;
40 using OHOS::iface_cast;
41 using OHOS::IRemoteObject;
42 using OHOS::sptr;
43 using testing::_;
44 using testing::Invoke;
45 using testing::InvokeWithoutArgs;
46 using testing::Return;
47 using testing::SetArgReferee;
48 using ::testing::DoAll;
49 
50 namespace OHOS {
51 namespace AppExecFwk {
52 class AppRunningProcessesInfoTest : public testing::Test {
53 public:
54     static void SetUpTestCase();
55     static void TearDownTestCase();
56     void SetUp();
57     void TearDown();
58 
59 protected:
GetTestProcessName()60     static const std::string GetTestProcessName()
61     {
62         return "com.ohos.test.helloworld";
63     }
GetTestAppName()64     static const std::string GetTestAppName()
65     {
66         return "com.ohos.test.helloworld";
67     }
GetTestAbilityName()68     static const std::string GetTestAbilityName()
69     {
70         return "test_ability_name";
71     }
GetTestUid()72     static int GetTestUid()
73     {
74         // a valid inner uid value which is not border value.
75         const static int VALID_UID_VALUE = 1010;
76         return VALID_UID_VALUE;
77     }
78 
79     std::shared_ptr<AppRunningRecord> GetTestAppRunningRecord();
80     sptr<IAppScheduler> GetMockedAppSchedulerClient() const;
81     std::shared_ptr<AppRunningRecord> StartLoadAbility(const sptr<IRemoteObject>& token,
82         const std::shared_ptr<AbilityInfo>& abilityInfo, const std::shared_ptr<ApplicationInfo>& appInfo,
83         const pid_t newPid) const;
GetMockToken() const84     sptr<MockAbilityToken> GetMockToken() const
85     {
86         return mock_token_;
87     }
88 
89 protected:
90     std::shared_ptr<AbilityRunningRecord> testAbilityRecord_;
91     sptr<IAppScheduler> client_;
92     sptr<MockAppScheduler> mockAppSchedulerClient_;
93     std::shared_ptr<AppRunningRecord> testAppRecord_;
94     std::unique_ptr<AppMgrServiceInner> service_;
95     sptr<MockAbilityToken> mock_token_;
96     sptr<BundleMgrService> mockBundleMgr;
97 };
98 
SetUpTestCase()99 void AppRunningProcessesInfoTest::SetUpTestCase()
100 {
101     MockNativeToken::SetNativeToken();
102 }
103 
TearDownTestCase()104 void AppRunningProcessesInfoTest::TearDownTestCase()
105 {}
106 
SetUp()107 void AppRunningProcessesInfoTest::SetUp()
108 {
109     mockAppSchedulerClient_ = new (std::nothrow) MockAppScheduler();
110     service_.reset(new (std::nothrow) AppMgrServiceInner());
111     mock_token_ = new (std::nothrow) MockAbilityToken();
112     client_ = iface_cast<IAppScheduler>(mockAppSchedulerClient_.GetRefPtr());
113     mockBundleMgr = new (std::nothrow) BundleMgrService();
114     service_->SetBundleManager(mockBundleMgr);
115 }
116 
TearDown()117 void AppRunningProcessesInfoTest::TearDown()
118 {
119     testAbilityRecord_.reset();
120     testAppRecord_.reset();
121 }
122 
GetMockedAppSchedulerClient() const123 sptr<IAppScheduler> AppRunningProcessesInfoTest::GetMockedAppSchedulerClient() const
124 {
125     if (client_) {
126         return client_;
127     }
128     return nullptr;
129 }
130 
GetTestAppRunningRecord()131 std::shared_ptr<AppRunningRecord> AppRunningProcessesInfoTest::GetTestAppRunningRecord()
132 {
133     if (!testAppRecord_) {
134         auto appInfo = std::make_shared<ApplicationInfo>();
135         appInfo->name = GetTestAppName();
136         testAppRecord_ = std::make_shared<AppRunningRecord>(appInfo, AppRecordId::Create(), GetTestProcessName());
137         testAppRecord_->SetApplicationClient(GetMockedAppSchedulerClient());
138         auto abilityInfo = std::make_shared<AbilityInfo>();
139         abilityInfo->name = GetTestAbilityName();
140         HapModuleInfo hapModuleInfo;
141         hapModuleInfo.moduleName = "module789";
142         testAppRecord_->AddModule(appInfo, abilityInfo, GetMockToken(), hapModuleInfo, nullptr);
143     }
144     return testAppRecord_;
145 }
146 
StartLoadAbility(const sptr<IRemoteObject> & token,const std::shared_ptr<AbilityInfo> & abilityInfo,const std::shared_ptr<ApplicationInfo> & appInfo,const pid_t newPid) const147 std::shared_ptr<AppRunningRecord> AppRunningProcessesInfoTest::StartLoadAbility(const sptr<IRemoteObject>& token,
148     const std::shared_ptr<AbilityInfo>& abilityInfo, const std::shared_ptr<ApplicationInfo>& appInfo,
149     const pid_t newPid) const
150 {
151     std::shared_ptr<MockAppSpawnClient> mockClientPtr = std::make_shared<MockAppSpawnClient>();
152     service_->SetAppSpawnClient(mockClientPtr);
153     EXPECT_CALL(*mockClientPtr, StartProcess(_, _)).Times(1).WillOnce(DoAll(SetArgReferee<1>(newPid), Return(ERR_OK)));
154 
155     service_->LoadAbility(token, nullptr, abilityInfo, appInfo, nullptr);
156 
157     BundleInfo bundleInfo;
158     bundleInfo.appId = "com.ohos.test.helloworld_code123";
159 
160     auto record = service_->appRunningManager_->CheckAppRunningRecordIsExist(
161         appInfo->name, GetTestProcessName(), appInfo->uid, bundleInfo);
162 
163     EXPECT_TRUE(record);
164     auto clent = GetMockedAppSchedulerClient();
165     record->SetApplicationClient(clent);
166     EXPECT_EQ(record->GetPriorityObject()->GetPid(), newPid);
167     EXPECT_NE(record->GetApplicationClient(), nullptr);
168     return record;
169 }
170 
171 /*
172  * Feature: AppMgrServiceInner
173  * Function: GetRunningProcessInfoByToken
174  * SubFunction: NA
175  * FunctionPoints: get running process info by token.
176  * EnvConditions: NA
177  * CaseDescription: creat apprunningrecord, set record state, call query function.
178  */
179 HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_001, TestSize.Level1)
180 {
181     auto abilityInfo = std::make_shared<AbilityInfo>();
182     abilityInfo->name = GetTestAbilityName();
183     auto appInfo = std::make_shared<ApplicationInfo>();
184     appInfo->name = GetTestAppName();
185     BundleInfo bundleInfo;
186     bundleInfo.appId = "com.ohos.test.helloworld_code123";
187     bundleInfo.jointUserId = "joint456";
188     HapModuleInfo hapModuleInfo;
189     hapModuleInfo.moduleName = "module789";
190     EXPECT_TRUE(service_ != nullptr);
191     auto record = service_->CreateAppRunningRecord(
192         GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr);
193     EXPECT_TRUE(record != nullptr);
194     record->SetState(ApplicationState::APP_STATE_FOREGROUND);
195     record->SetApplicationClient(GetMockedAppSchedulerClient());
196     RunningProcessInfo info;
197     service_->GetRunningProcessInfoByToken(GetMockToken(), info);
198     EXPECT_TRUE(info.processName_ == GetTestProcessName());
199 }
200 
201 /*
202  * Feature: AppMgrServiceInner
203  * Function: GetAllRunningProcesses
204  * SubFunction: NA
205  * FunctionPoints: get running process info by token.
206  * EnvConditions: NA
207  * CaseDescription: creat apprunningrecord, set record state, call query function.
208  */
209 HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_002, TestSize.Level1)
210 {
211     auto abilityInfo = std::make_shared<AbilityInfo>();
212     int uid = 0;
213     abilityInfo->name = GetTestAbilityName();
214     abilityInfo->applicationInfo.uid = uid;
215     auto appInfo = std::make_shared<ApplicationInfo>();
216     appInfo->name = GetTestAppName();
217     appInfo->uid = uid;
218     BundleInfo bundleInfo;
219     HapModuleInfo hapModuleInfo;
220     EXPECT_TRUE(service_->GetBundleAndHapInfo(*abilityInfo, appInfo, bundleInfo, hapModuleInfo));
221     EXPECT_TRUE(service_ != nullptr);
222     auto record = service_->CreateAppRunningRecord(
223         GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr);
224     EXPECT_TRUE(record != nullptr);
225 
226     record->SetUid(uid);
227     EXPECT_TRUE(record != nullptr) << ",create apprunningrecord fail!";
228 
229     sptr<MockApplication> mockApplication(new MockApplication());
230     sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
231     record->SetApplicationClient(client);
232     EXPECT_CALL(*mockApplication, ScheduleLaunchApplication(_, _))
233         .Times(1)
234         .WillOnce(Invoke(mockApplication.GetRefPtr(), &MockApplication::LaunchApplication));
235     Configuration config;
236     record->LaunchApplication(config);
237     mockApplication->Wait();
238 
239     EXPECT_CALL(*mockApplication, ScheduleForegroundApplication())
240         .Times(1)
241         .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post));
242     // application enter in foreground and check the result
243     record->ScheduleForegroundRunning();
244     mockApplication->Wait();
245 
246     // update application state and check the state
247     record->SetState(ApplicationState::APP_STATE_FOREGROUND);
248     auto newRecord = service_->appRunningManager_->CheckAppRunningRecordIsExist(
249         appInfo->name, GetTestProcessName(), appInfo->uid, bundleInfo);
250     EXPECT_TRUE(newRecord);
251     newRecord->SetUid(uid);
252     auto stateFromRec = newRecord->GetState();
253     EXPECT_EQ(stateFromRec, ApplicationState::APP_STATE_FOREGROUND);
254 
255     std::vector<RunningProcessInfo> info;
256     size_t infoCount{ 1 };
257     record->SetSpawned();
258     auto res = service_->GetAllRunningProcesses(info);
259     EXPECT_TRUE(res == ERR_OK);
260     EXPECT_TRUE(info.size() == infoCount);
261 }
262 
263 /*
264  * Feature: AppMgrServiceInner
265  * Function: GetAllRunningProcesses
266  * SubFunction: NA
267  * FunctionPoints: get running process info by token.
268  * EnvConditions: NA
269  * CaseDescription: creat two apprunningrecords, set record state, call query function.
270  */
271 HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_003, TestSize.Level1)
272 {
273     auto abilityInfo = std::make_shared<AbilityInfo>();
274     abilityInfo->name = GetTestAbilityName();
275     auto appInfo = std::make_shared<ApplicationInfo>();
276     appInfo->name = GetTestAppName();
277     int uid = 0;
278     BundleInfo bundleInfo;
279     bundleInfo.appId = "com.ohos.test.helloworld_code123";
280     bundleInfo.jointUserId = "joint456";
281     HapModuleInfo hapModuleInfo;
282     hapModuleInfo.moduleName = "module789";
283     EXPECT_TRUE(service_ != nullptr);
284     auto record = service_->CreateAppRunningRecord(
285         GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr);
286     EXPECT_TRUE(record != nullptr);
287 
288     record->SetUid(uid);
289     EXPECT_TRUE(record != nullptr) << ",create apprunningrecord fail!";
290 
291     sptr<MockApplication> mockApplication(new MockApplication());
292     sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
293     record->SetApplicationClient(client);
294     EXPECT_CALL(*mockApplication, ScheduleLaunchApplication(_, _))
295         .Times(1)
296         .WillOnce(Invoke(mockApplication.GetRefPtr(), &MockApplication::LaunchApplication));
297     Configuration config;
298     record->LaunchApplication(config);
299     mockApplication->Wait();
300 
301     EXPECT_CALL(*mockApplication, ScheduleForegroundApplication())
302         .Times(1)
303         .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post));
304     // application enter in foreground and check the result
305     record->ScheduleForegroundRunning();
306     mockApplication->Wait();
307 
308     // update application state and check the state
309     record->SetState(ApplicationState::APP_STATE_FOREGROUND);
310     auto newRecord = service_->appRunningManager_->CheckAppRunningRecordIsExist(
311         appInfo->name, GetTestProcessName(), appInfo->uid, bundleInfo);
312     EXPECT_TRUE(newRecord);
313     newRecord->SetUid(0);
314     auto stateFromRec = newRecord->GetState();
315     EXPECT_EQ(stateFromRec, ApplicationState::APP_STATE_FOREGROUND);
316 
317     auto abilityInfo2 = std::make_shared<AbilityInfo>();
318     abilityInfo2->name = GetTestAbilityName() + "2";
319     abilityInfo2->applicationInfo.uid = uid;
320     auto appInfo2 = std::make_shared<ApplicationInfo>();
321     appInfo2->name = GetTestAppName() + "2";
322     appInfo2->uid = uid;
323     BundleInfo bundleInfo2;
324     HapModuleInfo hapModuleInfo2;
325     EXPECT_TRUE(service_->GetBundleAndHapInfo(*abilityInfo2, appInfo2, bundleInfo2, hapModuleInfo2));
326     EXPECT_TRUE(service_ != nullptr);
327     auto mock_token = new (std::nothrow) MockAbilityToken();
328     auto record2 = service_->CreateAppRunningRecord(
329         mock_token, nullptr, appInfo2, abilityInfo2, GetTestProcessName() + "2", bundleInfo2, hapModuleInfo2, nullptr);
330     EXPECT_TRUE(record != nullptr);
331     record2->SetUid(uid);
332 
333     std::vector<RunningProcessInfo> info;
334     size_t infoCount{ 2 };
335     record->SetSpawned();
336     record2->SetSpawned();
337     auto res = service_->GetAllRunningProcesses(info);
338     EXPECT_TRUE(res == ERR_OK);
339     EXPECT_TRUE(info.size() == infoCount);
340 }
341 
342 /*
343  * Feature: AppMgrServiceInner
344  * Function: GetRunningProcessInfoByToken
345  * SubFunction: NA
346  * FunctionPoints: get running process info by token.
347  * EnvConditions: NA
348  * CaseDescription: creat apprunningrecords, set record state, call query function.
349  */
350 HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_004, TestSize.Level1)
351 {
352     auto abilityInfo = std::make_shared<AbilityInfo>();
353     abilityInfo->name = GetTestAbilityName();
354     auto appInfo = std::make_shared<ApplicationInfo>();
355     appInfo->name = GetTestAppName();
356     BundleInfo bundleInfo;
357     bundleInfo.appId = "com.ohos.test.helloworld_code123";
358     bundleInfo.jointUserId = "joint456";
359     HapModuleInfo hapModuleInfo;
360     hapModuleInfo.moduleName = "module789";
361     EXPECT_TRUE(service_ != nullptr);
362     auto record = service_->CreateAppRunningRecord(
363         GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr);
364     EXPECT_TRUE(record != nullptr);
365     record->SetState(ApplicationState::APP_STATE_BACKGROUND);
366     record->SetApplicationClient(GetMockedAppSchedulerClient());
367     RunningProcessInfo info;
368     service_->appRunningManager_->GetRunningProcessInfoByToken(GetMockToken(), info);
369     EXPECT_TRUE(info.processName_ == GetTestProcessName());
370 }
371 
372 /*
373  * Feature: AppMgrServiceInner
374  * Function: GetRunningProcessInfoByPid
375  * SubFunction: NA
376  * FunctionPoints: get running process info by pid.
377  * EnvConditions: NA
378  * CaseDescription: creat apprunningrecords, set record state, call query function.
379  */
380 HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_005, TestSize.Level1)
381 {
382     auto abilityInfo = std::make_shared<AbilityInfo>();
383     abilityInfo->name = GetTestAbilityName();
384     auto appInfo = std::make_shared<ApplicationInfo>();
385     appInfo->name = GetTestAppName();
386     BundleInfo bundleInfo;
387     bundleInfo.appId = "com.ohos.test.helloworld_code123";
388     bundleInfo.jointUserId = "joint456";
389     HapModuleInfo hapModuleInfo;
390     hapModuleInfo.moduleName = "module789";
391     EXPECT_TRUE(service_ != nullptr);
392     auto record = service_->CreateAppRunningRecord(
393         GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr);
394     EXPECT_TRUE(record != nullptr);
395     record->SetState(ApplicationState::APP_STATE_BACKGROUND);
396     record->SetApplicationClient(GetMockedAppSchedulerClient());
397     pid_t pid = 16738;
398     record->GetPriorityObject()->SetPid(pid);
399     RunningProcessInfo info;
400     service_->appRunningManager_->GetRunningProcessInfoByPid(pid, info);
401     EXPECT_TRUE(info.processName_ == GetTestProcessName());
402 }
403 
404 }  // namespace AppExecFwk
405 }  // namespace OHOS
406