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 "window_accessibility_controller.h"
19 #include "window_impl.h"
20 #include "window_test_utils.h"
21 #include "wm_common.h"
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace Rosen {
27 using Utils = WindowTestUtils;
28 class WindowDisplayZoomTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 virtual void SetUp() override;
33 virtual void TearDown() override;
34 Utils::TestWindowInfo windowInfo_;
35 };
36
SetUpTestCase()37 void WindowDisplayZoomTest::SetUpTestCase() {}
38
TearDownTestCase()39 void WindowDisplayZoomTest::TearDownTestCase() {}
40
SetUp()41 void WindowDisplayZoomTest::SetUp()
42 {
43 windowInfo_ = {
44 .name = "zoomWindow",
45 .rect = { 0, 0, 300, 100 },
46 .type = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
47 .mode = WindowMode::WINDOW_MODE_FLOATING,
48 .needAvoid = false,
49 .parentLimit = false,
50 .showWhenLocked = true,
51 .parentId = INVALID_WINDOW_ID,
52 };
53 }
54
TearDown()55 void WindowDisplayZoomTest::TearDown() {}
56
57 namespace {
58 /**
59 * @tc.name: DisplayZoom01
60 * @tc.desc: test interface SetAnchorAndScale
61 * @tc.type: FUNC
62 * @tc.require: issueI5NGWL
63 */
64 HWTEST_F(WindowDisplayZoomTest, DisplayZoom01, TestSize.Level1)
65 {
66 WindowAccessibilityController::GetInstance().OffWindowZoom();
67 sleep(1);
68
69 windowInfo_.name = "DisplayZoom01";
70 sptr<Window> window = Utils::CreateTestWindow(windowInfo_);
71 ASSERT_NE(window, nullptr);
72 Window* ptr = window.GetRefPtr();
73 WindowImpl* implPtr = (WindowImpl*)ptr;
74 ASSERT_EQ(WMError::WM_OK, window->Show());
75 sleep(1);
76 Transform expect;
77 ASSERT_TRUE(expect == implPtr->GetWindowProperty()->GetZoomTransform());
78
79 WindowAccessibilityController::GetInstance().SetAnchorAndScale(0, 0, 2);
80 sleep(1);
81 Rect rect = window->GetRect();
82 expect.pivotX_ = (0 - rect.posX_) * 1.0 / rect.width_;
83 expect.pivotY_ = (0 - rect.posY_) * 1.0 / rect.height_;
84 expect.scaleX_ = expect.scaleY_ = 2; // scale value
85 ASSERT_TRUE(expect == implPtr->GetWindowProperty()->GetZoomTransform());
86
87 WindowAccessibilityController::GetInstance().SetAnchorAndScale(0, 0, 2);
88 sleep(1);
89 expect.scaleX_ = expect.scaleY_ = 4; // scale value
90 ASSERT_TRUE(expect == implPtr->GetWindowProperty()->GetZoomTransform());
91
92 WindowAccessibilityController::GetInstance().SetAnchorAndScale(0, 0, 0.5);
93 sleep(1);
94 expect.scaleX_ = expect.scaleY_ = 2; // scale value
95 ASSERT_TRUE(expect == implPtr->GetWindowProperty()->GetZoomTransform());
96
97 WindowAccessibilityController::GetInstance().SetAnchorAndScale(0, 0, 0.1);
98 sleep(1);
99 ASSERT_TRUE(expect == implPtr->GetWindowProperty()->GetZoomTransform());
100
101 WindowAccessibilityController::GetInstance().SetAnchorAndScale(0, 0, -0.1);
102 sleep(1);
103 ASSERT_TRUE(expect == implPtr->GetWindowProperty()->GetZoomTransform());
104
105 WindowAccessibilityController::GetInstance().OffWindowZoom();
106 window->Destroy();
107 }
108
109 /**
110 * @tc.name: DisplayZoom02
111 * @tc.desc: test interface SetAnchorOffset
112 * @tc.type: FUNC
113 * @tc.require: issueI5NGWL
114 */
115 HWTEST_F(WindowDisplayZoomTest, DisplayZoom02, TestSize.Level1)
116 {
117 windowInfo_.name = "DisplayZoom02";
118 sptr<Window> window = Utils::CreateTestWindow(windowInfo_);
119 ASSERT_NE(window, nullptr);
120 ASSERT_EQ(WMError::WM_OK, window->Show());
121 sleep(1);
122
123 WindowAccessibilityController::GetInstance().SetAnchorAndScale(0, 0, 2);
124 sleep(1);
125 WindowAccessibilityController::GetInstance().SetAnchorOffset(-100, -100);
126 sleep(1);
127
128 Transform expect;
129 Rect rect = window->GetRect();
130 expect.pivotX_ = (0 - rect.posX_) * 1.0 / rect.width_;
131 expect.pivotY_ = (0 - rect.posY_) * 1.0 / rect.height_;
132 expect.scaleX_ = expect.scaleY_ = 2; // scale value
133 expect.translateX_ = expect.translateY_ = -100; // translate value
134
135 WindowAccessibilityController::GetInstance().SetAnchorOffset(200, 200);
136 sleep(1);
137 expect.translateX_ = expect.translateY_ = 0;
138 WindowAccessibilityController::GetInstance().OffWindowZoom();
139 window->Destroy();
140 }
141
142 /**
143 * @tc.name: DisplayZoom03
144 * @tc.desc: test interface OffWindowZoom
145 * @tc.type: FUNC
146 * @tc.require: issueI5NGWL
147 */
148 HWTEST_F(WindowDisplayZoomTest, DisplayZoom03, TestSize.Level1)
149 {
150 windowInfo_.name = "DisplayZoom03";
151 sptr<Window> window = Utils::CreateTestWindow(windowInfo_);
152 ASSERT_NE(window, nullptr);
153 Window* ptr = window.GetRefPtr();
154 WindowImpl* implPtr = (WindowImpl*)ptr;
155 ASSERT_EQ(WMError::WM_OK, window->Show());
156 sleep(1);
157
158 WindowAccessibilityController::GetInstance().SetAnchorAndScale(0, 0, 2);
159 sleep(1);
160 WindowAccessibilityController::GetInstance().OffWindowZoom();
161 sleep(1);
162
163 Transform expect;
164 ASSERT_TRUE(expect == implPtr->GetWindowProperty()->GetZoomTransform());
165
166 window->Destroy();
167 }
168
169 /**
170 * @tc.name: DisplayZoom04
171 * @tc.desc: test add and remove a window after zoom display
172 * @tc.type: FUNC
173 * @tc.require: issueI5NGWL
174 */
175 HWTEST_F(WindowDisplayZoomTest, DisplayZoom04, TestSize.Level1)
176 {
177 WindowAccessibilityController::GetInstance().SetAnchorAndScale(0, 0, 2);
178 sleep(1);
179
180 windowInfo_.name = "DisplayZoom04";
181 sptr<Window> window = Utils::CreateTestWindow(windowInfo_);
182 ASSERT_NE(window, nullptr);
183 Window* ptr = window.GetRefPtr();
184 WindowImpl* implPtr = (WindowImpl*)ptr;
185 ASSERT_EQ(WMError::WM_OK, window->Show());
186 sleep(1);
187
188 Transform expect;
189 Rect rect = window->GetRect();
190 expect.pivotX_ = (rect.width_ == 0) ? 0 : (0 - rect.posX_) * 1.0 / rect.width_;
191 expect.pivotY_ = (rect.height_ == 0) ? 0 : (0 - rect.posY_) * 1.0 / rect.height_;
192 expect.scaleX_ = expect.scaleY_ = 2; // scale value
193 ASSERT_TRUE(expect == implPtr->GetWindowProperty()->GetZoomTransform());
194
195 ASSERT_EQ(WMError::WM_OK, window->Hide());
196 WindowAccessibilityController::GetInstance().OffWindowZoom();
197 sleep(1);
198
199 ASSERT_EQ(WMError::WM_OK, window->Show());
200 sleep(1);
201 Transform identity;
202 ASSERT_TRUE(identity == implPtr->GetWindowProperty()->GetZoomTransform());
203
204 window->Destroy();
205 }
206
207 /**
208 * @tc.name: DisplayZoom05
209 * @tc.desc: test animate and zoom transform
210 * @tc.type: FUNC
211 * @tc.require: issueI5NGWL
212 */
213 HWTEST_F(WindowDisplayZoomTest, DisplayZoom05, TestSize.Level1)
214 {
215 WindowAccessibilityController::GetInstance().SetAnchorAndScale(0, 0, 2);
216 sleep(1);
217
218 windowInfo_.name = "DisplayZoom05";
219 sptr<Window> window = Utils::CreateTestWindow(windowInfo_);
220 ASSERT_NE(window, nullptr);
221 Window* ptr = window.GetRefPtr();
222 WindowImpl* implPtr = (WindowImpl*)ptr;
223 ASSERT_EQ(WMError::WM_OK, window->Show());
224 sleep(1);
225
226 Transform animate;
227 animate.translateX_ = -100; // translate x value
228 animate.translateZ_ = 100; // translate z value
229 window->SetTransform(animate);
230 sleep(1);
231
232 Transform expect;
233 Rect rect = window->GetRect();
234 expect.pivotX_ = (rect.width_ == 0) ? 0 : (0 - rect.posX_) * 1.0 / rect.width_;
235 expect.pivotY_ = (rect.height_ == 0) ? 0 : (0 - rect.posY_) * 1.0 / rect.height_;
236 expect.scaleX_ = expect.scaleY_ = 1.7; // scale value
237 Transform actual = implPtr->GetWindowProperty()->GetZoomTransform();
238
__anon8fd9a5c30202(float a, float b) 239 auto isExpec = [](float a, float b) -> bool { return abs(a - b) < 0.1; };
240 ASSERT_EQ(true, isExpec(actual.pivotX_, expect.pivotX_));
241 ASSERT_EQ(true, isExpec(actual.pivotY_, expect.pivotY_));
242 ASSERT_EQ(true, isExpec(actual.scaleX_, expect.scaleX_));
243 ASSERT_EQ(true, isExpec(actual.scaleY_, expect.scaleY_));
244
245 WindowAccessibilityController::GetInstance().OffWindowZoom();
246 window->Destroy();
247 }
248
249 } // namespace
250 } // namespace Rosen
251 } // namespace OHOS