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 #include <thread>
19 #include "ability_command.h"
20 #include "ability_manager_client.h"
21 #include "ability_manager_interface.h"
22 #include "tool_system_test.h"
23 
24 using namespace testing::ext;
25 using namespace OHOS;
26 using namespace OHOS::AAFwk;
27 using namespace OHOS::AppExecFwk;
28 
29 namespace {
30 const std::string STRING_DEVICE_NAME = "device";
31 
32 const std::string STRING_PAGE_ABILITY_BUNDLE_PATH = "/data/test/resource/aa/pageAbilityBundleForStart.hap";
33 const std::string STRING_PAGE_ABILITY_BUNDLE_NAME = "com.ohos.tools.pageAbilityBundleForStart";
34 const std::string STRING_PAGE_ABILITY_BUNDLE_NAME_INVALID = STRING_PAGE_ABILITY_BUNDLE_NAME + ".invalid";
35 const std::string STRING_PAGE_ABILITY_NAME = "com.ohos.tools.pageAbilityForStart.MainAbility";
36 const std::string STRING_PAGE_ABILITY_NAME_INVALID = STRING_PAGE_ABILITY_NAME + ".Invalid";
37 
38 const std::string STRING_SERVICE_ABILITY_BUNDLE_PATH = "/data/test/resource/aa/serviceAbilityBundleForStart.hap";
39 const std::string STRING_SERVICE_ABILITY_BUNDLE_NAME = "com.ohos.tools.serviceAbilityBundleForStart";
40 const std::string STRING_SERVICE_ABILITY_BUNDLE_NAME_INVALID = STRING_SERVICE_ABILITY_BUNDLE_NAME + ".invalid";
41 const std::string STRING_SERVICE_ABILITY_NAME = "com.ohos.tools.serviceAbilityBundleForStart.ServiceAbility";
42 const std::string STRING_SERVICE_ABILITY_NAME_INVALID = STRING_SERVICE_ABILITY_NAME + ".Invalid";
43 }  // namespace
44 
45 class AaCommandStartSystemTest : public ::testing::Test {
46 public:
47     static void SetUpTestCase();
48     static void TearDownTestCase();
49     void SetUp() override;
50     void TearDown() override;
51 };
52 
SetUpTestCase()53 void AaCommandStartSystemTest::SetUpTestCase()
54 {}
55 
TearDownTestCase()56 void AaCommandStartSystemTest::TearDownTestCase()
57 {}
58 
SetUp()59 void AaCommandStartSystemTest::SetUp()
60 {
61     // reset optind to 0
62     optind = 0;
63 }
64 
TearDown()65 void AaCommandStartSystemTest::TearDown()
66 {}
67 
68 /**
69  * @tc.number: Aa_Command_Start_SystemTest_0100
70  * @tc.name: ExecCommand
71  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name>" command.
72  */
73 HWTEST_F(AaCommandStartSystemTest, Aa_Command_Start_SystemTest_0100, Function | MediumTest | Level1)
74 {
75     // uninstall the bundle
76     ToolSystemTest::UninstallBundle(STRING_PAGE_ABILITY_BUNDLE_NAME);
77 
78     // install the bundle
79     ToolSystemTest::InstallBundle(STRING_PAGE_ABILITY_BUNDLE_PATH, true);
80 
81     // start the page ability
82     std::string command = "aa start -d " + STRING_DEVICE_NAME + " -a " + STRING_PAGE_ABILITY_NAME + " -b " +
83         STRING_PAGE_ABILITY_BUNDLE_NAME;
84     std::string commandResult = ToolSystemTest::ExecuteCommand(command);
85 
86     EXPECT_PRED2(ToolSystemTest::IsSubSequence, commandResult, STRING_START_ABILITY_OK + "\n");
87 
88     // uninstall the bundle
89     ToolSystemTest::UninstallBundle(STRING_PAGE_ABILITY_BUNDLE_NAME);
90 }
91 
92 /**
93  * @tc.number: Aa_Command_Start_SystemTest_0300
94  * @tc.name: ExecCommand
95  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name>" command.
96  */
97 HWTEST_F(AaCommandStartSystemTest, Aa_Command_Start_SystemTest_0300, Function | MediumTest | Level1)
98 {
99     // start the invalid page ability
100     std::string command = "aa start -d " + STRING_DEVICE_NAME + " -a " + STRING_PAGE_ABILITY_NAME_INVALID + " -b " +
101         STRING_PAGE_ABILITY_BUNDLE_NAME_INVALID;
102     std::string commandResult = ToolSystemTest::ExecuteCommand(command);
103 
104     EXPECT_PRED2(ToolSystemTest::IsSubSequence, commandResult, STRING_START_ABILITY_NG + "\n");
105 }
106 
107 /**
108  * @tc.number: Aa_Command_Start_SystemTest_0400
109  * @tc.name: ExecCommand
110  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name>" command.
111  */
112 HWTEST_F(AaCommandStartSystemTest, Aa_Command_Start_SystemTest_0400, Function | MediumTest | Level1)
113 {
114     // start the invalid service ability
115     std::string command = "aa start -d " + STRING_DEVICE_NAME + " -a " + STRING_SERVICE_ABILITY_NAME_INVALID + " -b " +
116         STRING_SERVICE_ABILITY_BUNDLE_NAME_INVALID;
117     std::string commandResult = ToolSystemTest::ExecuteCommand(command);
118 
119     EXPECT_PRED2(ToolSystemTest::IsSubSequence, commandResult, STRING_START_ABILITY_NG + "\n");
120 }
121 
122 /**
123  * @tc.number: Aa_Command_Start_SystemTest_0500
124  * @tc.name: ExecCommand
125  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name> -D" command.
126  * @tc.type: FUNC
127  */
128 HWTEST_F(AaCommandStartSystemTest, Aa_Command_Start_SystemTest_0500, Function | MediumTest | Level1)
129 {
130     // uninstall the bundle
131     ToolSystemTest::UninstallBundle(STRING_PAGE_ABILITY_BUNDLE_NAME);
132 
133     // install the bundle
134     ToolSystemTest::InstallBundle(STRING_PAGE_ABILITY_BUNDLE_PATH, true);
135 
136     // start the page ability
137     std::string command = "aa start -d " + STRING_DEVICE_NAME + " -a " + STRING_PAGE_ABILITY_NAME + " -b " +
138         STRING_PAGE_ABILITY_BUNDLE_NAME + " -D";
139     std::string commandResult = ToolSystemTest::ExecuteCommand(command);
140 
141     EXPECT_PRED2(ToolSystemTest::IsSubSequence, commandResult, STRING_START_ABILITY_OK + "\n");
142 
143     // uninstall the bundle
144     ToolSystemTest::UninstallBundle(STRING_PAGE_ABILITY_BUNDLE_NAME);
145 }
146 
147 /**
148  * @tc.number: Aa_Command_Start_SystemTest_0600
149  * @tc.name: ExecCommand
150  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name> -D" command.
151  * @tc.type: FUNC
152  */
153 HWTEST_F(AaCommandStartSystemTest, Aa_Command_Start_SystemTest_0600, Function | MediumTest | Level1)
154 {
155     // start the invalid page ability
156     std::string command = "aa start -d " + STRING_DEVICE_NAME + " -a " + STRING_PAGE_ABILITY_NAME_INVALID + " -b " +
157         STRING_PAGE_ABILITY_BUNDLE_NAME_INVALID + " -D";
158     std::string commandResult = ToolSystemTest::ExecuteCommand(command);
159 
160     EXPECT_PRED2(ToolSystemTest::IsSubSequence, commandResult, STRING_START_ABILITY_NG + "\n");
161 }
162