• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include <gmock/gmock.h>
18 
19 #define protected public
20 #include "ability_command.h"
21 #undef protected
22 #include "mock_ability_manager_stub.h"
23 #define private public
24 #include "ability_manager_client.h"
25 #undef private
26 #include "ability_manager_interface.h"
27 
28 using namespace testing::ext;
29 using namespace OHOS;
30 using namespace OHOS::AAFwk;
31 using testing::_;
32 using testing::Invoke;
33 using testing::Return;
34 
35 namespace {
36 const std::string STRING_BUNDLE_NAME = "bundle";
37 } // namespace
38 
39 class AaCommandForceStopTest : public ::testing::Test {
40 public:
41     static void SetUpTestCase();
42     static void TearDownTestCase();
43     void SetUp() override;
44     void TearDown() override;
45 
46     void MakeMockObjects() const;
47 
48     std::string cmd_ = "force-stop";
49 };
50 
SetUpTestCase()51 void AaCommandForceStopTest::SetUpTestCase()
52 {}
53 
TearDownTestCase()54 void AaCommandForceStopTest::TearDownTestCase()
55 {}
56 
SetUp()57 void AaCommandForceStopTest::SetUp()
58 {
59     // reset optind to 0
60     optind = 0;
61 
62     // make mock objects
63     MakeMockObjects();
64 }
65 
TearDown()66 void AaCommandForceStopTest::TearDown()
67 {}
68 
MakeMockObjects() const69 void AaCommandForceStopTest::MakeMockObjects() const
70 {
71     // mock a stub
72     auto managerStubPtr = sptr<IAbilityManager>(new MockAbilityManagerStub());
73 
74     // set the mock stub
75     auto managerClientPtr = AbilityManagerClient::GetInstance();
76     managerClientPtr->proxy_ = managerStubPtr;
77 }
78 
79 /**
80  * @tc.number: Aa_Command_Force_Stop_0100
81  * @tc.name: ExecCommand
82  * @tc.desc: Verify the "aa force-stop" command.
83  */
84 HWTEST_F(AaCommandForceStopTest, Aa_Command_Force_Stop_0100, Function | MediumTest | Level1)
85 {
86     char* argv[] = {
87         (char*)TOOL_NAME.c_str(),
88         (char*)cmd_.c_str(),
89         (char*)"",
90     };
91     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
92 
93     AbilityManagerShellCommand cmd(argc, argv);
94     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_FORCE_STOP);
95 }
96 
97 /**
98  * @tc.number: Aa_Command_Force_Stop_0200
99  * @tc.name: ExecCommand
100  * @tc.desc: Verify the "aa force-stop xxx" command.
101  */
102 HWTEST_F(AaCommandForceStopTest, Aa_Command_Force_Stop_0200, Function | MediumTest | Level1)
103 {
104     char* argv[] = {
105         (char*)TOOL_NAME.c_str(),
106         (char*)cmd_.c_str(),
107         (char*)"xxx",
108         (char*)"",
109     };
110     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
111     AbilityManagerShellCommand cmd(argc, argv);
112 
113     auto managerClientPtr = AbilityManagerClient::GetInstance();
114     auto mockAbilityManagerStub = sptr<MockAbilityManagerStub>(new MockAbilityManagerStub());
115     ASSERT_NE(mockAbilityManagerStub, nullptr);
116     EXPECT_CALL(*mockAbilityManagerStub, KillProcess(_, _, _))
117         .Times(1)
118         .WillOnce(Return(-1));
119     managerClientPtr->proxy_ = static_cast<IAbilityManager*>(mockAbilityManagerStub);
120 
121     EXPECT_EQ(cmd.ExecCommand(), STRING_FORCE_STOP_NG + "\n");
122     managerClientPtr->proxy_ = nullptr; // release MockAbilityManagerStub force
123     testing::Mock::AllowLeak(mockAbilityManagerStub);
124 }
125 
126 /**
127  * @tc.number: Aa_Command_Force_Stop_0300
128  * @tc.name: ExecCommand
129  * @tc.desc: Verify the "aa force-stop bundle" command.
130  */
131 HWTEST_F(AaCommandForceStopTest, Aa_Command_Force_Stop_0300, Function | MediumTest | Level1)
132 {
133     char* argv[] = {
134         (char*)TOOL_NAME.c_str(),
135         (char*)cmd_.c_str(),
136         (char*)"STRING_BUNDLE_NAME",
137         (char*)"",
138     };
139     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
140     AbilityManagerShellCommand cmd(argc, argv);
141 
142     auto managerClientPtr = AbilityManagerClient::GetInstance();
143     auto mockAbilityManagerStub = sptr<MockAbilityManagerStub>(new MockAbilityManagerStub());
144     ASSERT_NE(mockAbilityManagerStub, nullptr);
145     EXPECT_CALL(*mockAbilityManagerStub, KillProcess(_, _, _))
146         .Times(1)
147         .WillOnce(Return(0));
148     managerClientPtr->proxy_ = static_cast<IAbilityManager*>(mockAbilityManagerStub);
149 
150     EXPECT_EQ(cmd.ExecCommand(), STRING_FORCE_STOP_OK + "\n");
151     managerClientPtr->proxy_ = nullptr; // release MockAbilityManagerStub force
152     testing::Mock::AllowLeak(mockAbilityManagerStub);
153 }