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 "session/host/include/scene_session.h"
19 #include "session/host/include/session.h"
20 #include "session_manager/include/scene_session_manager.h"
21 #include "session_info.h"
22 #include "wm_common.h"
23
24 using namespace testing;
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace Rosen {
29 class LayoutControllerTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 void SetUp() override;
34 void TearDown() override;
35
36 private:
37 sptr<LayoutController> layoutController_ = nullptr;
38 };
39
SetUpTestCase()40 void LayoutControllerTest::SetUpTestCase() {}
41
TearDownTestCase()42 void LayoutControllerTest::TearDownTestCase() {}
43
SetUp()44 void LayoutControllerTest::SetUp()
45 {
46 sptr<WindowSessionProperty> sessionProperty = sptr<WindowSessionProperty>::MakeSptr();
47 layoutController_ = sptr<LayoutController>::MakeSptr(sessionProperty);
48 }
49
TearDown()50 void LayoutControllerTest::TearDown()
51 {
52 layoutController_ = nullptr;
53 }
54
55 namespace {
56 /**
57 * @tc.name: SetSessionGlobalRect
58 * @tc.desc: SetSessionGlobalRect
59 * @tc.type: FUNC
60 */
61 HWTEST_F(LayoutControllerTest, SetSessionGlobalRect, TestSize.Level1)
62 {
63 WSRect rect = { 500, 500, 800, 800 };
64 layoutController_->globalRect_ = rect;
65 EXPECT_EQ(layoutController_->SetSessionGlobalRect(rect), false);
66 EXPECT_EQ(layoutController_->globalRect_, rect);
67
68 layoutController_->globalRect_ = { 500, 500, 1000, 1000 };
69 EXPECT_EQ(layoutController_->SetSessionGlobalRect(rect), true);
70 EXPECT_EQ(layoutController_->globalRect_, rect);
71 }
72
73 /**
74 * @tc.name: GetSessionGlobalRect
75 * @tc.desc: GetSessionGlobalRect
76 * @tc.type: FUNC
77 */
78 HWTEST_F(LayoutControllerTest, GetSessionGlobalRect, TestSize.Level1)
79 {
80 WSRect globalRect = { 500, 500, 800, 800 };
81 WSRect winRect = { 600, 600, 800, 800 };
82 layoutController_->globalRect_ = globalRect;
83 layoutController_->winRect_ = winRect;
84
85 Session::SetScbCoreEnabled(true);
86 EXPECT_EQ(layoutController_->GetSessionGlobalRect(), globalRect);
87
88 Session::SetScbCoreEnabled(false);
89 EXPECT_EQ(layoutController_->GetSessionGlobalRect(), winRect);
90 }
91
92 /**
93 * @tc.name: SetClientRect
94 * @tc.desc: SetClientRect
95 * @tc.type: FUNC
96 */
97 HWTEST_F(LayoutControllerTest, SetClientRect, TestSize.Level1)
98 {
99 WSRect clientRect = { 500, 500, 800, 800 };
100 layoutController_->clientRect_ = clientRect;
101 layoutController_->SetClientRect(clientRect);
102 EXPECT_EQ(layoutController_->GetClientRect(), clientRect);
103
104 WSRect clientRect2 = { 500, 500, 1000, 1000 };
105 layoutController_->SetClientRect(clientRect2);
106 EXPECT_NE(layoutController_->GetClientRect(), clientRect);
107 }
108
109 /**
110 * @tc.name: ConvertRelativeRectToGlobal
111 * @tc.desc: ConvertRelativeRectToGlobal
112 * @tc.type: FUNC
113 */
114 HWTEST_F(LayoutControllerTest, ConvertRelativeRectToGlobal, TestSize.Level1)
115 {
116 SessionInfo info;
117 info.abilityName_ = "ConvertRelativeRectToGlobal";
118 info.bundleName_ = "ConvertRelativeRectToGlobal";
119 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
120
121 DisplayId defaultDisplayId = 0;
122 DisplayId invalidDisplayId = -1ULL;
123 WSRect relativeRect = { 500, 500, 800, 800 };
124 sceneSession->GetSessionProperty()->SetDisplayId(invalidDisplayId);
125 WSRect retRect = sceneSession->GetLayoutController()->ConvertRelativeRectToGlobal(relativeRect, invalidDisplayId);
126 EXPECT_EQ(retRect, relativeRect);
127
128 sceneSession->GetSessionProperty()->SetDisplayId(defaultDisplayId);
129 retRect = sceneSession->GetLayoutController()->ConvertRelativeRectToGlobal(relativeRect, invalidDisplayId);
130 EXPECT_EQ(retRect, relativeRect);
131 retRect = sceneSession->GetLayoutController()->ConvertRelativeRectToGlobal(relativeRect, defaultDisplayId);
132 EXPECT_EQ(retRect, relativeRect);
133 }
134
135 /**
136 * @tc.name: ConvertGlobalRectToRelative
137 * @tc.desc: ConvertGlobalRectToRelative
138 * @tc.type: FUNC
139 */
140 HWTEST_F(LayoutControllerTest, ConvertGlobalRectToRelative, TestSize.Level1)
141 {
142 SessionInfo info;
143 info.abilityName_ = "ConvertGlobalRectToRelative";
144 info.bundleName_ = "ConvertGlobalRectToRelative";
145 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
146
147 DisplayId defaultDisplayId = 0;
148 DisplayId invalidDisplayId = -1ULL;
149 WSRect globalRect = { 500, 500, 800, 800 };
150 sceneSession->GetSessionProperty()->SetDisplayId(invalidDisplayId);
151 WSRect retRect = sceneSession->GetLayoutController()->ConvertGlobalRectToRelative(globalRect, invalidDisplayId);
152 EXPECT_EQ(retRect, globalRect);
153
154 sceneSession->GetSessionProperty()->SetDisplayId(defaultDisplayId);
155 retRect = sceneSession->GetLayoutController()->ConvertGlobalRectToRelative(globalRect, invalidDisplayId);
156 EXPECT_EQ(retRect, globalRect);
157 retRect = sceneSession->GetLayoutController()->ConvertGlobalRectToRelative(globalRect, defaultDisplayId);
158 EXPECT_EQ(retRect, globalRect);
159 }
160
161 /**
162 * @tc.name: AdjustRectByAspectRatio
163 * @tc.desc: AdjustRectByAspectRatio function01
164 * @tc.type: FUNC
165 */
166 HWTEST_F(LayoutControllerTest, AdjustRectByAspectRatio, TestSize.Level1)
167 {
168 SessionInfo info;
169 info.abilityName_ = "AdjustRectByAspectRatio";
170 info.bundleName_ = "AdjustRectByAspectRatio";
171 info.isSystem_ = false;
172 sptr<SceneSession> session = sptr<SceneSession>::MakeSptr(info, nullptr);
__anon988c84c90202null173 session->GetLayoutController()->SetSystemConfigFunc([session] {
174 return session->GetSystemConfig();
175 });
176 WSRect rect;
177 EXPECT_EQ(false, session->GetLayoutController()->AdjustRectByAspectRatio(rect, false));
178 session->property_->SetWindowMode(WindowMode::WINDOW_MODE_UNDEFINED);
179 EXPECT_EQ(false, session->GetLayoutController()->AdjustRectByAspectRatio(rect, false));
180 session->property_->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
181 session->property_->SetWindowType(WindowType::APP_MAIN_WINDOW_END);
182 EXPECT_EQ(false, session->GetLayoutController()->AdjustRectByAspectRatio(rect, false));
183 session->property_->SetWindowType(WindowType::APP_MAIN_WINDOW_BASE);
184 EXPECT_EQ(false, session->GetLayoutController()->AdjustRectByAspectRatio(rect, false));
185 }
186
187 /**
188 * @tc.name: AdjustRectByAspectRatio01
189 * @tc.desc: AdjustRectByAspectRatio function01
190 * @tc.type: FUNC
191 */
192 HWTEST_F(LayoutControllerTest, AdjustRectByAspectRatio01, TestSize.Level1)
193 {
194 SessionInfo info;
195 info.abilityName_ = "AdjustRectByAspectRatio01";
196 info.bundleName_ = "AdjustRectByAspectRatio01";
197 info.isSystem_ = false;
198 sptr<SceneSession> session = sptr<SceneSession>::MakeSptr(info, nullptr);
199 WSRect rect;
200 session->property_->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
201 info.windowType_ = static_cast<uint32_t>(WindowType::APP_MAIN_WINDOW_BASE);
202 session->Session::SetAspectRatio(0.5f);
203 EXPECT_NE(nullptr, DisplayManager::GetInstance().GetDefaultDisplay());
204
205 SystemSessionConfig systemConfig;
206 systemConfig.isSystemDecorEnable_ = true;
207 systemConfig.decorWindowModeSupportType_ = 2;
208 session->SetSystemConfig(systemConfig);
209 EXPECT_EQ(true, session->GetLayoutController()->AdjustRectByAspectRatio(rect, true));
210
211 systemConfig.isSystemDecorEnable_ = false;
212 EXPECT_EQ(true, session->GetLayoutController()->AdjustRectByAspectRatio(rect, false));
213 }
214
215 /**
216 * @tc.name: SetSystemConfigFunc
217 * @tc.desc: SetSystemConfigFunc
218 * @tc.type: FUNC
219 */
220 HWTEST_F(LayoutControllerTest, SetSystemConfigFunc, TestSize.Level1)
221 {
222 WSRect rect = { 500, 500, 800, 800 };
223 layoutController_->SetSessionRect({ 1, 1, 1, 1 });
224 layoutController_->SetSystemConfigFunc(nullptr);
__anon988c84c90302null225 layoutController_->SetSystemConfigFunc([layoutController = this->layoutController_, rect] {
226 layoutController->SetSessionRect(rect);
227 SystemSessionConfig systemConfig;
228 return systemConfig;
229 });
230 layoutController_->getSystemConfigFunc_();
231 EXPECT_EQ(layoutController_->GetSessionRect(), rect);
232 }
233 } // namespace
234 } // namespace Rosen
235 } // namespace OHOS
236