• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include <gtest/gtest.h>
17 
18 #include "ability_context_impl.h"
19 #include "common_test_utils.h"
20 #include "extension_data_handler.h"
21 #include "iremote_object_mocker.h"
22 #include "mock_session.h"
23 #include "mock_uicontent.h"
24 #include "mock_window_adapter.h"
25 #include "singleton_mocker.h"
26 #include "window_session_impl.h"
27 
28 using namespace testing;
29 using namespace testing::ext;
30 
31 namespace OHOS {
32 namespace Rosen {
33 using Mocker = SingletonMocker<WindowAdapter, MockWindowAdapter>;
34 uint32_t MaxWith = 32;
35 
36 class WindowSessionImplRotationTest : public testing::Test {
37 public:
38     static void SetUpTestCase();
39     static void TearDownTestCase();
40     void SetUp() override;
41     void TearDown() override;
42     sptr<WindowSessionImpl> window_;
43 
44     std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext_;
45     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
46 
47 private:
48     RSSurfaceNode::SharedPtr CreateRSSurfaceNode();
49     static constexpr uint32_t WAIT_SYNC_IN_NS = 200000;
50 };
51 
SetUpTestCase()52 void WindowSessionImplRotationTest::SetUpTestCase() {}
53 
TearDownTestCase()54 void WindowSessionImplRotationTest::TearDownTestCase() {}
55 
SetUp()56 void WindowSessionImplRotationTest::SetUp()
57 {
58     abilityContext_ = std::make_shared<AbilityRuntime::AbilityContextImpl>();
59 }
60 
TearDown()61 void WindowSessionImplRotationTest::TearDown()
62 {
63     usleep(WAIT_SYNC_IN_NS);
64     abilityContext_ = nullptr;
65     if (window_ != nullptr) {
66         window_->Destroy();
67     }
68 }
69 
CreateRSSurfaceNode()70 RSSurfaceNode::SharedPtr WindowSessionImplRotationTest::CreateRSSurfaceNode()
71 {
72     struct RSSurfaceNodeConfig rsSurfaceNodeConfig;
73     rsSurfaceNodeConfig.SurfaceNodeName = "startingWindowTestSurfaceNode";
74     auto surfaceNode = RSSurfaceNode::Create(rsSurfaceNodeConfig, RSSurfaceNodeType::DEFAULT);
75     return surfaceNode;
76 }
77 
78 namespace {
GetTestWindowImpl(const std::string & name)79 sptr<WindowSessionImpl> GetTestWindowImpl(const std::string& name)
80 {
81     sptr<WindowOption> option = new (std::nothrow) WindowOption();
82     if (option == nullptr) {
83         return nullptr;
84     }
85     option->SetWindowName(name);
86     sptr<WindowSessionImpl> window = new (std::nothrow) WindowSessionImpl(option);
87     if (window == nullptr) {
88         return nullptr;
89     }
90     SessionInfo sessionInfo = { name, name, name };
91     sptr<SessionMocker> session = new (std::nothrow) SessionMocker(sessionInfo);
92     if (session == nullptr) {
93         return nullptr;
94     }
95     window->hostSession_ = session;
96     return window;
97 }
98 
99 /**
100  * @tc.name: UpdateRectForRotation
101  * @tc.desc: UpdateRectForRotation Test
102  * @tc.type: FUNC
103  */
104 HWTEST_F(WindowSessionImplRotationTest, UpdateRectForRotation, TestSize.Level1)
105 {
106     sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
107     option->SetWindowName("WindowSessionCreateCheck");
108     sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
109 
110     Rect wmRect;
111     wmRect.posX_ = 0;
112     wmRect.posY_ = 0;
113     wmRect.height_ = 50;
114     wmRect.width_ = 50;
115 
116     WSRect rect;
117     wmRect.posX_ = 0;
118     wmRect.posY_ = 0;
119     wmRect.height_ = 50;
120     wmRect.width_ = 50;
121 
122     Rect preRect;
123     preRect.posX_ = 0;
124     preRect.posY_ = 0;
125     preRect.height_ = 200;
126     preRect.width_ = 200;
127 
128     window->property_->SetWindowRect(preRect);
129     WindowSizeChangeReason wmReason = WindowSizeChangeReason{ 0 };
130     std::shared_ptr<RSTransaction> rsTransaction;
131     SceneAnimationConfig config{ .rsTransaction_ = rsTransaction };
132     window->UpdateRectForRotation(wmRect, preRect, wmReason, config);
133 
134     SizeChangeReason reason = SizeChangeReason::UNDEFINED;
135     auto res = window->UpdateRect(rect, reason);
136     ASSERT_EQ(res, WSError::WS_OK);
137 }
138 
139 /**
140  * @tc.name: SetCurrentRotation
141  * @tc.desc: SetCurrentRotation
142  * @tc.type: FUNC
143  */
144 HWTEST_F(WindowSessionImplRotationTest, SetCurrentRotation, Function | SmallTest | Level1)
145 {
146     sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
147     option->SetWindowName("SetCurrentRotation");
148     sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
149     auto ret = window->SetCurrentRotation(FULL_CIRCLE_DEGREE + 1);
150     EXPECT_EQ(ret, WSError::WS_ERROR_INVALID_PARAM);
151     ret = window->SetCurrentRotation(ZERO_CIRCLE_DEGREE - 1);
152     EXPECT_EQ(ret, WSError::WS_ERROR_INVALID_PARAM);
153     ret = window->SetCurrentRotation(ONE_FOURTH_FULL_CIRCLE_DEGREE);
154     EXPECT_EQ(ret, WSError::WS_OK);
155 }
156 
157 /**
158  * @tc.name: SetRequestedOrientation01
159  * @tc.desc: SetRequestedOrientation01
160  * @tc.type: FUNC
161  */
162 HWTEST_F(WindowSessionImplRotationTest, SetRequestedOrientation01, TestSize.Level1)
163 {
164     GTEST_LOG_(INFO) << "WindowSessionImplRotationTest: SetRequestedOrientation01 start";
165     sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
166     option->SetWindowName("SetRequestedOrientation01");
167     sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
168 
169     SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" };
170     sptr<SessionMocker> session = new (std::nothrow) SessionMocker(sessionInfo);
171     ASSERT_NE(nullptr, session);
172     ASSERT_EQ(WMError::WM_OK, window->Create(nullptr, session));
173 
174     window->hostSession_ = session;
175     window->property_->SetPersistentId(1);
176     window->state_ = WindowState::STATE_CREATED;
177 
178     Orientation ori = Orientation::VERTICAL;
179     window->SetRequestedOrientation(ori);
180     Orientation ret = window->property_->GetRequestedOrientation();
181     ASSERT_EQ(ret, ori);
182 
183     window->SetRequestedOrientation(Orientation::AUTO_ROTATION_UNSPECIFIED);
184     Orientation ret1 = window->property_->GetRequestedOrientation();
185     ASSERT_EQ(ret1, Orientation::AUTO_ROTATION_UNSPECIFIED);
186 
187     window->SetRequestedOrientation(Orientation::USER_ROTATION_PORTRAIT);
188     Orientation ret2 = window->property_->GetRequestedOrientation();
189     ASSERT_EQ(ret2, Orientation::USER_ROTATION_PORTRAIT);
190 
191     window->SetRequestedOrientation(Orientation::USER_ROTATION_LANDSCAPE);
192     Orientation ret3 = window->property_->GetRequestedOrientation();
193     ASSERT_EQ(ret3, Orientation::USER_ROTATION_LANDSCAPE);
194 
195     window->SetRequestedOrientation(Orientation::USER_ROTATION_PORTRAIT_INVERTED);
196     Orientation ret4 = window->property_->GetRequestedOrientation();
197     ASSERT_EQ(ret4, Orientation::USER_ROTATION_PORTRAIT_INVERTED);
198 
199     window->SetRequestedOrientation(Orientation::USER_ROTATION_LANDSCAPE_INVERTED);
200     Orientation ret5 = window->property_->GetRequestedOrientation();
201     ASSERT_EQ(ret5, Orientation::USER_ROTATION_LANDSCAPE_INVERTED);
202 
203     window->SetRequestedOrientation(Orientation::FOLLOW_DESKTOP);
204     Orientation ret6 = window->property_->GetRequestedOrientation();
205     ASSERT_EQ(ret6, Orientation::FOLLOW_DESKTOP);
206     ASSERT_EQ(WMError::WM_OK, window->Destroy());
207     GTEST_LOG_(INFO) << "WindowSessionImplRotationTest: SetRequestedOrientation01 end";
208 }
209 
210 /**
211  * @tc.name: SetRequestedOrientation02
212  * @tc.desc: SetRequestedOrientation02
213  * @tc.type: FUNC
214  */
215 HWTEST_F(WindowSessionImplRotationTest, SetRequestedOrientation02, TestSize.Level1)
216 {
217     auto window = GetTestWindowImpl("SetRequestedOrientation02");
218     ASSERT_NE(window, nullptr);
219     window->property_->SetRequestedOrientation(Orientation::BEGIN);
220     window->SetRequestedOrientation(Orientation::END);
221 
222     window->property_->SetRequestedOrientation(Orientation::USER_ROTATION_PORTRAIT);
223     window->SetRequestedOrientation(Orientation::USER_ROTATION_PORTRAIT);
224 
225     window->property_->SetRequestedOrientation(Orientation::BEGIN);
226     window->SetRequestedOrientation(Orientation::BEGIN);
227     window->Destroy();
228 }
229 
230 /**
231  * @tc.name: SetRequestedOrientation03
232  * @tc.desc: SetRequestedOrientation03
233  * @tc.type: FUNC
234  */
235 HWTEST_F(WindowSessionImplRotationTest, SetRequestedOrientation03, TestSize.Level1)
236 {
237     GTEST_LOG_(INFO) << "WindowSessionImplRotationTest: SetRequestedOrientation03 start";
238     window_ = GetTestWindowImpl("SetRequestedOrientation03");
239     ASSERT_NE(window_, nullptr);
240     window_->property_->SetPersistentId(1);
241     window_->state_ = WindowState::STATE_CREATED;
242     Orientation orientation = Orientation::VERTICAL;
243     window_->property_->requestedOrientation_ = Orientation::VERTICAL;
244     window_->SetRequestedOrientation(orientation);
245     orientation = Orientation::USER_ROTATION_PORTRAIT;
246     window_->SetRequestedOrientation(orientation);
247     auto ret = window_->property_->GetRequestedOrientation();
248     ASSERT_EQ(ret, orientation);
249     GTEST_LOG_(INFO) << "WindowSessionImplRotationTest: SetRequestedOrientation03 end";
250 }
251 
252 /**
253  * @tc.name: GetRequestedOrientationtest01
254  * @tc.desc: GetRequestedOrientation
255  * @tc.type: FUNC
256  */
257 HWTEST_F(WindowSessionImplRotationTest, GetRequestedOrientation, TestSize.Level1)
258 {
259     GTEST_LOG_(INFO) << "WindowSessionImplRotationTest: GetRequestedOrientationtest01 start";
260     sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
261 
262     option->SetWindowName("GetRequestedOrientation");
263 
264     sptr<WindowSessionImpl> window = new (std::nothrow) WindowSessionImpl(option);
265     ASSERT_NE(window, nullptr);
266 
267     SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" };
268     sptr<SessionMocker> session = new (std::nothrow) SessionMocker(sessionInfo);
269     ASSERT_NE(nullptr, session);
270     ASSERT_EQ(WMError::WM_OK, window->Create(nullptr, session));
271 
272     window->hostSession_ = session;
273     window->property_->SetPersistentId(1);
274     window->state_ = WindowState::STATE_CREATED;
275 
276     Orientation ori = Orientation::HORIZONTAL;
277     window->SetUserRequestedOrientation(ori);
278     Orientation ret = window->GetRequestedOrientation();
279     ASSERT_EQ(ret, ori);
280 
281     window->SetUserRequestedOrientation(Orientation::AUTO_ROTATION_UNSPECIFIED);
282     Orientation ret1 = window->GetRequestedOrientation();
283     ASSERT_EQ(ret1, Orientation::AUTO_ROTATION_UNSPECIFIED);
284 
285     window->SetUserRequestedOrientation(Orientation::USER_ROTATION_PORTRAIT);
286     Orientation ret2 = window->GetRequestedOrientation();
287     ASSERT_EQ(ret2, Orientation::USER_ROTATION_PORTRAIT);
288 
289     window->SetUserRequestedOrientation(Orientation::USER_ROTATION_LANDSCAPE);
290     Orientation ret3 = window->GetRequestedOrientation();
291     ASSERT_EQ(ret3, Orientation::USER_ROTATION_LANDSCAPE);
292 
293     window->SetUserRequestedOrientation(Orientation::USER_ROTATION_PORTRAIT_INVERTED);
294     Orientation ret4 = window->GetRequestedOrientation();
295     ASSERT_EQ(ret4, Orientation::USER_ROTATION_PORTRAIT_INVERTED);
296 
297     window->SetUserRequestedOrientation(Orientation::USER_ROTATION_LANDSCAPE_INVERTED);
298     Orientation ret5 = window->GetRequestedOrientation();
299     ASSERT_EQ(ret5, Orientation::USER_ROTATION_LANDSCAPE_INVERTED);
300 
301     window->SetUserRequestedOrientation(Orientation::FOLLOW_DESKTOP);
302     Orientation ret6 = window->GetRequestedOrientation();
303     ASSERT_EQ(ret6, Orientation::FOLLOW_DESKTOP);
304 
305     GTEST_LOG_(INFO) << "WindowSessionImplRotationTest: GetRequestedOrientationtest01 end";
306 }
307 
308 /**
309  * @tc.name: GetRequestedOrientation02
310  * @tc.desc: GetRequestedOrientation02
311  * @tc.type: FUNC
312  */
313 HWTEST_F(WindowSessionImplRotationTest, GetRequestedOrientation02, TestSize.Level1)
314 {
315     GTEST_LOG_(INFO) << "WindowSessionImplRotationTest: GetRequestedOrientation02 start";
316     window_ = GetTestWindowImpl("GetRequestedOrientation02");
317     ASSERT_NE(window_, nullptr);
318     window_->property_->SetPersistentId(INVALID_SESSION_ID);
319     auto ret = window_->GetRequestedOrientation();
320     ASSERT_EQ(ret, Orientation::UNSPECIFIED);
321     GTEST_LOG_(INFO) << "WindowSessionImplRotationTest: GetRequestedOrientation02 end";
322 }
323 
324 /**
325  * @tc.name: UpdateRectForRotation02
326  * @tc.desc: UpdateRectForRotation02 Test
327  * @tc.type: FUNC
328  */
329 HWTEST_F(WindowSessionImplRotationTest, UpdateRectForRotation02, TestSize.Level2)
330 {
331     sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
332     option->SetWindowName("UpdateRectForRotation02");
333     sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
334     auto runner = AppExecFwk::EventRunner::Create("UpdateRectForRotation02");
335     std::shared_ptr<AppExecFwk::EventHandler> handler = std::make_shared<AppExecFwk::EventHandler>(runner);
336     runner->Run();
337     window->handler_ = handler;
338 
339     Rect wmRect;
340     wmRect.posX_ = 0;
341     wmRect.posY_ = 0;
342     wmRect.height_ = 50;
343     wmRect.width_ = 50;
344 
345     Rect preRect;
346     preRect.posX_ = 0;
347     preRect.posY_ = 0;
348     preRect.height_ = 200;
349     preRect.width_ = 200;
350 
351     window->property_->SetWindowRect(preRect);
352     WindowSizeChangeReason wmReason = WindowSizeChangeReason::SNAPSHOT_ROTATION;
353     std::shared_ptr<RSTransaction> rsTransaction;
354     SceneAnimationConfig config{ .rsTransaction_ = rsTransaction };
355     window->UpdateRectForRotation(wmRect, preRect, wmReason, config);
356     usleep(WAIT_SYNC_IN_NS);
357     EXPECT_EQ(window->lastSizeChangeReason_, WindowSizeChangeReason::ROTATION);
358 
359     preRect.height_ = 200;
360     preRect.width_ = 200;
361     window->property_->SetWindowRect(preRect);
362     wmReason = WindowSizeChangeReason::ROTATION;
363     window->UpdateRectForRotation(wmRect, preRect, wmReason, config);
364     usleep(WAIT_SYNC_IN_NS);
365     EXPECT_EQ(window->lastSizeChangeReason_, WindowSizeChangeReason::ROTATION);
366 }
367 } // namespace
368 } // namespace Rosen
369 } // namespace OHOS