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 class AaCommandForceTimeOut : public ::testing::Test {
36 public:
37 static void SetUpTestCase();
38 static void TearDownTestCase();
39 void SetUp() override;
40 void TearDown() override;
41
42 void MakeMockObjects() const;
43
44 std::string cmd_ = "force-timeout";
45 };
46
SetUpTestCase()47 void AaCommandForceTimeOut::SetUpTestCase()
48 {}
49
TearDownTestCase()50 void AaCommandForceTimeOut::TearDownTestCase()
51 {}
52
SetUp()53 void AaCommandForceTimeOut::SetUp()
54 {
55 // reset optind to 0
56 optind = 0;
57
58 // make mock objects
59 MakeMockObjects();
60 }
61
TearDown()62 void AaCommandForceTimeOut::TearDown()
63 {}
64
MakeMockObjects() const65 void AaCommandForceTimeOut::MakeMockObjects() const
66 {
67 // mock a stub
68 auto managerStubPtr = sptr<IAbilityManager>(new MockAbilityManagerStub());
69
70 // set the mock stub
71 auto managerClientPtr = AbilityManagerClient::GetInstance();
72 managerClientPtr->proxy_ = managerStubPtr;
73 }
74
75 /**
76 * @tc.number: Aa_Command_Force_Timeout_0100
77 * @tc.name: ExecCommand
78 * @tc.desc: Verify the "aa force-timeout" command.
79 */
80 HWTEST_F(AaCommandForceTimeOut, Aa_Command_Force_Timeout_0100, Function | MediumTest | Level1)
81 {
82 char* argv[] = {
83 (char*)TOOL_NAME.c_str(),
84 (char*)cmd_.c_str(),
85 (char*)"",
86 };
87 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
88
89 AbilityManagerShellCommand cmd(argc, argv);
90 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_FORCE_TIMEOUT + "\n");
91 }
92
93 /**
94 * @tc.number: Aa_Command_Force_Timeout_0200
95 * @tc.name: ExecCommand
96 * @tc.desc: Verify the "aa force-timeout xxx" command.
97 */
98 HWTEST_F(AaCommandForceTimeOut, Aa_Command_Force_Timeout_0200, Function | MediumTest | Level1)
99 {
100 char* argv[] = {
101 (char*)TOOL_NAME.c_str(),
102 (char*)cmd_.c_str(),
103 (char*)"xxx",
104 (char*)"",
105 };
106 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
107
108 AbilityManagerShellCommand cmd(argc, argv);
109 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_FORCE_TIMEOUT + "\n");
110 }
111
112 /**
113 * @tc.number: Aa_Command_Force_Timeout_0300
114 * @tc.name: ExecCommand
115 * @tc.desc: Verify the "aa force-timeout clean" command.
116 */
117 HWTEST_F(AaCommandForceTimeOut, Aa_Command_Force_Timeout_0300, Function | MediumTest | Level1)
118 {
119 char* argv[] = {
120 (char*)TOOL_NAME.c_str(),
121 (char*)cmd_.c_str(),
122 (char*)"clean",
123 (char*)"",
124 };
125 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
126
127 AbilityManagerShellCommand cmd(argc, argv);
128 EXPECT_EQ(cmd.ExecCommand(), STRING_FORCE_TIMEOUT_OK + "\n");
129 }
130
131 /**
132 * @tc.number: Aa_Command_Force_Timeout_0400
133 * @tc.name: ExecCommand
134 * @tc.desc: Verify the "aa force-timeout xxx INITIAL" command.
135 */
136 HWTEST_F(AaCommandForceTimeOut, Aa_Command_Force_Timeout_0400, Function | MediumTest | Level1)
137 {
138 char* argv[] = {
139 (char*)TOOL_NAME.c_str(),
140 (char*)cmd_.c_str(),
141 (char*)"clean",
142 (char*)"",
143 };
144 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
145 AbilityManagerShellCommand cmd(argc, argv);
146
147 auto managerClientPtr = AbilityManagerClient::GetInstance();
148 auto mockAbilityManagerStub = sptr<MockAbilityManagerStub>(new MockAbilityManagerStub());
149 ASSERT_NE(mockAbilityManagerStub, nullptr);
150 EXPECT_CALL(*mockAbilityManagerStub, ForceTimeoutForTest(_, _))
151 .Times(1)
152 .WillOnce(Return(-1));
153 managerClientPtr->proxy_ = static_cast<IAbilityManager*>(mockAbilityManagerStub);
154
155 EXPECT_EQ(cmd.ExecCommand(), STRING_FORCE_TIMEOUT_NG + "\n");
156 testing::Mock::AllowLeak(mockAbilityManagerStub);
157 }
158
159 /**
160 * @tc.number: Aa_Command_Force_Timeout_0500
161 * @tc.name: ExecCommand
162 * @tc.desc: Verify the "aa force-timeout ability INITIAL" command.
163 */
164 HWTEST_F(AaCommandForceTimeOut, Aa_Command_Force_Timeout_0500, Function | MediumTest | Level1)
165 {
166 char* argv[] = {
167 (char*)TOOL_NAME.c_str(),
168 (char*)cmd_.c_str(),
169 (char*)"clean",
170 (char*)"",
171 };
172 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
173 AbilityManagerShellCommand cmd(argc, argv);
174
175 auto managerClientPtr = AbilityManagerClient::GetInstance();
176 auto mockAbilityManagerStub = sptr<MockAbilityManagerStub>(new MockAbilityManagerStub());
177 ASSERT_NE(mockAbilityManagerStub, nullptr);
178 EXPECT_CALL(*mockAbilityManagerStub, ForceTimeoutForTest(_, _))
179 .Times(1)
180 .WillOnce(Return(0));
181 managerClientPtr->proxy_ = static_cast<IAbilityManager*>(mockAbilityManagerStub);
182
183 EXPECT_EQ(cmd.ExecCommand(), STRING_FORCE_TIMEOUT_OK + "\n");
184 testing::Mock::AllowLeak(mockAbilityManagerStub);
185 }