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/event/focus_hub_test_ng.h" 16 17 using namespace testing; 18 using namespace testing::ext; 19 20 namespace OHOS::Ace::NG { 21 /** 22 * @tc.name: PaintFocusState0002 23 * @tc.desc: Test the function SwitchFocus. 24 * @tc.type: FUNC 25 */ 26 HWTEST_F(FocusHubTestNg, PaintFocusState0002, TestSize.Level1) 27 { 28 /** 29 * @tc.steps: step1. Create frameNode. 30 */ 31 auto themeManager = AceType::MakeRefPtr<MockThemeManager>(); 32 MockPipelineContext::GetCurrent()->SetThemeManager(themeManager); 33 EXPECT_CALL(*themeManager, GetTheme(_)).WillRepeatedly(Return(AceType::MakeRefPtr<AppTheme>())); 34 auto frameNode = AceType::MakeRefPtr<FrameNodeOnTree>(V2::ROW_ETS_TAG, -1, 35 AceType::MakeRefPtr<Pattern>()); 36 auto child = AceType::MakeRefPtr<FrameNodeOnTree>(V2::BUTTON_ETS_TAG, -1, 37 AceType::MakeRefPtr<ButtonPattern>()); 38 auto child2 = AceType::MakeRefPtr<FrameNodeOnTree>(V2::BUTTON_ETS_TAG, -1, 39 AceType::MakeRefPtr<ButtonPattern>()); 40 child->GetOrCreateFocusHub(); 41 child2->GetOrCreateFocusHub(); 42 frameNode->AddChild(child); 43 frameNode->AddChild(child2); 44 RefPtr<EventHub> eventHub = AceType::MakeRefPtr<EventHub>(); 45 eventHub->AttachHost(frameNode); 46 auto focusHub = AceType::MakeRefPtr<FocusHub>(AceType::WeakClaim(AceType::RawPtr(eventHub))); 47 auto context = PipelineContext::GetCurrentContext(); 48 ASSERT_NE(context, nullptr); 49 context->isFocusActive_ = true; 50 focusHub->focusType_ = FocusType::NODE; 51 eventHub->AddSupportedState(NG::UI_STATE_FOCUSED); 52 ASSERT_NE(eventHub->stateStyleMgr_, nullptr); 53 EXPECT_TRUE(focusHub->PaintFocusState(false)); 54 EXPECT_TRUE(focusHub->PaintFocusState(true)); 55 56 eventHub->stateStyleMgr_ = nullptr; 57 ASSERT_EQ(eventHub->stateStyleMgr_, nullptr); 58 context->isFocusActive_ = true; 59 FocusBoxStyle styledStyle; 60 styledStyle.strokeColor = Color::RED; 61 focusHub->box_.SetStyle(styledStyle); 62 EXPECT_TRUE(focusHub->IsNeedPaintFocusState()); 63 focusHub->focusStyleType_ = FocusStyleType::NONE; 64 EXPECT_FALSE(focusHub->HasFocusStateStyle()); 65 EXPECT_FALSE(focusHub->PaintFocusState(false)); 66 67 context->isFocusActive_ = true; 68 EXPECT_TRUE(focusHub->IsNeedPaintFocusState()); 69 focusHub->focusStyleType_ = FocusStyleType::FORCE_NONE; 70 EXPECT_FALSE(focusHub->HasFocusStateStyle()); 71 EXPECT_TRUE(focusHub->PaintFocusState(false)); 72 } 73 } // namespace OHOS::Ace::NG