• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include "display_manager.h"
21 #include "future.h"
22 #include "screen_manager.h"
23 #include "window_manager.h"
24 #include "window_accessibility_controller.h"
25 #include "window_impl.h"
26 #include "wm_common.h"
27 
28 using namespace testing;
29 using namespace testing::ext;
30 
31 namespace OHOS {
32 namespace Rosen {
33 using Utils = WindowTestUtils;
34 class DisplayListener : public DisplayManager::IDisplayListener {
35 public:
36     virtual void OnCreate(DisplayId) override;
37     virtual void OnDestroy(DisplayId) override;
38     virtual void OnChange(DisplayId) override;
39     RunnableFuture<DisplayId> changeFuture_;
40 };
41 
42 class ScreenListener : public ScreenManager::IScreenListener {
43 public:
44     virtual void OnConnect(ScreenId) override;
45     virtual void OnDisconnect(ScreenId) override;
46     virtual void OnChange(ScreenId) override;
47     RunnableFuture<ScreenId> changeFuture_;
48 };
49 
50 class WindowRotationTest : public testing::Test {
51 public:
52     static void SetUpTestCase();
53     static void TearDownTestCase();
54     virtual void SetUp() override;
55     virtual void TearDown() override;
56     std::vector<sptr<Window>> activeWindows_;
57     Utils::TestWindowInfo fullInfo_;
58     sptr<DisplayListener> displayListener_;
59     sptr<ScreenListener> screenListener_;
60 private:
61     static constexpr uint32_t SPLIT_TEST_SLEEP_S = 1;
62     static constexpr long FUTURE_GET_RESULT_TIMEOUT = 1000;
63 };
64 
OnCreate(DisplayId displayId)65 void DisplayListener::OnCreate(DisplayId displayId)
66 {
67 }
68 
OnDestroy(DisplayId displayId)69 void DisplayListener::OnDestroy(DisplayId displayId)
70 {
71 }
72 
OnChange(DisplayId displayId)73 void DisplayListener::OnChange(DisplayId displayId)
74 {
75     changeFuture_.SetValue(displayId);
76 }
77 
OnConnect(ScreenId screenId)78 void ScreenListener::OnConnect(ScreenId screenId)
79 {
80 }
81 
OnDisconnect(ScreenId screenId)82 void ScreenListener::OnDisconnect(ScreenId screenId)
83 {
84 }
85 
OnChange(ScreenId screenId)86 void ScreenListener::OnChange(ScreenId screenId)
87 {
88     changeFuture_.SetValue(screenId);
89 }
90 
SetUpTestCase()91 void WindowRotationTest::SetUpTestCase()
92 {
93 }
94 
TearDownTestCase()95 void WindowRotationTest::TearDownTestCase()
96 {
97 }
98 
SetUp()99 void WindowRotationTest::SetUp()
100 {
101     fullInfo_ = {
102             .name = "",
103             .rect = Utils::customAppRect_,
104             .type = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
105             .mode = WindowMode::WINDOW_MODE_FULLSCREEN,
106             .needAvoid = true,
107             .parentLimit = false,
108             .parentId = INVALID_WINDOW_ID,
109     };
110 
111     activeWindows_.clear();
112     displayListener_ = new DisplayListener();
113     DisplayManager::GetInstance().RegisterDisplayListener(displayListener_);
114     screenListener_ = new ScreenListener();
115     ScreenManager::GetInstance().RegisterScreenListener(screenListener_);
116 }
117 
TearDown()118 void WindowRotationTest::TearDown()
119 {
120     while (!activeWindows_.empty()) {
121         ASSERT_EQ(WMError::WM_OK, activeWindows_.back()->Destroy());
122         activeWindows_.pop_back();
123     }
124     DisplayManager::GetInstance().UnregisterDisplayListener(displayListener_);
125     ScreenManager::GetInstance().UnregisterScreenListener(screenListener_);
126 }
127 
128 namespace {
129 /**
130 * @tc.name: WindowRotationTest1
131 * @tc.desc: create window and SetRequestedOrientation.
132 * @tc.type: FUNC
133 */
134 HWTEST_F(WindowRotationTest, WindowRotationTest1, Function | MediumTest | Level3)
135 {
136     fullInfo_.name  = "fullscreen.1";
137     fullInfo_.orientation_ = Orientation::UNSPECIFIED;
138     const sptr<Window>& fullWindow = Utils::CreateTestWindow(fullInfo_);
139     activeWindows_.push_back(fullWindow);
140     ASSERT_EQ(WMError::WM_OK, fullWindow->Show());
141     ASSERT_EQ(WindowMode::WINDOW_MODE_FULLSCREEN, fullWindow->GetMode());
142     sleep(SPLIT_TEST_SLEEP_S);
143 
144     fullWindow->SetRequestedOrientation(Orientation::REVERSE_HORIZONTAL);
145     ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, fullWindow->GetRequestedOrientation());
146     DisplayId displayId = displayListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
147     displayListener_->changeFuture_.Reset(-1);
148     ScreenId screenId = screenListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
149     screenListener_->changeFuture_.Reset(-1);
150     auto screen = ScreenManager::GetInstance().GetScreenById(screenId);
151     auto display = DisplayManager::GetInstance().GetDisplayById(displayId);
152     ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, screen->GetOrientation());
153     ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, display->GetOrientation());
154     sleep(SPLIT_TEST_SLEEP_S);
155 
156     ASSERT_EQ(WMError::WM_OK, fullWindow->Hide());
157     sleep(SPLIT_TEST_SLEEP_S);
158 }
159 
160 /**
161 * @tc.name: WindowRotationTest2
162 * @tc.desc: create window with orientation property.
163 * @tc.type: FUNC
164 */
165 HWTEST_F(WindowRotationTest, WindowRotationTest2, Function | MediumTest | Level3)
166 {
167     fullInfo_.name  = "fullscreen.2";
168     fullInfo_.orientation_ = Orientation::REVERSE_HORIZONTAL;
169     const sptr<Window>& fullWindow = Utils::CreateTestWindow(fullInfo_);
170     activeWindows_.push_back(fullWindow);
171     ASSERT_EQ(WMError::WM_OK, fullWindow->Show());
172     ASSERT_EQ(WindowMode::WINDOW_MODE_FULLSCREEN, fullWindow->GetMode());
173 
174     ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, fullWindow->GetRequestedOrientation());
175     DisplayId displayId = displayListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
176     ScreenId screenId = screenListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
177     auto screen = ScreenManager::GetInstance().GetScreenById(screenId);
178     auto display = DisplayManager::GetInstance().GetDisplayById(displayId);
179     ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, screen->GetOrientation());
180     ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, display->GetOrientation());
181     sleep(SPLIT_TEST_SLEEP_S);
182 
183     ASSERT_EQ(WMError::WM_OK, fullWindow->Hide());
184     screen->SetOrientation(Orientation::UNSPECIFIED);
185     sleep(SPLIT_TEST_SLEEP_S);
186 }
187 
188 /**
189 * @tc.name: WindowRotationTest3
190 * @tc.desc: create floating window with orientation property
191 * @tc.type: FUNC
192 */
193 HWTEST_F(WindowRotationTest, WindowRotationTest3, Function | MediumTest | Level3)
194 {
195     fullInfo_.name  = "fullscreen.4";
196     fullInfo_.orientation_ = Orientation::REVERSE_HORIZONTAL;
197     fullInfo_.mode = WindowMode::WINDOW_MODE_FLOATING;
198     const sptr<Window>& fullWindow = Utils::CreateTestWindow(fullInfo_);
199     fullInfo_.mode = WindowMode::WINDOW_MODE_FULLSCREEN;
200     activeWindows_.push_back(fullWindow);
201     ASSERT_EQ(WMError::WM_OK, fullWindow->Show());
202     ASSERT_EQ(WindowMode::WINDOW_MODE_FLOATING, fullWindow->GetMode());
203     sleep(SPLIT_TEST_SLEEP_S);
204 
205     ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, fullWindow->GetRequestedOrientation());
206     auto display = DisplayManager::GetInstance().GetDefaultDisplay();
207     auto screen = ScreenManager::GetInstance().GetAllScreens()[0];
208     ASSERT_EQ(Orientation::UNSPECIFIED, screen->GetOrientation());
209     ASSERT_EQ(Orientation::UNSPECIFIED, display->GetOrientation());
210     sleep(SPLIT_TEST_SLEEP_S);
211 
212     ASSERT_EQ(WMError::WM_OK, fullWindow->Hide());
213     ASSERT_EQ(Orientation::UNSPECIFIED, screen->GetOrientation());
214     ASSERT_EQ(Orientation::UNSPECIFIED, display->GetOrientation());
215     sleep(SPLIT_TEST_SLEEP_S);
216 }
217 
218 /**
219 * @tc.name: WindowRotationTest4
220 * @tc.desc: create window with orientation after setting screen default orientation.
221 * @tc.type: FUNC
222 */
223 HWTEST_F(WindowRotationTest, WindowRotationTest4, Function | MediumTest | Level3)
224 {
225     ScreenId defaultScreenId = DisplayManager::GetInstance().GetDefaultDisplay()->GetScreenId();
226     auto defaultScreen = ScreenManager::GetInstance().GetScreenById(defaultScreenId);
227     defaultScreen->SetOrientation(Orientation::REVERSE_HORIZONTAL);
228     fullInfo_.name  = "fullscreen.5";
229     fullInfo_.orientation_ = Orientation::HORIZONTAL;
230     const sptr<Window>& fullWindow = Utils::CreateTestWindow(fullInfo_);
231     activeWindows_.push_back(fullWindow);
232     ASSERT_EQ(WMError::WM_OK, fullWindow->Show());
233     ASSERT_EQ(WindowMode::WINDOW_MODE_FULLSCREEN, fullWindow->GetMode());
234 
235     ASSERT_EQ(Orientation::HORIZONTAL, fullWindow->GetRequestedOrientation());
236     DisplayId displayId = displayListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
237     displayListener_->changeFuture_.Reset(-1);
238     ScreenId screenId = screenListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
239     screenListener_->changeFuture_.Reset(-1);
240     auto screen = ScreenManager::GetInstance().GetScreenById(screenId);
241     auto display = DisplayManager::GetInstance().GetDisplayById(displayId);
242     ASSERT_EQ(Orientation::HORIZONTAL, screen->GetOrientation());
243     ASSERT_EQ(Orientation::HORIZONTAL, display->GetOrientation());
244     sleep(SPLIT_TEST_SLEEP_S);
245 
246     ASSERT_EQ(WMError::WM_OK, fullWindow->Hide());
247     defaultScreen->SetOrientation(Orientation::UNSPECIFIED);
248     sleep(SPLIT_TEST_SLEEP_S);
249 }
250 
251 /**
252 * @tc.name: WindowRotationTest5
253 * @tc.desc: create window with orientation after setting screen default orientation, and toggle shown state for all app
254 *           windows.
255 * @tc.type: FUNC
256 */
257 HWTEST_F(WindowRotationTest, WindowRotationTest5, Function | MediumTest | Level3)
258 {
259     ScreenId defaultScreenId = DisplayManager::GetInstance().GetDefaultDisplay()->GetScreenId();
260     auto defaultScreen = ScreenManager::GetInstance().GetScreenById(defaultScreenId);
261     defaultScreen->SetOrientation(Orientation::REVERSE_HORIZONTAL);
262     fullInfo_.name  = "fullscreen.5";
263     fullInfo_.orientation_ = Orientation::HORIZONTAL;
264     const sptr<Window>& fullWindow = Utils::CreateTestWindow(fullInfo_);
265     activeWindows_.push_back(fullWindow);
266     ASSERT_EQ(WMError::WM_OK, fullWindow->Show());
267     ASSERT_EQ(WindowMode::WINDOW_MODE_FULLSCREEN, fullWindow->GetMode());
268 
269     ASSERT_EQ(Orientation::HORIZONTAL, fullWindow->GetRequestedOrientation());
270     DisplayId displayId = displayListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
271     displayListener_->changeFuture_.Reset(-1);
272     ScreenId screenId = screenListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
273     screenListener_->changeFuture_.Reset(-1);
274     auto screen = ScreenManager::GetInstance().GetScreenById(screenId);
275     auto display = DisplayManager::GetInstance().GetDisplayById(displayId);
276     ASSERT_EQ(Orientation::HORIZONTAL, screen->GetOrientation());
277     ASSERT_EQ(Orientation::HORIZONTAL, display->GetOrientation());
278     sleep(SPLIT_TEST_SLEEP_S);
279 
280     WindowManager::GetInstance().ToggleShownStateForAllAppWindows();
281     ASSERT_EQ(WMError::WM_OK, fullWindow->Hide());
282     sleep(SPLIT_TEST_SLEEP_S);
283 
284     WindowManager::GetInstance().ToggleShownStateForAllAppWindows();
285     sleep(SPLIT_TEST_SLEEP_S);
286 
287     ASSERT_EQ(WMError::WM_OK, fullWindow->Hide());
288     defaultScreen->SetOrientation(Orientation::UNSPECIFIED);
289     sleep(SPLIT_TEST_SLEEP_S);
290 }
291 
292 /**
293 * @tc.name: WindowRotationTest6
294 * @tc.desc: test window rotation when display is zoomed.
295 * @tc.type: FUNC
296 * @tc.require: issueI5NGWL
297 */
298 HWTEST_F(WindowRotationTest, WindowRotationTest6, Function | MediumTest | Level3)
299 {
300     WindowAccessibilityController::GetInstance().SetAnchorAndScale(0, 0, 2);
301     sleep(SPLIT_TEST_SLEEP_S);
302 
303     fullInfo_.name  = "fullscreen.2";
304     fullInfo_.orientation_ = Orientation::REVERSE_HORIZONTAL;
305     const sptr<Window>& fullWindow = Utils::CreateTestWindow(fullInfo_);
306     activeWindows_.push_back(fullWindow);
307     ASSERT_EQ(WMError::WM_OK, fullWindow->Show());
308 
309     DisplayId displayId = displayListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
310     ScreenId screenId = screenListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
311     auto screen = ScreenManager::GetInstance().GetScreenById(screenId);
312     auto display = DisplayManager::GetInstance().GetDisplayById(displayId);
313     ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, screen->GetOrientation());
314     ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, display->GetOrientation());
315     sleep(SPLIT_TEST_SLEEP_S);
316 
317     Window* ptr = fullWindow.GetRefPtr();
318     WindowImpl* implPtr = (WindowImpl*)ptr;
319     Transform expect;
320     ASSERT_NE(expect, implPtr->GetWindowProperty()->GetZoomTransform());
321 
322     WindowAccessibilityController::GetInstance().OffWindowZoom();
323     ASSERT_EQ(WMError::WM_OK, fullWindow->Hide());
324     screen->SetOrientation(Orientation::UNSPECIFIED);
325     sleep(SPLIT_TEST_SLEEP_S);
326 }
327 }
328 } // namespace Rosen
329 } // namespace OHOS
330