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 "test/unittest/core/pipeline/pipeline_context_test_ng.h" 17 // Add the following two macro definitions to test the private and protected method. 18 #define private public 19 #define protected public 20 21 #include "core/accessibility/accessibility_manager_ng.h" 22 #include "core/components_ng/pattern/container_modal/container_modal_pattern.h" 23 #include "core/components_ng/pattern/text_field/text_field_manager.h" 24 #include "core/event/axis_event.h" 25 #include "core/event/mouse_event.h" 26 27 using namespace testing; 28 using namespace testing::ext; 29 30 namespace OHOS::Ace { 31 namespace NG { 32 33 /** 34 * @tc.name: PipelineContextTestNg130 35 * @tc.desc: Test the function FlushMouseEventVoluntarily. 36 * @tc.type: FUNC 37 */ 38 HWTEST_F(PipelineContextTestNg, PipelineContextTestNg130, TestSize.Level1) 39 { 40 /** 41 * @tc.steps1: initialize parameters. 42 * @tc.expected: Create MouseEvent, then initialize pipeline. 43 */ 44 ASSERT_NE(context_, nullptr); 45 context_->SetRootSize(1.0f, 800, 1600); 46 MouseEvent event; 47 event.x = 12.345f; 48 event.y = 12.345f; 49 context_->lastMouseEvent_ = std::make_unique<MouseEvent>(event); 50 context_->lastMouseEvent_->action = MouseAction::WINDOW_LEAVE; 51 context_->SetupRootElement(); 52 EXPECT_NE(context_->rootNode_, nullptr); 53 context_->SetEventManager(AceType::MakeRefPtr<EventManager>()); 54 context_->viewScale_ = DEFAULT_DOUBLE2; 55 56 /** 57 * @tc.steps2: Call the function FlushMouseEventVoluntarily. 58 * @tc.expected: Test isNeedFlushMouseEvent_ is Changed to REJECT. 59 */ 60 context_->FlushMouseEventVoluntarily(); 61 EXPECT_NE(context_->eventManager_, nullptr); 62 context_->isNeedFlushMouseEvent_ = MockFlushEventType::REJECT; 63 context_->FlushMouseEventVoluntarily(); 64 EXPECT_NE(context_->eventManager_, nullptr); 65 } 66 67 /** 68 * @tc.name: PipelineContextTestNg131 69 * @tc.desc: Test the function FlushUITaskWithSingleDirtyNode. 70 * @tc.type: FUNC 71 */ 72 HWTEST_F(PipelineContextTestNg, PipelineContextTestNg131, TestSize.Level1) 73 { 74 /** 75 * @tc.steps1: initialize parameters. 76 * @tc.expected: initialize pipeline. 77 */ 78 ASSERT_NE(context_, nullptr); 79 context_->SetRootSize(1.0f, 800, 1600); 80 frameNodeId_ = ElementRegister::GetInstance()->MakeUniqueId(); 81 auto frameNode_1 = FrameNode::GetOrCreateFrameNode(TEST_TAG, frameNodeId_, nullptr); 82 EXPECT_NE(frameNode_1, nullptr); 83 auto layoutProperty = AceType::MakeRefPtr<LayoutProperty>(); 84 frameNode_1->SetLayoutProperty(layoutProperty); 85 86 /** 87 * @tc.steps2: Call the function FlushUITaskWithSingleDirtyNode. 88 * @tc.expected: Test the reaction of the function at different Rect. 89 */ 90 context_->FlushUITaskWithSingleDirtyNode(frameNode_1); 91 NG::RectF testRect = { 10.0f, 10.0f, 10.0f, 10.0f }; 92 layoutProperty->SetLayoutRect(testRect); 93 context_->FlushUITaskWithSingleDirtyNode(frameNode_1); 94 EXPECT_NE(frameNode_1->GetLayoutProperty(), nullptr); 95 } 96 97 /** 98 * @tc.name: PipelineContextTestNg132 99 * @tc.desc: Test the function FlushUITaskWithSingleDirtyNode. 100 * @tc.type: FUNC 101 */ 102 HWTEST_F(PipelineContextTestNg, PipelineContextTestNg132, TestSize.Level1) 103 { 104 /** 105 * @tc.steps1: initialize parameters. 106 * @tc.expected: initialize pipeline, SafeAreaManager, TextFieldManagerNG. 107 */ 108 ASSERT_NE(context_, nullptr); 109 context_->SetRootSize(1.0f, 800, 1600); 110 auto safeAreaManager = AceType::MakeRefPtr<SafeAreaManager>(); 111 context_->safeAreaManager_ = safeAreaManager; 112 auto textFieldManager = AceType::MakeRefPtr<TextFieldManagerNG>(); 113 textFieldManager->SetHeight(20); 114 context_->SetTextFieldManager(textFieldManager); 115 116 /** 117 * @tc.steps2: Call the function DoKeyboardAvoidFunc. 118 * @tc.expected: Test the stability of this function. 119 */ 120 context_->DoKeyboardAvoidFunc(400.0f, 0.0, 400.0f, false); 121 EXPECT_NE(context_->textFieldManager_, nullptr); 122 } 123 124 /** 125 * @tc.name: PipelineContextTestNg133 126 * @tc.desc: Test the function SetIsFocusActive. 127 * @tc.type: FUNC 128 */ 129 HWTEST_F(PipelineContextTestNg, PipelineContextTestNg133, TestSize.Level1) 130 { 131 /** 132 * @tc.steps1: initialize parameters. 133 * @tc.expected: initialize pipeline, properties. 134 */ 135 ASSERT_NE(context_, nullptr); 136 context_->SetRootSize(1.0f, 800, 1600); 137 context_->SetupRootElement(); 138 EXPECT_NE(context_->rootNode_, nullptr); 139 SystemProperties::focusCanBeActive_.store(true); 140 context_->isFocusActive_= true; 141 142 /** 143 * @tc.steps2: Call the function SetIsFocusActive. 144 * @tc.expected: Test the stability of this function. 145 */ 146 context_->SetIsFocusActive(false, FocusActiveReason::USE_API); 147 EXPECT_FALSE(context_->isFocusActive_); 148 } 149 150 /** 151 * @tc.name: PipelineContextTestNg134 152 * @tc.desc: Test the function SetHostParentOffsetToWindow. 153 * @tc.type: FUNC 154 */ 155 HWTEST_F(PipelineContextTestNg, PipelineContextTestNg134, TestSize.Level1) 156 { 157 /** 158 * @tc.steps1: initialize parameters. 159 * @tc.expected: initialize pipeline, offfset. 160 */ 161 ASSERT_NE(context_, nullptr); 162 context_->SetRootSize(1.0f, 800, 1600); 163 context_->SetupRootElement(); 164 EXPECT_NE(context_->rootNode_, nullptr); 165 Offset offfset(1.0, 1.0); 166 167 /** 168 * @tc.steps2: Call the function SetHostParentOffsetToWindow. 169 * @tc.expected: Test the stability of this function. 170 */ 171 context_->SetHostParentOffsetToWindow(Offset(offfset.GetX(), offfset.GetY())); 172 EXPECT_EQ(context_->lastHostParentOffsetToWindow_, offfset); 173 } 174 175 /** 176 * @tc.name: PipelineContextTestNg135 177 * @tc.desc: Test the function GetResponseRegion. 178 * @tc.type: FUNC 179 */ 180 HWTEST_F(PipelineContextTestNg, PipelineContextTestNg135, TestSize.Level1) 181 { 182 /** 183 * @tc.steps1: initialize parameters. 184 * @tc.expected: initialize pipeline, frame node. 185 */ 186 ASSERT_NE(context_, nullptr); 187 context_->SetRootSize(1.0f, 800, 1600); 188 context_->SetupRootElement(); 189 EXPECT_NE(context_->rootNode_, nullptr); 190 auto frameNode_1 = FrameNode::GetOrCreateFrameNode(TEST_TAG, frameNodeId_, nullptr); 191 EXPECT_NE(frameNode_1, nullptr); 192 193 /** 194 * @tc.steps2: Call the function GetResponseRegion. 195 * @tc.expected: Test the stability of this function. 196 */ 197 std::string result = context_->GetResponseRegion(frameNode_1); 198 EXPECT_TRUE(result.empty()); 199 } 200 201 /** 202 * @tc.name: PipelineContextTestNg136 203 * @tc.desc: Test the function SetWindowContainerColor. 204 * @tc.type: FUNC 205 */ 206 HWTEST_F(PipelineContextTestNg, PipelineContextTestNg136, TestSize.Level1) 207 { 208 /** 209 * @tc.steps1: initialize parameters. 210 * @tc.expected: initialize pipeline. 211 */ 212 ASSERT_NE(context_, nullptr); 213 context_->SetupRootElement(); 214 215 /** 216 * @tc.steps2: Call the function SetWindowContainerColor. 217 * @tc.expected: Test if this function is available. 218 */ 219 context_->SetWindowContainerColor(Color::BLUE, Color::BLACK); 220 EXPECT_NE(context_->rootNode_, nullptr); 221 auto containerNode = AceType::DynamicCast<FrameNode>(context_->rootNode_->GetChildren().front()); 222 EXPECT_NE(containerNode, nullptr); 223 auto containerPattern = containerNode->GetPattern<ContainerModalPattern>(); 224 EXPECT_NE(containerPattern, nullptr); 225 EXPECT_EQ(containerPattern->activeColor_, Color::BLUE); 226 227 /** 228 * @tc.steps3: Call the function GetContainerControlButtonVisible. 229 * @tc.expected: Test if this function is available. 230 */ 231 bool isControlButtonVisible = context_->GetContainerControlButtonVisible(); 232 EXPECT_FALSE(isControlButtonVisible); 233 234 /** 235 * @tc.steps4: Call the function GetContainerCustomTitleVisible. 236 * @tc.expected: Test if this function is available. 237 */ 238 bool isCustomTitleVisible = context_->GetContainerCustomTitleVisible(); 239 EXPECT_FALSE(isCustomTitleVisible); 240 241 /** 242 * @tc.steps4: Call the function GetContainerFloatingTitleVisible. 243 * @tc.expected: Test if this function is available. 244 */ 245 bool isFloatingTitleVisible = context_->GetContainerFloatingTitleVisible(); 246 EXPECT_FALSE(isFloatingTitleVisible); 247 } 248 249 /** 250 * @tc.name: PipelineContextTestNg137 251 * @tc.desc: Test the function OnMouseMoveEventForAxisEvent. 252 * @tc.type: FUNC 253 */ 254 HWTEST_F(PipelineContextTestNg, PipelineContextTestNg137, TestSize.Level1) 255 { 256 /** 257 * @tc.steps1: initialize parameters. 258 * @tc.expected: initialize pipeline, mouse event, frame node, event manager. 259 */ 260 ASSERT_NE(context_, nullptr); 261 context_->SetupRootElement(); 262 EXPECT_NE(context_->rootNode_, nullptr); 263 MouseEvent mouseEvent1; 264 mouseEvent1.x = 100.0f; 265 mouseEvent1.y = 200.0f; 266 mouseEvent1.action = MouseAction::MOVE; 267 mouseEvent1.button = MouseButton::NONE_BUTTON; 268 auto frameNode_1 = FrameNode::GetOrCreateFrameNode(TEST_TAG, frameNodeId_, nullptr); 269 EXPECT_NE(frameNode_1, nullptr); 270 271 /** 272 * @tc.steps2: Call the function SetEventManager. 273 * @tc.expected: Test if this function is available. 274 */ 275 context_->SetEventManager(AceType::MakeRefPtr<EventManager>()); 276 context_->accessibilityManagerNG_ = AceType::MakeRefPtr<AccessibilityManagerNG>(); 277 278 /** 279 * @tc.steps3: Call the function OnMouseMoveEventForAxisEvent. 280 * @tc.expected: Test if this function is available. 281 */ 282 context_->OnMouseMoveEventForAxisEvent(mouseEvent1, frameNode_1); 283 EXPECT_NE(context_->eventManager_, nullptr); 284 AxisEvent axisEvent; 285 286 /** 287 * @tc.steps4: Call the function DispatchAxisEventToDragDropManager. 288 * @tc.expected: Test if this function is available. 289 */ 290 axisEvent.action = AxisAction::BEGIN; 291 axisEvent.isRotationEvent = false; 292 SerializedGesture etsSerializedGesture; 293 ASSERT_NE(context_->dragDropManager_, nullptr); 294 context_->DispatchAxisEventToDragDropManager(axisEvent, frameNode_1, etsSerializedGesture); 295 296 /** 297 * @tc.steps5: Call the function DispatchAxisEventToDragDropManager. 298 * @tc.expected: Test the stability of this function. 299 */ 300 context_->dragDropManager_ = nullptr; 301 context_->isBeforeDragHandleAxis_ = true; 302 axisEvent.action = AxisAction::END; 303 context_->DispatchAxisEventToDragDropManager(axisEvent, frameNode_1, etsSerializedGesture); 304 EXPECT_FALSE(context_->isBeforeDragHandleAxis_); 305 } 306 307 /** 308 * @tc.name: PipelineContextTestNg138 309 * @tc.desc: Test the function UpdateLastMoveEvent. 310 * @tc.type: FUNC 311 */ 312 HWTEST_F(PipelineContextTestNg, PipelineContextTestNg138, TestSize.Level1) 313 { 314 /** 315 * @tc.steps1: initialize parameters. 316 * @tc.expected: initialize pipeline, mouse event. 317 */ 318 ASSERT_NE(context_, nullptr); 319 MouseEvent mouseEvent; 320 mouseEvent.x = 0.1f; 321 mouseEvent.y = 0.1f; 322 mouseEvent.button = MouseButton::NONE_BUTTON; 323 mouseEvent.action = MouseAction::MOVE; 324 mouseEvent.sourceType = SourceType::MOUSE; 325 mouseEvent.touchEventId = 101; 326 327 /** 328 * @tc.steps2: Call the function UpdateLastMoveEvent. 329 * @tc.expected: Test if this function is available. 330 */ 331 context_->UpdateLastMoveEvent(mouseEvent); 332 ASSERT_NE(context_->lastMouseEvent_, nullptr); 333 EXPECT_EQ(context_->lastMouseEvent_->x, 0.1f); 334 EXPECT_EQ(context_->lastMouseEvent_->y, 0.1f); 335 EXPECT_EQ(context_->lastMouseEvent_->button, MouseButton::NONE_BUTTON); 336 EXPECT_EQ(context_->lastMouseEvent_->action, MouseAction::MOVE); 337 EXPECT_EQ(context_->lastMouseEvent_->sourceType, SourceType::MOUSE); 338 EXPECT_EQ(context_->lastMouseEvent_->touchEventId, 101); 339 } 340 341 /** 342 * @tc.name: PipelineContextTestNg139 343 * @tc.desc: Test the function OnMouseMoveEventForAxisEvent. 344 * @tc.type: FUNC 345 */ 346 HWTEST_F(PipelineContextTestNg, PipelineContextTestNg139, TestSize.Level1) 347 { 348 /** 349 * @tc.steps1: initialize parameters. 350 * @tc.expected: initialize pipeline, touch event. 351 */ 352 ASSERT_NE(context_, nullptr); 353 context_->SetEventManager(AceType::MakeRefPtr<EventManager>()); 354 TouchEvent touchEvent; 355 touchEvent.sourceTool = SourceTool::PEN; 356 touchEvent.type = TouchType::DOWN; 357 touchEvent.force = 0.1f; 358 359 /** 360 * @tc.steps2: Call the function HandlePenHoverOut. 361 * @tc.expected: Test if this function is available. 362 */ 363 context_->HandlePenHoverOut(touchEvent); 364 365 /** 366 * @tc.steps3: Call the function OnPenHoverEvent. 367 * @tc.expected: Test if this function is available. 368 */ 369 auto frameNode_1 = FrameNode::GetOrCreateFrameNode(TEST_TAG, frameNodeId_, nullptr); 370 EXPECT_NE(frameNode_1, nullptr); 371 context_->OnPenHoverEvent(touchEvent, frameNode_1); 372 373 /** 374 * @tc.steps4: Call the function OnAccessibilityHoverEvent. 375 * @tc.expected: Test if this function is available. 376 */ 377 context_->accessibilityManagerNG_ = AceType::MakeRefPtr<AccessibilityManagerNG>(); 378 context_->OnAccessibilityHoverEvent(touchEvent, frameNode_1); 379 380 /** 381 * @tc.steps5: Call the function CompensateTouchMoveEvent. 382 * @tc.expected: Test if this function is available. 383 */ 384 touchEvent.type = TouchType::UP; 385 context_->CompensateTouchMoveEvent(touchEvent); 386 context_->touchEvents_.push_back(touchEvent); 387 context_->CompensateTouchMoveEventFromUnhandledEvents(touchEvent); 388 ASSERT_NE(context_->eventManager_, nullptr); 389 } 390 391 /** 392 * @tc.name: PipelineContextTestNg140 393 * @tc.desc: Test the function OnMouseMoveEventForAxisEvent. 394 * @tc.type: FUNC 395 */ 396 HWTEST_F(PipelineContextTestNg, PipelineContextTestNg140, TestSize.Level1) 397 { 398 /** 399 * @tc.steps1: Call the function SetIsFocusActive. 400 * @tc.expected: Test the stability of this function with different parameters passed in. 401 */ 402 ASSERT_NE(context_, nullptr); 403 context_->SetIsFocusActive(false, FocusActiveReason::USE_API, false); 404 EXPECT_FALSE(context_->autoFocusInactive_); 405 context_->SetIsFocusActive(false, FocusActiveReason::USE_API, true); 406 EXPECT_TRUE(context_->autoFocusInactive_); 407 auto result_1 = context_->SetIsFocusActive(false, FocusActiveReason::POINTER_EVENT, false); 408 EXPECT_FALSE(result_1); 409 410 /** 411 * @tc.steps2: Call the function OnCaretPositionChangeOrKeyboardHeightChange. 412 * @tc.expected: Test the stability of this function. 413 */ 414 auto safeAreaManager = AceType::MakeRefPtr<SafeAreaManager>(); 415 context_->safeAreaManager_ = safeAreaManager; 416 auto textFieldManager = AceType::MakeRefPtr<TextFieldManagerNG>(); 417 textFieldManager->SetHeight(20); 418 textFieldManager->SetUsingCustomKeyboardAvoid(false); 419 context_->SetTextFieldManager(textFieldManager); 420 context_->OnCaretPositionChangeOrKeyboardHeightChange(400.0f, 0.1f, 0.1f, nullptr, true); 421 ASSERT_NE(context_->textFieldManager_, nullptr); 422 } 423 424 } // namespace NG 425 } // namespace OHOS::Ace