1 /*
2 * Copyright (c) 2022 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 // gtest
17 #include <gtest/gtest.h>
18 #include "display_manager_proxy.h"
19 #include "window_test_utils.h"
20 #include "wm_common.h"
21 using namespace testing;
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace Rosen {
26 namespace {
27 constexpr uint32_t WAIT_ASYNC_US = 100000; // 100ms
28 }
29 using Utils = WindowTestUtils;
30 class WindowInputTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 virtual void SetUp() override;
35 virtual void TearDown() override;
36 Utils::TestWindowInfo fullScreenWindow_;
37 };
38
SetUpTestCase()39 void WindowInputTest::SetUpTestCase()
40 {
41 auto display = DisplayManager::GetInstance().GetDisplayById(0);
42 ASSERT_TRUE((display != nullptr));
43 Rect displayRect = {0, 0, display->GetWidth(), display->GetHeight()};
44 Utils::InitByDisplayRect(displayRect);
45 }
46
TearDownTestCase()47 void WindowInputTest::TearDownTestCase()
48 {
49 }
50
SetUp()51 void WindowInputTest::SetUp()
52 {
53 fullScreenWindow_ = {
54 .name = "FullWindow",
55 .rect = Utils::customAppRect_,
56 .type = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
57 .mode = WindowMode::WINDOW_MODE_FULLSCREEN,
58 .needAvoid = false,
59 .parentLimit = false,
60 .parentId = INVALID_WINDOW_ID,
61 };
62 }
63
TearDown()64 void WindowInputTest::TearDown()
65 {
66 }
67
68 namespace {
69 /**
70 * @tc.name: SetTouchHotAreas01
71 * @tc.desc: Normal scenario testing for Window#SetTouchHotAreas
72 * @tc.type: FUNC
73 */
74 HWTEST_F(WindowInputTest, SetTouchHotAreas01, Function | MediumTest | Level3)
75 {
76 fullScreenWindow_.name = "window_hot_areas.1";
77 const sptr<Window>& window = Utils::CreateTestWindow(fullScreenWindow_);
78 ASSERT_EQ(WMError::WM_OK, window->Show());
79
80 std::vector<Rect> requestedTouchHotAreas;
81 window->GetRequestedTouchHotAreas(requestedTouchHotAreas);
82 ASSERT_TRUE(requestedTouchHotAreas.empty());
83
84 usleep(WAIT_ASYNC_US);
85 Rect windowRect = window->GetRect();
86
87 std::vector<Rect> rects;
88 uint32_t hotAreasNum = 10;
89 uint32_t hotAreaWidth = windowRect.width_ / hotAreasNum;
90 uint32_t hotAreaHeight = windowRect.height_ / hotAreasNum;
91 for (uint32_t i = 0; i < hotAreasNum; ++i) {
92 rects.emplace_back(Rect{ hotAreaWidth * i, hotAreaHeight * i, hotAreaWidth, hotAreaHeight });
93 }
94 ASSERT_EQ(WMError::WM_OK, window->SetTouchHotAreas(rects));
95 window->GetRequestedTouchHotAreas(requestedTouchHotAreas);
96 ASSERT_EQ(rects.size(), requestedTouchHotAreas.size());
97 for (uint32_t i = 0; i < rects.size(); ++i) {
98 ASSERT_TRUE(rects[i] == requestedTouchHotAreas[i]);
99 }
100
101 rects.clear();
102 rects.emplace_back(Rect{ 0, 0, hotAreaWidth, hotAreaHeight });
103 ASSERT_EQ(WMError::WM_OK, window->SetTouchHotAreas(rects));
104 window->GetRequestedTouchHotAreas(requestedTouchHotAreas);
105 ASSERT_EQ(rects.size(), requestedTouchHotAreas.size());
106 for (uint32_t i = 0; i < rects.size(); ++i) {
107 ASSERT_TRUE(rects[i] == requestedTouchHotAreas[i]);
108 }
109
110 rects.clear();
111 ASSERT_EQ(WMError::WM_OK, window->SetTouchHotAreas(rects));
112 window->GetRequestedTouchHotAreas(requestedTouchHotAreas);
113 ASSERT_TRUE(requestedTouchHotAreas.empty());
114
115 ASSERT_EQ(WMError::WM_OK, window->Destroy());
116 }
117
118 /**
119 * @tc.name: SetTouchHotAreas02
120 * @tc.desc: Abnormal scenario testing for Window#SetTouchHotAreas
121 * @tc.type: FUNC
122 */
123 HWTEST_F(WindowInputTest, SetTouchHotAreas02, Function | MediumTest | Level3)
124 {
125 fullScreenWindow_.name = "window_hot_areas.2";
126 const sptr<Window>& window = Utils::CreateTestWindow(fullScreenWindow_);
127 ASSERT_EQ(WMError::WM_OK, window->Show());
128
129 usleep(WAIT_ASYNC_US);
130 Rect windowRect = window->GetRect();
131
132 std::vector<Rect> rects;
133 /* maximum hot areas: 10, test exceeding maximum hot areas */
134 uint32_t hotAreasNum = 11;
135 uint32_t hotAreaWidth = windowRect.width_ / hotAreasNum;
136 uint32_t hotAreaHeight = windowRect.height_ / hotAreasNum;
137 for (uint32_t i = 0; i < hotAreasNum; ++i) {
138 rects.emplace_back(Rect{ hotAreaWidth * i, hotAreaHeight * i, hotAreaWidth, hotAreaHeight });
139 }
140 ASSERT_EQ(WMError::WM_OK, window->SetTouchHotAreas(rects));
141
142 rects.clear();
143 rects.emplace_back(Rect{ -1, 0, windowRect.width_ / 2, windowRect.height_ / 2 });
144 ASSERT_EQ(WMError::WM_ERROR_INVALID_PARAM, window->SetTouchHotAreas(rects));
145
146 rects.clear();
147 rects.emplace_back(Rect{ 0, -1, windowRect.width_ / 2, windowRect.height_ / 2 });
148 ASSERT_EQ(WMError::WM_ERROR_INVALID_PARAM, window->SetTouchHotAreas(rects));
149
150 rects.clear();
151 rects.emplace_back(Rect{ 0, 0, 0, windowRect.height_ / 2 });
152 ASSERT_EQ(WMError::WM_ERROR_INVALID_PARAM, window->SetTouchHotAreas(rects));
153
154 rects.clear();
155 rects.emplace_back(Rect{ 0, 0, windowRect.width_ / 2, 0 });
156 ASSERT_EQ(WMError::WM_ERROR_INVALID_PARAM, window->SetTouchHotAreas(rects));
157
158 rects.clear();
159 rects.emplace_back(Rect{ windowRect.width_, 0, windowRect.width_ / 2, windowRect.height_ / 2 });
160 ASSERT_EQ(WMError::WM_ERROR_INVALID_PARAM, window->SetTouchHotAreas(rects));
161
162 rects.clear();
163 rects.emplace_back(Rect{ 0, windowRect.height_, windowRect.width_ / 2, windowRect.height_ / 2 });
164 ASSERT_EQ(WMError::WM_ERROR_INVALID_PARAM, window->SetTouchHotAreas(rects));
165
166 std::vector<Rect> requestedTouchHotAreas;
167 window->GetRequestedTouchHotAreas(requestedTouchHotAreas);
168 ASSERT_FALSE(requestedTouchHotAreas.empty());
169
170 ASSERT_EQ(WMError::WM_OK, window->Destroy());
171 }
172 } // namespace
173 } // namespace Rosen
174 } // namespace OHOS