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