• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_death_recipient.h"
18 #include "app_mgr_service_inner.h"
19 #undef private
20 #include <gtest/gtest.h>
21 #include "hilog_wrapper.h"
22 #include "mock_ability_token.h"
23 #include "mock_app_scheduler.h"
24 #include "mock_app_spawn_client.h"
25 #include "iremote_object.h"
26 #include "mock_bundle_manager.h"
27 
28 using namespace testing::ext;
29 using testing::_;
30 using testing::Return;
31 using testing::SetArgReferee;
32 using ::testing::DoAll;
33 
34 namespace OHOS {
35 namespace AppExecFwk {
36 class AppDeathRecipientTest : public testing::Test {
37 public:
38     static void SetUpTestCase();
39     static void TearDownTestCase();
40     void SetUp();
41     void TearDown();
42 
43 public:
44     const std::shared_ptr<AbilityInfo> GetAbilityInfoByIndex(const int32_t index) const;
45     const std::shared_ptr<ApplicationInfo> GetApplicationByIndex(const int32_t index) const;
46     const std::shared_ptr<AppRunningRecord> GetAppRunningRecordByIndex(const int32_t index) const;
47     sptr<IRemoteObject> GetApp(int32_t pid, int size);
48 
49 public:
50     std::shared_ptr<AAFwk::TaskHandlerWrap> handler_;
51     std::shared_ptr<AppMgrServiceInner> appMgrServiceInner_;
52     sptr<AppDeathRecipient> appDeathRecipientObject_;
53     OHOS::sptr<MockAbilityToken> mockToken_;
54     OHOS::sptr<BundleMgrService> mockBundleMgr;
55 };
56 
WaitUntilTaskFinished(std::shared_ptr<AAFwk::TaskHandlerWrap> handler)57 static void WaitUntilTaskFinished(std::shared_ptr<AAFwk::TaskHandlerWrap> handler)
58 {
59     if (!handler) {
60         return;
61     }
62 
63     const uint32_t MAX_RETRY_COUNT = 1000;
64     const uint32_t SLEEP_TIME = 1000;
65     uint32_t count = 0;
66     std::atomic<bool> taskCalled(false);
67     auto f = [&taskCalled]() { taskCalled.store(true); };
68     if (handler->SubmitTask(f)) {
69         while (!taskCalled.load()) {
70             ++count;
71             // if delay more than 1 second, break
72             if (count >= MAX_RETRY_COUNT) {
73                 break;
74             }
75 
76             usleep(SLEEP_TIME);
77         }
78     }
79 }
80 
SetUpTestCase()81 void AppDeathRecipientTest::SetUpTestCase()
82 {}
83 
TearDownTestCase()84 void AppDeathRecipientTest::TearDownTestCase()
85 {}
86 
SetUp()87 void AppDeathRecipientTest::SetUp()
88 {
89     appMgrServiceInner_ = std::make_shared<AppMgrServiceInner>();
90     appMgrServiceInner_->Init();
91 
92     handler_ = AAFwk::TaskHandlerWrap::CreateQueueHandler("AppDeathRecipientTest");
93 
94     appDeathRecipientObject_ = new (std::nothrow) AppDeathRecipient();
95 
96     mockBundleMgr = new (std::nothrow) BundleMgrService();
97     appMgrServiceInner_->SetBundleManager(mockBundleMgr);
98 }
99 
TearDown()100 void AppDeathRecipientTest::TearDown()
101 {}
102 
GetAbilityInfoByIndex(const int32_t index) const103 const std::shared_ptr<AbilityInfo> AppDeathRecipientTest::GetAbilityInfoByIndex(const int32_t index) const
104 {
105     std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
106     abilityInfo->name = "AppDeathRecipientTest_ability" + std::to_string(index);
107     abilityInfo->applicationName = "com.ohos.test.helloworld" + std::to_string(index);
108     abilityInfo->applicationInfo.bundleName = "com.ohos.test.helloworld" + std::to_string(index);
109     return abilityInfo;
110 }
111 
GetApplicationByIndex(const int32_t index) const112 const std::shared_ptr<ApplicationInfo> AppDeathRecipientTest::GetApplicationByIndex(const int32_t index) const
113 {
114     std::shared_ptr<ApplicationInfo> appInfo = std::make_shared<ApplicationInfo>();
115     appInfo->name = "com.ohos.test.helloworld" + std::to_string(index);
116     appInfo->bundleName = "com.ohos.test.helloworld" + std::to_string(index);
117     return appInfo;
118 }
119 
GetAppRunningRecordByIndex(const int32_t index) const120 const std::shared_ptr<AppRunningRecord> AppDeathRecipientTest::GetAppRunningRecordByIndex(const int32_t index) const
121 {
122     auto appInfo = GetApplicationByIndex(index);
123     BundleInfo bundleInfo;
124     bundleInfo.appId = "com.ohos.test.helloworld_code123";
125 
126     auto appRecord = appMgrServiceInner_->appRunningManager_->CheckAppRunningRecordIsExist(
127         appInfo->name, appInfo->name, appInfo->uid, bundleInfo);
128 
129     EXPECT_NE(nullptr, appRecord);
130     return appRecord;
131 }
132 
GetApp(int32_t pid,int size)133 sptr<IRemoteObject> AppDeathRecipientTest::GetApp(int32_t pid, int size)
134 {
135     auto abilityInfo = GetAbilityInfoByIndex(pid);
136     auto appInfo = GetApplicationByIndex(pid);
137     sptr<IRemoteObject> token = new MockAbilityToken();
138 
139     std::shared_ptr<MockAppSpawnClient> mockClientPtr = std::make_shared<MockAppSpawnClient>();
140     EXPECT_CALL(*mockClientPtr, StartProcess(_, _)).Times(1).WillOnce(DoAll(SetArgReferee<1>(pid), Return(ERR_OK)));
141     std::shared_ptr<MockAppSpawnClient> mockClientstr(mockClientPtr);
142     appMgrServiceInner_->SetAppSpawnClient(mockClientstr);
143 
144     appMgrServiceInner_->LoadAbility(token, nullptr, abilityInfo, appInfo, nullptr);
145 
146     auto appRecord = GetAppRunningRecordByIndex(pid);
147 
148     EXPECT_EQ(size, static_cast<int>(appMgrServiceInner_->GetRecentAppList().size()));
149 
150     sptr<MockAppScheduler> mockAppScheduler = new (std::nothrow) MockAppScheduler();
151     sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockAppScheduler.GetRefPtr());
152     appRecord->SetApplicationClient(client);
153 
154     return client->AsObject();
155 }
156 
157 /*
158  * Feature: Ams
159  * Function: SetTaskHandler ande SetAppMgrServiceInner.
160  * SubFunction: AppDeathRecipient
161  * FunctionPoints: initialization
162  * EnvConditions: have to an application
163  * CaseDescription: How to set parameters
164  */
165 
166 HWTEST_F(AppDeathRecipientTest, AppDeathRecipient_001, TestSize.Level1)
167 {
168     HILOG_INFO("AppDeathRecipient_001 start");
169     appDeathRecipientObject_->SetTaskHandler(handler_);
170     EXPECT_TRUE(appDeathRecipientObject_->handler_.lock() != nullptr);
171 
172     appDeathRecipientObject_->SetAppMgrServiceInner(appMgrServiceInner_);
173     EXPECT_TRUE(appDeathRecipientObject_->appMgrServiceInner_.lock() != nullptr);
174     HILOG_INFO("AppDeathRecipient_001 end");
175 }
176 
177 /*
178  * Feature: Ams
179  * Function: OnRemoteDied
180  * SubFunction: AppDeathRecipient
181  * FunctionPoints: Applied death notification
182  * EnvConditions: have to an application
183  * CaseDescription: Call back the death notification of the notification application
184  */
185 HWTEST_F(AppDeathRecipientTest, AppDeathRecipient_002, TestSize.Level1)
186 {
187     HILOG_INFO("AppDeathRecipient_002 start");
188     pid_t pid1 = 24;
189     pid_t pid2 = 25;
190     sptr<IRemoteObject> appOne = GetApp(pid1, 1);
191     sptr<IRemoteObject> appTwo = GetApp(pid2, 2);
192 
193     appDeathRecipientObject_->SetTaskHandler(handler_);
194     appDeathRecipientObject_->SetAppMgrServiceInner(appMgrServiceInner_);
195     appDeathRecipientObject_->OnRemoteDied(appOne);
196 
197     WaitUntilTaskFinished(handler_);
198     EXPECT_EQ(1, static_cast<int>(appDeathRecipientObject_->appMgrServiceInner_.lock()->GetRecentAppList().size()));
199     HILOG_INFO("AppDeathRecipient_002 end");
200 }
201 }  // namespace AppExecFwk
202 }  // namespace OHOS
203