1 /*
2 * Copyright (c) 2021 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 <thread>
17 #include <chrono>
18 #include <cstdio>
19 #include <gtest/gtest.h>
20 #include "hilog_wrapper.h"
21 #include "ability_connect_callback_proxy.h"
22 #include "ability_connect_callback_stub.h"
23 #include "ability_manager_service.h"
24 #include "ability_manager_errors.h"
25 #include "app_mgr_service.h"
26 #include "module_test_dump_util.h"
27 #include "st_ability_util.h"
28 #include "module_test_dump_util.h"
29 #include "sa_mgr_client.h"
30 #include "system_ability_definition.h"
31 #include "common_event.h"
32 #include "common_event_manager.h"
33
34 using namespace testing::ext;
35 using namespace OHOS;
36 using namespace OHOS::AAFwk;
37 using namespace OHOS::AppExecFwk;
38 using namespace OHOS::MTUtil;
39 using namespace OHOS::STABUtil;
40 using namespace OHOS::EventFwk;
41
42 namespace {
43 using MAP_STR_STR = std::map<std::string, std::string>;
44 std::vector<std::string> bundleNameSuffix = {"Q", "R", "DFX"};
45 std::string bundleNameBase = "com.ohos.amsst.app";
46 std::string hapNameBase = "amsSystemTest";
47 std::string abilityNameBase = "AmsStAbility";
48 std::string launcherBundle = "com.ohos.launcher";
49 std::string launcherAbility = "com.ohos.launcher.MainAbility";
50 std::string systemUiBundle = "com.ohos.systemui";
51 std::string terminatePageAbility = "requ_page_ability_terminate";
52
53 static const std::string DUMP_STACK_LIST = "--stack-list";
54 const std::string DUMP_SERVICE = "--serv";
55 static const std::string DUMP_STACK = "--stack";
56 static const std::string DUMP_MISSION = "--mission";
57 static const std::string DUMP_TOP = "--top";
58 static const std::string DUMP_ALL = "-a";
59 constexpr int WAIT_TIME = 7 * 1000;
60 constexpr int BACKGROUND_WAIT_TIME = 15 * 1000;
61 static const std::string abilityStateInit = ":Init";
62 static const std::string abilityStateOnStart = ":OnStart";
63 static const std::string abilityStateOnStop = ":OnStop";
64 static const std::string abilityStateOnActive = ":OnActive";
65 static const std::string abilityStateOnInactive = ":OnInactive";
66 static const std::string abilityStateOnBackground = ":OnBackground";
67 static const std::string abilityStateOnForeground = ":OnForeground";
68 static const std::string abilityStateOnNewWant = ":OnNewWant";
69 static const std::string abilityStateOnCommand = ":OnCommand";
70 static const std::string abilityStateOnConnect = ":OnConnect";
71 static const std::string abilityStateOnDisconnect = ":OnDisconnect";
72 static const int abilityStateCountOne = 1;
73 static const int abilityStateCountTwo = 2;
74 enum AbilityState_Test {
75 INITIAL = 0,
76 INACTIVE,
77 ACTIVE,
78 BACKGROUND,
79 SUSPENDED,
80 INACTIVATING,
81 ACTIVATING,
82 MOVING_BACKGROUND,
83 TERMINATING,
84 ALLSUM,
85 };
86 static const std::vector<std::string> abilityStateVec = {
87 "INITIAL",
88 "INACTIVE",
89 "ACTIVE",
90 "BACKGROUND",
91 "SUSPENDED",
92 "INACTIVATING",
93 "ACTIVATING",
94 "MOVING_BACKGROUND",
95 "TERMINATING",
96 };
97 } // namespace
98 class AppEventSubscriber;
99
100 class AmsDFXTest : public testing::Test {
101 public:
102 static void SetUpTestCase(void);
103 static void TearDownTestCase(void);
104 void SetUp();
105 void TearDown();
106
107 static std::vector<std::string> GetBundleNames(
108 const std::string &strBase, const std::vector<std::string> &strSuffixs);
109 static bool SubscribeEvent();
110 void ShowDump();
111 static void CheckAbilityStateByName(const std::string &abilityName, const std::vector<std::string> &info,
112 const std::string &state, const std::string &midState);
113 void ExpectAbilityCurrentState(const std::string &abilityName, const AbilityState_Test ¤tState,
114 const AbilityState_Test &midState = AbilityState_Test::ALLSUM, const std::string &args = (DUMP_STACK + " 1"));
115 void ExpectAbilityNumInStack(const std::string &abilityName, int abilityNum);
116 void ExpectServiceAbilityNumInStack(const std::string &abilityName, int abilityNum);
117 class AppEventSubscriber : public CommonEventSubscriber {
118 public:
AppEventSubscriber(const CommonEventSubscribeInfo & sp)119 explicit AppEventSubscriber(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
120 {}
~AppEventSubscriber()121 virtual ~AppEventSubscriber()
122 {}
123 virtual void OnReceiveEvent(const CommonEventData &data) override;
124 };
125
126 class AbilityConnectCallback : public AbilityConnectionStub {
127 public:
128 /**
129 * OnAbilityConnectDone, AbilityMs notify caller ability the result of connect.
130 *
131 * @param element,.service ability's ElementName.
132 * @param remoteObject,.the session proxy of service ability.
133 * @param resultCode, ERR_OK on success, others on failure.
134 */
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)135 void OnAbilityConnectDone(
136 const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode) override
137 {
138 GTEST_LOG_(INFO) << "AbilityConnectCallback::OnAbilityConnectDone:resultCode = " << resultCode;
139 if (resultCode == 0) {
140 onAbilityConnectDoneCount++;
141 }
142 resultConnectCode = resultCode;
143 }
144
145 /**
146 * OnAbilityDisconnectDone, AbilityMs notify caller ability the result of disconnect.
147 *
148 * @param element,.service ability's ElementName.
149 * @param resultCode, ERR_OK on success, others on failure.
150 */
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)151 void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override
152 {
153 GTEST_LOG_(INFO) << "AbilityConnectCallback::OnAbilityDisconnectDone:resultCode = " << resultCode;
154 if (resultCode == 0) {
155 onAbilityConnectDoneCount--;
156 }
157 resultConnectCode = resultCode;
158 }
159
160 static size_t onAbilityConnectDoneCount;
161 static int resultConnectCode;
162 };
163 static sptr<IAppMgr> appMs_;
164 static sptr<IAbilityManager> abilityMs_;
165 static STtools::Event event_;
166 static std::shared_ptr<AppEventSubscriber> subscriber_;
167 };
168
169 sptr<IAppMgr> AmsDFXTest::appMs_ = nullptr;
170 sptr<IAbilityManager> AmsDFXTest::abilityMs_ = nullptr;
171 STtools::Event AmsDFXTest::event_ = STtools::Event();
172 std::shared_ptr<AmsDFXTest::AppEventSubscriber> AmsDFXTest::subscriber_ = nullptr;
173 size_t AmsDFXTest::AbilityConnectCallback::onAbilityConnectDoneCount = 0;
174 int AmsDFXTest::AbilityConnectCallback::resultConnectCode = 0;
175
SetUpTestCase(void)176 void AmsDFXTest::SetUpTestCase(void)
177 {
178 GTEST_LOG_(INFO) << "void AmsDFXTest::SetUpTestCase(void)";
179
180 SubscribeEvent();
181 appMs_ = STAbilityUtil::GetAppMgrService();
182 abilityMs_ = STAbilityUtil::GetAbilityManagerService();
183 if (appMs_) {
184 int freeTime = 60;
185 appMs_->SetAppFreezingTime(freeTime);
186 int time = 0;
187 appMs_->GetAppFreezingTime(time);
188 std::cout << "appMs_->GetAppFreezingTime();" << time << std::endl;
189 }
190 }
191
TearDownTestCase(void)192 void AmsDFXTest::TearDownTestCase(void)
193 {
194 GTEST_LOG_(INFO) << "void AmsDFXTest::TearDownTestCase(void)";
195 CommonEventManager::UnSubscribeCommonEvent(subscriber_);
196 }
197
SetUp(void)198 void AmsDFXTest::SetUp(void)
199 {
200 GTEST_LOG_(INFO) << "void AmsDFXTest::SetUp(void)";
201 std::vector<std::string> hapNames = GetBundleNames(hapNameBase, bundleNameSuffix);
202 STAbilityUtil::InstallHaps(hapNames);
203 }
204
TearDown(void)205 void AmsDFXTest::TearDown(void)
206 {
207 GTEST_LOG_(INFO) << "void AmsDFXTest::TearDown(void)";
208
209 std::vector<std::string> bundleNames = GetBundleNames(bundleNameBase, bundleNameSuffix);
210 STAbilityUtil::UninstallBundle(bundleNames);
211
212 STAbilityUtil::CleanMsg(event_);
213 }
214
GetBundleNames(const std::string & strBase,const std::vector<std::string> & strSuffixs)215 std::vector<std::string> AmsDFXTest::GetBundleNames(
216 const std::string &strBase, const std::vector<std::string> &strSuffixs)
217 {
218 std::vector<std::string> bundleNames;
219 for (auto strSuffix : strSuffixs) {
220 bundleNames.push_back(strBase + strSuffix);
221 }
222 return bundleNames;
223 }
224
SubscribeEvent()225 bool AmsDFXTest::SubscribeEvent()
226 {
227 std::vector<std::string> eventList = {"resp_st_page_ability_callback"};
228 MatchingSkills matchingSkills;
229 for (const auto &e : eventList) {
230 matchingSkills.AddEvent(e);
231 }
232 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
233 subscribeInfo.SetPriority(1);
234 subscriber_ = std::make_shared<AppEventSubscriber>(subscribeInfo);
235 return CommonEventManager::SubscribeCommonEvent(subscriber_);
236 }
237
ShowDump()238 void AmsDFXTest::ShowDump()
239 {
240 if (abilityMs_) {
241 std::vector<std::string> dumpInfo;
242 abilityMs_->DumpState("-a", dumpInfo);
243 for (const auto &info : dumpInfo) {
244 std::cout << info << std::endl;
245 }
246 }
247 }
248
CheckAbilityStateByName(const std::string & abilityName,const std::vector<std::string> & info,const std::string & state,const std::string & midState)249 void AmsDFXTest::CheckAbilityStateByName(const std::string &abilityName, const std::vector<std::string> &info,
250 const std::string &state, const std::string &midState)
251 {
252 std::vector<std::string> result;
253 MTDumpUtil::GetInstance()->GetAll("AbilityName", info, result);
254 auto pos = MTDumpUtil::GetInstance()->GetSpecific(abilityName, result, result.begin());
255 // ability exist
256 EXPECT_NE(pos, result.end());
257 MTDumpUtil::GetInstance()->GetAll("State", info, result);
258 EXPECT_TRUE(pos < result.end());
259 if (pos == result.end()) {
260 HILOG_ERROR("pos == result.end()");
261 return;
262 }
263 // ability state
264 if (midState != "") {
265 bool compareResult = ((*pos == state) || (*pos == midState));
266 EXPECT_EQ(1, compareResult);
267 } else {
268 EXPECT_EQ(*pos, state);
269 }
270 }
271
ExpectAbilityCurrentState(const std::string & abilityName,const AbilityState_Test & currentState,const AbilityState_Test & midState,const std::string & args)272 void AmsDFXTest::ExpectAbilityCurrentState(const std::string &abilityName, const AbilityState_Test ¤tState,
273 const AbilityState_Test &midState, const std::string &args)
274 {
275 std::string strCurrentState = abilityStateVec.at(currentState);
276 std::string strMidState = "";
277 if (midState != AbilityState_Test::ALLSUM) {
278 strMidState = abilityStateVec.at(midState);
279 }
280 std::vector<std::string> dumpInfo;
281 if (abilityMs_ != nullptr) {
282 abilityMs_->DumpState(args, dumpInfo);
283 CheckAbilityStateByName(abilityName, dumpInfo, strCurrentState, strMidState);
284 } else {
285 HILOG_ERROR("ability manager service(abilityMs_) is nullptr");
286 }
287 }
288
ExpectAbilityNumInStack(const std::string & abilityName,int abilityNum)289 void AmsDFXTest::ExpectAbilityNumInStack(const std::string &abilityName, int abilityNum)
290 {
291 std::vector<std::string> dumpInfo;
292 if (abilityMs_ != nullptr) {
293 abilityMs_->DumpState("-a", dumpInfo);
294 std::vector<std::string> result;
295 MTDumpUtil::GetInstance()->GetAll("AbilityName", dumpInfo, result);
296 // only one record in stack
297 EXPECT_EQ(abilityNum, std::count(result.begin(), result.end(), abilityName));
298 } else {
299 HILOG_ERROR("ability manager service(abilityMs_) is nullptr");
300 }
301 }
302
ExpectServiceAbilityNumInStack(const std::string & abilityName,int abilityNum)303 void AmsDFXTest::ExpectServiceAbilityNumInStack(const std::string &abilityName, int abilityNum)
304 {
305 std::vector<std::string> dumpInfo;
306 if (abilityMs_ != nullptr) {
307 abilityMs_->DumpState(DUMP_SERVICE, dumpInfo);
308 std::vector<std::string> result;
309 MTDumpUtil::GetInstance()->GetAll("AbilityName", dumpInfo, result);
310 // only one record in stack
311 EXPECT_EQ(abilityNum, std::count(result.begin(), result.end(), abilityName));
312 } else {
313 HILOG_ERROR("ability manager service(abilityMs_) is nullptr");
314 }
315 }
316
OnReceiveEvent(const CommonEventData & data)317 void AmsDFXTest::AppEventSubscriber::OnReceiveEvent(const CommonEventData &data)
318 {
319 GTEST_LOG_(INFO) << "OnReceiveEvent: event=" << data.GetWant().GetAction();
320 GTEST_LOG_(INFO) << "OnReceiveEvent: data=" << data.GetData();
321 GTEST_LOG_(INFO) << "OnReceiveEvent: code=" << data.GetCode();
322
323 std::string eventName = data.GetWant().GetAction();
324 if (eventName == "resp_st_page_ability_callback") {
325 std::string target = data.GetData();
326 STAbilityUtil::Completed(event_, target, data.GetCode());
327 }
328 }
329
330 /*
331 * @tc.number : AMS_DFX_0100
332 * @tc.name : Lifecycle switch timeout.
333 * @tc.desc : 1.LoadAbility timeout pageAbility
334 * 2.Restart the previous ability(different bundle)
335 */
336 HWTEST_F(AmsDFXTest, AMS_DFX_0100, TestSize.Level1)
337 {
338 GTEST_LOG_(INFO) << "AmsDFXTest AMS_DFX_0100 start";
339 std::string bundleName = bundleNameBase + "Q";
340 std::string abilityName = abilityNameBase + "Q1";
341 std::string bundleName2 = bundleNameBase + "R";
342 std::string abilityName2 = abilityNameBase + "R1";
343
344 MAP_STR_STR params;
345 params["targetBundle"] = bundleName2;
346 params["targetAbility"] = abilityName2;
347 Want want = STAbilityUtil::MakeWant("device", abilityName, bundleName, params);
348 STAbilityUtil::StartAbility(want, abilityMs_);
349
350 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnActive, abilityStateCountOne), 0);
351 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnActive, abilityStateCountTwo), 0);
352 ExpectAbilityNumInStack(abilityName2, 0);
353 GTEST_LOG_(INFO) << "AmsDFXTest AMS_DFX_0100 end";
354 }
355
356 /*
357 * @tc.number : AMS_DFX_0200
358 * @tc.name : Lifecycle switch timeout.
359 * @tc.desc : 1.LoadAbility timeout pageAbility
360 * 2.Restart the previous ability(same bundle)
361 */
362 HWTEST_F(AmsDFXTest, AMS_DFX_0200, TestSize.Level1)
363 {
364 GTEST_LOG_(INFO) << "AmsDFXTest AMS_DFX_0200 start";
365 std::string bundleName = bundleNameBase + "Q";
366 std::string abilityName = abilityNameBase + "Q1";
367 std::string abilityName2 = abilityNameBase + "Q2";
368
369 MAP_STR_STR params;
370 params["targetBundle"] = bundleName;
371 params["targetAbility"] = abilityName2;
372 Want want = STAbilityUtil::MakeWant("device", abilityName, bundleName, params);
373 STAbilityUtil::StartAbility(want, abilityMs_);
374
375 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnActive, abilityStateCountOne), 0);
376 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnActive, abilityStateCountTwo), 0);
377 ExpectAbilityNumInStack(abilityName2, 0);
378 GTEST_LOG_(INFO) << "AmsDFXTest AMS_DFX_0200 end";
379 }
380
381 /*
382 * @tc.number : AMS_DFX_0300
383 * @tc.name : Lifecycle switch timeout.
384 * @tc.desc : 1.LoadAbility timeout pageAbility
385 * 2.Restart the previous ability(back launch)
386 */
387 HWTEST_F(AmsDFXTest, AMS_DFX_0300, TestSize.Level1)
388 {
389 std::this_thread::sleep_for(std::chrono::milliseconds(5000));
390 GTEST_LOG_(INFO) << "AmsDFXTest AMS_DFX_0300 start";
391 std::string bundleName = bundleNameBase + "R";
392 std::string abilityName = abilityNameBase + "R1";
393
394 MAP_STR_STR params;
395 Want want = STAbilityUtil::MakeWant("device", abilityName, bundleName, params);
396 STAbilityUtil::StartAbility(want, abilityMs_);
397 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
398 ExpectAbilityCurrentState(launcherAbility, AbilityState_Test::ACTIVE, AbilityState_Test::ACTIVATING, DUMP_ALL);
399 ExpectAbilityNumInStack(abilityName, 0);
400 GTEST_LOG_(INFO) << "AmsDFXTest AMS_DFX_0300 end";
401 }
402
403 /*
404 * @tc.number : AMS_DFX_0400
405 * @tc.name : Lifecycle switch timeout.
406 * @tc.desc : 1.LoadAbility timeout pageAbility
407 * 2.Restart the previous ability
408 */
409 HWTEST_F(AmsDFXTest, AMS_DFX_0400, TestSize.Level1)
410 {
411 GTEST_LOG_(INFO) << "AmsDFXTest AMS_DFX_0400 start";
412 std::string bundleName = bundleNameBase + "Q";
413 std::string abilityName = abilityNameBase + "Q1";
414 std::string bundleName2 = bundleNameBase + "R";
415 std::string abilityName2 = abilityNameBase + "R1";
416 std::string abilityName3 = abilityNameBase + "Q3";
417
418 MAP_STR_STR params;
419 params["targetBundle"] = bundleName + "," + bundleName2;
420 params["targetAbility"] = abilityName3 + "," + abilityName2;
421 Want want = STAbilityUtil::MakeWant("device", abilityName, bundleName, params);
422 STAbilityUtil::StartAbility(want, abilityMs_);
423
424 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnActive, abilityStateCountOne), 0);
425 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName3 + abilityStateOnActive, abilityStateCountOne), 0);
426 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName3 + abilityStateOnActive, abilityStateCountTwo), 0);
427 ExpectAbilityNumInStack(abilityName2, 0);
428 GTEST_LOG_(INFO) << "AmsDFXTest AMS_DFX_0400 end";
429 }
430
431 /*
432 * @tc.number : AMS_DFX_0500
433 * @tc.name : Lifecycle switch timeout.
434 * @tc.desc : 1.background timeout pageAbility
435 * 2.Force call CompleteBackground function(same bundle)
436 */
437 HWTEST_F(AmsDFXTest, AMS_DFX_0500, TestSize.Level1)
438 {
439 GTEST_LOG_(INFO) << "AmsDFXTest AMS_DFX_0500 start";
440 std::string bundleName = bundleNameBase + "Q";
441 std::string abilityName = abilityNameBase + "Q1";
442 std::string abilityName2 = abilityNameBase + "Q4";
443
444 MAP_STR_STR params;
445 params["targetBundle"] = bundleName;
446 params["targetAbility"] = abilityName;
447 Want want = STAbilityUtil::MakeWant("device", abilityName2, bundleName, params);
448 STAbilityUtil::StartAbility(want, abilityMs_);
449
450 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName2 + abilityStateOnActive, abilityStateCountOne), 0);
451 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnActive, abilityStateCountOne), 0);
452 std::this_thread::sleep_for(std::chrono::milliseconds(BACKGROUND_WAIT_TIME));
453 ExpectAbilityCurrentState(abilityName2, AbilityState_Test::BACKGROUND);
454 RunningProcessInfo pInfo = STAbilityUtil::GetAppProcessInfoByName(bundleName, appMs_, WAIT_TIME);
455 EXPECT_EQ(AppProcessState::APP_STATE_FOREGROUND, pInfo.state_);
456 ExpectAbilityNumInStack(abilityName2, 1);
457 GTEST_LOG_(INFO) << "AmsDFXTest AMS_DFX_0500 end";
458 }
459
460 /*
461 * @tc.number : AMS_DFX_0600
462 * @tc.name : Lifecycle switch timeout.
463 * @tc.desc : 1.background timeout pageAbility
464 * 2.Force call CompleteBackground function(different bundle)
465 */
466 HWTEST_F(AmsDFXTest, AMS_DFX_0600, TestSize.Level1)
467 {
468 GTEST_LOG_(INFO) << "AmsDFXTest AMS_DFX_0600 start";
469 std::string bundleName = bundleNameBase + "Q";
470 std::string abilityName = abilityNameBase + "Q1";
471 std::string bundleName2 = bundleNameBase + "R";
472 std::string abilityName2 = abilityNameBase + "R2";
473
474 MAP_STR_STR params;
475 params["targetBundle"] = bundleName;
476 params["targetAbility"] = abilityName;
477 Want want = STAbilityUtil::MakeWant("device", abilityName2, bundleName2, params);
478 STAbilityUtil::StartAbility(want, abilityMs_);
479
480 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName2 + abilityStateOnActive, abilityStateCountOne), 0);
481 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnActive, abilityStateCountOne), 0);
482 std::this_thread::sleep_for(std::chrono::milliseconds(BACKGROUND_WAIT_TIME));
483 ExpectAbilityCurrentState(abilityName2, AbilityState_Test::BACKGROUND);
484 RunningProcessInfo pInfo = STAbilityUtil::GetAppProcessInfoByName(bundleName2, appMs_, WAIT_TIME);
485 EXPECT_EQ(AppProcessState::APP_STATE_FOREGROUND, pInfo.state_);
486 std::cout << "pInfo.state_:" << static_cast<int>(pInfo.state_) << std::endl;
487 std::cout << "pInfo.pid_:" << static_cast<int>(pInfo.pid_) << std::endl;
488 ExpectAbilityNumInStack(abilityName2, 1);
489 GTEST_LOG_(INFO) << "AmsDFXTest AMS_DFX_0600 end";
490 }
491
492 /*
493 * @tc.number : AMS_DFX_0700
494 * @tc.name : Lifecycle switch timeout.
495 * @tc.desc : 1.background timeout pageAbility
496 * 2.Force call CompleteBackground function(different bundle)
497 */
498 HWTEST_F(AmsDFXTest, AMS_DFX_0700, TestSize.Level1)
499 {
500 GTEST_LOG_(INFO) << "AmsDFXTest AMS_DFX_0700 start";
501 std::string bundleName = bundleNameBase + "Q";
502 std::string abilityName = abilityNameBase + "Q1";
503 std::string bundleName2 = bundleNameBase + "R";
504 std::string abilityName2 = abilityNameBase + "R3";
505
506 MAP_STR_STR params;
507 params["targetBundle"] = bundleName;
508 params["targetAbility"] = abilityName;
509 Want want = STAbilityUtil::MakeWant("device", abilityName2, bundleName2, params);
510 STAbilityUtil::StartAbility(want, abilityMs_);
511
512 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName2 + abilityStateOnActive, abilityStateCountOne), 0);
513 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnActive, abilityStateCountOne), 0);
514 std::this_thread::sleep_for(std::chrono::milliseconds(BACKGROUND_WAIT_TIME));
515 ExpectAbilityCurrentState(abilityName2, AbilityState_Test::BACKGROUND);
516 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName2 + abilityStateOnBackground, abilityStateCountOne), 0);
517 RunningProcessInfo pInfo = STAbilityUtil::GetAppProcessInfoByName(bundleName2, appMs_, WAIT_TIME);
518 EXPECT_EQ(AppProcessState::APP_STATE_BACKGROUND, pInfo.state_);
519 std::cout << "pInfo.state_:" << static_cast<int>(pInfo.state_) << std::endl;
520 std::cout << "pInfo.pid_:" << static_cast<int>(pInfo.pid_) << std::endl;
521 ExpectAbilityNumInStack(abilityName2, 1);
522 GTEST_LOG_(INFO) << "AmsDFXTest AMS_DFX_0700 end";
523 }
524
525 /*
526 * @tc.number : AMS_DFX_0800
527 * @tc.name : Lifecycle switch timeout.
528 * @tc.desc : 1.Terminate timeout pageAbility
529 * 2.Force call CompleteTerminate function
530 */
531 HWTEST_F(AmsDFXTest, AMS_DFX_0800, TestSize.Level1)
532 {
533 GTEST_LOG_(INFO) << "AmsDFXTest AMS_DFX_0800 start";
534 std::string bundleName = bundleNameBase + "Q";
535 std::string abilityName = abilityNameBase + "Q1";
536 std::string bundleName2 = bundleNameBase + "R";
537 std::string abilityName2 = abilityNameBase + "R2";
538
539 MAP_STR_STR params;
540 params["targetBundle"] = bundleName;
541 params["targetAbility"] = abilityName;
542 Want want = STAbilityUtil::MakeWant("device", abilityName2, bundleName2, params);
543 STAbilityUtil::StartAbility(want, abilityMs_);
544
545 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName2 + abilityStateOnActive, abilityStateCountOne), 0);
546 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnActive, abilityStateCountOne), 0);
547 int eventCode = 0;
548 STAbilityUtil::PublishEvent(terminatePageAbility, eventCode, abilityName2);
549 std::this_thread::sleep_for(std::chrono::milliseconds(BACKGROUND_WAIT_TIME));
550 ExpectAbilityNumInStack(abilityName2, 0);
551 GTEST_LOG_(INFO) << "AmsDFXTest AMS_DFX_0800 end";
552 }
553
554 /*
555 * @tc.number : AMS_DFX_0900
556 * @tc.name : Lifecycle switch timeout.
557 * @tc.desc : 1.Terminate timeout pageAbility
558 * 2.Force call CompleteTerminate function
559 */
560 HWTEST_F(AmsDFXTest, AMS_DFX_0900, TestSize.Level1)
561 {
562 GTEST_LOG_(INFO) << "AmsDFXTest AMS_DFX_0900 start";
563 std::string bundleName = bundleNameBase + "Q";
564 std::string abilityName = abilityNameBase + "Q1";
565 std::string bundleName2 = bundleNameBase + "R";
566 std::string abilityName2 = abilityNameBase + "R2";
567
568 MAP_STR_STR params;
569 params["targetBundle"] = bundleName;
570 params["targetAbility"] = abilityName;
571 Want want = STAbilityUtil::MakeWant("device", abilityName2, bundleName2, params);
572 STAbilityUtil::StartAbility(want, abilityMs_);
573
574 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName2 + abilityStateOnActive, abilityStateCountOne), 0);
575 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnActive, abilityStateCountOne), 0);
576 int eventCode = 0;
577 STAbilityUtil::PublishEvent(terminatePageAbility, eventCode, abilityName2);
578 std::this_thread::sleep_for(std::chrono::milliseconds(BACKGROUND_WAIT_TIME));
579 ExpectAbilityNumInStack(abilityName2, 0);
580 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
581 params.clear();
582 want = STAbilityUtil::MakeWant("device", abilityName2, bundleName2, params);
583 STAbilityUtil::StartAbility(want, abilityMs_);
584 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName2 + abilityStateOnActive, abilityStateCountOne), 0);
585 GTEST_LOG_(INFO) << "AmsDFXTest AMS_DFX_0900 end";
586 }
587
588 /*
589 * @tc.number : AMS_DFX_1000
590 * @tc.name : service ability connection,disconnection exception.
591 * @tc.desc : 1.connection service ability timeout
592 * 2.verification callBack resultCode
593 */
594 HWTEST_F(AmsDFXTest, AMS_DFX_1000, TestSize.Level1)
595 {
596 GTEST_LOG_(INFO) << "AmsDFXTest AMS_DFX_1000 start";
597 std::string bundleName = bundleNameBase + "DFX";
598 std::string abilityName = "AmsDfxStServiceAbilityA1";
599
600 MAP_STR_STR params;
601 Want want = STAbilityUtil::MakeWant("device", abilityName, bundleName, params);
602 // connect service ability
603 sptr<AbilityConnectCallback> stub(new (std::nothrow) AbilityConnectCallback());
604 STAbilityUtil::ConnectAbility(want, stub, stub->AsObject());
605
606 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnStart, abilityStateCountOne), 0);
607 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
608 EXPECT_EQ(AbilityConnectCallback::resultConnectCode, CONNECTION_TIMEOUT);
609 ExpectServiceAbilityNumInStack(abilityName, 1);
610
611 RunningProcessInfo pInfo = STAbilityUtil::GetAppProcessInfoByName(bundleName, appMs_, WAIT_TIME);
612 EXPECT_TRUE(pInfo.pid_ > 0);
613
614 int eventCode = 0;
615 STAbilityUtil::PublishEvent(terminatePageAbility, eventCode, abilityName);
616 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnStop, abilityStateCountOne), 0);
617 pInfo = STAbilityUtil::GetAppProcessInfoByName(bundleName, appMs_, WAIT_TIME);
618 EXPECT_TRUE(pInfo.pid_ == 0);
619 GTEST_LOG_(INFO) << "AmsDFXTest AMS_DFX_1000 end";
620 }
621
622 /*
623 * @tc.number : AMS_DFX_1100
624 * @tc.name : service ability connection,disconnection exception.
625 * @tc.desc : 1.disconnection service ability timeout
626 * 2.verification disconnection
627 */
628 HWTEST_F(AmsDFXTest, AMS_DFX_1100, TestSize.Level1)
629 {
630 GTEST_LOG_(INFO) << "AmsDFXTest AMS_DFX_1100 start";
631 std::string bundleName = bundleNameBase + "DFX";
632 std::string abilityName = "AmsDfxStServiceAbilityA2";
633
634 MAP_STR_STR params;
635 Want want = STAbilityUtil::MakeWant("device", abilityName, bundleName, params);
636 // connect service ability
637 sptr<AbilityConnectCallback> stub(new (std::nothrow) AbilityConnectCallback());
638 STAbilityUtil::ConnectAbility(want, stub, stub->AsObject());
639
640 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnStart, abilityStateCountOne), 0);
641 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
642 EXPECT_EQ(AbilityConnectCallback::resultConnectCode, 0);
643 ExpectServiceAbilityNumInStack(abilityName, 1);
644
645 RunningProcessInfo pInfo = STAbilityUtil::GetAppProcessInfoByName(bundleName, appMs_, WAIT_TIME);
646 EXPECT_TRUE(pInfo.pid_ > 0);
647
648 STAbilityUtil::DisconnectAbility(stub);
649 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
650 ExpectServiceAbilityNumInStack(abilityName, 0);
651 GTEST_LOG_(INFO) << "AmsDFXTest AMS_DFX_1100 end";
652 }