• 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 #include <pointer_event.h>
18 #include <ui/rs_surface_node.h>
19 
20 #include "session/host/include/main_session.h"
21 #include "session/host/include/session.h"
22 #include "session/host/include/sub_session.h"
23 #include "session/screen/include/screen_session.h"
24 #include "window_helper.h"
25 #include "window_manager_hilog.h"
26 
27 using namespace testing;
28 using namespace testing::ext;
29 
30 namespace OHOS {
31 namespace Rosen {
32 class MainSessionLayoutTest : public testing::Test {
33 public:
34     static void SetUpTestCase();
35     static void TearDownTestCase();
36     void SetUp() override;
37     void TearDown() override;
38 
39 private:
40     RSSurfaceNode::SharedPtr CreateRSSurfaceNode();
41 };
42 
SetUpTestCase()43 void MainSessionLayoutTest::SetUpTestCase() {}
44 
TearDownTestCase()45 void MainSessionLayoutTest::TearDownTestCase() {}
46 
SetUp()47 void MainSessionLayoutTest::SetUp() {}
48 
TearDown()49 void MainSessionLayoutTest::TearDown() {}
50 
CreateRSSurfaceNode()51 RSSurfaceNode::SharedPtr MainSessionLayoutTest::CreateRSSurfaceNode()
52 {
53     struct RSSurfaceNodeConfig rsSurfaceNodeConfig;
54     rsSurfaceNodeConfig.SurfaceNodeName = "WindowSessionTestSurfaceNode";
55     auto surfaceNode = RSSurfaceNode::Create(rsSurfaceNodeConfig);
56     return surfaceNode;
57 }
58 
59 namespace {
60 /**
61  * @tc.name: SetSubWindowBoundsDuringCross
62  * @tc.desc: Check reason when setSubWindowBoundsDuringCross
63  * @tc.type: FUNC
64  */
65 HWTEST_F(MainSessionLayoutTest, SetSubWindowBoundsDuringCross, TestSize.Level1)
66 {
67     SessionInfo info;
68     info.bundleName_ = "SetSubWindowBoundsDuringCross";
69     info.moduleName_ = "SetSubWindowBoundsDuringCross";
70     info.abilityName_ = "SetSubWindowBoundsDuringCross";
71     sptr<MainSession> mainSession = sptr<MainSession>::MakeSptr(info, nullptr);
72     sptr<SceneSession> subSession = sptr<SceneSession>::MakeSptr(info, nullptr);
73     mainSession->subSession_.emplace_back(nullptr);
74     mainSession->subSession_.emplace_back(subSession);
75     mainSession->GetLayoutController()->SetSessionRect({ 50, 50, 500, 500 });
76     subSession->GetLayoutController()->SetSessionRect({ 0, 0, 200, 200 });
77 
78     subSession->state_ = SessionState::STATE_FOREGROUND;
79     subSession->windowAnchorInfo_.isAnchorEnabled_ = false;
80     subSession->Session::UpdateSizeChangeReason(SizeChangeReason::RESIZE);
81     mainSession->SetSubWindowBoundsDuringCross(mainSession->GetSessionRect(), true, true);
82     EXPECT_EQ(subSession->GetSizeChangeReason(), SizeChangeReason::RESIZE);
83 
84     subSession->windowAnchorInfo_.isAnchorEnabled_ = true;
85     subSession->Session::UpdateSizeChangeReason(SizeChangeReason::RESIZE);
86     mainSession->SetSubWindowBoundsDuringCross(mainSession->GetSessionRect(), true, true);
87     EXPECT_EQ(subSession->GetSizeChangeReason(), SizeChangeReason::UNDEFINED);
88 
89     subSession->state_ = SessionState::STATE_BACKGROUND;
90     subSession->Session::UpdateSizeChangeReason(SizeChangeReason::RESIZE);
91     mainSession->SetSubWindowBoundsDuringCross(mainSession->GetSessionRect(), true, true);
92     EXPECT_EQ(subSession->GetSizeChangeReason(), SizeChangeReason::RESIZE);
93 }
94 
95 /**
96  * @tc.name: NotifySubSessionRectChangeByAnchor
97  * @tc.desc: Check reason when NotifySubSessionRectChangeByAnchor
98  * @tc.type: FUNC
99  */
100 HWTEST_F(MainSessionLayoutTest, NotifySubSessionRectChangeByAnchor, TestSize.Level1)
101 {
102     SessionInfo info;
103     info.bundleName_ = "NotifySubSessionRectChangeByAnchor";
104     info.moduleName_ = "NotifySubSessionRectChangeByAnchor";
105     info.abilityName_ = "NotifySubSessionRectChangeByAnchor";
106     sptr<MainSession> mainSession = sptr<MainSession>::MakeSptr(info, nullptr);
107     sptr<SceneSession> subSession = sptr<SceneSession>::MakeSptr(info, nullptr);
108     mainSession->subSession_.emplace_back(nullptr);
109     mainSession->subSession_.emplace_back(subSession);
110     mainSession->GetLayoutController()->SetSessionRect({ 50, 50, 500, 500 });
111     subSession->GetLayoutController()->SetSessionRect({ 0, 0, 200, 200 });
112     const auto& func = [subSession](const WSRect& rect,
113                                     SizeChangeReason reason,
114                                     DisplayId displayId,
__anone555aa4d0202(const WSRect& rect, SizeChangeReason reason, DisplayId displayId, const RectAnimationConfig& rectAnimationConfig) 115                                     const RectAnimationConfig& rectAnimationConfig) {
116         subSession->GetLayoutController()->SetSessionRect(rect);
117         subSession->Session::UpdateSizeChangeReason(reason);
118     };
119     subSession->SetSessionRectChangeCallback(func);
120 
121     subSession->windowAnchorInfo_.isAnchorEnabled_ = false;
122     subSession->Session::UpdateSizeChangeReason(SizeChangeReason::UNDEFINED);
123     mainSession->NotifySubSessionRectChangeByAnchor(mainSession->GetSessionRect(), SizeChangeReason::RESIZE);
124     EXPECT_EQ(subSession->GetSizeChangeReason(), SizeChangeReason::UNDEFINED);
125 
126     subSession->windowAnchorInfo_.isAnchorEnabled_ = true;
127     subSession->Session::UpdateSizeChangeReason(SizeChangeReason::UNDEFINED);
128     mainSession->NotifySubSessionRectChangeByAnchor(mainSession->GetSessionRect(), SizeChangeReason::RESIZE);
129     EXPECT_EQ(subSession->GetSizeChangeReason(), SizeChangeReason::RESIZE);
130 }
131 
132 /**
133  * @tc.name: HandleSubSessionSurfaceNodeByWindowAnchor
134  * @tc.desc: Check reason when HandleSubSessionSurfaceNodeByWindowAnchor
135  * @tc.type: FUNC
136  */
137 HWTEST_F(MainSessionLayoutTest, HandleSubSessionSurfaceNodeByWindowAnchor, TestSize.Level1)
138 {
139     SessionInfo info;
140     info.bundleName_ = "HandleSubSessionSurfaceNodeByWindowAnchor";
141     info.moduleName_ = "HandleSubSessionSurfaceNodeByWindowAnchor";
142     info.abilityName_ = "HandleSubSessionSurfaceNodeByWindowAnchor";
143     sptr<MainSession> mainSession = sptr<MainSession>::MakeSptr(info, nullptr);
144     sptr<SubSession> subSession = sptr<SubSession>::MakeSptr(info, nullptr);
145     mainSession->subSession_.emplace_back(nullptr);
146     mainSession->subSession_.emplace_back(subSession);
147     ScreenSessionConfig config = {
148         .screenId = 100,
149         .rsId = 101,
150         .name = "OpenHarmony",
151     };
152     sptr<ScreenSession> screenSession =
153         sptr<ScreenSession>::MakeSptr(config, ScreenSessionReason::CREATE_SESSION_FOR_VIRTUAL);
154     auto surfaceNode = CreateRSSurfaceNode();
155     ASSERT_NE(nullptr, surfaceNode);
156     subSession->SetSurfaceNode(surfaceNode);
157 
158     subSession->state_ = SessionState::STATE_FOREGROUND;
159     subSession->windowAnchorInfo_.isAnchorEnabled_ = false;
160     subSession->cloneNodeCountDuringCross_.store(0);
161     mainSession->HandleSubSessionSurfaceNodeByWindowAnchor(SizeChangeReason::DRAG, screenSession);
162     EXPECT_EQ(subSession->cloneNodeCountDuringCross_, 0);
163 
164     subSession->windowAnchorInfo_.isAnchorEnabled_ = true;
165     mainSession->HandleSubSessionSurfaceNodeByWindowAnchor(SizeChangeReason::DRAG, screenSession);
166     EXPECT_EQ(subSession->cloneNodeCountDuringCross_, 1);
167 
168     subSession->state_ = SessionState::STATE_BACKGROUND;
169     mainSession->HandleSubSessionSurfaceNodeByWindowAnchor(SizeChangeReason::DRAG_END, screenSession);
170     EXPECT_EQ(subSession->cloneNodeCountDuringCross_, 1);
171 }
172 } // namespace
173 } // namespace Rosen
174 } // namespace OHOS