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 #ifdef ABILITY_COMMAND_FOR_TEST
132 /**
133 * @tc.number: Aa_Command_Force_Timeout_0400
134 * @tc.name: ExecCommand
135 * @tc.desc: Verify the "aa force-timeout xxx INITIAL" command.
136 */
137 HWTEST_F(AaCommandForceTimeOut, Aa_Command_Force_Timeout_0400, Function | MediumTest | Level1)
138 {
139 char* argv[] = {
140 (char*)TOOL_NAME.c_str(),
141 (char*)cmd_.c_str(),
142 (char*)"clean",
143 (char*)"",
144 };
145 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
146 AbilityManagerShellCommand cmd(argc, argv);
147
148 auto managerClientPtr = AbilityManagerClient::GetInstance();
149 auto mockAbilityManagerStub = sptr<MockAbilityManagerStub>(new MockAbilityManagerStub());
150 ASSERT_NE(mockAbilityManagerStub, nullptr);
151 EXPECT_CALL(*mockAbilityManagerStub, ForceTimeoutForTest(_, _))
152 .Times(1)
153 .WillOnce(Return(-1));
154 managerClientPtr->proxy_ = static_cast<IAbilityManager*>(mockAbilityManagerStub);
155
156 EXPECT_EQ(cmd.ExecCommand(), STRING_FORCE_TIMEOUT_NG + "\n");
157 testing::Mock::AllowLeak(mockAbilityManagerStub);
158 }
159
160 /**
161 * @tc.number: Aa_Command_Force_Timeout_0500
162 * @tc.name: ExecCommand
163 * @tc.desc: Verify the "aa force-timeout ability INITIAL" command.
164 */
165 HWTEST_F(AaCommandForceTimeOut, Aa_Command_Force_Timeout_0500, Function | MediumTest | Level1)
166 {
167 char* argv[] = {
168 (char*)TOOL_NAME.c_str(),
169 (char*)cmd_.c_str(),
170 (char*)"clean",
171 (char*)"",
172 };
173 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
174 AbilityManagerShellCommand cmd(argc, argv);
175
176 auto managerClientPtr = AbilityManagerClient::GetInstance();
177 auto mockAbilityManagerStub = sptr<MockAbilityManagerStub>(new MockAbilityManagerStub());
178 ASSERT_NE(mockAbilityManagerStub, nullptr);
179 EXPECT_CALL(*mockAbilityManagerStub, ForceTimeoutForTest(_, _))
180 .Times(1)
181 .WillOnce(Return(0));
182 managerClientPtr->proxy_ = static_cast<IAbilityManager*>(mockAbilityManagerStub);
183
184 EXPECT_EQ(cmd.ExecCommand(), STRING_FORCE_TIMEOUT_OK + "\n");
185 testing::Mock::AllowLeak(mockAbilityManagerStub);
186 }
187 #endif