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