1 /*
2 * Copyright (c) 2024 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 <regex>
18 #include <pointer_event.h>
19 #include <ui/rs_surface_node.h>
20
21 #include "mock/mock_session_stage.h"
22 #include "mock/mock_window_event_channel.h"
23 #include "mock/mock_pattern_detach_callback.h"
24 #include "session/host/include/extension_session.h"
25 #include "session/host/include/move_drag_controller.h"
26 #include "session/host/include/scene_session.h"
27 #include "session_manager/include/scene_session_manager.h"
28 #include "session/host/include/session.h"
29 #include "session_info.h"
30 #include "key_event.h"
31 #include "wm_common.h"
32 #include "window_manager_hilog.h"
33
34 using namespace testing;
35 using namespace testing::ext;
36
37 namespace OHOS {
38 namespace Rosen {
39 namespace {
40 const std::string UNDEFINED = "undefined";
41 }
42
43 class SessionLayoutTest : public testing::Test {
44 public:
45 static void SetUpTestCase();
46 static void TearDownTestCase();
47 void SetUp() override;
48 void TearDown() override;
49 int32_t GetTaskCount();
50 sptr<SceneSessionManager> ssm_;
51
52 private:
53 RSSurfaceNode::SharedPtr CreateRSSurfaceNode();
54 sptr<Session> session_ = nullptr;
55 static constexpr uint32_t WAIT_SYNC_IN_NS = 500000;
56
57 class TLifecycleListener : public ILifecycleListener {
58 public:
~TLifecycleListener()59 virtual ~TLifecycleListener() {}
OnActivation()60 void OnActivation() override {}
OnConnect()61 void OnConnect() override {}
OnForeground()62 void OnForeground() override {}
OnBackground()63 void OnBackground() override {}
OnDisconnect()64 void OnDisconnect() override {}
OnExtensionDied()65 void OnExtensionDied() override {}
OnExtensionTimeout(int32_t errorCode)66 void OnExtensionTimeout(int32_t errorCode) override {}
OnAccessibilityEvent(const Accessibility::AccessibilityEventInfo & info,int64_t uiExtensionIdLevel)67 void OnAccessibilityEvent(const Accessibility::AccessibilityEventInfo& info,
68 int64_t uiExtensionIdLevel) override {}
OnDrawingCompleted()69 void OnDrawingCompleted() override {}
OnAppRemoveStartingWindow()70 void OnAppRemoveStartingWindow() override {}
71 };
72 std::shared_ptr<TLifecycleListener> lifecycleListener_ = std::make_shared<TLifecycleListener>();
73
74 sptr<SessionStageMocker> mockSessionStage_ = nullptr;
75 sptr<WindowEventChannelMocker> mockEventChannel_ = nullptr;
76 };
77
SetUpTestCase()78 void SessionLayoutTest::SetUpTestCase()
79 {
80 }
81
TearDownTestCase()82 void SessionLayoutTest::TearDownTestCase()
83 {
84 }
85
SetUp()86 void SessionLayoutTest::SetUp()
87 {
88 SessionInfo info;
89 info.abilityName_ = "testSession1";
90 info.moduleName_ = "testSession2";
91 info.bundleName_ = "testSession3";
92 session_ = sptr<Session>::MakeSptr(info);
93 session_->surfaceNode_ = CreateRSSurfaceNode();
94 ssm_ = sptr<SceneSessionManager>::MakeSptr();
95 session_->SetEventHandler(ssm_->taskScheduler_->GetEventHandler(), ssm_->eventHandler_);
96 auto isScreenLockedCallback = [this]() {
97 return ssm_->IsScreenLocked();
98 };
99 session_->RegisterIsScreenLockedCallback(isScreenLockedCallback);
100 mockSessionStage_ = sptr<SessionStageMocker>::MakeSptr();
101 mockEventChannel_ = sptr<WindowEventChannelMocker>::MakeSptr(mockSessionStage_);
102 }
103
TearDown()104 void SessionLayoutTest::TearDown()
105 {
106 session_ = nullptr;
107 usleep(WAIT_SYNC_IN_NS);
108 }
109
CreateRSSurfaceNode()110 RSSurfaceNode::SharedPtr SessionLayoutTest::CreateRSSurfaceNode()
111 {
112 struct RSSurfaceNodeConfig rsSurfaceNodeConfig;
113 rsSurfaceNodeConfig.SurfaceNodeName = "WindowSessionTestSurfaceNode";
114 auto surfaceNode = RSSurfaceNode::Create(rsSurfaceNodeConfig);
115 if (surfaceNode == nullptr) {
116 GTEST_LOG_(INFO) << "SessionLayoutTest::CreateRSSurfaceNode surfaceNode is nullptr";
117 }
118 return surfaceNode;
119 }
120
GetTaskCount()121 int32_t SessionLayoutTest::GetTaskCount()
122 {
123 std::string dumpInfo = session_->handler_->GetEventRunner()->GetEventQueue()->DumpCurrentQueueSize();
124 std::regex pattern("\\d+");
125 std::smatch matches;
126 int32_t taskNum = 0;
127 while (std::regex_search(dumpInfo, matches, pattern)) {
128 taskNum += std::stoi(matches.str());
129 dumpInfo = matches.suffix();
130 }
131 return taskNum;
132 }
133
134 namespace {
135 /**
136 * @tc.name: UpdateRect01
137 * @tc.desc: update rect
138 * @tc.type: FUNC
139 * @tc.require: #I6JLSI
140 */
141 HWTEST_F(SessionLayoutTest, UpdateRect01, Function | SmallTest | Level2)
142 {
143 bool preBackgroundUpdateRectNotifyEnabled = Session::IsBackgroundUpdateRectNotifyEnabled();
144 Session::SetBackgroundUpdateRectNotifyEnabled(true);
145 sptr<SessionStageMocker> mockSessionStage = sptr<SessionStageMocker>::MakeSptr();
146 session_->sessionStage_ = mockSessionStage;
147 EXPECT_CALL(*(mockSessionStage), UpdateRect(_, _, _, _)).Times(AtLeast(1)).WillOnce(Return(WSError::WS_OK));
148
149 WSRect rect = {0, 0, 0, 0};
150 ASSERT_EQ(WSError::WS_ERROR_INVALID_SESSION, session_->UpdateRect(rect,
151 SizeChangeReason::UNDEFINED, "SessionLayoutTest"));
152 sptr<WindowEventChannelMocker> mockEventChannel = sptr<WindowEventChannelMocker>::MakeSptr(mockSessionStage);
153 SystemSessionConfig sessionConfig;
154 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
155 ASSERT_EQ(WSError::WS_OK, session_->Connect(mockSessionStage,
156 mockEventChannel, nullptr, sessionConfig, property));
157
158 rect = {0, 0, 100, 100};
159 ASSERT_EQ(WSError::WS_ERROR_INVALID_SESSION, session_->UpdateRect(rect,
160 SizeChangeReason::UNDEFINED, "SessionLayoutTest"));
161 ASSERT_EQ(rect, session_->winRect_);
162
163 rect = {0, 0, 200, 200};
164 session_->UpdateSessionState(SessionState::STATE_ACTIVE);
165 ASSERT_EQ(WSError::WS_OK, session_->UpdateRect(rect, SizeChangeReason::UNDEFINED, "SessionLayoutTest"));
166 ASSERT_EQ(rect, session_->winRect_);
167
168 rect = {0, 0, 300, 300};
169 session_->sessionStage_ = nullptr;
170 ASSERT_EQ(WSError::WS_OK, session_->UpdateRect(rect, SizeChangeReason::UNDEFINED, "SessionLayoutTest"));
171 ASSERT_EQ(rect, session_->winRect_);
172 Session::SetBackgroundUpdateRectNotifyEnabled(preBackgroundUpdateRectNotifyEnabled);
173 }
174
175 /**
176 * @tc.name: UpdateRect_TestForeground
177 * @tc.desc: update rect
178 * @tc.type: FUNC
179 * @tc.require: #I6JLSI
180 */
181 HWTEST_F(SessionLayoutTest, UpdateRect_TestForeground, Function | SmallTest | Level2)
182 {
183 bool preBackgroundUpdateRectNotifyEnabled = Session::IsBackgroundUpdateRectNotifyEnabled();
184 Session::SetBackgroundUpdateRectNotifyEnabled(false);
185 sptr<SessionStageMocker> mockSessionStage = sptr<SessionStageMocker>::MakeSptr();
186 session_->sessionStage_ = mockSessionStage;
187
188 WSRect rect = { 0, 0, 100, 100 };
189 session_->UpdateSessionState(SessionState::STATE_ACTIVE);
190 ASSERT_EQ(WSError::WS_OK, session_->UpdateRect(rect, SizeChangeReason::UNDEFINED, "SessionLayoutTest"));
191 session_->UpdateSessionState(SessionState::STATE_BACKGROUND);
192 ASSERT_EQ(WSError::WS_DO_NOTHING, session_->UpdateRect(rect, SizeChangeReason::UNDEFINED, "SessionLayoutTest"));
193 Session::SetBackgroundUpdateRectNotifyEnabled(preBackgroundUpdateRectNotifyEnabled);
194 }
195
196 /**
197 * @tc.name: UpdateSessionRect01
198 * @tc.desc: UpdateSessionRect
199 * @tc.type: FUNC
200 */
201 HWTEST_F(SessionLayoutTest, UpdateSessionRect01, Function | SmallTest | Level2)
202 {
203 SessionInfo info;
204 info.abilityName_ = "testSession1";
205 info.bundleName_ = "testSession3";
206 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
207 WSRect rect = {0, 0, 320, 240}; // width: 320, height: 240
208 auto result = sceneSession->UpdateSessionRect(rect, SizeChangeReason::RESIZE);
209 ASSERT_EQ(result, WSError::WS_OK);
210
211 result = sceneSession->UpdateSessionRect(rect, SizeChangeReason::RESIZE);
212 ASSERT_EQ(result, WSError::WS_OK);
213 }
214
215 /**
216 * @tc.name: SetSingleHandTransform
217 * @tc.desc: SetSingleHandTransform
218 * @tc.type: FUNC
219 */
220 HWTEST_F(SessionLayoutTest, SetSingleHandTransform, Function | SmallTest | Level2)
221 {
222 SessionInfo info;
223 info.abilityName_ = "SetSingleHandTransform";
224 info.bundleName_ = "SetSingleHandTransform";
225 sptr<Session> session = sptr<Session>::MakeSptr(info);
226 SingleHandTransform transform;
227 session->SetSingleHandTransform(transform);
228 ASSERT_EQ(transform, session->GetSingleHandTransform());
229 }
230 }
231 } // namespace Rosen
232 } // namespace OHOS
233