• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define BUFF_SIZE 100
17 #include "drag_client_test.h"
18 #include "ddm_adapter.h"
19 #include "devicestatus_service.h"
20 #include "drag_data_manager.h"
21 #include "drag_client.h"
22 #include "interaction_manager.h"
23 #include "ipc_skeleton.h"
24 #include "singleton.h"
25 
26 #include "accesstoken_kit.h"
27 #include "nativetoken_kit.h"
28 #include "token_setproc.h"
29 
30 namespace OHOS {
31 namespace Msdp {
32 namespace DeviceStatus {
33 using namespace testing::ext;
34 namespace {
35 constexpr int32_t TIME_WAIT_FOR_OP_MS { 20 };
36 constexpr int32_t PIXEL_MAP_WIDTH { 3 };
37 constexpr int32_t PIXEL_MAP_HEIGHT { 3 };
38 constexpr uint32_t DEFAULT_ICON_COLOR { 0xFF };
39 const std::string FILTER_INFO { "Undefined filter info" };
40 const std::string UD_KEY { "Unified data key" };
41 const std::string EXTRA_INFO { "Undefined extra info" };
42 const std::string CURVE_NAME { "cubic-bezier" };
43 constexpr int32_t DISPLAY_ID { 0 };
44 constexpr int32_t DISPLAY_X { 50 };
45 constexpr int32_t DISPLAY_Y { 50 };
46 constexpr int32_t INT32_BYTE { 4 };
47 int32_t g_shadowinfo_x { 0 };
48 int32_t g_shadowinfo_y { 0 };
49 ContextService *g_instance = nullptr;
50 DelegateTasks g_delegateTasks;
51 DeviceManager g_devMgr;
52 TimerManager g_timerMgr;
53 DragManager g_dragMgr;
54 DragClient g_dragClient;
55 SocketSessionManager g_socketSessionMgr;
56 std::unique_ptr<IInputAdapter> g_input { nullptr };
57 std::unique_ptr<IPluginManager> g_pluginMgr { nullptr };
58 std::unique_ptr<IDSoftbusAdapter> g_dsoftbus { nullptr };
59 constexpr int32_t ANIMATION_DURATION { 500 };
60 constexpr int32_t MAX_PIXEL_MAP_WIDTH { 600 };
61 constexpr int32_t MAX_PIXEL_MAP_HEIGHT { 600 };
62 constexpr bool HAS_CANCELED_ANIMATION { true };
63 std::shared_ptr<DragServer> g_dragServer { nullptr };
64 std::shared_ptr<DragServer> g_dragServerOne { nullptr };
65 IContext *g_context { nullptr };
66 IContext *g_contextOne { nullptr };
67 Security::AccessToken::HapInfoParams g_testInfoParms = {
68     .userID = 1,
69     .bundleName = "drag_server_test",
70     .instIndex = 0,
71     .appIDDesc = "test"
72 };
73 
74 Security::AccessToken::HapPolicyParams g_testPolicyPrams = {
75     .apl = Security::AccessToken::APL_NORMAL,
76     .domain = "test.domain",
77     .permList = {},
78     .permStateList = {}
79 };
80 } // namespace
81 
ContextService()82 ContextService::ContextService()
83 {
84     ddm_ = std::make_unique<DDMAdapter>();
85 }
86 
~ContextService()87 ContextService::~ContextService()
88 {
89 }
90 
GetDelegateTasks()91 IDelegateTasks& ContextService::GetDelegateTasks()
92 {
93     return g_delegateTasks;
94 }
95 
GetDeviceManager()96 IDeviceManager& ContextService::GetDeviceManager()
97 {
98     return g_devMgr;
99 }
100 
GetTimerManager()101 ITimerManager& ContextService::GetTimerManager()
102 {
103     return g_timerMgr;
104 }
105 
GetDragManager()106 IDragManager& ContextService::GetDragManager()
107 {
108     return g_dragMgr;
109 }
110 
GetInstance()111 ContextService* ContextService::GetInstance()
112 {
113     static std::once_flag flag;
114     std::call_once(flag, [&]() {
115         ContextService *cooContext = new (std::nothrow) ContextService();
116         CHKPL(cooContext);
117         g_instance = cooContext;
118     });
119     return g_instance;
120 }
121 
GetSocketSessionManager()122 ISocketSessionManager& ContextService::GetSocketSessionManager()
123 {
124     return g_socketSessionMgr;
125 }
126 
GetDDM()127 IDDMAdapter& ContextService::GetDDM()
128 {
129     return *ddm_;
130 }
131 
GetPluginManager()132 IPluginManager& ContextService::GetPluginManager()
133 {
134     return *g_pluginMgr;
135 }
136 
GetInput()137 IInputAdapter& ContextService::GetInput()
138 {
139     return *g_input;
140 }
141 
GetDSoftbus()142 IDSoftbusAdapter& ContextService::GetDSoftbus()
143 {
144     return *g_dsoftbus;
145 }
146 
SetUpTestCase()147 void DragClientTest::SetUpTestCase() {}
148 
SetUp()149 void DragClientTest::SetUp()
150 {
151     g_context = ContextService::GetInstance();
152     g_dragServer = std::make_shared<DragServer>(g_context);
153     g_dragServerOne = std::make_shared<DragServer>(g_contextOne);
154 }
155 
TearDown()156 void DragClientTest::TearDown()
157 {
158     g_dragServer = nullptr;
159     g_context = nullptr;
160     g_dragServerOne = nullptr;
161     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
162 }
163 
CreatePixelMap(int32_t width,int32_t height)164 std::shared_ptr<Media::PixelMap> DragClientTest::CreatePixelMap(int32_t width, int32_t height)
165 {
166     CALL_DEBUG_ENTER;
167     if (width <= 0 || width > MAX_PIXEL_MAP_WIDTH || height <= 0 || height > MAX_PIXEL_MAP_HEIGHT) {
168         FI_HILOGE("invalid, height:%{public}d, width:%{public}d", height, width);
169         return nullptr;
170     }
171     Media::InitializationOptions opts;
172     opts.size.width = width;
173     opts.size.height = height;
174     opts.pixelFormat = Media::PixelFormat::BGRA_8888;
175     opts.alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
176     opts.scaleMode = Media::ScaleMode::FIT_TARGET_SIZE;
177 
178     int32_t colorLen = width * height;
179     uint32_t *pixelColors = new (std::nothrow) uint32_t[BUFF_SIZE];
180     CHKPP(pixelColors);
181     int32_t colorByteCount = colorLen * INT32_BYTE;
182     errno_t ret = memset_s(pixelColors, BUFF_SIZE, DEFAULT_ICON_COLOR, colorByteCount);
183     if (ret != EOK) {
184         FI_HILOGE("memset_s failed");
185         delete[] pixelColors;
186         return nullptr;
187     }
188     std::shared_ptr<Media::PixelMap> pixelMap = Media::PixelMap::Create(pixelColors, colorLen, opts);
189     if (pixelMap == nullptr) {
190         FI_HILOGE("Create pixelMap failed");
191         delete[] pixelColors;
192         return nullptr;
193     }
194     delete[] pixelColors;
195     return pixelMap;
196 }
197 
CreateDragData(int32_t sourceType,int32_t pointerId,int32_t dragNum,bool hasCoordinateCorrected,int32_t shadowNum)198 std::optional<DragData> DragClientTest::CreateDragData(int32_t sourceType,
199     int32_t pointerId, int32_t dragNum, bool hasCoordinateCorrected, int32_t shadowNum)
200 {
201     CALL_DEBUG_ENTER;
202     DragData dragData;
203     for (int32_t i = 0; i < shadowNum; i++) {
204         std::shared_ptr<Media::PixelMap> pixelMap = CreatePixelMap(PIXEL_MAP_WIDTH, PIXEL_MAP_HEIGHT);
205         if (pixelMap == nullptr) {
206             FI_HILOGE("pixelMap nullptr");
207             return std::nullopt;
208         }
209         dragData.shadowInfos.push_back({ pixelMap, g_shadowinfo_x, g_shadowinfo_y });
210     }
211     dragData.buffer = std::vector<uint8_t>(MAX_BUFFER_SIZE, 0);
212     dragData.extraInfo = FILTER_INFO;
213     dragData.udKey = UD_KEY;
214     dragData.sourceType = sourceType;
215     dragData.extraInfo = EXTRA_INFO;
216     dragData.displayId = DISPLAY_ID;
217     dragData.pointerId = pointerId;
218     dragData.dragNum = dragNum;
219     dragData.displayX = DISPLAY_X;
220     dragData.displayY = DISPLAY_Y;
221     dragData.hasCoordinateCorrected = hasCoordinateCorrected;
222     dragData.hasCanceledAnimation = HAS_CANCELED_ANIMATION;
223     return dragData;
224 }
225 
NativeTokenGet()226 uint64_t NativeTokenGet()
227 {
228     uint64_t tokenId;
229     NativeTokenInfoParams infoInstance = {
230         .dcapsNum = 0,
231         .permsNum = 0,
232         .aclsNum = 0,
233         .dcaps = nullptr,
234         .perms = nullptr,
235         .acls = nullptr,
236         .aplStr = "system_basic",
237     };
238 
239     infoInstance.processName = " DragServerTest";
240     tokenId = GetAccessTokenId(&infoInstance);
241     SetSelfTokenID(tokenId);
242     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
243     return tokenId;
244 }
245 class TestStartDragListener : public IStartDragListener {
246 public:
TestStartDragListener(std::function<void (const DragNotifyMsg &)> function)247     explicit TestStartDragListener(std::function<void(const DragNotifyMsg&)> function) : function_(function) { }
OnDragEndMessage(const DragNotifyMsg & msg)248     void OnDragEndMessage(const DragNotifyMsg &msg) override
249     {
250         FI_HILOGD("DisplayX:%{public}d, displayY:%{public}d, targetPid:%{public}d, result:%{public}d",
251             msg.displayX, msg.displayY, msg.targetPid, static_cast<int32_t>(msg.result));
252         if (function_ != nullptr) {
253             function_(msg);
254         }
255         FI_HILOGD("Test OnDragEndMessage");
256     }
257 
OnHideIconMessage()258     void OnHideIconMessage() override
259     {
260         FI_HILOGD("Test OnHideIconMessage");
261     }
262 private:
263     std::function<void(const DragNotifyMsg&)> function_;
264 };
265 
266 class DragListenerTest : public IDragListener {
267 public:
DragListenerTest()268     DragListenerTest() {}
DragListenerTest(const std::string & name)269     explicit DragListenerTest(const std::string& name) : moduleName_(name) {}
OnDragMessage(DragState state)270     void OnDragMessage(DragState state) override
271     {
272         if (moduleName_.empty()) {
273             moduleName_ = std::string("DragListenerTest");
274         }
275         FI_HILOGD("%{public}s, state:%{public}s", moduleName_.c_str(), PrintDragMessage(state).c_str());
276     }
277 private:
PrintDragMessage(DragState state)278     std::string PrintDragMessage(DragState state)
279     {
280         std::string type = "unknow";
281         const std::map<DragState, std::string> stateType = {
282             { DragState::ERROR, "error"},
283             { DragState::START, "start"},
284             { DragState::STOP, "stop"},
285             { DragState::CANCEL, "cancel"}
286         };
287         auto item = stateType.find(state);
288         if (item != stateType.end()) {
289             type = item->second;
290         }
291         return type;
292     }
293 private:
294     std::string moduleName_;
295 };
296 
AssignToAnimation(PreviewAnimation & animation)297 void DragClientTest::AssignToAnimation(PreviewAnimation &animation)
298 {
299     animation.duration = ANIMATION_DURATION;
300     animation.curveName = CURVE_NAME;
301     animation.curve = { 0.33, 0, 0.67, 1 };
302 }
303 
304 /**
305  * @tc.name: DragClientTest1
306  * @tc.desc: DragClient
307  * @tc.type: FUNC
308  * @tc.require:
309  */
310 HWTEST_F(DragClientTest, DragClientTest1, TestSize.Level0)
311 {
312     CALL_TEST_DEBUG;
313     bool enable = false;
314     bool isJsCaller = false;
315     int32_t ret = g_dragClient.SetDragSwitchState(enable, isJsCaller);
316     EXPECT_EQ(ret, RET_OK);
317 }
318 /**
319  * @tc.name: DragClientTest2
320  * @tc.desc: DragClient
321  * @tc.type: FUNC
322  * @tc.require:
323  */
324 HWTEST_F(DragClientTest, DragClientTest2, TestSize.Level0)
325 {
326     CALL_TEST_DEBUG;
327     bool enable = false;
328     std::string pkgName = {"pkg"};
329     bool isJsCaller = false;
330     int32_t ret = g_dragClient.SetAppDragSwitchState(enable, pkgName, isJsCaller);
331     EXPECT_EQ(ret, RET_OK);
332 }
333 
334 /**
335  * @tc.name: DragClientTest3
336  * @tc.desc: DragClient
337  * @tc.type: FUNC
338  * @tc.require:
339  */
340 HWTEST_F(DragClientTest, DragClientTest3, TestSize.Level0)
341 {
342     CALL_TEST_DEBUG;
343     uint64_t displayId = 0;
344     uint64_t screenId = 0;
345     int32_t ret = g_dragClient.SetDragWindowScreenId(displayId, screenId);
346     EXPECT_EQ(ret, RET_OK);
347 }
348 
349 /**
350  * @tc.name: DragClientTest4
351  * @tc.desc: DragClient
352  * @tc.type: FUNC
353  * @tc.require:
354  */
355 HWTEST_F(DragClientTest, DragClientTest4, TestSize.Level0)
356 {
357     CALL_TEST_DEBUG;
358     bool state = false;
359     int32_t ret = g_dragClient.SetMouseDragMonitorState(state);
360     EXPECT_EQ(ret, RET_OK);
361 }
362 
363 /**
364  * @tc.name: DragClientTest5
365  * @tc.desc: DragClient
366  * @tc.type: FUNC
367  * @tc.require:
368  */
369 HWTEST_F(DragClientTest, DragClientTest5, TestSize.Level0)
370 {
371     CALL_TEST_DEBUG;
372     bool state = false;
373     int32_t ret = g_dragClient.SetDraggableState(state);
374     EXPECT_EQ(ret, RET_OK);
375 }
376 
377 /**
378  * @tc.name: DragClientTest6
379  * @tc.desc: DragClient
380  * @tc.type: FUNC
381  * @tc.require:
382  */
383 HWTEST_F(DragClientTest, DragClientTest6, TestSize.Level0)
384 {
385     CALL_TEST_DEBUG;
386     bool state = false;
387     int32_t ret = g_dragClient.GetAppDragSwitchState(state);
388     EXPECT_EQ(ret, RET_OK);
389 }
390 
391 /**
392  * @tc.name: DragClientTest7
393  * @tc.desc: DragClient
394  * @tc.type: FUNC
395  * @tc.require:
396  */
397 HWTEST_F(DragClientTest, DragClientTest7, TestSize.Level0)
398 {
399     CALL_TEST_DEBUG;
400     ASSERT_NO_FATAL_FAILURE(g_dragClient.OnDisconnected());
401 }
402 
403 /**
404  * @tc.name: DragClientTest8
405  * @tc.desc: DragClient
406  * @tc.type: FUNC
407  * @tc.require:
408  */
409 HWTEST_F(DragClientTest, DragClientTest8, TestSize.Level0)
410 {
411     CALL_TEST_DEBUG;
412     ASSERT_EQ(g_dragClient.IsDragStart(), false);
413 }
414 } // namespace DeviceStatus
415 } // namespace Msdp
416 } // namespace OHOS
417