• 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     auto res = service_->GetAllRunningProcesses(info);
258     EXPECT_TRUE (res == ERR_OK);
259     EXPECT_TRUE(info.size() == infoCount);
260 }
261 
262 /*
263  * Feature: AppMgrServiceInner
264  * Function: GetAllRunningProcesses
265  * SubFunction: NA
266  * FunctionPoints: get running process info by token.
267  * EnvConditions: NA
268  * CaseDescription: creat two apprunningrecords, set record state, call query function.
269  */
270 HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_003, TestSize.Level1)
271 {
272     auto abilityInfo = std::make_shared<AbilityInfo>();
273     abilityInfo->name = GetTestAbilityName();
274     auto appInfo = std::make_shared<ApplicationInfo>();
275     appInfo->name = GetTestAppName();
276     int uid = 0;
277     BundleInfo bundleInfo;
278     bundleInfo.appId = "com.ohos.test.helloworld_code123";
279     bundleInfo.jointUserId = "joint456";
280     HapModuleInfo hapModuleInfo;
281     hapModuleInfo.moduleName = "module789";
282     EXPECT_TRUE(service_ != nullptr);
283     auto record = service_->CreateAppRunningRecord(
284         GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr);
285     EXPECT_TRUE(record != nullptr);
286 
287     record->SetUid(uid);
288     EXPECT_TRUE(record != nullptr) << ",create apprunningrecord fail!";
289 
290     sptr<MockApplication> mockApplication(new MockApplication());
291     sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
292     record->SetApplicationClient(client);
293     EXPECT_CALL(*mockApplication, ScheduleLaunchApplication(_, _))
294         .Times(1)
295         .WillOnce(Invoke(mockApplication.GetRefPtr(), &MockApplication::LaunchApplication));
296     Configuration config;
297     record->LaunchApplication(config);
298     mockApplication->Wait();
299 
300     EXPECT_CALL(*mockApplication, ScheduleForegroundApplication())
301         .Times(1)
302         .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post));
303     // application enter in foreground and check the result
304     record->ScheduleForegroundRunning();
305     mockApplication->Wait();
306 
307     // update application state and check the state
308     record->SetState(ApplicationState::APP_STATE_FOREGROUND);
309     auto newRecord = service_->appRunningManager_->CheckAppRunningRecordIsExist(
310         appInfo->name, GetTestProcessName(), appInfo->uid, bundleInfo);
311     EXPECT_TRUE(newRecord);
312     newRecord->SetUid(0);
313     auto stateFromRec = newRecord->GetState();
314     EXPECT_EQ(stateFromRec, ApplicationState::APP_STATE_FOREGROUND);
315 
316     auto abilityInfo2 = std::make_shared<AbilityInfo>();
317     abilityInfo2->name = GetTestAbilityName() + "2";
318     abilityInfo2->applicationInfo.uid = uid;
319     auto appInfo2 = std::make_shared<ApplicationInfo>();
320     appInfo2->name = GetTestAppName() + "2";
321     appInfo2->uid = uid;
322     BundleInfo bundleInfo2;
323     HapModuleInfo hapModuleInfo2;
324     EXPECT_TRUE(service_->GetBundleAndHapInfo(*abilityInfo2, appInfo2, bundleInfo2, hapModuleInfo2));
325     EXPECT_TRUE(service_ != nullptr);
326     auto mock_token = new (std::nothrow) MockAbilityToken();
327     auto record2 = service_->CreateAppRunningRecord(
328         mock_token, nullptr, appInfo2, abilityInfo2, GetTestProcessName() + "2", bundleInfo2, hapModuleInfo2, nullptr);
329     EXPECT_TRUE(record != nullptr);
330     record2->SetUid(uid);
331 
332     std::vector<RunningProcessInfo> info;
333     size_t infoCount {2};
334     auto res = service_->GetAllRunningProcesses(info);
335     EXPECT_TRUE (res == ERR_OK);
336     EXPECT_TRUE(info.size() == infoCount);
337 }
338 
339 /*
340  * Feature: AppMgrServiceInner
341  * Function: GetRunningProcessInfoByToken
342  * SubFunction: NA
343  * FunctionPoints: get running process info by token.
344  * EnvConditions: NA
345  * CaseDescription: creat apprunningrecords, set record state, call query function.
346  */
347 HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_004, TestSize.Level1)
348 {
349     auto abilityInfo = std::make_shared<AbilityInfo>();
350     abilityInfo->name = GetTestAbilityName();
351     auto appInfo = std::make_shared<ApplicationInfo>();
352     appInfo->name = GetTestAppName();
353     BundleInfo bundleInfo;
354     bundleInfo.appId = "com.ohos.test.helloworld_code123";
355     bundleInfo.jointUserId = "joint456";
356     HapModuleInfo hapModuleInfo;
357     hapModuleInfo.moduleName = "module789";
358     EXPECT_TRUE(service_ != nullptr);
359     auto record = service_->CreateAppRunningRecord(
360         GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr);
361     EXPECT_TRUE(record != nullptr);
362     record->SetState(ApplicationState::APP_STATE_BACKGROUND);
363     record->SetApplicationClient(GetMockedAppSchedulerClient());
364     RunningProcessInfo info;
365     service_->appRunningManager_->GetRunningProcessInfoByToken(GetMockToken(), info);
366     EXPECT_TRUE(info.processName_ == GetTestProcessName());
367 }
368 
369 /*
370  * Feature: AppMgrServiceInner
371  * Function: GetRunningProcessInfoByPid
372  * SubFunction: NA
373  * FunctionPoints: get running process info by pid.
374  * EnvConditions: NA
375  * CaseDescription: creat apprunningrecords, set record state, call query function.
376  */
377 HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_005, TestSize.Level1)
378 {
379     auto abilityInfo = std::make_shared<AbilityInfo>();
380     abilityInfo->name = GetTestAbilityName();
381     auto appInfo = std::make_shared<ApplicationInfo>();
382     appInfo->name = GetTestAppName();
383     BundleInfo bundleInfo;
384     bundleInfo.appId = "com.ohos.test.helloworld_code123";
385     bundleInfo.jointUserId = "joint456";
386     HapModuleInfo hapModuleInfo;
387     hapModuleInfo.moduleName = "module789";
388     EXPECT_TRUE(service_ != nullptr);
389     auto record = service_->CreateAppRunningRecord(
390         GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr);
391     EXPECT_TRUE(record != nullptr);
392     record->SetState(ApplicationState::APP_STATE_BACKGROUND);
393     record->SetApplicationClient(GetMockedAppSchedulerClient());
394     pid_t pid = 16738;
395     record->GetPriorityObject()->SetPid(pid);
396     RunningProcessInfo info;
397     service_->appRunningManager_->GetRunningProcessInfoByPid(pid, info);
398     EXPECT_TRUE(info.processName_ == GetTestProcessName());
399 }
400 
401 }  // namespace AppExecFwk
402 }  // namespace OHOS
403