• 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 class AaCommandStartModuleTest : 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     std::string cmd_ = "start";
41 };
42 
SetUpTestCase()43 void AaCommandStartModuleTest::SetUpTestCase()
44 {}
45 
TearDownTestCase()46 void AaCommandStartModuleTest::TearDownTestCase()
47 {}
48 
SetUp()49 void AaCommandStartModuleTest::SetUp()
50 {
51     // reset optind to 0
52     optind = 0;
53 
54     // make mock objects
55     MakeMockObjects();
56 }
57 
TearDown()58 void AaCommandStartModuleTest::TearDown()
59 {}
60 
MakeMockObjects() const61 void AaCommandStartModuleTest::MakeMockObjects() const
62 {
63     // mock a stub
64     auto managerStubPtr = sptr<IRemoteObject>(new MockAbilityManagerStub());
65 
66     // set the mock stub
67     auto managerClientPtr = AbilityManagerClient::GetInstance();
68     managerClientPtr->remoteObject_ = managerStubPtr;
69 }
70 
71 /**
72  * @tc.number: Aa_Command_Start_ModuleTest_0100
73  * @tc.name: ExecCommand
74  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name>" command.
75  */
76 HWTEST_F(AaCommandStartModuleTest, Aa_Command_Start_ModuleTest_0100, Function | MediumTest | Level1)
77 {
78     char *argv[] = {
79         (char *)TOOL_NAME.c_str(),
80         (char *)cmd_.c_str(),
81         (char *)"-d",
82         (char *)STRING_DEVICE.c_str(),
83         (char *)"-a",
84         (char *)STRING_ABILITY_NAME.c_str(),
85         (char *)"-b",
86         (char *)STRING_BUNDLE_NAME.c_str(),
87         (char *)"",
88     };
89     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
90 
91     AbilityManagerShellCommand cmd(argc, argv);
92     EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_OK + "\n");
93 }
94 
95 /**
96  * @tc.number: Aa_Command_Start_ModuleTest_0200
97  * @tc.name: ExecCommand
98  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name>" command.
99  */
100 HWTEST_F(AaCommandStartModuleTest, Aa_Command_Start_ModuleTest_0200, Function | MediumTest | Level1)
101 {
102     char *argv[] = {
103         (char *)TOOL_NAME.c_str(),
104         (char *)cmd_.c_str(),
105         (char *)"-d",
106         (char *)STRING_DEVICE.c_str(),
107         (char *)"-a",
108         (char *)STRING_ABILITY_NAME_INVALID.c_str(),
109         (char *)"-b",
110         (char *)STRING_BUNDLE_NAME.c_str(),
111         (char *)"",
112     };
113     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
114 
115     AbilityManagerShellCommand cmd(argc, argv);
116     EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_NG + "\n" + cmd.messageMap_.at(RESOLVE_ABILITY_ERR) + "\n");
117 }
118 
119 /**
120  * @tc.number: Aa_Command_Start_ModuleTest_0300
121  * @tc.name: ExecCommand
122  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name>" command.
123  */
124 HWTEST_F(AaCommandStartModuleTest, Aa_Command_Start_ModuleTest_0300, Function | MediumTest | Level1)
125 {
126     char *argv[] = {
127         (char *)TOOL_NAME.c_str(),
128         (char *)cmd_.c_str(),
129         (char *)"-d",
130         (char *)STRING_DEVICE.c_str(),
131         (char *)"-a",
132         (char *)STRING_ABILITY_NAME.c_str(),
133         (char *)"-b",
134         (char *)STRING_BUNDLE_NAME_INVALID.c_str(),
135         (char *)"",
136     };
137     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
138 
139     AbilityManagerShellCommand cmd(argc, argv);
140     EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_NG + "\n" + cmd.messageMap_.at(RESOLVE_APP_ERR) + "\n");
141 }
142 
143 /**
144  * @tc.number: Aa_Command_Start_ModuleTest_0400
145  * @tc.name: ExecCommand
146  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name> -D" command.
147  * @tc.type: FUNC
148  * @tc.require: AR000GJUN4
149  */
150 HWTEST_F(AaCommandStartModuleTest, Aa_Command_Start_ModuleTest_0400, Function | MediumTest | Level1)
151 {
152     char *argv[] = {
153         (char *)TOOL_NAME.c_str(),
154         (char *)cmd_.c_str(),
155         (char *)"-d",
156         (char *)STRING_DEVICE.c_str(),
157         (char *)"-a",
158         (char *)STRING_ABILITY_NAME.c_str(),
159         (char *)"-b",
160         (char *)STRING_BUNDLE_NAME.c_str(),
161         (char *)"-D",
162         (char *)"",
163     };
164     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
165 
166     AbilityManagerShellCommand cmd(argc, argv);
167     EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_OK + "\n");
168 }
169