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 "bundle_command.h"
21 #include "tool_system_test.h"
22
23 using namespace testing::ext;
24 using namespace OHOS;
25 using namespace OHOS::AAFwk;
26 using namespace OHOS::AppExecFwk;
27
28 namespace {
29 const std::string STRING_DEVICE_NAME = "device";
30
31 const std::string STRING_SERVICE_ABILITY_BUNDLE_PATH = "/data/test/resource/aa/serviceAbilityBundleForStop.hap";
32 const std::string STRING_SERVICE_ABILITY_BUNDLE_NAME = "com.ohos.tools.serviceAbilityBundleForStop";
33 const std::string STRING_SERVICE_ABILITY_BUNDLE_NAME_INVALID = STRING_SERVICE_ABILITY_BUNDLE_NAME + ".invalid";
34 const std::string STRING_SERVICE_ABILITY_NAME = "MainAbility";
35 const std::string STRING_SERVICE_ABILITY_NAME_INVALID = STRING_SERVICE_ABILITY_NAME + ".Invalid";
36 } // namespace
37
38 class AaCommandStopServiceSystemTest : public ::testing::Test {
39 public:
40 static void SetUpTestCase();
41 static void TearDownTestCase();
42 void SetUp() override;
43 void TearDown() override;
44 };
45
SetUpTestCase()46 void AaCommandStopServiceSystemTest::SetUpTestCase()
47 {}
48
TearDownTestCase()49 void AaCommandStopServiceSystemTest::TearDownTestCase()
50 {}
51
SetUp()52 void AaCommandStopServiceSystemTest::SetUp()
53 {
54 // reset optind to 0
55 optind = 0;
56 }
57
TearDown()58 void AaCommandStopServiceSystemTest::TearDown()
59 {}
60
61 /**
62 * @tc.number: Aa_Command_StopService_SystemTest_0100
63 * @tc.name: ExecCommand
64 * @tc.desc: Verify the "aa stop-service -d <device-id> -a <ability-name> -b <bundle-name>" command.
65 */
66 HWTEST_F(AaCommandStopServiceSystemTest, Aa_Command_StopService_SystemTest_0100, Function | MediumTest | Level1)
67 {
68 // uninstall the bundle
69 ToolSystemTest::UninstallBundle(STRING_SERVICE_ABILITY_BUNDLE_NAME);
70
71 // install the bundle
72 ToolSystemTest::InstallBundle(STRING_SERVICE_ABILITY_BUNDLE_PATH, true);
73
74 // start the service ability
75 ToolSystemTest::StartAbility(
76 STRING_DEVICE_NAME, STRING_SERVICE_ABILITY_NAME, STRING_SERVICE_ABILITY_BUNDLE_NAME, true);
77
78 // stop the service ability
79 std::string command = "aa stop-service -d " + STRING_DEVICE_NAME + " -a " + STRING_SERVICE_ABILITY_NAME + " -b " +
80 STRING_SERVICE_ABILITY_BUNDLE_NAME;
81 std::string commandResult = ToolSystemTest::ExecuteCommand(command);
82
83 EXPECT_PRED2(ToolSystemTest::IsSubSequence, commandResult, STRING_STOP_SERVICE_ABILITY_OK + "\n");
84
85 // uninstall the bundle
86 ToolSystemTest::UninstallBundle(STRING_SERVICE_ABILITY_BUNDLE_NAME);
87 }
88
89 /**
90 * @tc.number: Aa_Command_StopService_SystemTest_0200
91 * @tc.name: ExecCommand
92 * @tc.desc: Verify the "aa stop-service -d <device-id> -a <ability-name> -b <bundle-name>" command.
93 */
94 HWTEST_F(AaCommandStopServiceSystemTest, Aa_Command_StopService_SystemTest_0200, Function | MediumTest | Level1)
95 {
96 // stop the invalid service ability
97 std::string command = "aa stop-service -d " + STRING_DEVICE_NAME + " -a " + STRING_SERVICE_ABILITY_NAME_INVALID +
98 " -b " + STRING_SERVICE_ABILITY_BUNDLE_NAME_INVALID;
99 std::string commandResult = ToolSystemTest::ExecuteCommand(command);
100
101 EXPECT_PRED2(ToolSystemTest::IsSubSequence, commandResult, STRING_STOP_SERVICE_ABILITY_NG + "\n");
102 }
103