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 activeWindows_.push_back(fullWindow);
142 ASSERT_EQ(WMError::WM_OK, fullWindow->Show());
143 ASSERT_EQ(WindowMode::WINDOW_MODE_FULLSCREEN, fullWindow->GetMode());
144 sleep(SPLIT_TEST_SLEEP_S);
145
146 fullWindow->SetRequestedOrientation(Orientation::REVERSE_HORIZONTAL);
147 sleep(SPLIT_TEST_SLEEP_S);
148 ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, fullWindow->GetRequestedOrientation());
149 DisplayId displayId = displayListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
150 displayListener_->changeFuture_.Reset(-1);
151 ScreenId screenId = screenListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
152 screenListener_->changeFuture_.Reset(-1);
153 auto screen = ScreenManager::GetInstance().GetScreenById(screenId);
154 auto display = DisplayManager::GetInstance().GetDisplayById(displayId);
155 ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, screen->GetOrientation());
156 ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, display->GetOrientation());
157 sleep(SPLIT_TEST_SLEEP_S);
158
159 ASSERT_EQ(WMError::WM_OK, fullWindow->Hide());
160 sleep(SPLIT_TEST_SLEEP_S);
161 ASSERT_EQ(Rotation::ROTATION_0, screen->GetRotation());
162 }
163
164 /**
165 * @tc.name: WindowRotationTest2
166 * @tc.desc: create window with orientation property.
167 * @tc.type: FUNC
168 */
169 HWTEST_F(WindowRotationTest, WindowRotationTest2, Function | MediumTest | Level3)
170 {
171 fullInfo_.name = "fullscreen.2";
172 fullInfo_.orientation_ = Orientation::REVERSE_HORIZONTAL;
173 const sptr<Window>& fullWindow = Utils::CreateTestWindow(fullInfo_);
174 activeWindows_.push_back(fullWindow);
175 ASSERT_EQ(WMError::WM_OK, fullWindow->Show());
176 ASSERT_EQ(WindowMode::WINDOW_MODE_FULLSCREEN, fullWindow->GetMode());
177 sleep(SPLIT_TEST_SLEEP_S);
178
179 ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, fullWindow->GetRequestedOrientation());
180 sleep(SPLIT_TEST_SLEEP_S);
181 DisplayId displayId = displayListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
182 ScreenId screenId = screenListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
183 auto screen = ScreenManager::GetInstance().GetScreenById(screenId);
184 auto display = DisplayManager::GetInstance().GetDisplayById(displayId);
185 ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, screen->GetOrientation());
186 ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, display->GetOrientation());
187 sleep(SPLIT_TEST_SLEEP_S);
188
189 ASSERT_EQ(WMError::WM_OK, fullWindow->Hide());
190 sleep(SPLIT_TEST_SLEEP_S);
191 screen->SetOrientation(Orientation::UNSPECIFIED);
192 displayListener_->changeFuture_.Reset(-1);
193 screenListener_->changeFuture_.Reset(-1);
194 sleep(SPLIT_TEST_SLEEP_S);
195 }
196
197 /**
198 * @tc.name: WindowRotationTest3
199 * @tc.desc: create floating window with orientation property
200 * @tc.type: FUNC
201 */
202 HWTEST_F(WindowRotationTest, WindowRotationTest3, Function | MediumTest | Level3)
203 {
204 auto display = DisplayManager::GetInstance().GetDefaultDisplay();
205 auto curDisplayOrientation = display->GetOrientation();
206
207 fullInfo_.name = "fullscreen.3";
208 fullInfo_.orientation_ = Orientation::REVERSE_HORIZONTAL;
209 fullInfo_.mode = WindowMode::WINDOW_MODE_FLOATING;
210 const sptr<Window>& fullWindow = Utils::CreateTestWindow(fullInfo_);
211 activeWindows_.push_back(fullWindow);
212 ASSERT_EQ(WMError::WM_OK, fullWindow->Show());
213 ASSERT_EQ(WindowMode::WINDOW_MODE_FLOATING, fullWindow->GetMode());
214 sleep(SPLIT_TEST_SLEEP_S);
215
216 ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, fullWindow->GetRequestedOrientation());
217 sleep(SPLIT_TEST_SLEEP_S);
218 ASSERT_EQ(curDisplayOrientation, display->GetOrientation());
219 sleep(SPLIT_TEST_SLEEP_S);
220
221 curDisplayOrientation = display->GetOrientation();
222 ASSERT_EQ(WMError::WM_OK, fullWindow->Hide());
223 sleep(SPLIT_TEST_SLEEP_S);
224 ASSERT_EQ(curDisplayOrientation, display->GetOrientation());
225 sleep(SPLIT_TEST_SLEEP_S);
226 }
227
228
229 /**
230 * @tc.name: WindowRotationTest4
231 * @tc.desc: create window with orientation after setting screen default orientation.
232 * @tc.type: FUNC
233 */
234 HWTEST_F(WindowRotationTest, WindowRotationTest4, Function | MediumTest | Level3)
235 {
236 ScreenId defaultScreenId = DisplayManager::GetInstance().GetDefaultDisplay()->GetScreenId();
237 auto defaultScreen = ScreenManager::GetInstance().GetScreenById(defaultScreenId);
238 defaultScreen->SetOrientation(Orientation::REVERSE_HORIZONTAL);
239 sleep(SPLIT_TEST_SLEEP_S);
240
241 fullInfo_.name = "fullscreen.4";
242 fullInfo_.orientation_ = Orientation::HORIZONTAL;
243 const sptr<Window>& fullWindow = Utils::CreateTestWindow(fullInfo_);
244 activeWindows_.push_back(fullWindow);
245 ASSERT_EQ(WMError::WM_OK, fullWindow->Show());
246 ASSERT_EQ(WindowMode::WINDOW_MODE_FULLSCREEN, fullWindow->GetMode());
247 sleep(SPLIT_TEST_SLEEP_S);
248
249 ASSERT_EQ(Orientation::HORIZONTAL, fullWindow->GetRequestedOrientation());
250 DisplayId displayId = displayListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
251 displayListener_->changeFuture_.Reset(-1);
252 ScreenId screenId = screenListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
253 screenListener_->changeFuture_.Reset(-1);
254 auto screen = ScreenManager::GetInstance().GetScreenById(screenId);
255 auto display = DisplayManager::GetInstance().GetDisplayById(displayId);
256 sleep(SPLIT_TEST_SLEEP_S);
257 ASSERT_EQ(Orientation::HORIZONTAL, screen->GetOrientation());
258 ASSERT_EQ(Orientation::HORIZONTAL, display->GetOrientation());
259
260 ASSERT_EQ(WMError::WM_OK, fullWindow->Hide());
261 sleep(SPLIT_TEST_SLEEP_S);
262 defaultScreen->SetOrientation(Orientation::UNSPECIFIED);
263 sleep(SPLIT_TEST_SLEEP_S);
264 }
265
266 /**
267 * @tc.name: WindowRotationTest5
268 * @tc.desc: create window with orientation after setting screen default orientation, and toggle shown state for all app
269 * windows.
270 * @tc.type: FUNC
271 */
272 HWTEST_F(WindowRotationTest, WindowRotationTest5, Function | MediumTest | Level3)
273 {
274 ScreenId defaultScreenId = DisplayManager::GetInstance().GetDefaultDisplay()->GetScreenId();
275 auto defaultScreen = ScreenManager::GetInstance().GetScreenById(defaultScreenId);
276 defaultScreen->SetOrientation(Orientation::REVERSE_HORIZONTAL);
277 sleep(SPLIT_TEST_SLEEP_S);
278
279 fullInfo_.name = "fullscreen.5";
280 fullInfo_.orientation_ = Orientation::HORIZONTAL;
281 const sptr<Window>& fullWindow = Utils::CreateTestWindow(fullInfo_);
282 activeWindows_.push_back(fullWindow);
283 ASSERT_EQ(WMError::WM_OK, fullWindow->Show());
284 ASSERT_EQ(WindowMode::WINDOW_MODE_FULLSCREEN, fullWindow->GetMode());
285 sleep(SPLIT_TEST_SLEEP_S);
286
287 ASSERT_EQ(Orientation::HORIZONTAL, fullWindow->GetRequestedOrientation());
288 sleep(SPLIT_TEST_SLEEP_S);
289 DisplayId displayId = displayListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
290 displayListener_->changeFuture_.Reset(-1);
291 ScreenId screenId = screenListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
292 screenListener_->changeFuture_.Reset(-1);
293 auto screen = ScreenManager::GetInstance().GetScreenById(screenId);
294 auto display = DisplayManager::GetInstance().GetDisplayById(displayId);
295 ASSERT_EQ(Orientation::HORIZONTAL, display->GetOrientation());
296
297 WindowManager::GetInstance().ToggleShownStateForAllAppWindows();
298 sleep(SPLIT_TEST_SLEEP_S);
299 ASSERT_EQ(WMError::WM_OK, fullWindow->Hide());
300 sleep(SPLIT_TEST_SLEEP_S);
301
302 WindowManager::GetInstance().ToggleShownStateForAllAppWindows();
303 sleep(SPLIT_TEST_SLEEP_S);
304
305 ASSERT_EQ(WMError::WM_OK, fullWindow->Hide());
306 sleep(SPLIT_TEST_SLEEP_S);
307 defaultScreen->SetOrientation(Orientation::UNSPECIFIED);
308 sleep(SPLIT_TEST_SLEEP_S);
309 }
310 }
311 } // namespace Rosen
312 } // namespace OHOS
313