1 /*
2 * Copyright (c) 2023 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 #define private public
19 #include "app_debug_manager.h"
20 #include "app_debug_listener_proxy.h"
21 #undef private
22
23 #include "mock_app_debug_listener_stub.h"
24 #include "parcel.h"
25
26 using namespace testing;
27 using namespace testing::ext;
28 namespace OHOS {
29 namespace AppExecFwk {
30 namespace {
31 std::string DEBUG_START_NAME = "debugStartBundle";
32 std::string NO_DEBUG_START_NAME = "noDebugStartBundle";
33 const bool IS_DEBUG_START = true;
34 const bool NO_DEBUG_START = false;
35 const unsigned int SIZE_ONE = 1;
36 const unsigned int SIZE_TWO = 2;
37 }
38 class AppDebugManagerTest : public testing::Test {
39 public:
40 static void SetUpTestCase();
41 static void TearDownTestCase();
42 void SetUp() override;
43 void TearDown() override;
44
45 std::shared_ptr<AppDebugManager> manager_;
46 sptr<MockAppDebugListenerStub> listener_;
47 };
48
SetUpTestCase(void)49 void AppDebugManagerTest::SetUpTestCase(void)
50 {}
51
TearDownTestCase(void)52 void AppDebugManagerTest::TearDownTestCase(void)
53 {}
54
SetUp()55 void AppDebugManagerTest::SetUp()
56 {
57 manager_ = std::make_shared<AppDebugManager>();
58 listener_ = new MockAppDebugListenerStub();
59 manager_->listeners_.insert(listener_);
60 }
61
TearDown()62 void AppDebugManagerTest::TearDown()
63 {}
64
65 /**
66 * @tc.name: RegisterAppDebugListener_0100
67 * @tc.desc: Register listener for app debug listener, check nullptr listener.
68 * @tc.type: FUNC
69 */
70 HWTEST_F(AppDebugManagerTest, RegisterAppDebugListener_0100, TestSize.Level1)
71 {
72 sptr<MockAppDebugListenerStub> listener = new MockAppDebugListenerStub();
73 AppDebugInfo appDebugInfo;
74 manager_->debugInfos_.push_back(appDebugInfo);
75
76 EXPECT_CALL(*listener, OnAppDebugStarted(_)).Times(1);
77 auto result = manager_->RegisterAppDebugListener(listener);
78 EXPECT_EQ(result, ERR_OK);
79 EXPECT_EQ(manager_->listeners_.size(), SIZE_TWO);
80
81 listener = nullptr;
82 result = manager_->RegisterAppDebugListener(listener);
83 EXPECT_EQ(result, ERR_INVALID_DATA);
84 EXPECT_EQ(manager_->listeners_.size(), SIZE_TWO);
85 }
86
87 /**
88 * @tc.name: UnregisterAppDebugListener_0100
89 * @tc.desc: Unregister listener for app debug listener, check nullptr listener.
90 * @tc.type: FUNC
91 */
92 HWTEST_F(AppDebugManagerTest, UnregisterAppDebugListener_0100, TestSize.Level1)
93 {
94 EXPECT_NE(manager_, nullptr);
95 sptr<MockAppDebugListenerStub> listener = nullptr;
96 auto result = manager_->UnregisterAppDebugListener(listener);
97 EXPECT_EQ(result, ERR_INVALID_DATA);
98 EXPECT_EQ(manager_->listeners_.size(), SIZE_ONE);
99
100 EXPECT_NE(listener_, nullptr);
101 result = manager_->UnregisterAppDebugListener(listener_);
102 EXPECT_EQ(result, ERR_OK);
103 EXPECT_TRUE(manager_->listeners_.empty());
104 }
105
106 /**
107 * @tc.name: StartDebug_0100
108 * @tc.desc: Start debug by AppDebugInfo, notify AppDebugListener.
109 * @tc.type: FUNC
110 */
111 HWTEST_F(AppDebugManagerTest, StartDebug_0100, TestSize.Level1)
112 {
113 EXPECT_NE(manager_, nullptr);
114 EXPECT_TRUE(manager_->debugInfos_.empty());
115 std::vector<AppDebugInfo> debugInfos;
116 AppDebugInfo info;
117 debugInfos.push_back(info);
118
119 EXPECT_NE(listener_, nullptr);
120 EXPECT_CALL(*listener_, OnAppDebugStarted(_)).Times(1);
121 manager_->StartDebug(debugInfos);
122 EXPECT_FALSE(manager_->debugInfos_.empty());
123 }
124
125 /**
126 * @tc.name: StopDebug_0100
127 * @tc.desc: Start debug by AppDebugInfo, notify AppDebugListener.
128 * @tc.type: FUNC
129 */
130 HWTEST_F(AppDebugManagerTest, StopDebug_0100, TestSize.Level1)
131 {
132 EXPECT_NE(manager_, nullptr);
133 EXPECT_TRUE(manager_->debugInfos_.empty());
134 std::vector<AppDebugInfo> debugInfos;
135 AppDebugInfo info;
136 info.bundleName = DEBUG_START_NAME;
137 info.isDebugStart = IS_DEBUG_START;
138 info.pid = 10;
139 info.uid = 12345;
140 debugInfos.push_back(info);
141 manager_->debugInfos_ = debugInfos;
142
143 EXPECT_NE(listener_, nullptr);
144 EXPECT_CALL(*listener_, OnAppDebugStoped(_)).Times(1);
145 manager_->StopDebug(debugInfos);
146 EXPECT_TRUE(manager_->debugInfos_.empty());
147 }
148
149 /**
150 * @tc.name: IsAttachDebug_0100
151 * @tc.desc: Given the bundleName, return true if not DebugStart, otherwise return false.
152 * @tc.type: FUNC
153 */
154 HWTEST_F(AppDebugManagerTest, IsAttachDebug_0100, TestSize.Level1)
155 {
156 EXPECT_NE(manager_, nullptr);
157 AppDebugInfo debugStart_info;
158 debugStart_info.bundleName = DEBUG_START_NAME;
159 debugStart_info.isDebugStart = IS_DEBUG_START;
160 manager_->debugInfos_.push_back(debugStart_info);
161
162 AppDebugInfo noDebugStart_info;
163 noDebugStart_info.bundleName = NO_DEBUG_START_NAME;
164 noDebugStart_info.isDebugStart = NO_DEBUG_START;
165 manager_->debugInfos_.push_back(noDebugStart_info);
166
167 auto result = manager_->IsAttachDebug(DEBUG_START_NAME);
168 EXPECT_FALSE(result);
169
170 result = manager_->IsAttachDebug(NO_DEBUG_START_NAME);
171 EXPECT_TRUE(result);
172 }
173
174 /**
175 * @tc.name: GetIncrementAppDebugInfo_0100
176 * @tc.desc: Add new debug info into debugInfos, or update the isDebugStart flag.
177 * @tc.type: FUNC
178 */
179 HWTEST_F(AppDebugManagerTest, GetIncrementAppDebugInfo_0100, TestSize.Level1)
180 {
181 EXPECT_NE(manager_, nullptr);
182 EXPECT_TRUE(manager_->debugInfos_.empty());
183
184 std::vector<AppDebugInfo> debugInfos;
185 std::vector<AppDebugInfo> increment;
186 AppDebugInfo debugStart_info;
187 debugStart_info.bundleName = DEBUG_START_NAME;
188 debugStart_info.isDebugStart = IS_DEBUG_START;
189
190 int pid = 10;
191 int uid = 12345;
192 AppDebugInfo noDebugStart_info;
193 noDebugStart_info.bundleName = NO_DEBUG_START_NAME;
194 noDebugStart_info.isDebugStart = NO_DEBUG_START;
195 noDebugStart_info.pid = pid;
196 noDebugStart_info.pid = uid;
197 debugInfos.push_back(debugStart_info);
198 debugInfos.push_back(noDebugStart_info);
199
200 manager_->GetIncrementAppDebugInfos(debugInfos, increment);
201 EXPECT_EQ(manager_->debugInfos_.size(), SIZE_TWO);
202
203 increment.clear();
204 debugInfos.clear();
205 noDebugStart_info.isDebugStart = IS_DEBUG_START;
206 debugInfos.push_back(noDebugStart_info);
207
208 manager_->GetIncrementAppDebugInfos(debugInfos, increment);
209 EXPECT_EQ(manager_->debugInfos_.size(), SIZE_TWO);
210 EXPECT_TRUE(manager_->debugInfos_.at(1).isDebugStart);
211 }
212
213 /**
214 * @tc.name: RemoveAppDebugInfo_0100
215 * @tc.desc: Remove app debug info with bundleName, pid, uid and isDebugStart flag.
216 * @tc.type: FUNC
217 */
218 HWTEST_F(AppDebugManagerTest, RemoveAppDebugInfo_0100, TestSize.Level1)
219 {
220 EXPECT_NE(manager_, nullptr);
221 EXPECT_TRUE(manager_->debugInfos_.empty());
222
223 std::vector<AppDebugInfo> debugInfos;
224 int pid = 10;
225 int uid = 12345;
226 AppDebugInfo debugInfo;
227 debugInfo.bundleName = DEBUG_START_NAME;
228 debugInfo.pid = pid;
229 debugInfo.uid = uid;
230 debugInfo.isDebugStart = IS_DEBUG_START;
231
232 manager_->debugInfos_.push_back(debugInfo);
233 EXPECT_EQ(manager_->debugInfos_.size(), SIZE_ONE);
234
235 EXPECT_CALL(*listener_, OnAppDebugStoped(_)).Times(1);
236 manager_->RemoveAppDebugInfo(debugInfo);
237 EXPECT_TRUE(manager_->debugInfos_.empty());
238 }
239 } // namespace AppExecFwk
240 } // namespace OHOS
241