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