1 /*
2 * Copyright (c) 2024 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 #include <gtest/gtest.h>
17
18 #define private public
19 #define protected public
20 #include "app_exit_reason_helper.h"
21 #include "task_handler_wrap.h"
22 #include "ability_event_handler.h"
23 #include "ability_manager_service.h"
24 #undef private
25 #undef protected
26
27 using namespace testing;
28 using namespace testing::ext;
29
30 namespace OHOS {
31 namespace AbilityRuntime {
32 namespace {
33 } // namespace
34 class AppExitReasonHelperTest : public testing::Test {
35 public:
36 static void SetUpTestCase(void);
37 static void TearDownTestCase(void);
38 void SetUp();
39 void TearDown();
40 };
41
SetUpTestCase(void)42 void AppExitReasonHelperTest::SetUpTestCase(void)
43 {}
44
TearDownTestCase(void)45 void AppExitReasonHelperTest::TearDownTestCase(void)
46 {}
47
SetUp()48 void AppExitReasonHelperTest::SetUp()
49 {}
50
TearDown()51 void AppExitReasonHelperTest::TearDown()
52 {}
53
54 /**
55 * @tc.name: RecordAppExitReason_0100
56 * @tc.desc: RecordAppExitReason
57 * @tc.type: FUNC
58 * @tc.require:
59 */
60 HWTEST_F(AppExitReasonHelperTest, RecordAppExitReason_0100, TestSize.Level1)
61 {
62 std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler_;
63 taskHandler_ = AAFwk::TaskHandlerWrap::CreateQueueHandler(AAFwk::AbilityConfig::NAME_ABILITY_MGR_SERVICE);
64 std::weak_ptr<AAFwk::AbilityManagerService> service;
65 service = AAFwk::AbilityManagerService::GetPubInstance();
66 std::shared_ptr<AAFwk::AbilityEventHandler> eventHandler_;
67 eventHandler_ = std::make_shared<AAFwk::AbilityEventHandler>(taskHandler_, service);
68 std::shared_ptr<AAFwk::SubManagersHelper> subManagersHelper_;
69 subManagersHelper_ = std::make_shared<AAFwk::SubManagersHelper>(taskHandler_, eventHandler_);
70 std::shared_ptr<AAFwk::AppExitReasonHelper> appExitReasonHelper_;
71 appExitReasonHelper_ = std::make_shared<AAFwk::AppExitReasonHelper>(subManagersHelper_);
72 AAFwk::ExitReason exitReason(AAFwk::REASON_MAX, "");
73 int32_t result = appExitReasonHelper_->RecordAppExitReason(exitReason);
74 EXPECT_NE(result, ERR_INVALID_VALUE);
75
76 AAFwk::ExitReason exitReason2;
77 result = appExitReasonHelper_->RecordAppExitReason(exitReason2);
78 EXPECT_NE(result, ERR_OK);
79 }
80
81 /**
82 * @tc.name: RecordProcessExitReason_0100
83 * @tc.desc: RecordProcessExitReason
84 * @tc.type: FUNC
85 * @tc.require:
86 */
87 HWTEST_F(AppExitReasonHelperTest, RecordProcessExitReason_0100, TestSize.Level1)
88 {
89 std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler_;
90 taskHandler_ = AAFwk::TaskHandlerWrap::CreateQueueHandler(AAFwk::AbilityConfig::NAME_ABILITY_MGR_SERVICE);
91 std::weak_ptr<AAFwk::AbilityManagerService> service;
92 service = AAFwk::AbilityManagerService::GetPubInstance();
93 std::shared_ptr<AAFwk::AbilityEventHandler> eventHandler_;
94 eventHandler_ = std::make_shared<AAFwk::AbilityEventHandler>(taskHandler_, service);
95 std::shared_ptr<AAFwk::SubManagersHelper> subManagersHelper_;
96 subManagersHelper_ = std::make_shared<AAFwk::SubManagersHelper>(taskHandler_, eventHandler_);
97 std::shared_ptr<AAFwk::AppExitReasonHelper> appExitReasonHelper_;
98 appExitReasonHelper_ = std::make_shared<AAFwk::AppExitReasonHelper>(subManagersHelper_);
99
100 int32_t pid = 0;
101 AAFwk::ExitReason exitReason;
102 int32_t result = appExitReasonHelper_->RecordProcessExitReason(pid, exitReason, false);
103 EXPECT_NE(result, ERR_OK);
104 }
105
106 /**
107 * @tc.name: RecordAppExitReason_0200
108 * @tc.desc: RecordAppExitReason
109 * @tc.type: FUNC
110 * @tc.require:
111 */
112 HWTEST_F(AppExitReasonHelperTest, RecordAppExitReason_0200, TestSize.Level1)
113 {
114 std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler_;
115 taskHandler_ = AAFwk::TaskHandlerWrap::CreateQueueHandler(AAFwk::AbilityConfig::NAME_ABILITY_MGR_SERVICE);
116 std::weak_ptr<AAFwk::AbilityManagerService> service;
117 service = AAFwk::AbilityManagerService::GetPubInstance();
118 std::shared_ptr<AAFwk::AbilityEventHandler> eventHandler_;
119 eventHandler_ = std::make_shared<AAFwk::AbilityEventHandler>(taskHandler_, service);
120 std::shared_ptr<AAFwk::SubManagersHelper> subManagersHelper_;
121 subManagersHelper_ = std::make_shared<AAFwk::SubManagersHelper>(taskHandler_, eventHandler_);
122 std::shared_ptr<AAFwk::AppExitReasonHelper> appExitReasonHelper_;
123 appExitReasonHelper_ = std::make_shared<AAFwk::AppExitReasonHelper>(subManagersHelper_);
124
125 int32_t uid = 0;
126 AAFwk::ExitReason exitReason;
127 std::string bundleName("bundleNameTest");
128 int32_t appIndex = 0;
129 int32_t result = appExitReasonHelper_->RecordAppExitReason(bundleName, uid, appIndex, exitReason);
130 EXPECT_NE(result, ERR_OK);
131 }
132
133 /**
134 * @tc.name: RecordProcessExitReason_0200
135 * @tc.desc: RecordProcessExitReason
136 * @tc.type: FUNC
137 * @tc.require:
138 */
139 HWTEST_F(AppExitReasonHelperTest, RecordProcessExitReason_0200, TestSize.Level1)
140 {
141 std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler_;
142 taskHandler_ = AAFwk::TaskHandlerWrap::CreateQueueHandler(AAFwk::AbilityConfig::NAME_ABILITY_MGR_SERVICE);
143 std::weak_ptr<AAFwk::AbilityManagerService> service;
144 service = AAFwk::AbilityManagerService::GetPubInstance();
145 std::shared_ptr<AAFwk::AbilityEventHandler> eventHandler_;
146 eventHandler_ = std::make_shared<AAFwk::AbilityEventHandler>(taskHandler_, service);
147 std::shared_ptr<AAFwk::SubManagersHelper> subManagersHelper_;
148 subManagersHelper_ = std::make_shared<AAFwk::SubManagersHelper>(taskHandler_, eventHandler_);
149 std::shared_ptr<AAFwk::AppExitReasonHelper> appExitReasonHelper_;
150 appExitReasonHelper_ = std::make_shared<AAFwk::AppExitReasonHelper>(subManagersHelper_);
151
152 int32_t pid = 0;
153 std::string bundleName("test");
154 int32_t uid = 0;
155 uint32_t accessTokenId = 0;
156 AAFwk::ExitReason exitReason;
157 int32_t result = appExitReasonHelper_->RecordProcessExitReason(pid, bundleName, uid, accessTokenId,
158 exitReason, {}, false);
159 EXPECT_NE(result, ERR_OK);
160 }
161
162 /**
163 * @tc.name: RecordProcessExtensionExitReason_0100
164 * @tc.desc: RecordProcessExtensionExitReason
165 * @tc.type: FUNC
166 * @tc.require:
167 */
168 HWTEST_F(AppExitReasonHelperTest, RecordProcessExtensionExitReason_0100, TestSize.Level1)
169 {
170 std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler_;
171 taskHandler_ = AAFwk::TaskHandlerWrap::CreateQueueHandler(AAFwk::AbilityConfig::NAME_ABILITY_MGR_SERVICE);
172 std::weak_ptr<AAFwk::AbilityManagerService> service;
173 service = AAFwk::AbilityManagerService::GetPubInstance();
174 std::shared_ptr<AAFwk::AbilityEventHandler> eventHandler_;
175 eventHandler_ = std::make_shared<AAFwk::AbilityEventHandler>(taskHandler_, service);
176 std::shared_ptr<AAFwk::SubManagersHelper> subManagersHelper_;
177 subManagersHelper_ = std::make_shared<AAFwk::SubManagersHelper>(taskHandler_, eventHandler_);
178 std::shared_ptr<AAFwk::AppExitReasonHelper> appExitReasonHelper_;
179 appExitReasonHelper_ = std::make_shared<AAFwk::AppExitReasonHelper>(subManagersHelper_);
180
181 int32_t pid = 0;
182 std::string bundleName("test");
183 AAFwk::ExitReason exitReason;
184 int32_t result = appExitReasonHelper_->RecordProcessExtensionExitReason(pid, bundleName, exitReason, {}, false);
185 EXPECT_NE(result, ERR_OK);
186 }
187
188 /**
189 * @tc.name: GetActiveAbilityList_0100
190 * @tc.desc: GetActiveAbilityList
191 * @tc.type: FUNC
192 * @tc.require:
193 */
194 HWTEST_F(AppExitReasonHelperTest, GetActiveAbilityList_0100, TestSize.Level1)
195 {
196 std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler_;
197 taskHandler_ = AAFwk::TaskHandlerWrap::CreateQueueHandler(AAFwk::AbilityConfig::NAME_ABILITY_MGR_SERVICE);
198 std::weak_ptr<AAFwk::AbilityManagerService> service;
199 service = AAFwk::AbilityManagerService::GetPubInstance();
200 std::shared_ptr<AAFwk::AbilityEventHandler> eventHandler_;
201 eventHandler_ = std::make_shared<AAFwk::AbilityEventHandler>(taskHandler_, service);
202 std::shared_ptr<AAFwk::SubManagersHelper> subManagersHelper_;
203 subManagersHelper_ = std::make_shared<AAFwk::SubManagersHelper>(taskHandler_, eventHandler_);
204 std::shared_ptr<AAFwk::AppExitReasonHelper> appExitReasonHelper_;
205 appExitReasonHelper_ = std::make_shared<AAFwk::AppExitReasonHelper>(subManagersHelper_);
206
207 int32_t uid = 0;
208 std::vector<std::string> abilityLists;
209 int32_t pid = 0;
210 appExitReasonHelper_->GetActiveAbilityList(uid, abilityLists, pid);
211 EXPECT_EQ(abilityLists.size(), 0);
212 }
213
214 /**
215 * @tc.name: GetActiveAbilityListFromUIAbilityManager_0100
216 * @tc.desc: GetActiveAbilityListFromUIAbilityManager
217 * @tc.type: FUNC
218 * @tc.require:
219 */
220 HWTEST_F(AppExitReasonHelperTest, GetActiveAbilityListFromUIAbilityManager_0100, TestSize.Level1)
221 {
222 std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler_;
223 taskHandler_ = AAFwk::TaskHandlerWrap::CreateQueueHandler(AAFwk::AbilityConfig::NAME_ABILITY_MGR_SERVICE);
224 std::weak_ptr<AAFwk::AbilityManagerService> service;
225 service = AAFwk::AbilityManagerService::GetPubInstance();
226 std::shared_ptr<AAFwk::AbilityEventHandler> eventHandler_;
227 eventHandler_ = std::make_shared<AAFwk::AbilityEventHandler>(taskHandler_, service);
228 std::shared_ptr<AAFwk::SubManagersHelper> subManagersHelper_;
229 subManagersHelper_ = std::make_shared<AAFwk::SubManagersHelper>(taskHandler_, eventHandler_);
230 std::shared_ptr<AAFwk::AppExitReasonHelper> appExitReasonHelper_;
231 appExitReasonHelper_ = std::make_shared<AAFwk::AppExitReasonHelper>(subManagersHelper_);
232
233 int32_t uid = 0;
234 std::vector<std::string> abilityLists;
235 int32_t pid = 0;
236 appExitReasonHelper_->GetActiveAbilityListFromUIAbilityManager(uid, abilityLists, pid);
237 EXPECT_EQ(abilityLists.size(), 0);
238 }
239 } // namespace AbilityRuntime
240 } // namespace OHOS
241