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 <gtest/gtest.h>
17
18 #define protected public
19 #include "ability_command.h"
20 #undef protected
21 #include "mock_ability_manager_stub.h"
22 #define private public
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 class AaCommandTest : public ::testing::Test {
32 public:
33 static void SetUpTestCase();
34 static void TearDownTestCase();
35 void SetUp() override;
36 void TearDown() override;
37
38 void MakeMockObjects() const;
39 };
40
SetUpTestCase()41 void AaCommandTest::SetUpTestCase()
42 {}
43
TearDownTestCase()44 void AaCommandTest::TearDownTestCase()
45 {}
46
SetUp()47 void AaCommandTest::SetUp()
48 {
49 // reset optind to 0
50 optind = 0;
51
52 // make mock objects
53 MakeMockObjects();
54 }
55
TearDown()56 void AaCommandTest::TearDown()
57 {}
58
MakeMockObjects() const59 void AaCommandTest::MakeMockObjects() const
60 {
61 // mock a stub
62 auto managerStubPtr = sptr<IRemoteObject>(new MockAbilityManagerStub());
63
64 // set the mock stub
65 auto managerClientPtr = AbilityManagerClient::GetInstance();
66 managerClientPtr->remoteObject_ = managerStubPtr;
67 }
68
69 /**
70 * @tc.number: Aa_Command_0100
71 * @tc.name: ExecCommand
72 * @tc.desc: Verify the "aa" command.
73 */
74 HWTEST_F(AaCommandTest, Aa_Command_0100, Function | MediumTest | Level1)
75 {
76 char *argv[] = {
77 (char *)TOOL_NAME.c_str(),
78 (char *)"",
79 };
80 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
81
82 AbilityManagerShellCommand cmd(argc, argv);
83 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG);
84 }
85
86 /**
87 * @tc.number: Aa_Command_0200
88 * @tc.name: ExecCommand
89 * @tc.desc: Verify the "aa xxx" command.
90 */
91 HWTEST_F(AaCommandTest, Aa_Command_0200, Function | MediumTest | Level1)
92 {
93 char *argv[] = {
94 (char *)TOOL_NAME.c_str(),
95 (char *)"xxx",
96 (char *)"",
97 };
98 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
99
100 AbilityManagerShellCommand cmd(argc, argv);
101 EXPECT_EQ(cmd.ExecCommand(), cmd.GetCommandErrorMsg() + HELP_MSG);
102 }
103
104 /**
105 * @tc.number: Aa_Command_0300
106 * @tc.name: ExecCommand
107 * @tc.desc: Verify the "aa -xxx" command.
108 */
109 HWTEST_F(AaCommandTest, Aa_Command_0300, Function | MediumTest | Level1)
110 {
111 char *argv[] = {
112 (char *)TOOL_NAME.c_str(),
113 (char *)"-xxx",
114 (char *)"",
115 };
116 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
117
118 AbilityManagerShellCommand cmd(argc, argv);
119 EXPECT_EQ(cmd.ExecCommand(), cmd.GetCommandErrorMsg() + HELP_MSG);
120 }
121
122 /**
123 * @tc.number: Aa_Command_0400
124 * @tc.name: ExecCommand
125 * @tc.desc: Verify the "aa --xxx" command.
126 */
127 HWTEST_F(AaCommandTest, Aa_Command_0400, Function | MediumTest | Level1)
128 {
129 char *argv[] = {
130 (char *)TOOL_NAME.c_str(),
131 (char *)"--xxx",
132 (char *)"",
133 };
134 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
135
136 AbilityManagerShellCommand cmd(argc, argv);
137 EXPECT_EQ(cmd.ExecCommand(), cmd.GetCommandErrorMsg() + HELP_MSG);
138 }
139
140 /**
141 * @tc.number: Aa_Command_0500
142 * @tc.name: ExecCommand
143 * @tc.desc: Verify the "aa help" command.
144 */
145 HWTEST_F(AaCommandTest, Aa_Command_0500, Function | MediumTest | Level1)
146 {
147 char *argv[] = {
148 (char *)TOOL_NAME.c_str(),
149 (char *)"help",
150 (char *)"",
151 };
152 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
153
154 AbilityManagerShellCommand cmd(argc, argv);
155 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG);
156 }
157