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