1 /*
2 * Copyright (c) 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 #include <gtest/gtest.h>
17
18 #define private public
19 #define protected public
20 #include "ability_tool_command.h"
21 #undef protected
22 #include "mock_ability_manager_stub.h"
23 #include "ability_manager_client.h"
24 #undef private
25 #include "ability_manager_interface.h"
26
27 using namespace testing::ext;
28 using namespace OHOS;
29 using namespace OHOS::AAFwk;
30
31 namespace OHOS {
32 namespace AAFwk {
33 namespace {
34 const std::string ABILITY_TOOL_HELP_MSG =
35 "usage: ability_tool <command> <options>\n"
36 "ability_tool commands list:\n"
37 " help list available commands\n"
38 " start start ability with options\n"
39 " stop-service stop service with options\n"
40 " force-stop force stop the process with bundle name\n"
41 " test start the test framework with options\n";
42
43 const std::string ABILITY_TOOL_HELP_MSG_START =
44 "usage: ability_tool start <options>\n"
45 "ability_tool start options list:\n"
46 " --help list available options\n"
47 " --device <device-id> device Id\n"
48 " --ability <ability-name> ability name, mandatory\n"
49 " --bundle <bundle-name> bundle name, mandatory\n"
50 " --options <key> <value> start options, such as windowMode 102\n"
51 " --flags <flag> flags in a want\n"
52 " -C cold start\n"
53 " -D start with debug mode\n";
54
55 const std::string ABILITY_TOOL_HELP_MSG_STOP_SERVICE =
56 "usage: ability_tool stop-service <options>\n"
57 "ability_tool stop-service options list:\n"
58 " --help list available options\n"
59 " --device <device-id> device Id\n"
60 " --ability <ability-name> ability name, mandatory\n"
61 " --bundle <bundle-name> bundle name, mandatory\n";
62
63 const std::string ABILITY_TOOL_HELP_MSG_FORCE_STOP =
64 "usage: ability_tool force-stop <options>\n"
65 "ability_tool force-stop options list:\n"
66 " --help list available options\n"
67 " <bundle-name> bundle name, mandatory\n";
68
69 const std::string ABILITY_TOOL_HELP_MSG_TEST =
70 "usage: ability_tool test <options>\n"
71 "ability_tool test options list:\n"
72 " --help list available options\n"
73 " --bundle <bundle-name> bundle name, mandatory\n"
74 " --options unittest <test-runner> test runner need to start, mandatory\n"
75 " --package-name <package-name> package name, required for the FA model\n"
76 " --module-name <module-name> module name, required for the STAGE model\n"
77 " --options <key> <value> test options, such as testcase test_001\n"
78 " --watchdog <wait-time> max execute time for this test\n"
79 " -D test with debug mode\n";
80
81 const std::string ABILITY_TOOL_HELP_MSG_NO_ABILITY_NAME_OPTION = "error: --ability <ability-name> is expected";
82 const std::string ABILITY_TOOL_HELP_MSG_NO_BUNDLE_NAME_OPTION = "error: --bundle <bundle-name> is expected";
83 const std::string ABILITY_TOOL_HELP_MSG_WINDOW_MODE_INVALID = "error: --options windowMode <value> with invalid param";
84 const std::string ABILITY_TOOL_HELP_MSG_LACK_VALUE = "error: lack of value of key";
85 const std::string ABILITY_TOOL_HELP_MSG_ONLY_NUM = "error: current option only support number";
86 const std::string ABILITY_TOOL_HELP_LACK_OPTIONS = "error: lack of essential args";
87 } // namespace
88
89 class AbilityToolTest : public testing::Test {
90 public:
91 static void SetUpTestCase();
92 static void TearDownTestCase();
93 void SetUp();
94 void TearDown();
95 };
96
SetUpTestCase()97 void AbilityToolTest::SetUpTestCase()
98 {}
99
TearDownTestCase()100 void AbilityToolTest::TearDownTestCase()
101 {}
102
SetUp()103 void AbilityToolTest::SetUp()
104 {
105 // reset optind to 0
106 optind = 0;
107 }
108
TearDown()109 void AbilityToolTest::TearDown()
110 {}
111
112 /**
113 * @tc.name: AbilityTool_Cmd_0100
114 * @tc.desc: "ability_tool" test.
115 * @tc.type: FUNC
116 */
117 HWTEST_F(AbilityToolTest, AbilityTool_Cmd_0100, TestSize.Level1)
118 {
119 char* argv[] = {
120 const_cast<char*>("ability_tool"),
121 };
122 int argc = sizeof(argv) / sizeof(argv[0]);
123 AbilityToolCommand cmd(argc, argv);
124 EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG);
125 }
126
127 /**
128 * @tc.name: AbilityTool_Cmd_0200
129 * @tc.desc: invalid options "ability_tool xxx" test.
130 * @tc.type: FUNC
131 */
132 HWTEST_F(AbilityToolTest, AbilityTool_Cmd_0200, TestSize.Level1)
133 {
134 // "ability_tool"
135 char* argv[] = {
136 const_cast<char*>("ability_tool"),
137 const_cast<char*>("xxx"),
138 };
139 int argc = sizeof(argv) / sizeof(argv[0]);
140 AbilityToolCommand cmd(argc, argv);
141 EXPECT_EQ(cmd.ExecCommand(), cmd.GetCommandErrorMsg() + ABILITY_TOOL_HELP_MSG);
142 }
143
144 /**
145 * @tc.name: AbilityTool_Cmd_0300
146 * @tc.desc: "ability_tool help" test.
147 * @tc.type: FUNC
148 */
149 HWTEST_F(AbilityToolTest, AbilityTool_Cmd_0300, TestSize.Level1)
150 {
151 char* argv[] = {
152 const_cast<char*>("ability_tool"),
153 const_cast<char*>("help"),
154 };
155 int argc = sizeof(argv) / sizeof(argv[0]);
156 AbilityToolCommand cmd(argc, argv);
157 EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG);
158 }
159
160 /**
161 * @tc.name: AbilityTool_Start_0100
162 * @tc.desc: "ability_tool start" test.
163 * @tc.type: FUNC
164 */
165 HWTEST_F(AbilityToolTest, AbilityTool_Start_0100, TestSize.Level1)
166 {
167 char* argv[] = {
168 const_cast<char*>("ability_tool"),
169 const_cast<char*>("start"),
170 };
171 int argc = sizeof(argv) / sizeof(argv[0]);
172 AbilityToolCommand cmd(argc, argv);
173 EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_NO_ABILITY_NAME_OPTION + "\n" +
174 ABILITY_TOOL_HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n" +
175 ABILITY_TOOL_HELP_MSG_START);
176 }
177
178 /**
179 * @tc.name: AbilityTool_Start_0200
180 * @tc.desc: "ability_tool start --help" test.
181 * @tc.type: FUNC
182 */
183 HWTEST_F(AbilityToolTest, AbilityTool_Start_0200, TestSize.Level1)
184 {
185 char* argv[] = {
186 const_cast<char*>("ability_tool"),
187 const_cast<char*>("start"),
188 const_cast<char*>("--help"),
189 };
190 int argc = sizeof(argv) / sizeof(argv[0]);
191 AbilityToolCommand cmd(argc, argv);
192 EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_NO_ABILITY_NAME_OPTION + "\n" +
193 ABILITY_TOOL_HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n" +
194 ABILITY_TOOL_HELP_MSG_START);
195 }
196
197 /**
198 * @tc.name: AbilityTool_Start_0300
199 * @tc.desc: without bundleName "ability_tool start --ability TestAbility" test.
200 * @tc.type: FUNC
201 */
202 HWTEST_F(AbilityToolTest, AbilityTool_Start_0300, TestSize.Level1)
203 {
204 char* argv[] = {
205 const_cast<char*>("ability_tool"),
206 const_cast<char*>("start"),
207 const_cast<char*>("--ability"),
208 const_cast<char*>("TestAbility"),
209 };
210 int argc = sizeof(argv) / sizeof(argv[0]);
211 AbilityToolCommand cmd(argc, argv);
212 EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n" +
213 ABILITY_TOOL_HELP_MSG_START);
214 }
215
216 /**
217 * @tc.name: AbilityTool_Start_0400
218 * @tc.desc: without abilityName "ability_tool start --bundle com.example.abilitytooltest" test.
219 * @tc.type: FUNC
220 */
221 HWTEST_F(AbilityToolTest, AbilityTool_Start_0400, TestSize.Level1)
222 {
223 char* argv[] = {
224 const_cast<char*>("ability_tool"),
225 const_cast<char*>("start"),
226 const_cast<char*>("--bundle"),
227 const_cast<char*>("com.example.abilitytooltest"),
228 };
229 int argc = sizeof(argv) / sizeof(argv[0]);
230 AbilityToolCommand cmd(argc, argv);
231 EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_NO_ABILITY_NAME_OPTION + "\n" +
232 ABILITY_TOOL_HELP_MSG_START);
233 }
234
235 /**
236 * @tc.name: AbilityTool_Start_0500
237 * @tc.desc: lack of windowMode test.
238 * "ability_tool start --ability TestAbility --bundle com.example.abilitytooltest --options windowMode"
239 * @tc.type: FUNC
240 */
241 HWTEST_F(AbilityToolTest, AbilityTool_Start_0500, TestSize.Level1)
242 {
243 char* argv[] = {
244 const_cast<char*>("ability_tool"),
245 const_cast<char*>("start"),
246 const_cast<char*>("--ability"),
247 const_cast<char*>("TestAbility"),
248 const_cast<char*>("--bundle"),
249 const_cast<char*>("com.example.abilitytooltest"),
250 const_cast<char*>("--options"),
251 const_cast<char*>("windowMode"),
252 };
253 int argc = sizeof(argv) / sizeof(argv[0]);
254 AbilityToolCommand cmd(argc, argv);
255 EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_LACK_VALUE + "\n" +
256 ABILITY_TOOL_HELP_MSG_START);
257 }
258
259 /**
260 * @tc.name: AbilityTool_Start_0600
261 * @tc.desc: invalid windowMode test.
262 * "ability_tool start --ability TestAbility --bundle com.example.abilitytooltest --options windowMode 20"
263 * @tc.type: FUNC
264 */
265 HWTEST_F(AbilityToolTest, AbilityTool_Start_0600, TestSize.Level1)
266 {
267 char* argv[] = {
268 const_cast<char*>("ability_tool"),
269 const_cast<char*>("start"),
270 const_cast<char*>("--ability"),
271 const_cast<char*>("TestAbility"),
272 const_cast<char*>("--bundle"),
273 const_cast<char*>("com.example.abilitytooltest"),
274 const_cast<char*>("--options"),
275 const_cast<char*>("windowMode"),
276 const_cast<char*>("20"),
277 };
278 int argc = sizeof(argv) / sizeof(argv[0]);
279 AbilityToolCommand cmd(argc, argv);
280 EXPECT_NE(cmd.ExecCommand().find(ABILITY_TOOL_HELP_MSG_START), string::npos);
281 }
282
283 /**
284 * @tc.name: AbilityTool_Start_0700
285 * @tc.desc: flag isn't num test. flag is not a number, didn't parse.
286 * "ability_tool start --ability TestAbility --bundle com.example.abilitytooltest --flags abc"
287 * @tc.type: FUNC
288 */
289 HWTEST_F(AbilityToolTest, AbilityTool_Start_0700, TestSize.Level1)
290 {
291 char* argv[] = {
292 const_cast<char*>("ability_tool"),
293 const_cast<char*>("start"),
294 const_cast<char*>("--ability"),
295 const_cast<char*>("TestAbility"),
296 const_cast<char*>("--bundle"),
297 const_cast<char*>("com.example.abilitytooltest"),
298 const_cast<char*>("--flags"),
299 const_cast<char*>("abc"),
300 };
301 int argc = sizeof(argv) / sizeof(argv[0]);
302 AbilityToolCommand cmd(argc, argv);
303 EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_NG + "\n");
304 }
305
306 /**
307 * @tc.name: AbilityTool_StopService_0100
308 * @tc.desc: "ability_tool stop-service" test.
309 * @tc.type: FUNC
310 */
311 HWTEST_F(AbilityToolTest, AbilityTool_StopService_0100, TestSize.Level1)
312 {
313 char* argv[] = {
314 const_cast<char*>("ability_tool"),
315 const_cast<char*>("stop-service"),
316 };
317 int argc = sizeof(argv) / sizeof(argv[0]);
318 AbilityToolCommand cmd(argc, argv);
319 EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_NO_ABILITY_NAME_OPTION + "\n" +
320 ABILITY_TOOL_HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n" +
321 ABILITY_TOOL_HELP_MSG_STOP_SERVICE);
322 }
323
324 /**
325 * @tc.name: AbilityTool_StopService_0200
326 * @tc.desc: "ability_tool stop-service --help" test.
327 * @tc.type: FUNC
328 */
329 HWTEST_F(AbilityToolTest, AbilityTool_StopService_0200, TestSize.Level1)
330 {
331 char* argv[] = {
332 const_cast<char*>("ability_tool"),
333 const_cast<char*>("stop-service"),
334 const_cast<char*>("--help"),
335 };
336 int argc = sizeof(argv) / sizeof(argv[0]);
337 AbilityToolCommand cmd(argc, argv);
338 EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_NO_ABILITY_NAME_OPTION + "\n" +
339 ABILITY_TOOL_HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n" +
340 ABILITY_TOOL_HELP_MSG_STOP_SERVICE);
341 }
342
343 /**
344 * @tc.name: AbilityTool_StopService_0300
345 * @tc.desc: without bundleName "ability_tool stop-service --ability TestAbility" test.
346 * @tc.type: FUNC
347 */
348 HWTEST_F(AbilityToolTest, AbilityTool_StopService_0300, TestSize.Level1)
349 {
350 char* argv[] = {
351 const_cast<char*>("ability_tool"),
352 const_cast<char*>("stop-service"),
353 const_cast<char*>("--ability"),
354 const_cast<char*>("TestAbility"),
355 };
356 int argc = sizeof(argv) / sizeof(argv[0]);
357 AbilityToolCommand cmd(argc, argv);
358 EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n" +
359 ABILITY_TOOL_HELP_MSG_STOP_SERVICE);
360 }
361
362 /**
363 * @tc.name: AbilityTool_StopService_0400
364 * @tc.desc: without abilityName "ability_tool stop-service --ability TestAbility" test.
365 * @tc.type: FUNC
366 */
367 HWTEST_F(AbilityToolTest, AbilityTool_StopService_0400, TestSize.Level1)
368 {
369 char* argv[] = {
370 const_cast<char*>("ability_tool"),
371 const_cast<char*>("stop-service"),
372 const_cast<char*>("--bundle"),
373 const_cast<char*>("com.example.abilitytooltest"),
374 };
375 int argc = sizeof(argv) / sizeof(argv[0]);
376 AbilityToolCommand cmd(argc, argv);
377 EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_NO_ABILITY_NAME_OPTION + "\n" +
378 ABILITY_TOOL_HELP_MSG_STOP_SERVICE);
379 }
380
381 /**
382 * @tc.name: AbilityTool_StopService_0500
383 * @tc.desc: stop-service "ability_tool stop-service --ability TestAbility" test.
384 * @tc.type: FUNC
385 */
386 HWTEST_F(AbilityToolTest, AbilityTool_StopService_0500, TestSize.Level1)
387 {
388 char* argv[] = {
389 const_cast<char*>("ability_tool"),
390 const_cast<char*>("stop-service"),
391 const_cast<char*>("--ability"),
392 const_cast<char*>("com.ohos.screenshot.ServiceExtAbility"),
393 const_cast<char*>("--bundle"),
394 const_cast<char*>("com.ohos.screenshot"),
395 };
396 int argc = sizeof(argv) / sizeof(argv[0]);
397 AbilityToolCommand cmd(argc, argv);
398 EXPECT_EQ(cmd.ExecCommand(), STRING_STOP_SERVICE_ABILITY_NG + "\n");
399 }
400
401 /**
402 * @tc.name: AbilityTool_ForceStop_0100
403 * @tc.desc: "ability_tool force-stop" test.
404 * @tc.type: FUNC
405 */
406 HWTEST_F(AbilityToolTest, AbilityTool_ForceStop_0100, TestSize.Level1)
407 {
408 char* argv[] = {
409 const_cast<char*>("ability_tool"),
410 const_cast<char*>("force-stop"),
411 };
412 int argc = sizeof(argv) / sizeof(argv[0]);
413 AbilityToolCommand cmd(argc, argv);
414 EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_FORCE_STOP);
415 }
416
417 /**
418 * @tc.name: AbilityTool_Test_0100
419 * @tc.desc: "ability_tool test" test.
420 * @tc.type: FUNC
421 */
422 HWTEST_F(AbilityToolTest, AbilityTool_Test_0100, TestSize.Level1)
423 {
424 char* argv[] = {
425 const_cast<char*>("ability_tool"),
426 const_cast<char*>("test"),
427 };
428 int argc = sizeof(argv) / sizeof(argv[0]);
429 AbilityToolCommand cmd(argc, argv);
430 EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_LACK_OPTIONS + "\n" + ABILITY_TOOL_HELP_MSG_TEST);
431 }
432
433 /**
434 * @tc.name: AbilityTool_Test_0200
435 * @tc.desc: "ability_tool test --help" test.
436 * @tc.type: FUNC
437 */
438 HWTEST_F(AbilityToolTest, AbilityTool_Test_0200, TestSize.Level1)
439 {
440 char* argv[] = {
441 const_cast<char*>("ability_tool"),
442 const_cast<char*>("test"),
443 const_cast<char*>("--help"),
444 };
445 int argc = sizeof(argv) / sizeof(argv[0]);
446 AbilityToolCommand cmd(argc, argv);
447 EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_LACK_OPTIONS + "\n" + ABILITY_TOOL_HELP_MSG_TEST);
448 }
449
450 /**
451 * @tc.name: AbilityTool_Test_0300
452 * @tc.desc: without bundleName "ability_tool test --options unittest testRunner" test.
453 * @tc.type: FUNC
454 */
455 HWTEST_F(AbilityToolTest, AbilityTool_Test_0300, TestSize.Level1)
456 {
457 char* argv[] = {
458 const_cast<char*>("ability_tool"),
459 const_cast<char*>("test"),
460 const_cast<char*>("--options"),
461 const_cast<char*>("unittest"),
462 const_cast<char*>("testRunner"),
463 };
464 int argc = sizeof(argv) / sizeof(argv[0]);
465 AbilityToolCommand cmd(argc, argv);
466 EXPECT_NE(cmd.ExecCommand().find(ABILITY_TOOL_HELP_MSG_TEST), string::npos);
467 }
468
469 /**
470 * @tc.name: AbilityTool_Test_0400
471 * @tc.desc: without unittest "ability_tool test --bundle com.example.abilitytooltest" test.
472 * @tc.type: FUNC
473 */
474 HWTEST_F(AbilityToolTest, AbilityTool_Test_0400, TestSize.Level1)
475 {
476 char* argv[] = {
477 const_cast<char*>("ability_tool"),
478 const_cast<char*>("test"),
479 const_cast<char*>("--bundle"),
480 const_cast<char*>("com.example.abilitytooltest"),
481 };
482 int argc = sizeof(argv) / sizeof(argv[0]);
483 AbilityToolCommand cmd(argc, argv);
484 EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_LACK_OPTIONS + "\n" + ABILITY_TOOL_HELP_MSG_TEST);
485 }
486
487 /**
488 * @tc.name: AbilityTool_Test_0500
489 * @tc.desc: without value of unittest "ability_tool test --bundle com.example.abilitytooltest --options unittest" test.
490 * @tc.type: FUNC
491 */
492 HWTEST_F(AbilityToolTest, AbilityTool_Test_0500, TestSize.Level1)
493 {
494 char* argv[] = {
495 const_cast<char*>("ability_tool"),
496 const_cast<char*>("test"),
497 const_cast<char*>("--bundle"),
498 const_cast<char*>("com.example.abilitytooltest"),
499 const_cast<char*>("--options"),
500 const_cast<char*>("unittest"),
501 };
502 int argc = sizeof(argv) / sizeof(argv[0]);
503 AbilityToolCommand cmd(argc, argv);
504 EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_LACK_VALUE + "\n" + ABILITY_TOOL_HELP_MSG_TEST);
505 }
506
507 /**
508 * @tc.number: AbilityTool_Test_0600
509 * @tc.name: RunAsTestCommand
510 * @tc.desc: "ability_tool test --bundle com.example.abilitytooltest --options --package-name unittest"
511 */
512 HWTEST_F(AbilityToolTest, AbilityTool_Test_0600, TestSize.Level1)
513 {
514 char* argv[] = {
515 const_cast<char*>("ability_tool"),
516 const_cast<char*>("test"),
517 const_cast<char*>("--bundle"),
518 const_cast<char*>("com.example.abilitytooltest"),
519 const_cast<char*>("--options"),
520 const_cast<char*>("--package-name"),
521 };
522 int argc = sizeof(argv) / sizeof(argv[0]);
523 AbilityToolCommand cmd(argc, argv);
524 EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_LACK_VALUE + "\n" + ABILITY_TOOL_HELP_MSG_TEST);
525 }
526
527 /**
528 * @tc.number: AbilityTool_Test_0700
529 * @tc.name: RunAsTestCommand
530 * @tc.desc: "ability_tool test --bundle com.example.abilitytooltest --options --module-name unittest"
531 */
532 HWTEST_F(AbilityToolTest, AbilityTool_Test_0700, TestSize.Level1)
533 {
534 char* argv[] = {
535 const_cast<char*>("ability_tool"),
536 const_cast<char*>("test"),
537 const_cast<char*>("--bundle"),
538 const_cast<char*>("com.example.abilitytooltest"),
539 const_cast<char*>("--options"),
540 const_cast<char*>("--module-name"),
541 };
542 int argc = sizeof(argv) / sizeof(argv[0]);
543 AbilityToolCommand cmd(argc, argv);
544 EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_LACK_VALUE + "\n" + ABILITY_TOOL_HELP_MSG_TEST);
545 }
546
547 /**
548 * @tc.number: AbilityTool_Test_0800
549 * @tc.name: RunAsTestCommand
550 * @tc.desc: "ability_tool test --bundle com.example.abilitytooltest --options --watchdog unittest"
551 */
552 HWTEST_F(AbilityToolTest, AbilityTool_Test_0800, TestSize.Level1)
553 {
554 char* argv[] = {
555 const_cast<char*>("ability_tool"),
556 const_cast<char*>("test"),
557 const_cast<char*>("--bundle"),
558 const_cast<char*>("com.example.abilitytooltest"),
559 const_cast<char*>("--options"),
560 const_cast<char*>("--watchdog"),
561 };
562 int argc = sizeof(argv) / sizeof(argv[0]);
563 AbilityToolCommand cmd(argc, argv);
564 EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_LACK_VALUE + "\n" + ABILITY_TOOL_HELP_MSG_TEST);
565 }
566
567 /**
568 * @tc.number: AbilityTool_Test_0900
569 * @tc.name: RunAsTestCommand
570 * @tc.desc: "ability_tool test --bundle com.example.abilitytooltest --options -D unittest"
571 */
572 HWTEST_F(AbilityToolTest, AbilityTool_Test_0900, TestSize.Level1)
573 {
574 char* argv[] = {
575 const_cast<char*>("ability_tool"),
576 const_cast<char*>("test"),
577 const_cast<char*>("--bundle"),
578 const_cast<char*>("com.example.abilitytooltest"),
579 const_cast<char*>("--options"),
580 const_cast<char*>("-D"),
581 };
582 int argc = sizeof(argv) / sizeof(argv[0]);
583 AbilityToolCommand cmd(argc, argv);
584 EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_LACK_VALUE + "\n" + ABILITY_TOOL_HELP_MSG_TEST);
585 }
586
587 /**
588 * @tc.number: AbilityTool_Start_0800
589 * @tc.name: RunAsStartAbility
590 * @tc.desc: Run As StartAbility sucess
591 */
592 HWTEST_F(AbilityToolTest, AbilityTool_Start_0800, TestSize.Level1)
593 {
594 char* argv[] = {
595 const_cast<char*>("ability_tool"),
596 const_cast<char*>("start"),
597 const_cast<char*>("--ability"),
598 const_cast<char*>("TestAbility"),
599 const_cast<char*>("--bundle"),
600 const_cast<char*>("com.example.abilitytooltest"),
601 const_cast<char*>("--flags"),
602 const_cast<char*>("abc"),
603 };
604 int argc = sizeof(argv) / sizeof(argv[0]);
605 AbilityToolCommand cmd(argc, argv);
606
607 auto managerClientPtr = AbilityManagerClient::GetInstance();
608 auto mockAbilityManagerStub = sptr<MockAbilityManagerStub>(new MockAbilityManagerStub());
609 ASSERT_NE(mockAbilityManagerStub, nullptr);
610 managerClientPtr->proxy_ = static_cast<IAbilityManager*>(mockAbilityManagerStub);
611
612 EXPECT_EQ(cmd.RunAsStartAbility(), OHOS::ERR_OK);
613 testing::Mock::AllowLeak(mockAbilityManagerStub);
614 }
615
616 /**
617 * @tc.number: AbilityTool_Start_0900
618 * @tc.name: RunAsStartAbility
619 * @tc.desc: "ability_tool start --device --ability"
620 */
621 HWTEST_F(AbilityToolTest, AbilityTool_Start_0900, TestSize.Level1)
622 {
623 char* argv[] = {
624 const_cast<char*>("ability_tool"),
625 const_cast<char*>("start"),
626 const_cast<char*>("--device"),
627 const_cast<char*>("--ability"),
628 };
629 int argc = sizeof(argv) / sizeof(argv[0]);
630 AbilityToolCommand cmd(argc, argv);
631 EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_NO_ABILITY_NAME_OPTION + "\n" +
632 ABILITY_TOOL_HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n" + ABILITY_TOOL_HELP_MSG_START);
633 }
634
635 /**
636 * @tc.number: AbilityTool_Start_1000
637 * @tc.name: RunAsStartAbility
638 * @tc.desc: "ability_tool start --ability TestAbility --bundle com.example.abilitytooltest --flags --cold-start"
639 */
640 HWTEST_F(AbilityToolTest, AbilityTool_Start_1000, TestSize.Level1)
641 {
642 char* argv[] = {
643 const_cast<char*>("ability_tool"),
644 const_cast<char*>("start"),
645 const_cast<char*>("--ability"),
646 const_cast<char*>("TestAbility"),
647 const_cast<char*>("--bundle"),
648 const_cast<char*>("com.example.abilitytooltest"),
649 const_cast<char*>("--flags"),
650 const_cast<char*>("--cold-start"),
651 };
652 int argc = sizeof(argv) / sizeof(argv[0]);
653 AbilityToolCommand cmd(argc, argv);
654 EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_OK + "\n");
655 }
656
657 /**
658 * @tc.number: AbilityTool_Start_1100
659 * @tc.name: RunAsStartAbility
660 * @tc.desc: "ability_tool start --ability TestAbility --bundle com.example.abilitytooltest --flags -D"
661 */
662 HWTEST_F(AbilityToolTest, AbilityTool_Start_1100, TestSize.Level1)
663 {
664 char* argv[] = {
665 const_cast<char*>("ability_tool"),
666 const_cast<char*>("start"),
667 const_cast<char*>("--ability"),
668 const_cast<char*>("TestAbility"),
669 const_cast<char*>("--bundle"),
670 const_cast<char*>("com.example.abilitytooltest"),
671 const_cast<char*>("--flags"),
672 const_cast<char*>("-D"),
673 };
674 int argc = sizeof(argv) / sizeof(argv[0]);
675 AbilityToolCommand cmd(argc, argv);
676 EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_OK + "\n");
677 }
678
679 /**
680 * @tc.name: AbilityTool_StopService_0600
681 * @tc.desc: RunAsStopService
682 * @tc.type: stop-service "ability_tool stop-service --ability TestAbility" test.
683 */
684 HWTEST_F(AbilityToolTest, AbilityTool_StopService_0600, TestSize.Level1)
685 {
686 char* argv[] = {
687 const_cast<char*>("ability_tool"),
688 const_cast<char*>("stop-service"),
689 const_cast<char*>("--ability"),
690 const_cast<char*>("com.ohos.screenshot.ServiceExtAbility"),
691 const_cast<char*>("--bundle"),
692 const_cast<char*>("com.ohos.screenshot"),
693 };
694 int argc = sizeof(argv) / sizeof(argv[0]);
695 AbilityToolCommand cmd(argc, argv);
696
697 auto managerClientPtr = AbilityManagerClient::GetInstance();
698 auto mockAbilityManagerStub = sptr<MockAbilityManagerStub>(new MockAbilityManagerStub());
699 ASSERT_NE(mockAbilityManagerStub, nullptr);
700 managerClientPtr->proxy_ = static_cast<IAbilityManager*>(mockAbilityManagerStub);
701
702 EXPECT_EQ(cmd.RunAsStopService(), OHOS::ERR_OK);
703 testing::Mock::AllowLeak(mockAbilityManagerStub);
704 }
705
706 /**
707 * @tc.name: AbilityTool_StopService_0700
708 * @tc.desc: RunAsStopService
709 * @tc.type: stop-service "ability_tool stop-service --device --ability" test.
710 */
711 HWTEST_F(AbilityToolTest, AbilityTool_StopService_0700, TestSize.Level1)
712 {
713 char* argv[] = {
714 const_cast<char*>("ability_tool"),
715 const_cast<char*>("stop-service"),
716 const_cast<char*>("--device"),
717 const_cast<char*>("--ability"),
718 };
719 int argc = sizeof(argv) / sizeof(argv[0]);
720 AbilityToolCommand cmd(argc, argv);
721 EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_NO_ABILITY_NAME_OPTION + "\n" +
722 ABILITY_TOOL_HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n" + ABILITY_TOOL_HELP_MSG_STOP_SERVICE);
723 }
724
725 /**
726 * @tc.name: AbilityTool_ForceStop_0200
727 * @tc.desc: RunAsForceStop
728 * @tc.type: "ability_tool force-stop" test
729 */
730 HWTEST_F(AbilityToolTest, AbilityTool_ForceStop_0200, TestSize.Level1)
731 {
732 char* argv[] = {
733 const_cast<char*>("ability_tool"),
734 const_cast<char*>("force-stop"),
735 const_cast<char*>("com.ohos.screenshot.ServiceExtAbility"),
736 };
737 int argc = sizeof(argv) / sizeof(argv[0]);
738 AbilityToolCommand cmd(argc, argv);
739 EXPECT_EQ(cmd.ExecCommand(), STRING_FORCE_STOP_OK + "\n");
740 }
741
742 /**
743 * @tc.name: AbilityTool_ForceStop_0300
744 * @tc.desc: RunAsForceStop
745 * @tc.type: "ability_tool force-stop" test
746 */
747 HWTEST_F(AbilityToolTest, AbilityTool_ForceStop_0300, TestSize.Level1)
748 {
749 char* argv[] = {
750 const_cast<char*>("ability_tool"),
751 const_cast<char*>("force-stop"),
752 const_cast<char*>("com.ohos.screenshot.ServiceExtAbility"),
753 };
754 int argc = sizeof(argv) / sizeof(argv[0]);
755 AbilityToolCommand cmd(argc, argv);
756
757 auto managerClientPtr = AbilityManagerClient::GetInstance();
758 auto mockAbilityManagerStub = sptr<MockAbilityManagerStub>(new MockAbilityManagerStub());
759 ASSERT_NE(mockAbilityManagerStub, nullptr);
760 EXPECT_CALL(*mockAbilityManagerStub, KillProcess(testing::_, testing::_))
761 .Times(1)
762 .WillOnce(testing::Return(0));
763 managerClientPtr->proxy_ = static_cast<IAbilityManager*>(mockAbilityManagerStub);
764
765 EXPECT_EQ(cmd.RunAsForceStop(), OHOS::ERR_OK);
766 testing::Mock::AllowLeak(mockAbilityManagerStub);
767 }
768 } // namespace AAFwk
769 } // namespace OHOS
770