1 /*
2 * Copyright (c) 2025 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 <vector>
17 #include <memory>
18
19 #include <unistd.h>
20
21 #include "device_manager.h"
22 #include <gtest/gtest.h>
23 #include "boomerang_dumper.h"
24
25 #include "devicestatus_define.h"
26 #include "devicestatus_errors.h"
27 #include "stationary_server.h"
28 #include "intention_service_test.h"
29 #include "ddm_adapter.h"
30 #include "dsoftbus_adapter.h"
31 #include "fi_log.h"
32 #include "input_adapter.h"
33 #include "intention_service.h"
34 #include "interaction_manager.h"
35 #include "ipc_skeleton.h"
36 #include "plugin_manager.h"
37
38 #undef LOG_TAG
39 #define LOG_TAG "BoomerangDumperTest"
40
41 namespace OHOS {
42 namespace Msdp {
43 namespace DeviceStatus {
44 using namespace testing::ext;
45 namespace {
46 int32_t g_fd { 1 };
47 BoomerangServer boomerang;
48 std::shared_ptr<ContextService> g_context { nullptr };
49 constexpr int32_t TIME_WAIT_FOR_OP_MS { 20 };
50 } // namespace
51
52 class BoomerangDumperTest : public testing::Test {
53 public:
54 void SetUp();
55 void TearDown();
56 static void SetUpTestCase();
57 static void TearDownTestCase(void);
58 };
SetUpTestCase()59 void BoomerangDumperTest::SetUpTestCase() {}
60
TearDownTestCase()61 void BoomerangDumperTest::TearDownTestCase() {}
62
SetUp()63 void BoomerangDumperTest::SetUp() {}
64
TearDown()65 void BoomerangDumperTest::TearDown()
66 {
67 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
68 }
69
PostSyncTask(DTaskCallback callback)70 int32_t MockDelegateTasks::PostSyncTask(DTaskCallback callback)
71 {
72 return callback();
73 }
74
PostAsyncTask(DTaskCallback callback)75 int32_t MockDelegateTasks::PostAsyncTask(DTaskCallback callback)
76 {
77 return callback();
78 }
79
GetInstance()80 ContextService* ContextService::GetInstance()
81 {
82 static std::once_flag flag;
83 std::call_once(flag, [&]() {
84 g_context = std::make_shared<ContextService>();
85 });
86 return g_context.get();
87 }
88
ContextService()89 ContextService::ContextService()
90 {
91 ddm_ = std::make_unique<DDMAdapter>();
92 input_ = std::make_unique<InputAdapter>();
93 pluginMgr_ = std::make_unique<PluginManager>(this);
94 dsoftbus_ = std::make_unique<DSoftbusAdapter>();
95 }
96
GetDelegateTasks()97 IDelegateTasks& ContextService::GetDelegateTasks()
98 {
99 return delegateTasks_;
100 }
101
GetDeviceManager()102 IDeviceManager& ContextService::GetDeviceManager()
103 {
104 return devMgr_;
105 }
106
GetTimerManager()107 ITimerManager& ContextService::GetTimerManager()
108 {
109 return timerMgr_;
110 }
111
GetDragManager()112 IDragManager& ContextService::GetDragManager()
113 {
114 return dragMgr_;
115 }
116
GetSocketSessionManager()117 ISocketSessionManager& ContextService::GetSocketSessionManager()
118 {
119 return socketSessionMgr_;
120 }
121
GetDDM()122 IDDMAdapter& ContextService::GetDDM()
123 {
124 return *ddm_;
125 }
126
GetPluginManager()127 IPluginManager& ContextService::GetPluginManager()
128 {
129 return *pluginMgr_;
130 }
131
GetInput()132 IInputAdapter& ContextService::GetInput()
133 {
134 return *input_;
135 }
136
GetDSoftbus()137 IDSoftbusAdapter& ContextService::GetDSoftbus()
138 {
139 return *dsoftbus_;
140 }
141
142 /**
143 * @tc.name: BoomerangDumperTestTest001
144 * @tc.desc: test Boomerang Dumper
145 * @tc.type: FUNC
146 * @tc.require:
147 */
148 HWTEST_F(BoomerangDumperTest, BoomerangDumperTest001, TestSize.Level1)
149 {
150 CALL_TEST_DEBUG;
151 auto env = ContextService::GetInstance();
152 ASSERT_NE(env, nullptr);
153 BoomerangDumper dumper = BoomerangDumper(env, boomerang);
154 std::vector<std::string> argList = {"s", "h", "l", "c", "d"};
155 ASSERT_NO_FATAL_FAILURE(dumper.Dump(g_fd, argList));
156 }
157
158 /**
159 * @tc.name: BoomerangDumperTestTest002
160 * @tc.desc: test Boomerang Dumper
161 * @tc.type: FUNC
162 * @tc.require:
163 */
164 HWTEST_F(BoomerangDumperTest, BoomerangDumperTest002, TestSize.Level1)
165 {
166 CALL_TEST_DEBUG;
167 auto env = ContextService::GetInstance();
168 ASSERT_NE(env, nullptr);
169 BoomerangDumper dumper = BoomerangDumper(env, boomerang);
170 int32_t value = 's';
171 ASSERT_NO_FATAL_FAILURE(dumper.DumpOnce(g_fd, value));
172 }
173
174 /**
175 * @tc.name: BoomerangDumperTest003
176 * @tc.desc: test Boomerang Dumper
177 * @tc.type: FUNC
178 * @tc.require:
179 */
180 HWTEST_F(BoomerangDumperTest, BoomerangDumperTest003, TestSize.Level1)
181 {
182 CALL_TEST_DEBUG;
183 auto env = ContextService::GetInstance();
184 ASSERT_NE(env, nullptr);
185 BoomerangDumper dumper = BoomerangDumper(env, boomerang);
186 int32_t value = 'l';
187 ASSERT_NO_FATAL_FAILURE(dumper.DumpOnce(g_fd, value));
188 }
189
190 /**
191 * @tc.name: BoomerangDumperTest004
192 * @tc.desc: test Boomerang Dumper
193 * @tc.type: FUNC
194 * @tc.require:
195 */
196 HWTEST_F(BoomerangDumperTest, BoomerangDumperTest004, TestSize.Level1)
197 {
198 CALL_TEST_DEBUG;
199 auto env = ContextService::GetInstance();
200 ASSERT_NE(env, nullptr);
201 BoomerangDumper dumper = BoomerangDumper(env, boomerang);
202 int32_t value = 'c';
203 ASSERT_NO_FATAL_FAILURE(dumper.DumpOnce(g_fd, value));
204 }
205
206 /**
207 * @tc.name: BoomerangDumperTest005
208 * @tc.desc: test Boomerang Dumper
209 * @tc.type: FUNC
210 * @tc.require:
211 */
212 HWTEST_F(BoomerangDumperTest, BoomerangDumperTest005, TestSize.Level1)
213 {
214 CALL_TEST_DEBUG;
215 auto env = ContextService::GetInstance();
216 ASSERT_NE(env, nullptr);
217 BoomerangDumper dumper = BoomerangDumper(env, boomerang);
218 int32_t value = 'd';
219 ASSERT_NO_FATAL_FAILURE(dumper.DumpOnce(g_fd, value));
220 }
221
222 /**
223 * @tc.name: BoomerangDumperTest006
224 * @tc.desc: test Boomerang Dumper
225 * @tc.type: FUNC
226 * @tc.require:
227 */
228 HWTEST_F(BoomerangDumperTest, BoomerangDumperTest006, TestSize.Level1)
229 {
230 CALL_TEST_DEBUG;
231 auto env = ContextService::GetInstance();
232 ASSERT_NE(env, nullptr);
233 BoomerangDumper dumper = BoomerangDumper(env, boomerang);
234 int32_t value = 'm';
235 ASSERT_NO_FATAL_FAILURE(dumper.DumpOnce(g_fd, value));
236 }
237
238 /**
239 * @tc.name: BoomerangDumperTest007
240 * @tc.desc: test Boomerang Dumper
241 * @tc.type: FUNC
242 * @tc.require:
243 */
244 HWTEST_F(BoomerangDumperTest, BoomerangDumperTest007, TestSize.Level1)
245 {
246 CALL_TEST_DEBUG;
247 auto env = ContextService::GetInstance();
248 ASSERT_NE(env, nullptr);
249 BoomerangDumper dumper = BoomerangDumper(env, boomerang);
250 ASSERT_NO_FATAL_FAILURE(dumper.DumpOnce(g_fd, std::atoi("x")));
251 }
252
253 /**
254 * @tc.name: BoomerangDumperTest008
255 * @tc.desc: test Boomerang Dumper
256 * @tc.type: FUNC
257 * @tc.require:
258 */
259 HWTEST_F(BoomerangDumperTest, BoomerangDumperTest008, TestSize.Level1)
260 {
261 CALL_TEST_DEBUG;
262 auto env = ContextService::GetInstance();
263 ASSERT_NE(env, nullptr);
264 BoomerangDumper dumper = BoomerangDumper(env, boomerang);
265 std::vector<std::string> argList = {"s", "h", "l", "c", "d"};
266 ASSERT_NO_FATAL_FAILURE(dumper.Dump(g_fd, argList));
267 }
268 } // namespace DeviceStatus
269 } // namespace Msdp
270 } // namespace OHOS