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 #include "test/unittest/core/base/frame_node_test_ng.h" 16 17 #include "core/event/touch_event.h" 18 19 using namespace testing; 20 using namespace testing::ext; 21 22 namespace OHOS::Ace::NG { 23 namespace {} // namespace 24 25 /** 26 * @tc.name: ResetLayoutAlgorithmTest1 27 * @tc.desc: Test the function ResetLayoutAlgorithm with default param 28 * @tc.type: FUNC 29 */ 30 HWTEST_F(FrameNodeTestNg, ResetLayoutAlgorithmTest1, TestSize.Level1) 31 { 32 /** 33 * @tc.steps: step1. create frameNode. 34 */ 35 auto frameNode1 = FrameNode::CreateFrameNode("framenode", 1, AceType::MakeRefPtr<Pattern>(), true); 36 auto frameNode2 = FrameNode::CreateFrameNode("framenode", 2, AceType::MakeRefPtr<Pattern>()); 37 auto frameNode3 = FrameNode::CreateFrameNode("framenode", 3, AceType::MakeRefPtr<Pattern>()); 38 /** 39 * @tc.steps: step2. mount nodes and create layoutAlgorithm 40 */ 41 frameNode2->MountToParent(frameNode1); 42 frameNode3->MountToParent(frameNode2); 43 EXPECT_NE(frameNode1->GetLayoutAlgorithm(), nullptr); 44 EXPECT_NE(frameNode2->GetLayoutAlgorithm(), nullptr); 45 EXPECT_NE(frameNode3->GetLayoutAlgorithm(), nullptr); 46 /** 47 * @tc.steps: step2. start to clear. layoutAlgo of frameNode1 will not not be cleared 48 */ 49 frameNode1->ClearSubtreeLayoutAlgorithm(false); 50 EXPECT_TRUE(frameNode1->HasLayoutAlgorithm()); 51 EXPECT_FALSE(frameNode2->HasLayoutAlgorithm()); 52 EXPECT_FALSE(frameNode3->HasLayoutAlgorithm()); 53 } 54 55 /** 56 * @tc.name: ResetLayoutAlgorithmTest2 57 * @tc.desc: Test the function ResetLayoutAlgorithm with default param 58 * @tc.type: FUNC 59 */ 60 HWTEST_F(FrameNodeTestNg, ResetLayoutAlgorithmTest2, TestSize.Level1) 61 { 62 /** 63 * @tc.steps: step1. create frameNode. 64 */ 65 auto frameNode1 = FrameNode::CreateFrameNode("framenode", 1, AceType::MakeRefPtr<Pattern>(), true); 66 auto frameNode2 = FrameNode::CreateFrameNode("framenode", 2, AceType::MakeRefPtr<Pattern>()); 67 auto frameNode3 = FrameNode::CreateFrameNode("framenode", 3, AceType::MakeRefPtr<Pattern>()); 68 69 /** 70 * @tc.steps: step2. mount nodes and create layoutAlgorithm 71 */ 72 frameNode2->MountToParent(frameNode1); 73 frameNode3->MountToParent(frameNode2); 74 EXPECT_NE(frameNode1->GetLayoutAlgorithm(), nullptr); 75 EXPECT_NE(frameNode2->GetLayoutAlgorithm(), nullptr); 76 EXPECT_NE(frameNode3->GetLayoutAlgorithm(), nullptr); 77 /** 78 * @tc.steps: step3. start to clear. layoutAlgo of frameNode1 will not not be cleared 79 */ 80 frameNode1->ClearSubtreeLayoutAlgorithm(true); 81 EXPECT_FALSE(frameNode1->HasLayoutAlgorithm()); 82 EXPECT_FALSE(frameNode2->HasLayoutAlgorithm()); 83 EXPECT_FALSE(frameNode3->HasLayoutAlgorithm()); 84 } 85 86 /** 87 * @tc.name: ResetLayoutAlgorithmTest3 88 * @tc.desc: Test the function ResetLayoutAlgorithm and node3 should still have layoutAlgorithm 89 * because chain broke at node2 90 * @tc.type: FUNC 91 */ 92 HWTEST_F(FrameNodeTestNg, ResetLayoutAlgorithmTest3, TestSize.Level1) 93 { 94 /** 95 * @tc.steps: step1. create frameNode. 96 */ 97 auto frameNode1 = FrameNode::CreateFrameNode("framenode", 1, AceType::MakeRefPtr<Pattern>(), true); 98 auto frameNode2 = FrameNode::CreateFrameNode("framenode", 2, AceType::MakeRefPtr<Pattern>()); 99 auto frameNode3 = FrameNode::CreateFrameNode("framenode", 3, AceType::MakeRefPtr<Pattern>()); 100 /** 101 * @tc.steps: step2. mount nodes and create layoutAlgorithm 102 */ 103 frameNode2->MountToParent(frameNode1); 104 frameNode3->MountToParent(frameNode2); 105 EXPECT_NE(frameNode1->GetLayoutAlgorithm(), nullptr); 106 EXPECT_NE(frameNode3->GetLayoutAlgorithm(), nullptr); 107 /** 108 * @tc.steps: step3. start to clear. layoutAlgo of frameNode1 will not not be cleared 109 */ 110 frameNode1->ClearSubtreeLayoutAlgorithm(true); 111 EXPECT_FALSE(frameNode1->HasLayoutAlgorithm()); 112 EXPECT_FALSE(frameNode2->HasLayoutAlgorithm()); 113 EXPECT_TRUE(frameNode3->HasLayoutAlgorithm()); 114 } 115 116 /** 117 * @tc.name: ResetLayoutAlgorithmTest4 118 * @tc.desc: Test the function ResetLayoutAlgorithm with clearing the whole tree 119 * @tc.type: FUNC 120 */ 121 HWTEST_F(FrameNodeTestNg, ResetLayoutAlgorithmTest4, TestSize.Level1) 122 { 123 /** 124 * @tc.steps: step1. create frameNode. 125 */ 126 auto frameNode1 = FrameNode::CreateFrameNode("framenode", 1, AceType::MakeRefPtr<Pattern>(), true); 127 auto frameNode2 = FrameNode::CreateFrameNode("framenode", 2, AceType::MakeRefPtr<Pattern>()); 128 auto frameNode3 = FrameNode::CreateFrameNode("framenode", 3, AceType::MakeRefPtr<Pattern>()); 129 /** 130 * @tc.steps: step2. mount nodes and create layoutAlgorithm 131 */ 132 frameNode2->MountToParent(frameNode1); 133 frameNode3->MountToParent(frameNode2); 134 EXPECT_NE(frameNode1->GetLayoutAlgorithm(), nullptr); 135 EXPECT_NE(frameNode3->GetLayoutAlgorithm(), nullptr); 136 /** 137 * @tc.steps: step3. start to clear. layoutAlgo of frameNode1 will not not be cleared 138 */ 139 frameNode1->ClearSubtreeLayoutAlgorithm(true, true); 140 EXPECT_FALSE(frameNode1->HasLayoutAlgorithm()); 141 EXPECT_FALSE(frameNode2->HasLayoutAlgorithm()); 142 EXPECT_FALSE(frameNode3->HasLayoutAlgorithm()); 143 } 144 /** 145 * @tc.name: FrameNodeGetExtraCustomProperty001 146 * @tc.desc: Test GetExtraCustomProperty. 147 * @tc.type: FUNC 148 */ 149 HWTEST_F(FrameNodeTestNg, FrameNodeGetExtraCustomProperty001, TestSize.Level1) 150 { 151 /** 152 * @tc.steps: step1. initialize parameters. 153 */ 154 auto frameNode = FrameNode::CreateFrameNode("main", 1, AceType::MakeRefPtr<Pattern>(), true); 155 char value[7]= "value1"; 156 /** 157 * @tc.steps: step2. set key is value1 158 * @tc.expected: expect result value. 159 */ 160 frameNode->setIsCNode(true); 161 frameNode->AddExtraCustomProperty("key", value); 162 bool result = frameNode->GetExtraCustomProperty("key1"); 163 EXPECT_EQ(result, false); 164 } 165 166 /** 167 * @tc.name: FrameNodeGetExtraCustomProperty002 168 * @tc.desc: Test GetExtraCustomProperty. 169 * @tc.type: FUNC 170 */ 171 HWTEST_F(FrameNodeTestNg, FrameNodeGetExtraCustomProperty002, TestSize.Level1) 172 { 173 /** 174 * @tc.steps: step1. initialize parameters. 175 */ 176 auto frameNode = FrameNode::CreateFrameNode("main", 1, AceType::MakeRefPtr<Pattern>(), true); 177 178 /** 179 * @tc.steps: step2. set key is value 180 * @tc.expected: expect result value. 181 */ 182 char value[7]= "value1"; 183 frameNode->setIsCNode(true); 184 frameNode->AddExtraCustomProperty("key", value); 185 bool result = frameNode->GetExtraCustomProperty("key"); 186 EXPECT_EQ(result, true); 187 } 188 189 /** 190 * @tc.name: FrameNodeRemoveExtraCustomProperty001 191 * @tc.desc: Test RemoveExtraCustomProperty. 192 * @tc.type: FUNC 193 */ 194 HWTEST_F(FrameNodeTestNg, FrameNodeRemoveExtraCustomProperty001, TestSize.Level1) 195 { 196 /** 197 * @tc.steps: step1. initialize parameters. 198 */ 199 auto frameNode = FrameNode::CreateFrameNode("main", 1, AceType::MakeRefPtr<Pattern>(), true); 200 201 /** 202 * @tc.steps: step2. set key is value1, remove key. 203 * @tc.expected: expect result false. 204 */ 205 char value[7]= "value1"; 206 frameNode->setIsCNode(true); 207 frameNode->AddExtraCustomProperty("key", value); 208 frameNode->RemoveExtraCustomProperty("key"); 209 bool result = frameNode->GetExtraCustomProperty("key"); 210 EXPECT_EQ(result, false); 211 } 212 213 /** 214 * @tc.name: FrameTestNg100 215 * @tc.desc: Test CollectSelfAxisResult. 216 * @tc.type: FUNC 217 */ 218 HWTEST_F(FrameNodeTestNg, FrameNodeTestNg100, TestSize.Level1) 219 { 220 /** 221 * @tc.steps: step1. initialize parameters. 222 */ 223 auto frameNode = FrameNode::CreateFrameNode("main", 1, AceType::MakeRefPtr<Pattern>(), true); 224 PointF globalPoint; 225 PointF localPoint; 226 PointF parentRevertPoint; 227 AxisTestResult onAxisResult; 228 HitTestResult testResult; 229 TouchRestrict touchRestrict; 230 bool isConsumed = true; 231 bool isPreventBubbling = false; 232 SystemProperties::debugEnabled_ = true; 233 auto eventHub = AceType::MakeRefPtr<EventHub>(); 234 eventHub->host_ = AceType::WeakClaim(AceType::RawPtr(frameNode)); 235 auto gestureEventHub = AceType::MakeRefPtr<GestureEventHub>(AceType::WeakClaim(AceType::RawPtr(eventHub))); 236 gestureEventHub->SetHitTestMode(HitTestMode::HTMNONE); 237 eventHub->SetGestureEventHub(gestureEventHub); 238 frameNode->eventHub_ = eventHub; 239 frameNode->CollectSelfAxisResult(globalPoint, localPoint, isConsumed, parentRevertPoint, 240 onAxisResult, isPreventBubbling, testResult, touchRestrict); 241 EXPECT_EQ(testResult, HitTestResult::BUBBLING); 242 gestureEventHub->SetHitTestMode(HitTestMode::HTMBLOCK); 243 isConsumed = false; 244 frameNode->CollectSelfAxisResult(globalPoint, localPoint, isConsumed, parentRevertPoint, 245 onAxisResult, isPreventBubbling, testResult, touchRestrict); 246 EXPECT_EQ(testResult, HitTestResult::STOP_BUBBLING); 247 } 248 249 /** 250 * @tc.name: FrameTestNg101 251 * @tc.desc: Test GetOrCreateFocusHub. 252 * @tc.type: FUNC 253 */ 254 HWTEST_F(FrameNodeTestNg, FrameNodeTestNg101, TestSize.Level1) 255 { 256 /** 257 * @tc.steps: step1. initialize parameters. 258 */ 259 auto frameNode = FrameNode::CreateFrameNode("main", 1, AceType::MakeRefPtr<Pattern>(), true); 260 frameNode->focusHub_ = nullptr; 261 frameNode->pattern_ = nullptr; 262 auto focusHub = frameNode->GetOrCreateFocusHub(); 263 EXPECT_NE(focusHub, nullptr); 264 auto focusHub2 = frameNode->GetOrCreateFocusHub(); 265 EXPECT_NE(focusHub2, nullptr); 266 frameNode->focusHub_ = nullptr; 267 frameNode->pattern_ = AceType::MakeRefPtr<Pattern>(); 268 auto focusHub3 = frameNode->GetOrCreateFocusHub(); 269 EXPECT_NE(focusHub3, nullptr); 270 FocusPattern focusPattern = { FocusType::NODE, false }; 271 auto focusHub4 = frameNode->GetOrCreateFocusHub(focusPattern); 272 EXPECT_NE(focusHub4, nullptr); 273 frameNode->focusHub_ = nullptr; 274 frameNode->pattern_ = nullptr; 275 auto focusHub5 = frameNode->GetOrCreateFocusHub(focusPattern); 276 frameNode->pattern_ = AceType::MakeRefPtr<Pattern>(); 277 EXPECT_NE(focusHub5, nullptr); 278 } 279 280 /** 281 * @tc.name: FrameTestNg102 282 * @tc.desc: Test Window state and position. 283 * @tc.type: FUNC 284 */ 285 HWTEST_F(FrameNodeTestNg, FrameNodeTestNg102, TestSize.Level1) 286 { 287 /** 288 * @tc.steps: step1. initialize parameters. 289 */ 290 auto frameNode = FrameNode::CreateFrameNode("main", 1, AceType::MakeRefPtr<Pattern>(), true); 291 frameNode->OnWindowActivated(); 292 frameNode->OnWindowDeactivated(); 293 frameNode->GetPositionToScreen(); 294 frameNode->GetPositionToScreenWithTransform(); 295 EXPECT_NE(frameNode->renderContext_, nullptr); 296 auto parent = FrameNode::CreateFrameNode("Parent", 10, AceType::MakeRefPtr<Pattern>(), true); 297 auto one = FrameNode::CreateFrameNode("One", 20, AceType::MakeRefPtr<Pattern>()); 298 parent->AddChild(one); 299 parent->topWindowBoundary_ = false; 300 one->GetPaintRectOffsetNG(true, false); 301 EXPECT_NE(one->renderContext_, nullptr); 302 } 303 304 /** 305 * @tc.name: FrameTestNg103 306 * @tc.desc: Test GetPaintRectToWindowWithTransform. 307 * @tc.type: FUNC 308 */ 309 HWTEST_F(FrameNodeTestNg, FrameNodeTestNg103, TestSize.Level1) 310 { 311 auto frameNode = FrameNode::CreateFrameNode("main", 1, AceType::MakeRefPtr<Pattern>(), true); 312 auto child = FrameNode::CreateFrameNode("child", 20, AceType::MakeRefPtr<Pattern>()); 313 frameNode->AddChild(child); 314 child->GetPaintRectToWindowWithTransform(); 315 EXPECT_NE(child->geometryNode_, nullptr); 316 } 317 318 /** 319 * @tc.name: FrameTestNg104 320 * @tc.desc: Test UpdateAccessibilityNodeRect. 321 * @tc.type: FUNC 322 */ 323 HWTEST_F(FrameNodeTestNg, FrameNodeTestNg104, TestSize.Level1) 324 { 325 auto frameNode = FrameNode::CreateFrameNode("main", 1, AceType::MakeRefPtr<Pattern>(), true); 326 AceApplicationInfo::GetInstance().isAccessibilityEnabled_ = true; 327 frameNode->OnAccessibilityEvent(AccessibilityEventType::CLICK, ""); 328 frameNode->UpdateAccessibilityNodeRect(); 329 EXPECT_NE(frameNode->renderContext_, nullptr); 330 } 331 332 /** 333 * @tc.name: FrameTestNg105 334 * @tc.desc: Test OnAutoEventParamUpdate. 335 * @tc.type: FUNC 336 */ 337 HWTEST_F(FrameNodeTestNg, FrameNodeTestNg105, TestSize.Level1) 338 { 339 auto frameNode = FrameNode::CreateFrameNode("framenode", 1, AceType::MakeRefPtr<Pattern>(), true); 340 auto jsonValue = std::make_unique<JsonValue>(); 341 jsonValue->Put(Recorder::ORIGIN_PARAM, "ORIGIN_PARAM"); 342 frameNode->exposureProcessor_ = nullptr; 343 auto exposureCfg = std::make_unique<JsonValue>(); 344 exposureCfg->Put(Recorder::EXPOSURE_CONFIG_RATIO, 2.2f); 345 exposureCfg->Put(Recorder::EXPOSURE_CONFIG_DURATION, 5); 346 jsonValue->Put(Recorder::EXPOSURE_CONFIG_PARAM, exposureCfg); 347 frameNode->OnAutoEventParamUpdate(jsonValue->ToString()); 348 EXPECT_EQ(frameNode->exposureProcessor_, nullptr); 349 auto ep = AceType::MakeRefPtr<Recorder::ExposureProcessor>("test", "0"); 350 frameNode->SetExposureProcessor(ep); 351 EXPECT_NE(frameNode->exposureProcessor_, nullptr); 352 RectF visibleRect; 353 RectF frameRect; 354 frameNode->GetVisibleRect(visibleRect, frameRect); 355 EXPECT_NE(frameNode->renderContext_, nullptr); 356 } 357 358 /** 359 * @tc.name: FrameTestNg106 360 * @tc.desc: Test GetVisibleRectWithClip. 361 * @tc.type: FUNC 362 */ 363 HWTEST_F(FrameNodeTestNg, FrameNodeTestNg106, TestSize.Level1) 364 { 365 auto frameNode = FrameNode::CreateFrameNode("framenode", 1, AceType::MakeRefPtr<Pattern>(), true); 366 RectF visibleRect, visibleInnerRect, frameRect; 367 frameNode->GetVisibleRectWithClip(visibleRect, visibleInnerRect, frameRect); 368 auto parent = FrameNode::CreateFrameNode("parent", 20, AceType::MakeRefPtr<Pattern>()); 369 parent->AddChild(frameNode); 370 frameNode->isWindowBoundary_ = false; 371 frameNode->onMainTree_ = true; 372 frameNode->GetVisibleRectWithClip(visibleRect, visibleInnerRect, frameRect); 373 EXPECT_FALSE(frameNode->isWindowBoundary_); 374 } 375 376 /** 377 * @tc.name: GetCacheVisibleRectTest 378 * @tc.desc: Test GetCacheVisibleRectTest. 379 * @tc.type: FUNC 380 */ 381 HWTEST_F(FrameNodeTestNg, GetCacheVisibleRectTest001, TestSize.Level1) 382 { 383 /** 384 * @tc.steps: step1. initialize parameters. 385 */ 386 auto frameNode = FrameNode::CreateFrameNode("test", 1, AceType::MakeRefPtr<Pattern>(), true); 387 auto parentNode = FrameNode::CreateFrameNode("testParent", 1, AceType::MakeRefPtr<Pattern>(), true); 388 parentNode->AddChild(frameNode); 389 /** 390 * @tc.steps: step2. set key is value1, remove key. 391 * @tc.expected: expect result false. 392 */ 393 auto result = frameNode->GetCacheVisibleRect(0, false); 394 EXPECT_EQ(result.cumulativeScale.x, 1.0); 395 parentNode->GetRenderContext()->UpdateClipEdge(true); 396 result = frameNode->GetCacheVisibleRect(1, true); 397 EXPECT_EQ(result.cumulativeScale.x, 1.0); 398 frameNode->isWindowBoundary_ = true; 399 result = frameNode->GetCacheVisibleRect(1, true); 400 EXPECT_EQ(result.cumulativeScale.x, 1.0); 401 } 402 403 /** 404 * @tc.name: DumpSafeAreaInfoTest 405 * @tc.desc: Test DumpSafeAreaInfo. 406 * @tc.type: FUNC 407 */ 408 HWTEST_F(FrameNodeTestNg, DumpSafeAreaInfoTest001, TestSize.Level1) 409 { 410 /** 411 * @tc.steps: step1. initialize parameters. 412 */ 413 auto frameNode = FrameNode::CreateFrameNode("test", 1, AceType::MakeRefPtr<Pattern>(), true); 414 std::unique_ptr<JsonValue> json = JsonUtil::Create(true); 415 /** 416 * @tc.steps: step2. set key is value1, remove key. 417 * @tc.expected: expect result false. 418 */ 419 frameNode->DumpExtensionHandlerInfo(json); 420 421 RefPtr<NG::DrawModifier> drawModifier = AceType::MakeRefPtr<NG::DrawModifier>(); 422 ASSERT_NE(drawModifier, nullptr); 423 frameNode->SetDrawModifier(drawModifier); 424 425 frameNode->DumpExtensionHandlerInfo(json); 426 EXPECT_FALSE(json->GetValue("HasCustomerMeasure")->GetBool()); 427 } 428 429 /** 430 * @tc.name: GetCurrentPageRootNodeTest001 431 * @tc.desc: Test GetCurrentPageRootNode. 432 * @tc.type: FUNC 433 */ 434 HWTEST_F(FrameNodeTestNg, GetCurrentPageRootNodeTest001, TestSize.Level1) 435 { 436 /** 437 * @tc.steps: step1. initialize parameters. 438 */ 439 auto frameNode = FrameNode::CreateFrameNode("page", 1, AceType::MakeRefPtr<Pattern>(), true); 440 auto child = FrameNode::CreateFrameNode(V2::JS_VIEW_ETS_TAG, 3, AceType::MakeRefPtr<Pattern>(), true); 441 frameNode->AddChild(child); 442 EXPECT_FALSE(frameNode->GetCurrentPageRootNode() != nullptr); 443 } 444 445 /** 446 * @tc.name: GetCurrentPageRootNodeTest002 447 * @tc.desc: Test GetCurrentPageRootNode. 448 * @tc.type: FUNC 449 */ 450 HWTEST_F(FrameNodeTestNg, GetCurrentPageRootNodeTest002, TestSize.Level1) 451 { 452 /** 453 * @tc.steps: step1. initialize parameters. 454 */ 455 auto frameNode = FrameNode::CreateFrameNode("page", 1, AceType::MakeRefPtr<Pattern>(), true); 456 auto child = FrameNode::CreateFrameNode("text", 3, AceType::MakeRefPtr<Pattern>(), true); 457 frameNode->AddChild(child); 458 EXPECT_TRUE(frameNode->GetCurrentPageRootNode() != nullptr); 459 } 460 } // namespace OHOS::Ace::NG