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_manager_service.h"
22 #include "ability_manager_errors.h"
23 #include "app_mgr_service.h"
24 #include "module_test_dump_util.h"
25 #include "st_ability_util.h"
26 #include "module_test_dump_util.h"
27 #include "sa_mgr_client.h"
28 #include "system_ability_definition.h"
29 #include "common_event.h"
30 #include "common_event_manager.h"
31
32 using namespace testing::ext;
33 using namespace OHOS;
34 using namespace OHOS::AAFwk;
35 using namespace OHOS::AppExecFwk;
36 using namespace OHOS::MTUtil;
37 using namespace OHOS::STABUtil;
38 using namespace OHOS::EventFwk;
39
40 namespace {
41 using MAP_STR_STR = std::map<std::string, std::string>;
42 std::vector<std::string> bundleNameSuffix = {"A", "B", "N"};
43 std::string bundleNameBase = "com.ohos.amsst.app";
44 std::string hapNameBase = "amsSystemTest";
45 std::string abilityNameBase = "AmsStAbility";
46 std::string launcherBundle = "com.ohos.launcher";
47 std::string launcherAbility = "com.ohos.launcher.MainAbility";
48 std::string systemUiBundle = "com.ohos.systemui";
49 std::string terminatePageAbility = "requ_page_ability_terminate";
50
51 static const std::string DUMP_STACK_LIST = "--stack-list";
52 static const std::string DUMP_STACK = "--stack";
53 static const std::string DUMP_MISSION = "--mission";
54 static const std::string DUMP_TOP = "--top";
55 static const std::string DUMP_ALL = "-a";
56 constexpr int WAIT_TIME = 3 * 1000;
57 constexpr int WAIT_LAUNCHER_OK = 5 * 1000;
58 static const std::string abilityStateInit = ":Init";
59 static const std::string abilityStateOnStart = ":OnStart";
60 static const std::string abilityStateOnStop = ":OnStop";
61 static const std::string abilityStateOnActive = ":OnActive";
62 static const std::string abilityStateOnInactive = ":OnInactive";
63 static const std::string abilityStateOnBackground = ":OnBackground";
64 static const std::string abilityStateOnForeground = ":OnForeground";
65 static const std::string abilityStateOnNewWant = ":OnNewWant";
66 static const int abilityStateCountOne = 1;
67 static const int abilityStateCountTwo = 2;
68 enum AbilityState_Test {
69 INITIAL = 0,
70 INACTIVE,
71 ACTIVE,
72 BACKGROUND,
73 SUSPENDED,
74 INACTIVATING,
75 ACTIVATING,
76 MOVING_BACKGROUND,
77 TERMINATING,
78 ALLSUM,
79 };
80 static const std::vector<std::string> abilityStateVec = {
81 "INITIAL",
82 "INACTIVE",
83 "ACTIVE",
84 "BACKGROUND",
85 "SUSPENDED",
86 "INACTIVATING",
87 "ACTIVATING",
88 "MOVING_BACKGROUND",
89 "TERMINATING",
90 };
91 } // namespace
92 class AppEventSubscriber;
93
94 class AmsPowerTest : public testing::Test {
95 public:
96 static void SetUpTestCase(void);
97 static void TearDownTestCase(void);
98 void SetUp();
99 void TearDown();
100
101 static std::vector<std::string> GetBundleNames(
102 const std::string &strBase, const std::vector<std::string> &strSuffixs);
103 static bool SubscribeEvent();
104 void ShowDump();
105 static void CheckAbilityStateByName(const std::string &abilityName, const std::vector<std::string> &info,
106 const std::string &state, const std::string &midState);
107 void ExpectAbilityCurrentState(const std::string &abilityName, const AbilityState_Test ¤tState,
108 const AbilityState_Test &midState = AbilityState_Test::ALLSUM, const std::string &args = (DUMP_STACK + " 1"));
109 void ExpectAbilityNumInStack(const std::string &abilityName, int abilityNum);
110 class AppEventSubscriber : public CommonEventSubscriber {
111 public:
AppEventSubscriber(const CommonEventSubscribeInfo & sp)112 explicit AppEventSubscriber(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
113 {}
~AppEventSubscriber()114 virtual ~AppEventSubscriber()
115 {}
116 virtual void OnReceiveEvent(const CommonEventData &data) override;
117 };
118 static sptr<IAppMgr> appMs_;
119 static sptr<IAbilityManager> abilityMs_;
120 static STtools::Event event_;
121 static std::shared_ptr<AppEventSubscriber> subscriber_;
122 };
123
124 sptr<IAppMgr> AmsPowerTest::appMs_ = nullptr;
125 sptr<IAbilityManager> AmsPowerTest::abilityMs_ = nullptr;
126 STtools::Event AmsPowerTest::event_ = STtools::Event();
127 std::shared_ptr<AmsPowerTest::AppEventSubscriber> AmsPowerTest::subscriber_ = nullptr;
128
SetUpTestCase(void)129 void AmsPowerTest::SetUpTestCase(void)
130 {
131 GTEST_LOG_(INFO) << "void AmsPowerTest::SetUpTestCase(void)";
132 std::vector<std::string> hapNames = GetBundleNames(hapNameBase, bundleNameSuffix);
133 STAbilityUtil::InstallHaps(hapNames);
134 SubscribeEvent();
135 appMs_ = STAbilityUtil::GetAppMgrService();
136 abilityMs_ = STAbilityUtil::GetAbilityManagerService();
137 if (appMs_) {
138 int freeTime = 60;
139 appMs_->SetAppFreezingTime(freeTime);
140 int time = 0;
141 appMs_->GetAppFreezingTime(time);
142 std::cout << "appMs_->GetAppFreezingTime();" << time << std::endl;
143 }
144 }
145
TearDownTestCase(void)146 void AmsPowerTest::TearDownTestCase(void)
147 {
148 GTEST_LOG_(INFO) << "void AmsPowerTest::TearDownTestCase(void)";
149 CommonEventManager::UnSubscribeCommonEvent(subscriber_);
150 std::vector<std::string> bundleNames = GetBundleNames(bundleNameBase, bundleNameSuffix);
151 STAbilityUtil::UninstallBundle(bundleNames);
152 }
153
SetUp(void)154 void AmsPowerTest::SetUp(void)
155 {
156 GTEST_LOG_(INFO) << "void AmsPowerTest::SetUp(void)";
157 }
158
TearDown(void)159 void AmsPowerTest::TearDown(void)
160 {
161 GTEST_LOG_(INFO) << "void AmsPowerTest::TearDown(void)";
162 std::vector<std::string> vecBundleName;
163 for (const auto &suffix : bundleNameSuffix) {
164 vecBundleName.push_back(bundleNameBase + suffix);
165 }
166 STAbilityUtil::KillBundleProcess(vecBundleName);
167
168 STAbilityUtil::CleanMsg(event_);
169 }
170
GetBundleNames(const std::string & strBase,const std::vector<std::string> & strSuffixs)171 std::vector<std::string> AmsPowerTest::GetBundleNames(
172 const std::string &strBase, const std::vector<std::string> &strSuffixs)
173 {
174 std::vector<std::string> bundleNames;
175 for (auto strSuffix : strSuffixs) {
176 bundleNames.push_back(strBase + strSuffix);
177 }
178 return bundleNames;
179 }
180
SubscribeEvent()181 bool AmsPowerTest::SubscribeEvent()
182 {
183 std::vector<std::string> eventList = {"resp_st_page_ability_callback"};
184 MatchingSkills matchingSkills;
185 for (const auto &e : eventList) {
186 matchingSkills.AddEvent(e);
187 }
188 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
189 subscribeInfo.SetPriority(1);
190 subscriber_ = std::make_shared<AppEventSubscriber>(subscribeInfo);
191 return CommonEventManager::SubscribeCommonEvent(subscriber_);
192 }
193
ShowDump()194 void AmsPowerTest::ShowDump()
195 {
196 if (abilityMs_) {
197 std::vector<std::string> dumpInfo;
198 abilityMs_->DumpState("-a", dumpInfo);
199 for (const auto &info : dumpInfo) {
200 std::cout << info << std::endl;
201 }
202 }
203 }
204
CheckAbilityStateByName(const std::string & abilityName,const std::vector<std::string> & info,const std::string & state,const std::string & midState)205 void AmsPowerTest::CheckAbilityStateByName(const std::string &abilityName, const std::vector<std::string> &info,
206 const std::string &state, const std::string &midState)
207 {
208 std::vector<std::string> result;
209 MTDumpUtil::GetInstance()->GetAll("AbilityName", info, result);
210 auto pos = MTDumpUtil::GetInstance()->GetSpecific(abilityName, result, result.begin());
211 // ability exist
212 EXPECT_NE(pos, result.end());
213 MTDumpUtil::GetInstance()->GetAll("State", info, result);
214 EXPECT_TRUE(pos < result.end());
215 if (pos == result.end()) {
216 HILOG_ERROR("pos == result.end()");
217 return;
218 }
219 // ability state
220 if (midState != "") {
221 bool compareResult = ((*pos == state) || (*pos == midState));
222 EXPECT_EQ(1, compareResult);
223 } else {
224 EXPECT_EQ(*pos, state);
225 }
226 }
227
ExpectAbilityCurrentState(const std::string & abilityName,const AbilityState_Test & currentState,const AbilityState_Test & midState,const std::string & args)228 void AmsPowerTest::ExpectAbilityCurrentState(const std::string &abilityName, const AbilityState_Test ¤tState,
229 const AbilityState_Test &midState, const std::string &args)
230 {
231 std::string strCurrentState = abilityStateVec.at(currentState);
232 std::string strMidState = "";
233 if (midState != AbilityState_Test::ALLSUM) {
234 strMidState = abilityStateVec.at(midState);
235 }
236 std::vector<std::string> dumpInfo;
237 if (abilityMs_ != nullptr) {
238 abilityMs_->DumpState(args, dumpInfo);
239 CheckAbilityStateByName(abilityName, dumpInfo, strCurrentState, strMidState);
240 } else {
241 HILOG_ERROR("ability manager service(abilityMs_) is nullptr");
242 }
243 }
244
ExpectAbilityNumInStack(const std::string & abilityName,int abilityNum)245 void AmsPowerTest::ExpectAbilityNumInStack(const std::string &abilityName, int abilityNum)
246 {
247 std::vector<std::string> dumpInfo;
248 if (abilityMs_ != nullptr) {
249 abilityMs_->DumpState("-a", dumpInfo);
250 std::vector<std::string> result;
251 MTDumpUtil::GetInstance()->GetAll("AbilityName", dumpInfo, result);
252 // only one record in stack
253 EXPECT_EQ(abilityNum, std::count(result.begin(), result.end(), abilityName));
254 } else {
255 HILOG_ERROR("ability manager service(abilityMs_) is nullptr");
256 }
257 }
258
OnReceiveEvent(const CommonEventData & data)259 void AmsPowerTest::AppEventSubscriber::OnReceiveEvent(const CommonEventData &data)
260 {
261 GTEST_LOG_(INFO) << "OnReceiveEvent: event=" << data.GetWant().GetAction();
262 GTEST_LOG_(INFO) << "OnReceiveEvent: data=" << data.GetData();
263 GTEST_LOG_(INFO) << "OnReceiveEvent: code=" << data.GetCode();
264
265 std::string eventName = data.GetWant().GetAction();
266 if (eventName == "resp_st_page_ability_callback") {
267 std::string target = data.GetData();
268 STAbilityUtil::Completed(event_, target, data.GetCode());
269 }
270 }
271
272 /*
273 * @tc.number : AMS_Power_0100
274 * @tc.name : Verify poweroff and poweron functions
275 * @tc.desc : 1.start ability
276 * 2.called PowerOff
277 * 3.Check the stack status and process status,
278 * and verify the application callback function at the same time.
279 * 4.called PowerOn
280 * 5.Check the stack status and process status,
281 * and verify the application callback function at the same time.
282 */
283 HWTEST_F(AmsPowerTest, AMS_Power_0100, TestSize.Level1)
284 {
285 GTEST_LOG_(INFO) << "AmsPowerTest AMS_Power_0100 start";
286 std::string bundleName = bundleNameBase + "A";
287 std::string abilityName = abilityNameBase + "A1";
288
289 MAP_STR_STR params;
290 Want want = STAbilityUtil::MakeWant("device", abilityName, bundleName, params);
291 STAbilityUtil::StartAbility(want, abilityMs_);
292
293 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnActive, abilityStateCountOne), 0);
294
295 if (abilityMs_) {
296 abilityMs_->PowerOff();
297 }
298 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnInactive, abilityStateCountOne), 0);
299 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnBackground, abilityStateCountOne), 0);
300 ExpectAbilityCurrentState(abilityName, AbilityState_Test::BACKGROUND, AbilityState_Test::MOVING_BACKGROUND);
301 RunningProcessInfo pInfo = STAbilityUtil::GetAppProcessInfoByName(bundleName, appMs_, WAIT_TIME);
302 EXPECT_EQ(AppProcessState::APP_STATE_BACKGROUND, pInfo.state_);
303 if (abilityMs_) {
304 abilityMs_->PowerOn();
305 }
306 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnForeground, abilityStateCountOne), 0);
307 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnActive, abilityStateCountTwo), 0);
308 ExpectAbilityCurrentState(abilityName, AbilityState_Test::ACTIVATING, AbilityState_Test::ACTIVE);
309 pInfo = STAbilityUtil::GetAppProcessInfoByName(bundleName, appMs_, WAIT_TIME);
310 EXPECT_EQ(AppProcessState::APP_STATE_FOREGROUND, pInfo.state_);
311
312 GTEST_LOG_(INFO) << "AmsPowerTest AMS_Power_0100 end";
313 }
314
315 /*
316 * @tc.number : AMS_Power_0200
317 * @tc.name : Verify poweroff and poweron functions(different bundle)[A1,B1:singleton]
318 * @tc.desc : 1.start ability
319 * 2.called PowerOff
320 * 3.Check the stack status and process status,
321 * and verify the application callback function at the same time.
322 * 4.called PowerOn
323 * 5.Check the stack status and process status,
324 * and verify the application callback function at the same time.
325 */
326 HWTEST_F(AmsPowerTest, AMS_Power_0200, TestSize.Level1)
327 {
328 GTEST_LOG_(INFO) << "AmsPowerTest AMS_Power_0200 start";
329 std::string bundleName = bundleNameBase + "A";
330 std::string abilityName = abilityNameBase + "A1";
331 std::string bundleName2 = bundleNameBase + "B";
332 std::string abilityName2 = abilityNameBase + "B1";
333
334 MAP_STR_STR params;
335 params["targetBundle"] = bundleName2;
336 params["targetAbility"] = abilityName2;
337 Want want = STAbilityUtil::MakeWant("device", abilityName, bundleName, params);
338 STAbilityUtil::StartAbility(want, abilityMs_);
339
340 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnActive, abilityStateCountOne), 0);
341 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName2 + abilityStateOnActive, abilityStateCountOne), 0);
342 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnBackground, abilityStateCountOne), 0);
343 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
344 ShowDump();
345 if (abilityMs_) {
346 abilityMs_->PowerOff();
347 }
348 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
349 ShowDump();
350 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName2 + abilityStateOnInactive, abilityStateCountOne), 0);
351 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName2 + abilityStateOnBackground, abilityStateCountOne), 0);
352 ExpectAbilityCurrentState(abilityName2, AbilityState_Test::BACKGROUND, AbilityState_Test::MOVING_BACKGROUND);
353 RunningProcessInfo pInfo = STAbilityUtil::GetAppProcessInfoByName(bundleName2, appMs_, WAIT_TIME);
354 EXPECT_EQ(AppProcessState::APP_STATE_BACKGROUND, pInfo.state_);
355 if (abilityMs_) {
356 abilityMs_->PowerOn();
357 }
358 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
359 ShowDump();
360 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName2 + abilityStateOnForeground, abilityStateCountOne), 0);
361 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName2 + abilityStateOnActive, abilityStateCountTwo), 0);
362 ExpectAbilityCurrentState(abilityName2, AbilityState_Test::ACTIVATING, AbilityState_Test::ACTIVE);
363 pInfo = STAbilityUtil::GetAppProcessInfoByName(bundleName2, appMs_, WAIT_TIME);
364 EXPECT_EQ(AppProcessState::APP_STATE_FOREGROUND, pInfo.state_);
365
366 GTEST_LOG_(INFO) << "AmsPowerTest AMS_Power_0200 end";
367 }
368
369 /*
370 * @tc.number : AMS_Power_0300
371 * @tc.name : Verify poweroff and poweron functions(different bundle)[A1,B1:singleton]
372 * @tc.desc : 1.start ability(PowerOff now)
373 * 2.called PowerOff
374 * 3.Check the stack status and process status,
375 * and verify the application callback function at the same time.
376 * 4.called PowerOn
377 * 5.Check the stack status and process status,
378 * and verify the application callback function at the same time.
379 */
380 HWTEST_F(AmsPowerTest, AMS_Power_0300, TestSize.Level1)
381 {
382 GTEST_LOG_(INFO) << "AmsPowerTest AMS_Power_0300 start";
383 std::string bundleName = bundleNameBase + "A";
384 std::string abilityName = abilityNameBase + "A1";
385 std::string bundleName2 = bundleNameBase + "B";
386 std::string abilityName2 = abilityNameBase + "B1";
387
388 MAP_STR_STR params;
389 params["targetBundle"] = bundleName2;
390 params["targetAbility"] = abilityName2;
391 Want want = STAbilityUtil::MakeWant("device", abilityName, bundleName, params);
392 STAbilityUtil::StartAbility(want, abilityMs_);
393
394 if (abilityMs_) {
395 int errCode = abilityMs_->PowerOff();
396 EXPECT_EQ(POWER_OFF_WAITING, errCode);
397 }
398 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName2 + abilityStateOnBackground, abilityStateCountOne), 0);
399 ExpectAbilityCurrentState(abilityName2, AbilityState_Test::BACKGROUND, AbilityState_Test::MOVING_BACKGROUND);
400 RunningProcessInfo pInfo = STAbilityUtil::GetAppProcessInfoByName(bundleName2, appMs_, WAIT_TIME);
401 EXPECT_EQ(AppProcessState::APP_STATE_BACKGROUND, pInfo.state_);
402 if (abilityMs_) {
403 abilityMs_->PowerOn();
404 }
405 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName2 + abilityStateOnActive, abilityStateCountTwo), 0);
406 ExpectAbilityCurrentState(abilityName2, AbilityState_Test::ACTIVATING, AbilityState_Test::ACTIVE);
407 pInfo = STAbilityUtil::GetAppProcessInfoByName(bundleName2, appMs_, WAIT_TIME);
408 EXPECT_EQ(AppProcessState::APP_STATE_FOREGROUND, pInfo.state_);
409
410 GTEST_LOG_(INFO) << "AmsPowerTest AMS_Power_0300 end";
411 }
412
413 /*
414 * @tc.number : AMS_Power_0400
415 * @tc.name : Verify poweroff and poweron functions(different bundle)[A1:singleton,N3:singleton]
416 * @tc.desc : 1.start ability(PowerOff now)
417 * 2.called PowerOff
418 * 3.Check the stack status and process status,
419 * and verify the application callback function at the same time.
420 * 4.called PowerOn
421 * 5.Check the stack status and process status,
422 * and verify the application callback function at the same time.
423 */
424 HWTEST_F(AmsPowerTest, AMS_Power_0400, TestSize.Level1)
425 {
426 GTEST_LOG_(INFO) << "AmsPowerTest AMS_Power_0400 start";
427 std::string bundleName = bundleNameBase + "A";
428 std::string abilityName = abilityNameBase + "A1";
429 std::string bundleName2 = bundleNameBase + "N";
430 std::string abilityName2 = abilityNameBase + "N3";
431
432 MAP_STR_STR params;
433 params["targetBundle"] = bundleName2;
434 params["targetAbility"] = abilityName2;
435 Want want = STAbilityUtil::MakeWant("device", abilityName, bundleName, params);
436 STAbilityUtil::StartAbility(want, abilityMs_);
437 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnActive, abilityStateCountOne), 0);
438 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName2 + abilityStateOnActive, abilityStateCountOne), 0);
439 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnBackground, abilityStateCountOne), 0);
440
441 if (abilityMs_) {
442 abilityMs_->PowerOff();
443 }
444 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName2 + abilityStateOnBackground, abilityStateCountOne), 0);
445 ExpectAbilityCurrentState(abilityName2, AbilityState_Test::BACKGROUND, AbilityState_Test::MOVING_BACKGROUND);
446 RunningProcessInfo pInfo = STAbilityUtil::GetAppProcessInfoByName(bundleName2, appMs_, WAIT_TIME);
447 EXPECT_EQ(AppProcessState::APP_STATE_BACKGROUND, pInfo.state_);
448 if (abilityMs_) {
449 abilityMs_->PowerOn();
450 }
451 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName2 + abilityStateOnActive, abilityStateCountTwo), 0);
452 ExpectAbilityCurrentState(abilityName2, AbilityState_Test::ACTIVATING, AbilityState_Test::ACTIVE);
453 pInfo = STAbilityUtil::GetAppProcessInfoByName(bundleName2, appMs_, WAIT_TIME);
454 EXPECT_EQ(AppProcessState::APP_STATE_FOREGROUND, pInfo.state_);
455
456 GTEST_LOG_(INFO) << "AmsPowerTest AMS_Power_0400 end";
457 }
458
459 /*
460 * @tc.number : AMS_Power_0500
461 * @tc.name : Verify poweroff and poweron functions(different bundle)[A1:singleton,N3:singleton]
462 * @tc.desc : 1.start ability
463 * 2.called PowerOff(PowerOn now)
464 * 3.called PowerOn
465 * 4.Check the stack status and process status,
466 * and verify the application callback function at the same time.
467 */
468 HWTEST_F(AmsPowerTest, AMS_Power_0500, TestSize.Level1)
469 {
470 GTEST_LOG_(INFO) << "AmsPowerTest AMS_Power_0500 start";
471 std::string bundleName = bundleNameBase + "A";
472 std::string abilityName = abilityNameBase + "A1";
473 std::string bundleName2 = bundleNameBase + "N";
474 std::string abilityName2 = abilityNameBase + "N3";
475
476 MAP_STR_STR params;
477 params["targetBundle"] = bundleName2;
478 params["targetAbility"] = abilityName2;
479 Want want = STAbilityUtil::MakeWant("device", abilityName, bundleName, params);
480 STAbilityUtil::StartAbility(want, abilityMs_);
481 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnActive, abilityStateCountOne), 0);
482 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName2 + abilityStateOnActive, abilityStateCountOne), 0);
483 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName + abilityStateOnBackground, abilityStateCountOne), 0);
484
485 if (abilityMs_) {
486 abilityMs_->PowerOff();
487 }
488 if (abilityMs_) {
489 abilityMs_->PowerOn();
490 }
491 EXPECT_EQ(STAbilityUtil::WaitCompleted(event_, abilityName2 + abilityStateOnActive, abilityStateCountTwo), 0);
492 ExpectAbilityCurrentState(abilityName2, AbilityState_Test::ACTIVATING, AbilityState_Test::ACTIVE);
493 RunningProcessInfo pInfo = STAbilityUtil::GetAppProcessInfoByName(bundleName2, appMs_, WAIT_TIME);
494 EXPECT_EQ(AppProcessState::APP_STATE_FOREGROUND, pInfo.state_);
495
496 GTEST_LOG_(INFO) << "AmsPowerTest AMS_Power_0500 end";
497 }
498
499 /*
500 * @tc.number : AMS_Power_0600
501 * @tc.name : Verify poweroff and poweron functions(launcher)[singleton]
502 * @tc.desc : 1.called PowerOff
503 * 2.Check the launcher process status
504 * 3.called PowerOn
505 * 4.Check the launcher process status
506 */
507 HWTEST_F(AmsPowerTest, AMS_Power_0600, TestSize.Level1)
508 {
509 GTEST_LOG_(INFO) << "AmsPowerTest AMS_Power_0600 start";
510 ExpectAbilityCurrentState(launcherAbility, AbilityState_Test::ACTIVE, AbilityState_Test::ACTIVATING, DUMP_ALL);
511 ShowDump();
512 if (abilityMs_) {
513 abilityMs_->PowerOff();
514 }
515 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
516 ShowDump();
517 ExpectAbilityCurrentState(
518 launcherAbility, AbilityState_Test::BACKGROUND, AbilityState_Test::MOVING_BACKGROUND, DUMP_ALL);
519 if (abilityMs_) {
520 abilityMs_->PowerOn();
521 }
522 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
523 ExpectAbilityCurrentState(launcherAbility, AbilityState_Test::ACTIVE, AbilityState_Test::ACTIVATING, DUMP_ALL);
524 ShowDump();
525
526 GTEST_LOG_(INFO) << "AmsPowerTest AMS_Power_0600 end";
527 }