• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace {
32 const std::string STRING_DEVICE = "device";
33 const std::string STRING_ABILITY_NAME = "ability";
34 const std::string STRING_ABILITY_NAME_INVALID = "invalid_ability";
35 const std::string STRING_BUNDLE_NAME = "bundle";
36 const std::string STRING_BUNDLE_NAME_INVALID = "invalid_bundle";
37 const std::string STRING_RECORD_ID = "1024";
38 const std::string STRING_RECORD_ID_INVALID = "2048";
39 const std::string STRING_STATE_ON = "on";
40 const std::string STRING_STATE_ON_INVALID = "invalid_on";
41 const std::string STRING_STATE_OFF = "off";
42 const std::string STRING_STATE_OFF_INVALID = "invalid_off";
43 }  // namespace
44 
45 class AaCommandStartModuleTest : public ::testing::Test {
46 public:
47     static void SetUpTestCase();
48     static void TearDownTestCase();
49     void SetUp() override;
50     void TearDown() override;
51 
52     void MakeMockObjects() const;
53 
54     std::string cmd_ = "start";
55 };
56 
SetUpTestCase()57 void AaCommandStartModuleTest::SetUpTestCase()
58 {}
59 
TearDownTestCase()60 void AaCommandStartModuleTest::TearDownTestCase()
61 {}
62 
SetUp()63 void AaCommandStartModuleTest::SetUp()
64 {
65     // reset optind to 0
66     optind = 0;
67 
68     // make mock objects
69     MakeMockObjects();
70 }
71 
TearDown()72 void AaCommandStartModuleTest::TearDown()
73 {}
74 
MakeMockObjects() const75 void AaCommandStartModuleTest::MakeMockObjects() const
76 {
77     // mock a stub
78     auto managerStubPtr = sptr<IAbilityManager>(new MockAbilityManagerStub());
79 
80     // set the mock stub
81     auto managerClientPtr = AbilityManagerClient::GetInstance();
82     managerClientPtr->proxy_ = managerStubPtr;
83 }
84 
85 /**
86  * @tc.number: Aa_Command_Start_ModuleTest_0100
87  * @tc.name: ExecCommand
88  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name>" command.
89  */
90 HWTEST_F(AaCommandStartModuleTest, Aa_Command_Start_ModuleTest_0100, Function | MediumTest | Level1)
91 {
92     char* argv[] = {
93         (char*)TOOL_NAME.c_str(),
94         (char*)cmd_.c_str(),
95         (char*)"-d",
96         (char*)STRING_DEVICE.c_str(),
97         (char*)"-a",
98         (char*)STRING_ABILITY_NAME.c_str(),
99         (char*)"-b",
100         (char*)STRING_BUNDLE_NAME.c_str(),
101         (char*)"",
102     };
103     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
104 
105     AbilityManagerShellCommand cmd(argc, argv);
106     EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_OK + "\n");
107 }
108 
109 /**
110  * @tc.number: Aa_Command_Start_ModuleTest_0200
111  * @tc.name: ExecCommand
112  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name>" command.
113  */
114 HWTEST_F(AaCommandStartModuleTest, Aa_Command_Start_ModuleTest_0200, Function | MediumTest | Level1)
115 {
116     char* argv[] = {
117         (char*)TOOL_NAME.c_str(),
118         (char*)cmd_.c_str(),
119         (char*)"-d",
120         (char*)STRING_DEVICE.c_str(),
121         (char*)"-a",
122         (char*)STRING_ABILITY_NAME_INVALID.c_str(),
123         (char*)"-b",
124         (char*)STRING_BUNDLE_NAME.c_str(),
125         (char*)"",
126     };
127     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
128 
129     AbilityManagerShellCommand cmd(argc, argv);
130     EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_NG + "\n" + cmd.messageMap_.at(RESOLVE_ABILITY_ERR) + "\n");
131 }
132 
133 /**
134  * @tc.number: Aa_Command_Start_ModuleTest_0300
135  * @tc.name: ExecCommand
136  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name>" command.
137  */
138 HWTEST_F(AaCommandStartModuleTest, Aa_Command_Start_ModuleTest_0300, Function | MediumTest | Level1)
139 {
140     char* argv[] = {
141         (char*)TOOL_NAME.c_str(),
142         (char*)cmd_.c_str(),
143         (char*)"-d",
144         (char*)STRING_DEVICE.c_str(),
145         (char*)"-a",
146         (char*)STRING_ABILITY_NAME.c_str(),
147         (char*)"-b",
148         (char*)STRING_BUNDLE_NAME_INVALID.c_str(),
149         (char*)"",
150     };
151     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
152 
153     AbilityManagerShellCommand cmd(argc, argv);
154     EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_NG + "\n" + cmd.messageMap_.at(RESOLVE_APP_ERR) + "\n");
155 }
156 
157 /**
158  * @tc.number: Aa_Command_Start_ModuleTest_0400
159  * @tc.name: ExecCommand
160  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name> -D" command.
161  * @tc.type: FUNC
162  * @tc.require: AR000GJUN4
163  */
164 HWTEST_F(AaCommandStartModuleTest, Aa_Command_Start_ModuleTest_0400, Function | MediumTest | Level1)
165 {
166     char* argv[] = {
167         (char*)TOOL_NAME.c_str(),
168         (char*)cmd_.c_str(),
169         (char*)"-d",
170         (char*)STRING_DEVICE.c_str(),
171         (char*)"-a",
172         (char*)STRING_ABILITY_NAME.c_str(),
173         (char*)"-b",
174         (char*)STRING_BUNDLE_NAME.c_str(),
175         (char*)"-D",
176         (char*)"",
177     };
178     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
179 
180     AbilityManagerShellCommand cmd(argc, argv);
181     EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_OK + "\n");
182 }
183 
184 /**
185  * @tc.number: Aa_Command_Start_ModuleTest_0500
186  * @tc.name: ExecCommand
187  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name> --pi <key> <integer-value>" command.
188  * @tc.type: FUNC
189  * @tc.require: AR000GJUN4
190  */
191 HWTEST_F(AaCommandStartModuleTest, Aa_Command_Start_ModuleTest_0500, Function | MediumTest | Level1)
192 {
193     char* argv[] = {
194         (char*)TOOL_NAME.c_str(),
195         (char*)cmd_.c_str(),
196         (char*)"-d",
197         (char*)STRING_DEVICE.c_str(),
198         (char*)"-a",
199         (char*)STRING_ABILITY_NAME.c_str(),
200         (char*)"-b",
201         (char*)STRING_BUNDLE_NAME.c_str(),
202         (char*)"--pi",
203         (char*)"kinteger",
204         (char*)"900",
205         (char*)"",
206     };
207     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
208 
209     AbilityManagerShellCommand cmd(argc, argv);
210     EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_OK + "\n");
211 }
212 
213 /**
214  * @tc.number: Aa_Command_Start_ModuleTest_0600
215  * @tc.name: ExecCommand
216  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name> --pb <key> <bool-value>" command.
217  * @tc.type: FUNC
218  * @tc.require: AR000GJUN4
219  */
220 HWTEST_F(AaCommandStartModuleTest, Aa_Command_Start_ModuleTest_0600, Function | MediumTest | Level1)
221 {
222     char* argv[] = {
223         (char*)TOOL_NAME.c_str(),
224         (char*)cmd_.c_str(),
225         (char*)"-d",
226         (char*)STRING_DEVICE.c_str(),
227         (char*)"-a",
228         (char*)STRING_ABILITY_NAME.c_str(),
229         (char*)"-b",
230         (char*)STRING_BUNDLE_NAME.c_str(),
231         (char*)"--pb",
232         (char*)"kbool",
233         (char*)"true",
234         (char*)"",
235     };
236     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
237 
238     AbilityManagerShellCommand cmd(argc, argv);
239     EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_OK + "\n");
240 }
241 
242 /**
243  * @tc.number: Aa_Command_Start_ModuleTest_0700
244  * @tc.name: ExecCommand
245  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name> --ps <key> <value>" command.
246  * @tc.type: FUNC
247  * @tc.require: AR000GJUN4
248  */
249 HWTEST_F(AaCommandStartModuleTest, Aa_Command_Start_ModuleTest_0700, Function | MediumTest | Level1)
250 {
251     char* argv[] = {
252         (char*)TOOL_NAME.c_str(),
253         (char*)cmd_.c_str(),
254         (char*)"-d",
255         (char*)STRING_DEVICE.c_str(),
256         (char*)"-a",
257         (char*)STRING_ABILITY_NAME.c_str(),
258         (char*)"-b",
259         (char*)STRING_BUNDLE_NAME.c_str(),
260         (char*)"--ps",
261         (char*)"kstring",
262         (char*)"string-value",
263         (char*)"",
264     };
265     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
266 
267     AbilityManagerShellCommand cmd(argc, argv);
268     EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_OK + "\n");
269 }
270 
271 /**
272  * @tc.number: Aa_Command_Start_ModuleTest_0800
273  * @tc.name: ExecCommand
274  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name> \
275  * --psn <key> <integer-value>" command.
276  * @tc.type: FUNC
277  * @tc.require: AR000GJUN4
278  */
279 HWTEST_F(AaCommandStartModuleTest, Aa_Command_Start_ModuleTest_0800, Function | MediumTest | Level1)
280 {
281     char* argv[] = {
282         (char*)TOOL_NAME.c_str(),
283         (char*)cmd_.c_str(),
284         (char*)"-d",
285         (char*)STRING_DEVICE.c_str(),
286         (char*)"-a",
287         (char*)STRING_ABILITY_NAME.c_str(),
288         (char*)"-b",
289         (char*)STRING_BUNDLE_NAME.c_str(),
290         (char*)"--psn",
291         (char*)"knullstring",
292         (char*)"",
293     };
294     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
295 
296     AbilityManagerShellCommand cmd(argc, argv);
297     EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_OK + "\n");
298 }