• 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 
24 using namespace testing;
25 using namespace testing::ext;
26 
27 namespace OHOS {
28 namespace Rosen {
29 using utils = WindowTestUtils;
30 class DisplayListener : public DisplayManager::IDisplayListener {
31 public:
32     virtual void OnCreate(DisplayId) override;
33     virtual void OnDestroy(DisplayId) override;
34     virtual void OnChange(DisplayId) override;
35     RunnableFuture<DisplayId> changeFuture_;
36 };
37 
38 class ScreenListener : public ScreenManager::IScreenListener {
39 public:
40     virtual void OnConnect(ScreenId) override;
41     virtual void OnDisconnect(ScreenId) override;
42     virtual void OnChange(ScreenId) override;
43     RunnableFuture<ScreenId> changeFuture_;
44 };
45 
46 class WindowRotationTest : public testing::Test {
47 public:
48     static void SetUpTestCase();
49     static void TearDownTestCase();
50     virtual void SetUp() override;
51     virtual void TearDown() override;
52     std::vector<sptr<Window>> activeWindows_;
53     utils::TestWindowInfo fullInfo_;
54     sptr<DisplayListener> displayListener_;
55     sptr<ScreenListener> screenListener_;
56 private:
57     static constexpr uint32_t SPLIT_TEST_SLEEP_S = 1;
58     static constexpr long FUTURE_GET_RESULT_TIMEOUT = 1000;
59 };
60 
OnCreate(DisplayId displayId)61 void DisplayListener::OnCreate(DisplayId displayId)
62 {
63 }
64 
OnDestroy(DisplayId displayId)65 void DisplayListener::OnDestroy(DisplayId displayId)
66 {
67 }
68 
OnChange(DisplayId displayId)69 void DisplayListener::OnChange(DisplayId displayId)
70 {
71     changeFuture_.SetValue(displayId);
72 }
73 
OnConnect(ScreenId screenId)74 void ScreenListener::OnConnect(ScreenId screenId)
75 {
76 }
77 
OnDisconnect(ScreenId screenId)78 void ScreenListener::OnDisconnect(ScreenId screenId)
79 {
80 }
81 
OnChange(ScreenId screenId)82 void ScreenListener::OnChange(ScreenId screenId)
83 {
84     changeFuture_.SetValue(screenId);
85 }
86 
SetUpTestCase()87 void WindowRotationTest::SetUpTestCase()
88 {
89 }
90 
TearDownTestCase()91 void WindowRotationTest::TearDownTestCase()
92 {
93 }
94 
SetUp()95 void WindowRotationTest::SetUp()
96 {
97     fullInfo_ = {
98             .name = "",
99             .rect = utils::customAppRect_,
100             .type = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
101             .mode = WindowMode::WINDOW_MODE_FULLSCREEN,
102             .needAvoid = true,
103             .parentLimit = false,
104             .parentName = "",
105     };
106 
107     activeWindows_.clear();
108     displayListener_ = new DisplayListener();
109     DisplayManager::GetInstance().RegisterDisplayListener(displayListener_);
110     screenListener_ = new ScreenListener();
111     ScreenManager::GetInstance().RegisterScreenListener(screenListener_);
112 }
113 
TearDown()114 void WindowRotationTest::TearDown()
115 {
116     while (!activeWindows_.empty()) {
117         ASSERT_EQ(WMError::WM_OK, activeWindows_.back()->Destroy());
118         activeWindows_.pop_back();
119     }
120     DisplayManager::GetInstance().UnregisterDisplayListener(displayListener_);
121     ScreenManager::GetInstance().UnregisterScreenListener(screenListener_);
122 }
123 
124 namespace {
125 /**
126 * @tc.name: WindowRotationTest1
127 * @tc.desc: create window and SetRequestedOrientation.
128 * @tc.type: FUNC
129 */
130 HWTEST_F(WindowRotationTest, WindowRotationTest1, Function | MediumTest | Level3)
131 {
132     fullInfo_.name  = "fullscreen.1";
133     fullInfo_.orientation_ = Orientation::UNSPECIFIED;
134     const sptr<Window>& fullWindow = utils::CreateTestWindow(fullInfo_);
135     activeWindows_.push_back(fullWindow);
136     ASSERT_EQ(WMError::WM_OK, fullWindow->Show());
137     ASSERT_EQ(WindowMode::WINDOW_MODE_FULLSCREEN, fullWindow->GetMode());
138     sleep(SPLIT_TEST_SLEEP_S);
139 
140     fullWindow->SetRequestedOrientation(Orientation::REVERSE_HORIZONTAL);
141     ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, fullWindow->GetRequestedOrientation());
142     DisplayId displayId = displayListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
143     displayListener_->changeFuture_.Reset(-1);
144     ScreenId screenId = screenListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
145     screenListener_->changeFuture_.Reset(-1);
146     auto screen = ScreenManager::GetInstance().GetScreenById(screenId);
147     auto display = DisplayManager::GetInstance().GetDisplayById(displayId);
148     ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, screen->GetOrientation());
149     ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, display->GetOrientation());
150     sleep(SPLIT_TEST_SLEEP_S);
151 
152     ASSERT_EQ(WMError::WM_OK, fullWindow->Hide());
153     sleep(SPLIT_TEST_SLEEP_S);
154 }
155 
156 /**
157 * @tc.name: WindowRotationTest2
158 * @tc.desc: create window with orientation property.
159 * @tc.type: FUNC
160 */
161 HWTEST_F(WindowRotationTest, WindowRotationTest2, Function | MediumTest | Level3)
162 {
163     fullInfo_.name  = "fullscreen.2";
164     fullInfo_.orientation_ = Orientation::REVERSE_HORIZONTAL;
165     const sptr<Window>& fullWindow = utils::CreateTestWindow(fullInfo_);
166     activeWindows_.push_back(fullWindow);
167     ASSERT_EQ(WMError::WM_OK, fullWindow->Show());
168     ASSERT_EQ(WindowMode::WINDOW_MODE_FULLSCREEN, fullWindow->GetMode());
169 
170     ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, fullWindow->GetRequestedOrientation());
171     DisplayId displayId = displayListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
172     displayListener_->changeFuture_.Reset(-1);
173     ScreenId screenId = screenListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
174     screenListener_->changeFuture_.Reset(-1);
175     auto screen = ScreenManager::GetInstance().GetScreenById(screenId);
176     auto display = DisplayManager::GetInstance().GetDisplayById(displayId);
177     ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, screen->GetOrientation());
178     ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, display->GetOrientation());
179     sleep(SPLIT_TEST_SLEEP_S);
180 
181     ASSERT_EQ(WMError::WM_OK, fullWindow->Hide());
182     displayId = displayListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
183     displayListener_->changeFuture_.Reset(-1);
184     screenId = screenListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
185     screenListener_->changeFuture_.Reset(-1);
186     screen = ScreenManager::GetInstance().GetScreenById(screenId);
187     display = DisplayManager::GetInstance().GetDisplayById(displayId);
188     ASSERT_EQ(Orientation::UNSPECIFIED, screen->GetOrientation());
189     ASSERT_EQ(Orientation::UNSPECIFIED, display->GetOrientation());
190     sleep(SPLIT_TEST_SLEEP_S);
191 }
192 
193 /**
194 * @tc.name: WindowRotationTest3
195 * @tc.desc: create window with orientation property after setting screen default orientation.
196 * @tc.type: FUNC
197 */
198 HWTEST_F(WindowRotationTest, WindowRotationTest3, Function | MediumTest | Level3)
199 {
200     fullInfo_.name  = "fullscreen.4";
201     fullInfo_.orientation_ = Orientation::REVERSE_HORIZONTAL;
202     fullInfo_.mode = WindowMode::WINDOW_MODE_FLOATING;
203     const sptr<Window>& fullWindow = utils::CreateTestWindow(fullInfo_);
204     fullInfo_.mode = WindowMode::WINDOW_MODE_FULLSCREEN;
205     activeWindows_.push_back(fullWindow);
206     ASSERT_EQ(WMError::WM_OK, fullWindow->Show());
207     ASSERT_EQ(WindowMode::WINDOW_MODE_FLOATING, fullWindow->GetMode());
208     sleep(SPLIT_TEST_SLEEP_S);
209 
210     ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, fullWindow->GetRequestedOrientation());
211     auto display = DisplayManager::GetInstance().GetDefaultDisplay();
212     auto screen = ScreenManager::GetInstance().GetAllScreens()[0];
213     ASSERT_EQ(Orientation::UNSPECIFIED, screen->GetOrientation());
214     ASSERT_EQ(Orientation::UNSPECIFIED, display->GetOrientation());
215     sleep(SPLIT_TEST_SLEEP_S);
216 
217     ASSERT_EQ(WMError::WM_OK, fullWindow->Hide());
218     ASSERT_EQ(Orientation::UNSPECIFIED, screen->GetOrientation());
219     ASSERT_EQ(Orientation::UNSPECIFIED, display->GetOrientation());
220     sleep(SPLIT_TEST_SLEEP_S);
221 }
222 
223 /**
224 * @tc.name: WindowRotationTest4
225 * @tc.desc: create window with orientation.
226 * @tc.type: FUNC
227 */
228 HWTEST_F(WindowRotationTest, WindowRotationTest4, Function | MediumTest | Level3)
229 {
230     ScreenId defaultScreenId = DisplayManager::GetInstance().GetDefaultDisplay()->GetScreenId();
231     auto defaultScreen = ScreenManager::GetInstance().GetScreenById(defaultScreenId);
232     defaultScreen->SetOrientation(Orientation::REVERSE_HORIZONTAL);
233     fullInfo_.name  = "fullscreen.5";
234     fullInfo_.orientation_ = Orientation::HORIZONTAL;
235     const sptr<Window>& fullWindow = utils::CreateTestWindow(fullInfo_);
236     activeWindows_.push_back(fullWindow);
237     ASSERT_EQ(WMError::WM_OK, fullWindow->Show());
238     ASSERT_EQ(WindowMode::WINDOW_MODE_FULLSCREEN, fullWindow->GetMode());
239 
240     ASSERT_EQ(Orientation::HORIZONTAL, fullWindow->GetRequestedOrientation());
241     DisplayId displayId = displayListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
242     displayListener_->changeFuture_.Reset(-1);
243     ScreenId screenId = screenListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
244     screenListener_->changeFuture_.Reset(-1);
245     auto screen = ScreenManager::GetInstance().GetScreenById(screenId);
246     auto display = DisplayManager::GetInstance().GetDisplayById(displayId);
247     ASSERT_EQ(Orientation::HORIZONTAL, screen->GetOrientation());
248     ASSERT_EQ(Orientation::HORIZONTAL, display->GetOrientation());
249     sleep(SPLIT_TEST_SLEEP_S);
250 
251     ASSERT_EQ(WMError::WM_OK, fullWindow->Hide());
252     displayId = displayListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
253     displayListener_->changeFuture_.Reset(-1);
254     screenId = screenListener_->changeFuture_.GetResult(FUTURE_GET_RESULT_TIMEOUT);
255     screenListener_->changeFuture_.Reset(-1);
256     screen = ScreenManager::GetInstance().GetScreenById(screenId);
257     display = DisplayManager::GetInstance().GetDisplayById(displayId);
258     ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, screen->GetOrientation());
259     ASSERT_EQ(Orientation::REVERSE_HORIZONTAL, display->GetOrientation());
260     sleep(SPLIT_TEST_SLEEP_S);
261     defaultScreen->SetOrientation(Orientation::UNSPECIFIED);
262 }
263 }
264 } // namespace Rosen
265 } // namespace OHOS
266