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 "gtest/gtest.h" 16 #include "test/unittest/core/event/focus_hub_test_ng.h" 17 18 using namespace testing; 19 using namespace testing::ext; 20 21 namespace OHOS::Ace::NG { 22 /** 23 * @tc.name: PaintFocusState0002 24 * @tc.desc: Test the function SwitchFocus. 25 * @tc.type: FUNC 26 */ 27 HWTEST_F(FocusHubTestNg, PaintFocusState0002, TestSize.Level1) 28 { 29 /** 30 * @tc.steps: step1. Create frameNode. 31 */ 32 auto themeManager = AceType::MakeRefPtr<MockThemeManager>(); 33 MockPipelineContext::GetCurrent()->SetThemeManager(themeManager); 34 EXPECT_CALL(*themeManager, GetTheme(_)).WillRepeatedly(Return(AceType::MakeRefPtr<AppTheme>())); 35 auto frameNode = AceType::MakeRefPtr<FrameNodeOnTree>(V2::ROW_ETS_TAG, -1, 36 AceType::MakeRefPtr<Pattern>()); 37 auto child = AceType::MakeRefPtr<FrameNodeOnTree>(V2::BUTTON_ETS_TAG, -1, 38 AceType::MakeRefPtr<ButtonPattern>()); 39 auto child2 = AceType::MakeRefPtr<FrameNodeOnTree>(V2::BUTTON_ETS_TAG, -1, 40 AceType::MakeRefPtr<ButtonPattern>()); 41 child->GetOrCreateFocusHub(); 42 child2->GetOrCreateFocusHub(); 43 frameNode->AddChild(child); 44 frameNode->AddChild(child2); 45 RefPtr<EventHub> eventHub = AceType::MakeRefPtr<EventHub>(); 46 eventHub->AttachHost(frameNode); 47 auto focusHub = AceType::MakeRefPtr<FocusHub>(AceType::WeakClaim(AceType::RawPtr(eventHub))); 48 auto context = PipelineContext::GetCurrentContext(); 49 ASSERT_NE(context, nullptr); 50 auto focusManager = context->GetOrCreateFocusManager(); 51 ASSERT_NE(focusManager, nullptr); 52 focusManager->isFocusActive_ = true; 53 focusHub->focusType_ = FocusType::NODE; 54 eventHub->AddSupportedState(NG::UI_STATE_FOCUSED); 55 ASSERT_NE(eventHub->stateStyleMgr_, nullptr); 56 EXPECT_TRUE(focusHub->PaintFocusState(false)); 57 EXPECT_TRUE(focusHub->PaintFocusState(true)); 58 59 eventHub->stateStyleMgr_ = nullptr; 60 ASSERT_EQ(eventHub->stateStyleMgr_, nullptr); 61 focusManager->isFocusActive_ = true; 62 FocusBoxStyle styledStyle; 63 styledStyle.strokeColor = Color::RED; 64 focusHub->box_.SetStyle(styledStyle); 65 EXPECT_TRUE(focusHub->IsNeedPaintFocusState()); 66 focusHub->focusStyleType_ = FocusStyleType::NONE; 67 EXPECT_FALSE(focusHub->HasFocusStateStyle()); 68 EXPECT_FALSE(focusHub->PaintFocusState(false)); 69 70 focusManager->isFocusActive_ = true; 71 EXPECT_TRUE(focusHub->IsNeedPaintFocusState()); 72 focusHub->focusStyleType_ = FocusStyleType::FORCE_NONE; 73 EXPECT_FALSE(focusHub->HasFocusStateStyle()); 74 EXPECT_TRUE(focusHub->PaintFocusState(false)); 75 } 76 77 /** 78 * @tc.name: GetRootFocusHub002 79 * @tc.desc: Test the function GetRootFocusHub. 80 * @tc.type: FUNC 81 */ 82 HWTEST_F(FocusHubTestNg, GetRootFocusHub002, TestSize.Level1) 83 { 84 /** 85 * @tc.steps: step1. Create FocusHub with the WeakPtr eventHub_ owning frameNode. 86 * @tc.expected: eventHub can be updated and can get frameNode and its related properties. 87 */ 88 RefPtr<EventHub> eventHub = AceType::MakeRefPtr<EventHub>(); 89 auto frameNode = AceType::MakeRefPtr<FrameNodeOnTree>(V2::TEXT_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>()); 90 eventHub->AttachHost(frameNode); 91 92 /** 93 * @tc.steps: step2. Create focusHub and its frameNode has parent node. 94 * @tc.expected: FocusHub will return frameNode' parent focusHub. 95 */ 96 auto nodeParent = 97 AceType::MakeRefPtr<FrameNodeOnTree>(V2::BLANK_ETS_TAG, -1, AceType::MakeRefPtr<FlexLayoutPattern>()); 98 nodeParent->GetOrCreateFocusHub(); 99 frameNode->SetParent(nodeParent); 100 eventHub->AttachHost(frameNode); 101 auto focusHub = AceType::MakeRefPtr<FocusHub>(AceType::WeakClaim(AceType::RawPtr(eventHub))); 102 103 /** 104 * @tc.steps: step3. Expect the result is not nullptr. 105 */ 106 EXPECT_TRUE(focusHub->GetParentFocusHub() != nullptr); 107 ASSERT_NE(focusHub->GetRootFocusHub(), nullptr); 108 } 109 110 /** 111 * @tc.name: GetFocusLeaf001 112 * @tc.desc: Test the function GetFocusLeaf. 113 * @tc.type: FUNC 114 */ 115 HWTEST_F(FocusHubTestNg, GetFocusLeaf001, TestSize.Level1) 116 { 117 /** 118 * @tc.steps: step1. Create FocusHub with the WeakPtr eventHub_ owning frameNode. 119 * @tc.expected: eventHub can be updated and can get frameNode and its related properties. 120 */ 121 RefPtr<EventHub> eventHub = AceType::MakeRefPtr<EventHub>(); 122 auto frameNode = AceType::MakeRefPtr<FrameNodeOnTree>(V2::TEXT_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>()); 123 eventHub->AttachHost(frameNode); 124 125 /** 126 * @tc.steps: step2. Create focusHub and its frameNode has parent node. 127 * @tc.expected: FocusHub will return frameNode' parent focusHub. 128 */ 129 auto nodeParent = 130 AceType::MakeRefPtr<FrameNodeOnTree>(V2::BLANK_ETS_TAG, -1, AceType::MakeRefPtr<FlexLayoutPattern>()); 131 nodeParent->GetOrCreateFocusHub(); 132 frameNode->SetParent(nodeParent); 133 eventHub->AttachHost(frameNode); 134 auto focusHub = AceType::MakeRefPtr<FocusHub>(AceType::WeakClaim(AceType::RawPtr(eventHub))); 135 136 /** 137 * @tc.steps: step3. Set the corresponding variables to ensure entry into the target branch. 138 * @tc.expected: The result is not nullptr. 139 */ 140 focusHub->focusType_ = FocusType::NODE; 141 focusHub->focusable_ = true; 142 focusHub->parentFocusable_ = true; 143 focusHub->eventHub_ = nullptr; 144 focusHub->frameNode_ = nullptr; 145 focusHub->focusDepend_ = FocusDependence::SELF; 146 ASSERT_NE(focusHub->GetFocusLeaf(), nullptr); 147 } 148 149 /** 150 * @tc.name: LostFocusToTabStop003 151 * @tc.desc: LostFocusToTabStop 152 * @tc.type: FUNC 153 */ 154 HWTEST_F(FocusHubTestNg, LostFocusToTabStop003, TestSize.Level1) 155 { 156 /** 157 * @tc.steps: step1. Create focusHub. 158 */ 159 auto frameNode = AceType::MakeRefPtr<FrameNodeOnTree>(V2::ROW_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>()); 160 RefPtr<EventHub> eventHub = AceType::MakeRefPtr<EventHub>(); 161 eventHub->AttachHost(frameNode); 162 auto focusHub = AceType::MakeRefPtr<FocusHub>(AceType::WeakClaim(AceType::RawPtr(eventHub))); 163 auto focusNode = AceType::MakeRefPtr<FocusHub>(AceType::WeakClaim(AceType::RawPtr(eventHub))); 164 165 /** 166 * @tc.steps: step2. Set the corresponding variables to ensure entry into the target branch. 167 */ 168 focusNode->SetCurrentFocus(false); 169 auto focusHubTest = AceType::MakeRefPtr<FocusHub>(AceType::WeakClaim(AceType::RawPtr(eventHub))); 170 focusNode->lastWeakFocusNode_ = focusHubTest; 171 172 /** 173 * @tc.steps: step3. Make ensure entry into the target branch. 174 */ 175 focusHub->LostFocusToTabStop(focusNode); 176 EXPECT_NE(focusNode->lastWeakFocusNode_.Upgrade(), nullptr); 177 } 178 179 /** 180 * @tc.name: LostChildFocusToSelf002 181 * @tc.desc: LostChildFocusToSelf 182 * @tc.type: FUNC 183 */ 184 HWTEST_F(FocusHubTestNg, LostChildFocusToSelf002, TestSize.Level1) 185 { 186 /** 187 * @tc.steps: step1. Create focusHub. 188 */ 189 auto frameNode = AceType::MakeRefPtr<FrameNodeOnTree>(V2::ROW_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>()); 190 RefPtr<EventHub> eventHub = AceType::MakeRefPtr<EventHub>(); 191 eventHub->AttachHost(frameNode); 192 auto focusHub = AceType::MakeRefPtr<FocusHub>(AceType::WeakClaim(AceType::RawPtr(eventHub))); 193 focusHub->SetCurrentFocus(true); 194 195 /** 196 * @tc.steps: step2. Set the corresponding variables to ensure entry into the target branch. 197 */ 198 auto focusHubTest = AceType::MakeRefPtr<FocusHub>(AceType::WeakClaim(AceType::RawPtr(eventHub))); 199 focusHub->lastWeakFocusNode_ = focusHubTest; 200 focusHub->focusDepend_ = FocusDependence::AUTO; 201 focusHub->LostChildFocusToSelf(); 202 203 /** 204 * @tc.steps: step3. Expect the focusDepend_ is FocusDependence::SELF. 205 */ 206 EXPECT_EQ(focusHub->focusDepend_, FocusDependence::SELF); 207 } 208 209 /** 210 * @tc.name: RequestNextFocusOfKeyTab002 211 * @tc.desc: RequestNextFocusOfKeyTab 212 * @tc.type: FUNC 213 */ 214 HWTEST_F(FocusHubTestNg, RequestNextFocusOfKeyTab002, TestSize.Level1) 215 { 216 /** 217 * @tc.steps: step1. Create focusHub. 218 */ 219 auto frameNode = AceType::MakeRefPtr<FrameNodeOnTree>(V2::ROW_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>()); 220 RefPtr<EventHub> eventHub = AceType::MakeRefPtr<EventHub>(); 221 eventHub->AttachHost(frameNode); 222 auto focusHub = AceType::MakeRefPtr<FocusHub>(AceType::WeakClaim(AceType::RawPtr(eventHub))); 223 KeyEvent keyEvent; 224 keyEvent.pressedCodes.emplace_back(KeyCode::KEY_HOME); 225 FocusEvent focusEvent(keyEvent); 226 227 /** 228 * @tc.steps: step2. Set the corresponding variables to ensure entry into the target branch. 229 */ 230 focusHub->isGroup_ = false; 231 EXPECT_EQ(focusHub->IsInFocusGroup(), false); 232 focusEvent.intension = FocusIntension::TAB; 233 focusHub->GetFrameNode()->GetContext()->focusWindowId_ = 32; 234 235 /** 236 * @tc.steps: step3. Expect the ret is false. 237 */ 238 auto ret = focusHub->RequestNextFocusOfKeyTab(focusEvent); 239 EXPECT_EQ(ret, false); 240 241 /** 242 * @tc.steps: step4. Set the corresponding variables to ensure entry into the target branch. 243 */ 244 focusHub->isGroup_ = false; 245 EXPECT_EQ(focusHub->IsInFocusGroup(), false); 246 focusEvent.intension = FocusIntension::SHIFT_TAB; 247 focusHub->GetFrameNode()->GetContext()->focusWindowId_ = 32; 248 249 /** 250 * @tc.steps: step3. Expect the ret is false. 251 */ 252 ret = focusHub->RequestNextFocusOfKeyTab(focusEvent); 253 EXPECT_EQ(ret, false); 254 } 255 256 /** 257 * @tc.name: RequestNextFocus002 258 * @tc.desc: RequestNextFocus 259 * @tc.type: FUNC 260 */ 261 HWTEST_F(FocusHubTestNg, RequestNextFocus002, TestSize.Level1) 262 { 263 /** 264 * @tc.steps: step1. Create focusHub. 265 */ 266 auto frameNode = AceType::MakeRefPtr<FrameNodeOnTree>(V2::ROW_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>()); 267 RefPtr<EventHub> eventHub = AceType::MakeRefPtr<EventHub>(); 268 eventHub->AttachHost(frameNode); 269 auto focusHub = AceType::MakeRefPtr<FocusHub>(AceType::WeakClaim(AceType::RawPtr(eventHub))); 270 271 /** 272 * @tc.steps: step2. Set the corresponding variables to ensure entry into the target branch. 273 */ 274 auto focusNode = AceType::MakeRefPtr<FocusHub>(AceType::WeakClaim(AceType::RawPtr(eventHub))); 275 std::list<RefPtr<FocusHub>> focusNodes; 276 focusNode->SetCurrentFocus(true); 277 focusNodes.push_front(focusNode); 278 focusHub->lastWeakFocusNode_ = AceType::WeakClaim(AceType::RawPtr(*(focusNodes.begin()))); 279 auto ret = focusHub->CalculatePosition(); 280 EXPECT_EQ(ret, true); 281 282 /** 283 * @tc.steps: step3. Expect the result is false. 284 */ 285 auto result = focusHub->RequestNextFocus(FocusStep::DOWN); 286 EXPECT_EQ(result, false); 287 } 288 289 /** 290 * @tc.name: TryRequestFocus002 291 * @tc.desc: Test the function TryRequestFocus. 292 * @tc.type: FUNC obsolete 293 */ 294 HWTEST_F(FocusHubTestNg, TryRequestFocus002, TestSize.Level1) 295 { 296 /** 297 * @tc.steps1: create focusHub. 298 */ 299 auto frameNode = FrameNodeOnTree::CreateFrameNode("frameNode", 101, AceType::MakeRefPtr<ButtonPattern>()); 300 frameNode->GetOrCreateFocusHub(); 301 auto focusHub = frameNode->GetFocusHub(); 302 ASSERT_NE(focusHub, nullptr); 303 304 /** 305 * @tc.steps1: Build parameters. 306 */ 307 auto frameNode1 = FrameNodeOnTree::CreateFrameNode("frameNode", 101, AceType::MakeRefPtr<ButtonPattern>()); 308 frameNode1->GetOrCreateFocusHub(); 309 auto focusHub1 = frameNode1->GetFocusHub(); 310 311 /** 312 * @tc.steps: step3. Expect the ret is true. 313 */ 314 auto res = focusHub->TryRequestFocus(focusHub1, RectF(), FocusStep::LEFT); 315 ASSERT_TRUE(res); 316 } 317 318 /** 319 * @tc.name: RequestNextFocusByCustomAlgorithm002 320 * @tc.desc: 321 * @tc.type: FUNC 322 */ 323 HWTEST_F(FocusHubTestNg, RequestNextFocusByCustomAlgorithm002, TestSize.Level1) 324 { 325 /** 326 * @tc.steps1: create focusHub. 327 */ 328 auto frameNode = AceType::MakeRefPtr<FrameNodeOnTree>(V2::ROW_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>()); 329 RefPtr<EventHub> eventHub = AceType::MakeRefPtr<EventHub>(); 330 eventHub->AttachHost(frameNode); 331 auto focusHub = AceType::MakeRefPtr<FocusHub>(AceType::WeakClaim(AceType::RawPtr(eventHub))); 332 333 /** 334 * @tc.steps2: Build parameters. 335 */ 336 FocusStep moveStep = FocusStep::LEFT; 337 auto frameOffset = frameNode->GetTransformRelativeOffset(); 338 auto geometryNode = frameNode->GetGeometryNode(); 339 RectF frameRect = RectF(frameOffset, geometryNode->GetFrameRect().GetSize()); 340 341 /** 342 * @tc.steps: step3. Set the corresponding variables to ensure entry into the target branch. 343 */ 344 std::list<RefPtr<FocusHub>> focusNodes; 345 focusHub->FlushChildrenFocusHub(focusNodes); 346 focusHub->lastWeakFocusNode_ = AceType::MakeRefPtr<FocusHub>(AceType::WeakClaim(AceType::RawPtr(eventHub))); 347 348 /** 349 * @tc.steps: step4. Expect the ret is false. 350 */ 351 auto ret = focusHub->RequestNextFocusByCustomAlgorithm(moveStep, frameRect); 352 EXPECT_EQ(ret, false); 353 } 354 355 /** 356 * @tc.name: PaintFocusStateToRenderContext001 357 * @tc.desc: Test the function PaintFocusStateToRenderContext. 358 * @tc.type: FUNC 359 */ 360 HWTEST_F(FocusHubTestNg, PaintFocusStateToRenderContext001, TestSize.Level1) 361 { 362 /** 363 * @tc.steps1: create focusHub. 364 */ 365 RefPtr<EventHub> eventHub = AceType::MakeRefPtr<EventHub>(); 366 auto frameNode = AceType::MakeRefPtr<FrameNodeOnTree>(V2::TEXT_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>()); 367 eventHub->AttachHost(frameNode); 368 auto focusHub = AceType::MakeRefPtr<FocusHub>(AceType::WeakClaim(AceType::RawPtr(eventHub))); 369 ASSERT_NE(focusHub, nullptr); 370 bool result = focusHub->PaintFocusStateToRenderContext(); 371 EXPECT_TRUE(result); 372 } 373 374 /** 375 * @tc.name: IsNeedPaintFocusState001 376 * @tc.desc: Test the function IsNeedPaintFocusState. 377 * @tc.type: FUNC 378 */ 379 HWTEST_F(FocusHubTestNg, IsNeedPaintFocusState001, TestSize.Level1) 380 { 381 /** 382 * @tc.steps: step1. Create focusHub. 383 */ 384 auto frameNode = FrameNodeOnTree::CreateFrameNode("frameNode", 101, AceType::MakeRefPtr<ButtonPattern>()); 385 frameNode->GetOrCreateFocusHub(); 386 auto focusHub = frameNode->GetFocusHub(); 387 ASSERT_NE(focusHub, nullptr); 388 auto frameNode1 = FrameNodeOnTree::CreateFrameNode("frameNode", 101, AceType::MakeRefPtr<ButtonPattern>()); 389 frameNode1->GetOrCreateFocusHub(); 390 auto focusHub1 = frameNode1->GetFocusHub(); 391 focusHub->focusType_ = FocusType::SCOPE; 392 frameNode1->parent_ = AceType::WeakClaim(AceType::RawPtr(frameNode)); 393 frameNode->children_.push_back(frameNode1); 394 ASSERT_TRUE(focusHub->IsFocusableNode()); 395 396 /** 397 * @tc.steps: step2. Set the corresponding variables to ensure entry into the target branch. 398 */ 399 focusHub->currentFocus_ = false; 400 focusHub->lastWeakFocusNode_ = AceType::WeakClaim(AceType::RawPtr(focusHub1)); 401 ASSERT_NE(focusHub->lastWeakFocusNode_.Upgrade(), nullptr); 402 focusHub->lastWeakFocusNode_.Upgrade()->focusable_ = true; 403 focusHub->lastWeakFocusNode_.Upgrade()->parentFocusable_ = true; 404 focusHub->lastWeakFocusNode_.Upgrade()->frameNode_ = nullptr; 405 focusHub->lastWeakFocusNode_.Upgrade()->eventHub_ = nullptr; 406 focusHub->lastWeakFocusNode_.Upgrade()->currentFocus_ = true; 407 focusHub->focusDepend_ = FocusDependence::AUTO; 408 focusHub->focusType_ = FocusType::DISABLE; 409 focusHub->lastWeakFocusNode_.Upgrade()->focusType_ = FocusType::SCOPE; 410 EXPECT_FALSE(focusHub->IsNeedPaintFocusState()); 411 } 412 413 /** 414 * @tc.name: HandleFocusTravel002 415 * @tc.desc: HandleFocusTravel 416 * @tc.type: FUNC 417 */ 418 HWTEST_F(FocusHubTestNg, HandleFocusTravel002, TestSize.Level1) 419 { 420 /** 421 * @tc.steps: step1. Create focusHub. 422 */ 423 auto frameNode = AceType::MakeRefPtr<FrameNodeOnTree>(V2::ROW_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>()); 424 RefPtr<EventHub> eventHub = AceType::MakeRefPtr<EventHub>(); 425 eventHub->AttachHost(frameNode); 426 auto focusHub = AceType::MakeRefPtr<FocusHub>(AceType::WeakClaim(AceType::RawPtr(eventHub))); 427 KeyEvent keyEvent; 428 keyEvent.pressedCodes.emplace_back(KeyCode::KEY_HOME); 429 FocusEvent focusEvent(keyEvent); 430 focusEvent.intension = FocusIntension::LEFT; 431 432 auto frameNode1 = FrameNodeOnTree::CreateFrameNode("frameNode", 101, AceType::MakeRefPtr<ButtonPattern>()); 433 focusHub->frameNode_ = AceType::WeakClaim(AceType::RawPtr(frameNode1)); 434 435 /** 436 * @tc.steps: step2. Set the corresponding variables to ensure entry into the target branch. 437 */ 438 focusHub->currentFocus_ = true; 439 EXPECT_TRUE(focusHub->IsCurrentFocus()); 440 focusHub->GetFrameNode()->GetContext()->eventManager_->isTabJustTriggerOnKeyEvent_ = true; 441 auto node = focusHub->GetFrameNode(); 442 EXPECT_NE(node, nullptr); 443 auto* pipeline = node->GetContext(); 444 EXPECT_NE(pipeline, nullptr); 445 EXPECT_TRUE(pipeline->GetIsFocusActive()); 446 EXPECT_TRUE(pipeline->IsTabJustTriggerOnKeyEvent()); 447 448 auto ret = focusHub->HandleFocusTravel(focusEvent); 449 EXPECT_EQ(ret, false); 450 451 /** 452 * @tc.steps: step3. Set the corresponding variables to ensure entry into the target branch. 453 */ 454 EXPECT_TRUE(focusHub->IsCurrentFocus()); 455 focusHub->GetFrameNode()->GetContext()->focusManager_ = nullptr; 456 node = focusHub->GetFrameNode(); 457 EXPECT_NE(node, nullptr); 458 auto* pipelineone = node->GetContext(); 459 EXPECT_NE(pipelineone, nullptr); 460 EXPECT_FALSE(pipelineone->GetIsFocusActive()); 461 462 ret = focusHub->HandleFocusTravel(focusEvent); 463 EXPECT_EQ(ret, false); 464 } 465 466 /** 467 * @tc.name: DumpFocusUie001 468 * @tc.desc: Test the function DumpFocusUie. 469 * @tc.type: FUNC 470 */ 471 HWTEST_F(FocusHubTestNg, DumpFocusUie001, TestSize.Level1) 472 { 473 /** 474 * @tc.steps: step1. Create focusHub. 475 */ 476 auto frameNode = AceType::MakeRefPtr<FrameNodeOnTree>(V2::ROW_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>()); 477 RefPtr<EventHub> eventHub = AceType::MakeRefPtr<EventHub>(); 478 eventHub->AttachHost(frameNode); 479 auto focusHub = AceType::MakeRefPtr<FocusHub>(AceType::WeakClaim(AceType::RawPtr(eventHub))); 480 auto frameNode1 = 481 AceType::MakeRefPtr<FrameNodeOnTree>(V2::UI_EXTENSION_COMPONENT_TAG, -1, AceType::MakeRefPtr<Pattern>()); 482 483 /** 484 * @tc.steps: step2. Set the corresponding variables to ensure entry into the target branch. 485 */ 486 focusHub->frameNode_ = AceType::WeakClaim(AceType::RawPtr(frameNode1)); 487 auto node = focusHub->GetFrameNode(); 488 EXPECT_NE(node, nullptr); 489 auto pattern = node->GetPattern(); 490 EXPECT_NE(pattern, nullptr); 491 EXPECT_EQ(node->GetTag(), V2::UI_EXTENSION_COMPONENT_TAG); 492 focusHub->DumpFocusUie(); 493 } 494 495 /** 496 * @tc.name: SetEnabledAndSetShow002 497 * @tc.desc: Test the function SetEnabled and SetShow. 498 * @tc.type: FUNC 499 */ 500 HWTEST_F(FocusHubTestNg, SetEnabledAndSetShow002, TestSize.Level1) 501 { 502 /** 503 * @tc.steps: step1. Create focusHub. 504 */ 505 auto frameNode = AceType::MakeRefPtr<FrameNodeOnTree>(V2::ROW_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>()); 506 RefPtr<EventHub> eventHub = AceType::MakeRefPtr<EventHub>(); 507 eventHub->AttachHost(frameNode); 508 auto focusHub = AceType::MakeRefPtr<FocusHub>(AceType::WeakClaim(AceType::RawPtr(eventHub))); 509 510 /** 511 * @tc.steps: step2. Set the corresponding variables to ensure entry into the target branch. 512 */ 513 bool show = false; 514 SystemProperties::debugEnabled_ = true; 515 focusHub->SetEnabled(show); 516 focusHub->SetShow(show); 517 EXPECT_TRUE(SystemProperties::GetDebugEnabled()); 518 } 519 520 /** 521 * @tc.name: RequestNextFocusOfKeyEscTest002 522 * @tc.desc: Test the function RequestNextFocusOfKeyEsc 523 * @tc.type: FUNC 524 */ 525 HWTEST_F(FocusHubTestNg, RequestNextFocusOfKeyEscTest002, TestSize.Level1) 526 { 527 /** 528 * @tc.steps: step1. Create focusHub 529 */ 530 auto frameNode = AceType::MakeRefPtr<FrameNodeOnTree>(V2::ROW_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>()); 531 auto nodeParent = 532 AceType::MakeRefPtr<FrameNodeOnTree>(V2::BLANK_ETS_TAG, -1, AceType::MakeRefPtr<FlexLayoutPattern>()); 533 frameNode->GetOrCreateFocusHub(); 534 nodeParent->GetOrCreateFocusHub(); 535 frameNode->SetParent(nodeParent); 536 RefPtr<EventHub> eventHub = AceType::MakeRefPtr<EventHub>(); 537 eventHub->AttachHost(frameNode); 538 auto focusHub = AceType::MakeRefPtr<FocusHub>(AceType::WeakClaim(AceType::RawPtr(eventHub))); 539 540 /** 541 * @tc.steps: step2. Set the corresponding variables to ensure entry into the target branch. 542 */ 543 focusHub->GetFrameNode()->GetContext()->isWindowHasFocused_ = true; 544 focusHub->GetFrameNode()->GetContext()->onFocus_ = true; 545 focusHub->FocusToHeadOrTailChild(true); 546 focusHub->HandleLastFocusNodeInFocusWindow(); 547 548 auto framenode = focusHub->GetFrameNode(); 549 EXPECT_NE(framenode, nullptr); 550 auto* context = framenode->GetContext(); 551 EXPECT_NE(context, nullptr); 552 EXPECT_EQ(context->IsWindowFocused(), true); 553 } 554 555 /** 556 * @tc.name: AcceptFocusOfSpecifyChild01 557 * @tc.desc: Test the function AcceptFocusOfSpecifyChild. 558 * @tc.type: FUNC 559 */ 560 HWTEST_F(FocusHubTestNg, AcceptFocusOfSpecifyChild01, TestSize.Level1) 561 { 562 /** 563 * @tc.steps1: create focusHub. 564 */ 565 auto frameNode = FrameNodeOnTree::CreateFrameNode("frameNode", 101, AceType::MakeRefPtr<ButtonPattern>()); 566 frameNode->GetOrCreateFocusHub(); 567 auto frameNode1 = FrameNodeOnTree::CreateFrameNode("frameNode", 101, AceType::MakeRefPtr<ButtonPattern>()); 568 frameNode1->GetOrCreateFocusHub(); 569 frameNode->children_.push_back(frameNode1); 570 auto focusHub = frameNode->GetFocusHub(); 571 ASSERT_NE(focusHub, nullptr); 572 573 /** 574 * @tc.steps: step2. Set the corresponding variables to ensure entry into the target branch. 575 */ 576 focusHub->focusType_ = FocusType::SCOPE; 577 focusHub->focusDepend_ = FocusDependence::SELF; 578 EXPECT_EQ(focusHub->IsFocusableScope(), true); 579 auto res = focusHub->AcceptFocusOfSpecifyChild(FocusStep::TAB); 580 ASSERT_TRUE(res); 581 } 582 583 /** 584 * @tc.name: ScrollByOffsetToParent001 585 * @tc.desc: Test the function ScrollByOffsetToParent. 586 * @tc.type: FUNC 587 */ 588 HWTEST_F(FocusHubTestNg, ScrollByOffsetToParent001, TestSize.Level1) 589 { 590 /** 591 * @tc.steps1: create focusHub. 592 */ 593 auto listNode = FrameNodeOnTree::CreateFrameNode( 594 V2::LIST_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<ListPattern>()); 595 auto listItemGroupNode = 596 FrameNodeOnTree::CreateFrameNode(V2::LIST_ITEM_GROUP_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), 597 AceType::MakeRefPtr<ListItemGroupPattern>(nullptr, V2::ListItemGroupStyle::NONE)); 598 auto listItemPattern = AceType::MakeRefPtr<ListItemPattern>(nullptr); 599 auto listItemNode = FrameNodeOnTree::CreateFrameNode( 600 V2::LIST_ITEM_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), listItemPattern); 601 auto buttonNode = FrameNodeOnTree::CreateFrameNode( 602 V2::BUTTON_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<ButtonPattern>()); 603 ASSERT_NE(listNode, nullptr); 604 ASSERT_NE(listItemGroupNode, nullptr); 605 ASSERT_NE(listItemPattern, nullptr); 606 ASSERT_NE(listItemNode, nullptr); 607 ASSERT_NE(buttonNode, nullptr); 608 buttonNode->MountToParent(listItemNode); 609 listItemNode->MountToParent(listItemGroupNode); 610 listItemGroupNode->MountToParent(listNode); 611 auto focusHub = buttonNode->GetOrCreateFocusHub(); 612 ASSERT_NE(focusHub, nullptr); 613 auto listPattern = listNode->GetPattern<ListPattern>(); 614 ASSERT_NE(listPattern, nullptr); 615 EXPECT_NE(listPattern->targetIndexInGroup_, 0); 616 listItemPattern->indexInListItemGroup_ = 0; 617 618 /** 619 * @tc.steps: step2. Set the corresponding variables to ensure entry into the target branch. 620 */ 621 focusHub->ScrollByOffsetToParent(listNode); 622 EXPECT_EQ(listPattern->targetIndexInGroup_, 0); 623 } 624 } // namespace OHOS::Ace::NG