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 #include <optional>
16 
17 #include "gtest/gtest.h"
18 
19 #define private public
20 
21 #include "base/geometry/ng/offset_t.h"
22 #include "base/geometry/ng/rect_t.h"
23 #include "base/geometry/ng/size_t.h"
24 #include "base/image/pixel_map.h"
25 #include "base/memory/ace_type.h"
26 #include "base/memory/referenced.h"
27 #include "core/components_ng/base/frame_node.h"
28 #include "core/components_ng/base/geometry_node.h"
29 #include "core/components_ng/base/ui_node.h"
30 #include "core/components_ng/event/event_hub.h"
31 #include "core/components_ng/manager/drag_drop/drag_drop_manager.h"
32 #include "core/components_ng/manager/drag_drop/drag_drop_proxy.h"
33 #include "frameworks/core/components_ng/pattern/pattern.h"
34 #include "test/mock/core/pipeline/mock_pipeline_context.h"
35 
36 using namespace testing;
37 using namespace testing::ext;
38 namespace OHOS::Ace::NG {
39 namespace {
40 constexpr int64_t PROXY_ID = 1;
41 constexpr int64_t PROXY_ID_NOT_FIT = 101;
42 const std::string GET_EXTRA("GetExtraInfoFromClipboard");
43 const std::string NODE_TAG("custom_node");
44 const std::string EXTRA_INFO_DRAG_START("drag_start_info");
45 const std::string ON_DRAG_START("OnDragStart");
46 const std::string ITEM_DRAG_START("OnItemDragStart");
47 constexpr int32_t DRAGGED_INDEX = 0;
48 const DragType DRAG_TYPE_GRID = DragType::GRID;
49 } // namespace
50 
51 class DragDropProxyTestNg : public testing::Test {
52 public:
53     static void SetUpTestCase();
54     static void TearDownTestCase();
55 
56 protected:
57 };
58 
SetUpTestCase()59 void DragDropProxyTestNg::SetUpTestCase()
60 {
61     MockPipelineContext::SetUp();
62 }
63 
TearDownTestCase()64 void DragDropProxyTestNg::TearDownTestCase()
65 {
66     MockPipelineContext::TearDown();
67 }
68 
69 /**
70  * @tc.name: DragDropProxyTest001
71  * @tc.desc: Test OnDragStart
72  * @tc.type: FUNC
73  * @tc.author:
74  */
75 HWTEST_F(DragDropProxyTestNg, DragDropProxyTest001, TestSize.Level1)
76 {
77     /**
78      * @tc.steps: step1. construct a DragDropProxy with unfitted proxyId
79      * @tc.expected: step1. id_ = PROXY_ID_NOT_FIT
80      */
81     auto proxyUnFitted = AceType::MakeRefPtr<DragDropProxy>(PROXY_ID_NOT_FIT);
82     EXPECT_EQ(proxyUnFitted->id_, PROXY_ID_NOT_FIT);
83 
84     /**
85      * @tc.steps: step2. call onDragStart
86      * @tc.expected: step2. no fatal errors happended
87      *                      DragDropManager->OnDragStart() & AddDataToClipboard() will not be called
88      *                      frameNode's nodeName_ not change
89      */
90     auto frameNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<Pattern>());
91     GestureEvent info;
92     proxyUnFitted->OnDragStart(info, EXTRA_INFO_DRAG_START, frameNode);
93     auto nodeName = frameNode->GetNodeName();
94     EXPECT_EQ(nodeName, "");
95 
96     /**
97      * @tc.steps: step3. construct a DragDropProxy with fitted proxyId
98      * @tc.expected: step3. id_ = PROXY_ID
99      */
100     auto proxy = AceType::MakeRefPtr<DragDropProxy>(PROXY_ID);
101     EXPECT_EQ(proxy->id_, PROXY_ID);
102 
103     /**
104      * @tc.steps: step4. call onDragStart
105      * @tc.expected: step4. DragDropManager->OnDragStart() & AddDataToClipboard() will be called
106      *                      some logs will be print
107      *                      they are defined in "mock_drag_drop_manager.cpp"
108      *                      frameNode's nodeName_ not change
109      */
110     proxy->OnDragStart(info, EXTRA_INFO_DRAG_START, frameNode);
111     nodeName = frameNode->GetNodeName();
112     EXPECT_EQ(nodeName, "");
113 }
114 
115 /**
116  * @tc.name: DragDropProxyTest002
117  * @tc.desc: Test OnDragMove
118  * @tc.type: FUNC
119  * @tc.author:
120  */
121 HWTEST_F(DragDropProxyTestNg, DragDropProxyTest002, TestSize.Level1)
122 {
123     /**
124      * @tc.steps: step1. construct a DragDropProxy with unfitted proxyId
125      * @tc.expected: step1. id_ = PROXY_ID_NOT_FIT
126      */
127     auto proxyUnFitted = AceType::MakeRefPtr<DragDropProxy>(PROXY_ID_NOT_FIT);
128     EXPECT_EQ(proxyUnFitted->id_, PROXY_ID_NOT_FIT);
129 
130     /**
131      * @tc.steps: step2. call OnDragMove
132      * @tc.expected: step2. no fatal errors happended
133      *                      DragDropManager->OnDragMove() & GetExtraInfoFromClipboard() will not be called
134      */
135     GestureEvent info;
136     proxyUnFitted->OnDragMove(info);
137 
138     /**
139      * @tc.steps: step3. construct a DragDropProxy with fitted proxyId
140      * @tc.expected: step3. id_ = PROXY_ID
141      */
142     auto proxy = AceType::MakeRefPtr<DragDropProxy>(PROXY_ID);
143     EXPECT_EQ(proxy->id_, PROXY_ID);
144 
145     /**
146      * @tc.steps: step4. call OnDragMove
147      * @tc.expected: step4. DragDropManager->OnDragMove() & GetExtraInfoFromClipboard() will be called
148      *                      some logs will be print
149      *                      they are defined in "mock_drag_drop_manager.cpp"
150      */
151     proxy->OnDragMove(info);
152 }
153 
154 /**
155  * @tc.name: DragDropProxyTest003
156  * @tc.desc: Test OnDragEnd
157  * @tc.type: FUNC
158  * @tc.author:
159  */
160 HWTEST_F(DragDropProxyTestNg, DragDropProxyTest003, TestSize.Level1)
161 {
162     /**
163      * @tc.steps: step1. construct a DragDropProxy with unfitted proxyId
164      * @tc.expected: step1. id_ = PROXY_ID_NOT_FIT
165      */
166     auto proxyUnFitted = AceType::MakeRefPtr<DragDropProxy>(PROXY_ID_NOT_FIT);
167     EXPECT_EQ(proxyUnFitted->id_, PROXY_ID_NOT_FIT);
168 
169     /**
170      * @tc.steps: step2. call OnDragEnd
171      * @tc.expected: step2. no fatal errors happended
172      *                      DragDropManager->OnDragEnd() & GetExtraInfoFromClipboard() &
173      *                      RestoreClipboardData() will not be called
174      */
175     GestureEvent info;
176     proxyUnFitted->OnDragEnd(info);
177 
178     /**
179      * @tc.steps: step3. construct a DragDropProxy with fitted proxyId
180      * @tc.expected: step3. id_ = PROXY_ID
181      */
182     auto proxy = AceType::MakeRefPtr<DragDropProxy>(PROXY_ID);
183     EXPECT_EQ(proxy->id_, PROXY_ID);
184 
185     /**
186      * @tc.steps: step4. call OnDragEnd
187      * @tc.expected: step4. DragDropManager->OnDragEnd() & GetExtraInfoFromClipboard() &
188      *                      RestoreClipboardData() will be called
189      *                      some logs will be print they are defined in "mock_drag_drop_manager.cpp"
190      */
191     proxy->OnDragEnd(info);
192 }
193 
194 /**
195  * @tc.name: DragDropProxyOnDragEndTest001
196  * @tc.desc: Test OnDragEnd
197  * @tc.type: FUNC
198  * @tc.author:
199  */
200 HWTEST_F(DragDropProxyTestNg, DragDropProxyOnDragEndTest001, TestSize.Level1)
201 {
202     /**
203      * @tc.steps: step1. construct a DragDropProxy with unfitted proxyId
204      * @tc.expected: step1. id_ = PROXY_ID_NOT_FIT
205      */
206     auto proxyUnFitted = AceType::MakeRefPtr<DragDropProxy>(PROXY_ID_NOT_FIT);
207     EXPECT_EQ(proxyUnFitted->id_, PROXY_ID_NOT_FIT);
208 
209     /**
210      * @tc.steps: step2. call OnDragEnd
211      * @tc.expected: step2. no fatal errors happended
212      *                      DragDropManager->OnDragEnd() & GetExtraInfoFromClipboard() &
213      *                      RestoreClipboardData() will not be called
214      */
215     GestureEvent info;
216     proxyUnFitted->OnDragEnd(info, true);
217 
218     /**
219      * @tc.steps: step3. construct a DragDropProxy with fitted proxyId
220      * @tc.expected: step3. id_ = PROXY_ID
221      */
222     auto proxy = AceType::MakeRefPtr<DragDropProxy>(PROXY_ID);
223     EXPECT_EQ(proxy->id_, PROXY_ID);
224 
225     /**
226      * @tc.steps: step4. call OnDragEnd
227      * @tc.expected: step4. DragDropManager->OnDragEnd() & GetExtraInfoFromClipboard() &
228      *                      RestoreClipboardData() will be called
229      *                      some logs will be print they are defined in "mock_drag_drop_manager.cpp"
230      */
231     proxy->OnDragEnd(info, true);
232     EXPECT_EQ(proxy->id_, PROXY_ID);
233 }
234 
235 /**
236  * @tc.name: DragDropProxyTest004
237  * @tc.desc: Test onDragCancel
238  * @tc.type: FUNC
239  * @tc.author:
240  */
241 HWTEST_F(DragDropProxyTestNg, DragDropProxyTest004, TestSize.Level1)
242 {
243     /**
244      * @tc.steps: step1. construct a DragDropProxy with unfitted proxyId
245      * @tc.expected: step1. id_ = PROXY_ID_NOT_FIT
246      */
247     auto proxyUnFitted = AceType::MakeRefPtr<DragDropProxy>(PROXY_ID_NOT_FIT);
248     EXPECT_EQ(proxyUnFitted->id_, PROXY_ID_NOT_FIT);
249 
250     /**
251      * @tc.steps: step2. call onDragCancel
252      * @tc.expected: step2. no fatal errors happended
253      *                      DragDropManager->onDragCancel() will not be called
254      */
255     proxyUnFitted->onDragCancel();
256 
257     /**
258      * @tc.steps: step3. construct a DragDropProxy with fitted proxyId
259      * @tc.expected: step3. id_ = PROXY_ID
260      */
261     auto proxy = AceType::MakeRefPtr<DragDropProxy>(PROXY_ID);
262     EXPECT_EQ(proxy->id_, PROXY_ID);
263 
264     /**
265      * @tc.steps: step4. call onDragCancel
266      * @tc.expected: step4. DragDropManager->onDragCancel() will be called
267      *                      some logs will be print they are defined in "mock_drag_drop_manager.cpp"
268      */
269     auto pipeline = PipelineContext::GetCurrentContext();
270     auto manager = pipeline->GetDragDropManager();
271     manager->currentId_ = PROXY_ID;
272     proxy->onDragCancel();
273 }
274 
275 /**
276  * @tc.name: DragDropProxyTest005
277  * @tc.desc: Test onItemDragStart
278  * @tc.type: FUNC
279  * @tc.author:
280  */
281 HWTEST_F(DragDropProxyTestNg, DragDropProxyTest005, TestSize.Level1)
282 {
283     /**
284      * @tc.steps: step1. construct a DragDropProxy with unfitted proxyId
285      * @tc.expected: step1. id_ = PROXY_ID_NOT_FIT
286      */
287     auto proxyUnFitted = AceType::MakeRefPtr<DragDropProxy>(PROXY_ID_NOT_FIT);
288     EXPECT_EQ(proxyUnFitted->id_, PROXY_ID_NOT_FIT);
289 
290     /**
291      * @tc.steps: step2. call OnItemDragStart
292      * @tc.expected: step2. no fatal errors happended
293      *                      DragDropManager->OnItemDragStart() will not be called
294      *                      frameNode' nodeName not change
295      */
296     auto frameNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<Pattern>());
297     GestureEvent info;
298     proxyUnFitted->OnItemDragStart(info, frameNode);
299     auto nodeName = frameNode->GetNodeName();
300     EXPECT_EQ(nodeName, "");
301 
302     /**
303      * @tc.steps: step3. construct a DragDropProxy with fitted proxyId
304      * @tc.expected: step3. id_ = PROXY_ID
305      */
306     auto proxy = AceType::MakeRefPtr<DragDropProxy>(PROXY_ID);
307     EXPECT_EQ(proxy->id_, PROXY_ID);
308 
309     /**
310      * @tc.steps: step4. call OnItemDragStart
311      * @tc.expected: step4. DragDropManager->OnItemDragStart() will be called
312      *                      some logs will be print they are defined in "mock_drag_drop_manager.cpp"
313      *                      frameNode' nodeName not change
314      */
315     proxy->OnItemDragStart(info, frameNode);
316     nodeName = frameNode->GetNodeName();
317     EXPECT_EQ(nodeName, "");
318 }
319 
320 /**
321  * @tc.name: DragDropProxyTest006
322  * @tc.desc: Test OnItemDragMove
323  * @tc.type: FUNC
324  * @tc.author:
325  */
326 HWTEST_F(DragDropProxyTestNg, DragDropProxyTest006, TestSize.Level1)
327 {
328     /**
329      * @tc.steps: step1. construct a DragDropProxy with unfitted proxyId
330      * @tc.expected: step1. id_ = PROXY_ID_NOT_FIT
331      */
332     auto proxyUnFitted = AceType::MakeRefPtr<DragDropProxy>(PROXY_ID_NOT_FIT);
333     EXPECT_EQ(proxyUnFitted->id_, PROXY_ID_NOT_FIT);
334 
335     /**
336      * @tc.steps: step2. call OnItemDragMove
337      * @tc.expected: step2. no fatal errors happended
338      *                      DragDropManager->OnItemDragMove() will not be called
339      */
340     GestureEvent info;
341     proxyUnFitted->OnItemDragMove(info, DRAGGED_INDEX, DRAG_TYPE_GRID);
342 
343     /**
344      * @tc.steps: step3. construct a DragDropProxy with fitted proxyId
345      * @tc.expected: step3. id_ = PROXY_ID
346      */
347     auto proxy = AceType::MakeRefPtr<DragDropProxy>(PROXY_ID);
348     EXPECT_EQ(proxy->id_, PROXY_ID);
349 
350     /**
351      * @tc.steps: step4. call OnItemDragMove
352      * @tc.expected: step4. DragDropManager->OnItemDragMove() will be called
353      *                      some logs will be print they are defined in "mock_drag_drop_manager.cpp"
354      */
355     proxy->OnItemDragMove(info, DRAGGED_INDEX, DRAG_TYPE_GRID);
356 }
357 
358 /**
359  * @tc.name: DragDropProxyTest007
360  * @tc.desc: Test OnItemDragEnd
361  * @tc.type: FUNC
362  * @tc.author:
363  */
364 HWTEST_F(DragDropProxyTestNg, DragDropProxyTest007, TestSize.Level1)
365 {
366     /**
367      * @tc.steps: step1. construct a DragDropProxy with unfitted proxyId
368      * @tc.expected: step1. id_ = PROXY_ID_NOT_FIT
369      */
370     auto proxyUnFitted = AceType::MakeRefPtr<DragDropProxy>(PROXY_ID_NOT_FIT);
371     EXPECT_EQ(proxyUnFitted->id_, PROXY_ID_NOT_FIT);
372 
373     /**
374      * @tc.steps: step2. call OnItemDragEnd
375      * @tc.expected: step2. no fatal errors happended
376      *                      DragDropManager->OnItemDragEnd() will not be called
377      */
378     GestureEvent info;
379     proxyUnFitted->OnItemDragEnd(info, DRAGGED_INDEX, DRAG_TYPE_GRID);
380 
381     /**
382      * @tc.steps: step3. construct a DragDropProxy with fitted proxyId
383      * @tc.expected: step3. id_ = PROXY_ID
384      */
385     auto proxy = AceType::MakeRefPtr<DragDropProxy>(PROXY_ID);
386     EXPECT_EQ(proxy->id_, PROXY_ID);
387 
388     /**
389      * @tc.steps: step4. call OnItemDragEnd
390      * @tc.expected: step4. DragDropManager->OnItemDragEnd() will be called
391      *                      some logs will be print they are defined in "mock_drag_drop_manager.cpp"
392      */
393     proxy->OnItemDragEnd(info, DRAGGED_INDEX, DRAG_TYPE_GRID);
394 }
395 
396 /**
397  * @tc.name: DragDropProxyTest008
398  * @tc.desc: Test onItemDragCancel
399  * @tc.type: FUNC
400  * @tc.author:
401  */
402 HWTEST_F(DragDropProxyTestNg, DragDropProxyTest008, TestSize.Level1)
403 {
404     /**
405      * @tc.steps: step1. construct a DragDropProxy with unfitted proxyId
406      * @tc.expected: step1. id_ = PROXY_ID_NOT_FIT
407      */
408     auto proxyUnFitted = AceType::MakeRefPtr<DragDropProxy>(PROXY_ID_NOT_FIT);
409     EXPECT_EQ(proxyUnFitted->id_, PROXY_ID_NOT_FIT);
410 
411     /**
412      * @tc.steps: step2. call onItemDragCancel
413      * @tc.expected: step2. no fatal errors happended
414      *                      DragDropManager->onItemDragCancel() will not be called
415      */
416     GestureEvent info;
417     proxyUnFitted->onItemDragCancel();
418 
419     /**
420      * @tc.steps: step3. construct a DragDropProxy with fitted proxyId
421      * @tc.expected: step3. id_ = PROXY_ID
422      */
423     auto proxy = AceType::MakeRefPtr<DragDropProxy>(PROXY_ID);
424     EXPECT_EQ(proxy->id_, PROXY_ID);
425 
426     /**
427      * @tc.steps: step4. call onItemDragCancel
428      * @tc.expected: step4. DragDropManager->onItemDragCancel() will be called
429      *                      some logs will be print they are defined in "mock_drag_drop_manager.cpp"
430      */
431     proxy->onItemDragCancel();
432 }
433 
434 /**
435  * @tc.name: DragDropProxyTest009
436  * @tc.desc: Test OnTextDragStart
437  * @tc.type: FUNC
438  * @tc.author:
439  */
440 HWTEST_F(DragDropProxyTestNg, DragDropProxyTest009, TestSize.Level1)
441 {
442     /**
443      * @tc.steps: step1. construct a DragDropProxy with unfitted proxyId.
444      * @tc.expected: step1. id_ is equal to PROXY_ID_NOT_FIT.
445      */
446     auto proxyUnFitted = AceType::MakeRefPtr<DragDropProxy>(PROXY_ID_NOT_FIT);
447     EXPECT_EQ(proxyUnFitted->id_, PROXY_ID_NOT_FIT);
448 
449     /**
450      * @tc.steps: step2. construct a manager and update currentId_.
451      */
452     auto pipeline = PipelineContext::GetCurrentContext();
453     auto manager = pipeline->GetDragDropManager();
454     manager->currentId_ = PROXY_ID_NOT_FIT;
455 
456     /**
457      * @tc.steps: step3. call OnTextDragStart with ON_DRAG_START.
458      * @tc.expected: step3. retFlag is true.
459      */
460     proxyUnFitted->OnTextDragStart(ON_DRAG_START);
461     auto retFlag = manager->CheckDragDropProxy(PROXY_ID_NOT_FIT);
462     EXPECT_TRUE(retFlag);
463 }
464 
465 /**
466  * @tc.name: DragDropProxyTest010
467  * @tc.desc: Test CheckDragDropProxy out of OnDragStart is a true branch
468  * @tc.type: FUNC
469  * @tc.author:
470  */
471 HWTEST_F(DragDropProxyTestNg, DragDropProxyTest010, TestSize.Level1)
472 {
473     /**
474      * @tc.steps: step1. construct a DragDropProxy with unfitted proxyUnFitted.
475      * @tc.expected: step1. id_ is equal to PROXY_ID_NOT_FIT.
476      */
477     auto proxyUnFitted = AceType::MakeRefPtr<DragDropProxy>(PROXY_ID_NOT_FIT);
478     EXPECT_EQ(proxyUnFitted->id_, PROXY_ID_NOT_FIT);
479 
480     /**
481      * @tc.steps: step2. construct a manager and update currentId_.
482      */
483     auto pipeline = PipelineContext::GetCurrentContext();
484     auto manager = pipeline->GetDragDropManager();
485     manager->currentId_ = PROXY_ID_NOT_FIT;
486 
487     /**
488      * @tc.steps: step3. call onDragStart with GestureEvent and frameNode.
489      * @tc.expected: step3. proxyUnFitted->id_ is equal to manager->currentId_.
490      */
491     GestureEvent info;
492     auto frameNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<Pattern>());
493     proxyUnFitted->OnDragStart(info, EXTRA_INFO_DRAG_START, frameNode);
494     EXPECT_EQ(proxyUnFitted->id_, manager->currentId_);
495 }
496 
497 /**
498  * @tc.name: DragDropProxyTest011
499  * @tc.desc: Test isTextDragEnd out of OnDragEnd is a true branch
500  * @tc.type: FUNC
501  * @tc.author:
502  */
503 HWTEST_F(DragDropProxyTestNg, DragDropProxyTest011, TestSize.Level1)
504 {
505     /**
506      * @tc.steps: step1. construct a DragDropProxy with fitted proxy.
507      * @tc.expected: step1. id_ is equal to PROXY_ID.
508      */
509     auto proxy = AceType::MakeRefPtr<DragDropProxy>(PROXY_ID);
510     EXPECT_EQ(proxy->id_, PROXY_ID);
511 
512     /**
513      * @tc.steps: step2. construct a manager and update currentId_.
514      */
515     auto pipeline = PipelineContext::GetCurrentContext();
516     auto manager = pipeline->GetDragDropManager();
517     EXPECT_TRUE(manager);
518 
519     /**
520      * @tc.steps: step3. call OnDragEnd with GestureEvent and true.
521      * @tc.expected: step3. manager->currentId_ is equal to -1.
522      */
523     GestureEvent info;
524     manager->currentId_ = PROXY_ID;
525     proxy->OnDragEnd(info, true);
526     EXPECT_EQ(manager->currentId_, -1);
527 }
528 
529 /**
530  * @tc.name: DragDropProxyTest012
531  * @tc.desc: DestroyDragWindow
532  * @tc.type: FUNC
533  * @tc.author:
534  */
535 HWTEST_F(DragDropProxyTestNg, DragDropProxyTest012, TestSize.Level1)
536 {
537     /**
538      * @tc.steps: step1. construct a DragDropProxy with fitted proxy.
539      * @tc.expected: step1. id_ is equal to PROXY_ID.
540      */
541     auto proxy = AceType::MakeRefPtr<DragDropProxy>(PROXY_ID);
542     EXPECT_EQ(proxy->id_, PROXY_ID);
543 
544     /**
545      * @tc.steps: step2. construct a manager and update currentId_.
546      */
547     auto pipeline = PipelineContext::GetCurrentContext();
548     auto manager = pipeline->GetDragDropManager();
549     manager->currentId_ = PROXY_ID;
550 
551     /**
552      * @tc.steps: step3. call DestroyDragWindow.
553      * @tc.expected: step3. manager->currentId_ is equal to proxy->id_.
554      */
555     proxy->DestroyDragWindow();
556     EXPECT_EQ(manager->currentId_, -1);
557 }
558 } // namespace OHOS::Ace::NG
559