• 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