1 /*
2 * Copyright (c) 2020-2021 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 "components/ui_label.h"
17 #include "dock/focus_manager.h"
18
19 #include <climits>
20 #include <gtest/gtest.h>
21
22 #if ENABLE_FOCUS_MANAGER
23 using namespace testing::ext;
24 namespace OHOS {
25 class FocusManagerTest : public testing::Test {
26 public:
27 static void SetUpTestCase(void);
28 static void TearDownTestCase(void);
29 static UIViewGroup* viewGroup_;
30 static UILabel* label1_;
31 static UILabel* label2_;
32 };
33
34 UIViewGroup* FocusManagerTest::viewGroup_ = nullptr;
35 UILabel* FocusManagerTest::label1_ = nullptr;
36 UILabel* FocusManagerTest::label2_ = nullptr;
37
SetUpTestCase(void)38 void FocusManagerTest::SetUpTestCase(void)
39 {
40 if (viewGroup_ == nullptr) {
41 viewGroup_ = new UIViewGroup();
42 viewGroup_->SetPosition(0, 0, 200, 200); // 200: width, 200: height
43 }
44 if (label1_ == nullptr) {
45 label1_ = new UILabel();
46 label1_->SetFocusable(true);
47 label1_->SetPosition(50, 50, 50, 50); // 50: x, 50: y, 50: width, 50: height
48 }
49 if (label2_ == nullptr) {
50 label2_ = new UILabel();
51 label2_->SetFocusable(true);
52 label2_->SetPosition(150, 150, 50, 50); // 150: x, 150: y, 50: width, 50: height
53 }
54 viewGroup_->Add(label1_);
55 viewGroup_->Add(label2_);
56 }
57
TearDownTestCase(void)58 void FocusManagerTest::TearDownTestCase(void)
59 {
60 FocusManager::GetInstance()->ClearFocus();
61 if (viewGroup_ != nullptr) {
62 viewGroup_->Remove(label1_);
63 viewGroup_->Remove(label2_);
64 delete viewGroup_;
65 viewGroup_ = nullptr;
66 }
67 if (label1_ != nullptr) {
68 delete label1_;
69 label1_ = nullptr;
70 }
71 if (label2_ != nullptr) {
72 delete label2_;
73 label2_ = nullptr;
74 }
75 }
76
77 /**
78 * @tc.name: GetFocusedView_001
79 * @tc.desc: Verify GetFocusedView function, equal.
80 * @tc.type: FUNC
81 * @tc.require: AR000EVI2R
82 */
83 HWTEST_F(FocusManagerTest, GetFocusedView_001, TestSize.Level0)
84 {
85 UIView* focusedView = FocusManager::GetInstance()->GetFocusedView();
86 EXPECT_EQ(focusedView, nullptr);
87 }
88
89 /**
90 * @tc.name: RequestFocus_001
91 * @tc.desc: Verify RequestFocus function, equal.
92 * @tc.type: FUNC
93 * @tc.require: AR000EVI2R
94 */
95 HWTEST_F(FocusManagerTest, RequestFocus_001, TestSize.Level1)
96 {
97 FocusManager::GetInstance()->RequestFocus(label1_);
98 UIView* focusedView = FocusManager::GetInstance()->GetFocusedView();
99 EXPECT_EQ(focusedView, label1_);
100 FocusManager::GetInstance()->RequestFocus(label2_);
101 focusedView = FocusManager::GetInstance()->GetFocusedView();
102 EXPECT_EQ(focusedView, label2_);
103 }
104
105 /**
106 * @tc.name: ClearFocus_001
107 * @tc.desc: Verify ClearFocus function, equal.
108 * @tc.type: FUNC
109 * @tc.require: AR000EVI2R
110 */
111 HWTEST_F(FocusManagerTest, ClearFocus_001, TestSize.Level1)
112 {
113 FocusManager::GetInstance()->RequestFocus(label1_);
114 UIView* focusedView = FocusManager::GetInstance()->GetFocusedView();
115 EXPECT_EQ(focusedView, label1_);
116 FocusManager::GetInstance()->ClearFocus();
117 focusedView = FocusManager::GetInstance()->GetFocusedView();
118 EXPECT_EQ(focusedView, nullptr);
119 }
120
121 /**
122 * @tc.name: RequestFocusByDirection_001
123 * @tc.desc: Verify RequestFocusByDirection function, equal.
124 * @tc.type: FUNC
125 * @tc.require: AR000EVI2R
126 */
127 HWTEST_F(FocusManagerTest, RequestFocusByDirection_001, TestSize.Level0)
128 {
129 FocusManager::GetInstance()->RequestFocus(label1_);
130 UIView* focusedView = FocusManager::GetInstance()->GetFocusedView();
131 EXPECT_EQ(focusedView, label1_);
132 FocusManager::GetInstance()->RequestFocusByDirection(FOCUS_DIRECTION_RIGHT);
133 focusedView = FocusManager::GetInstance()->GetFocusedView();
134 EXPECT_EQ(focusedView, label2_);
135 FocusManager::GetInstance()->RequestFocusByDirection(FOCUS_DIRECTION_LEFT);
136 focusedView = FocusManager::GetInstance()->GetFocusedView();
137 EXPECT_EQ(focusedView, label1_);
138 FocusManager::GetInstance()->RequestFocusByDirection(FOCUS_DIRECTION_DOWN);
139 focusedView = FocusManager::GetInstance()->GetFocusedView();
140 EXPECT_EQ(focusedView, label2_);
141 FocusManager::GetInstance()->RequestFocusByDirection(FOCUS_DIRECTION_UP);
142 focusedView = FocusManager::GetInstance()->GetFocusedView();
143 EXPECT_EQ(focusedView, label1_);
144 }
145 } // namespace OHOS
146 #endif
147