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