1 /*
2 * Copyright (c) 2024 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 <optional>
17
18 #include "gtest/gtest.h"
19 #define private public
20 #define protected public
21
22 #include "test/mock/base/mock_task_executor.h"
23 #include "test/mock/base/mock_event_report.h"
24 #include "test/mock/core/common/mock_container.h"
25 #include "test/mock/core/pipeline/mock_pipeline_context.h"
26
27 #include "core/components_ng/manager/drag_drop/drag_drop_behavior_reporter/drag_drop_behavior_reporter.h"
28
29 #include "base/log/event_report.h"
30
31 using namespace testing;
32 using namespace testing::ext;
33
34 namespace OHOS::Ace::NG {
35 struct DragReportData {
36 CrossingEnd isCrossing = CrossingEnd::NOT_CROSSING;
37 DragStartResult startResult = DragStartResult::UNKNOW;
38 DragStopResult stopResult = DragStopResult::UNKNOW;
39 int32_t summaryNum = -1;
40 DragReporterPharse dragBehavior;
41 std::string summaryType = "";
42 std::set<std::string> allowDropType = {""};
DragReportDataOHOS::Ace::NG::DragReportData43 DragReportData(CrossingEnd isCrossing, DragStartResult startResult, DragStopResult stopResult, int32_t summaryNum,
44 DragReporterPharse dragBehavior)
45 : isCrossing(isCrossing), startResult(startResult), stopResult(stopResult), summaryNum(summaryNum),
46 dragBehavior(dragBehavior) {}
47 };
48
49 const std::vector<DragStartResult> dragStartResult = {
50 DragStartResult::DRAG_START_SUCCESS,
51 DragStartResult::APP_REFUSE_DRAG,
52 DragStartResult::DRAG_START_SUCCESS,
53 DragStartResult::APP_REFUSE_DRAG,
54 DragStartResult::DRAGFWK_START_FAIL,
55 DragStartResult::SET_DATA_FAIL,
56 DragStartResult::REPEAT_DRAG_FAIL,
57 DragStartResult::SNAPSHOT_FAIL,
58 DragStartResult::TEXT_NOT_SELECT,
59 };
60
61 const std::vector<DragStopResult> dragStopResult = {
62 DragStopResult::DRAG_SOTP_SUCCESS,
63 DragStopResult::APP_REFUSE_DATA,
64 DragStopResult::APP_RECEIVE_FAIL,
65 DragStopResult::APP_DATA_UNSUPPORT,
66 DragStopResult::USER_STOP_DRAG,
67 DragStopResult::GET_UDKEY_FAIL,
68 DragStopResult::GET_UDMF_FAIL,
69 DragStopResult::DRAGFWK_STOP_FAIL,
70 };
71
72 const std::vector<CrossingEnd> crossingEnd = {
73 CrossingEnd::NOT_CROSSING,
74 CrossingEnd::IS_CROSSING,
75 };
76
77 class DragReporterTestNg : public testing::Test {
78 public:
79 static void SetUpTestCase();
80 static void TearDownTestCase();
81 static void SetDragStartReportData(const DragReportData& dragReportData);
82 };
83
SetUpTestCase()84 void DragReporterTestNg::SetUpTestCase()
85 {
86 MockPipelineContext::SetUp();
87 MockContainer::SetUp();
88 }
89
TearDownTestCase()90 void DragReporterTestNg::TearDownTestCase()
91 {
92 MockPipelineContext::TearDown();
93 MockContainer::TearDown();
94 }
95
SetDragStartReportData(const DragReportData & dragReportData)96 void DragReporterTestNg::SetDragStartReportData(const DragReportData& dragReportData)
97 {
98 DragDropBehaviorReporter::GetInstance().UpdateDragStartResult(dragReportData.startResult);
99 DragDropBehaviorReporter::GetInstance().UpdateDragStopResult(dragReportData.stopResult);
100 DragDropBehaviorReporter::GetInstance().UpdateRecordSize(dragReportData.summaryNum);
101 DragDropBehaviorReporter::GetInstance().UpdateSummaryType(dragReportData.summaryType);
102 DragDropBehaviorReporter::GetInstance().UpdateIsCrossing(dragReportData.isCrossing);
103 DragDropBehaviorReporter::GetInstance().UpdateAllowDropType(dragReportData.allowDropType);
104 }
105
106 /**
107 * @tc.name: DragReporterTest001
108 * @tc.desc: Test drag report
109 * @tc.type: FUNC
110 * @tc.author: catpoison
111 */
112 HWTEST_F(DragReporterTestNg, DragReporterTest001, TestSize.Level1)
113 {
114 /**
115 * @tc.steps: step1. test drag start report.
116 */
117 for (const auto& isCrossing : crossingEnd) {
118 for (const auto& startResult : dragStartResult) {
119 for (const auto& stopResult : dragStopResult) {
120 DragReportData dragReportCase = DragReportData(isCrossing,
121 startResult, stopResult, 1, DragReporterPharse::DRAG_START);
122 MockContainer::SetUp();
123 auto container = MockContainer::Current();
124 container->pipelineContext_ = NG::MockPipelineContext::GetCurrent();
125 auto containerId = MockContainer::CurrentId();
126 DragReporterTestNg::SetDragStartReportData(dragReportCase);
127 auto mockTaskExecutor = AceType::MakeRefPtr<MockTaskExecutor>();
128 MockPipelineContext::GetCurrentContext()->taskExecutor_ = mockTaskExecutor;
129 DragDropBehaviorReporter::GetInstance().Submit(dragReportCase.dragBehavior, containerId);
130 DragInfo dragReportInfo = MockEventReport::dragInfo_;
131 EXPECT_EQ(dragReportInfo.result, static_cast<int32_t>(dragReportCase.startResult));
132 EXPECT_EQ(dragReportInfo.summaryNum, static_cast<int32_t>(dragReportCase.summaryNum));
133 EXPECT_EQ(dragReportInfo.summaryType, dragReportCase.summaryType);
134 EXPECT_EQ(dragReportInfo.isCrossing, static_cast<int32_t>(dragReportCase.isCrossing));
135 MockContainer::TearDown();
136 }
137 }
138 }
139
140 /**
141 * @tc.steps: step1. test drag stop report.
142 */
143 for (const auto& isCrossing : crossingEnd) {
144 for (const auto& startResult : dragStartResult) {
145 for (const auto& stopResult : dragStopResult) {
146 DragReportData dragReportCase = DragReportData(isCrossing,
147 startResult, stopResult, 1, DragReporterPharse::DRAG_STOP);
148 MockContainer::SetUp();
149 auto container = MockContainer::Current();
150 container->pipelineContext_ = NG::MockPipelineContext::GetCurrent();
151 auto containerId = MockContainer::CurrentId();
152 DragReporterTestNg::SetDragStartReportData(dragReportCase);
153 auto mockTaskExecutor = AceType::MakeRefPtr<MockTaskExecutor>();
154 MockPipelineContext::GetCurrentContext()->taskExecutor_ = mockTaskExecutor;
155 DragDropBehaviorReporter::GetInstance().Submit(dragReportCase.dragBehavior, containerId);
156 DragInfo dragReportInfo = MockEventReport::dragInfo_;
157 EXPECT_EQ(dragReportInfo.result, static_cast<int32_t>(dragReportCase.stopResult));
158 EXPECT_EQ(dragReportInfo.summaryNum, static_cast<int32_t>(dragReportCase.summaryNum));
159 EXPECT_EQ(dragReportInfo.summaryType, dragReportCase.summaryType);
160 EXPECT_EQ(dragReportInfo.isCrossing, static_cast<int32_t>(dragReportCase.isCrossing));
161 MockContainer::TearDown();
162 }
163 }
164 }
165 }
166 } // namespace OHOS::Ace::NG
167