/* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include "mock/mock_session_stage.h" #include "mock/mock_window_event_channel.h" #include "mock/mock_pattern_detach_callback.h" #include "session/host/include/extension_session.h" #include "session/host/include/move_drag_controller.h" #include "session/host/include/scene_session.h" #include "session_manager/include/scene_session_manager.h" #include "session/host/include/session.h" #include "session_info.h" #include "key_event.h" #include "wm_common.h" #include "window_manager_hilog.h" using namespace testing; using namespace testing::ext; namespace OHOS { namespace Rosen { namespace { const std::string UNDEFINED = "undefined"; } class SessionLayoutTest : public testing::Test { public: static void SetUpTestCase(); static void TearDownTestCase(); void SetUp() override; void TearDown() override; int32_t GetTaskCount(); sptr ssm_; private: RSSurfaceNode::SharedPtr CreateRSSurfaceNode(); sptr session_ = nullptr; static constexpr uint32_t WAIT_SYNC_IN_NS = 500000; class TLifecycleListener : public ILifecycleListener { public: virtual ~TLifecycleListener() {} void OnActivation() override {} void OnConnect() override {} void OnForeground() override {} void OnBackground() override {} void OnDisconnect() override {} void OnExtensionDied() override {} void OnExtensionTimeout(int32_t errorCode) override {} void OnAccessibilityEvent(const Accessibility::AccessibilityEventInfo& info, int64_t uiExtensionIdLevel) override {} void OnDrawingCompleted() override {} void OnAppRemoveStartingWindow() override {} }; std::shared_ptr lifecycleListener_ = std::make_shared(); sptr mockSessionStage_ = nullptr; sptr mockEventChannel_ = nullptr; }; void SessionLayoutTest::SetUpTestCase() { } void SessionLayoutTest::TearDownTestCase() { } void SessionLayoutTest::SetUp() { SessionInfo info; info.abilityName_ = "testSession1"; info.moduleName_ = "testSession2"; info.bundleName_ = "testSession3"; session_ = sptr::MakeSptr(info); session_->surfaceNode_ = CreateRSSurfaceNode(); ssm_ = sptr::MakeSptr(); session_->SetEventHandler(ssm_->taskScheduler_->GetEventHandler(), ssm_->eventHandler_); auto isScreenLockedCallback = [this]() { return ssm_->IsScreenLocked(); }; session_->RegisterIsScreenLockedCallback(isScreenLockedCallback); mockSessionStage_ = sptr::MakeSptr(); mockEventChannel_ = sptr::MakeSptr(mockSessionStage_); } void SessionLayoutTest::TearDown() { session_ = nullptr; usleep(WAIT_SYNC_IN_NS); } RSSurfaceNode::SharedPtr SessionLayoutTest::CreateRSSurfaceNode() { struct RSSurfaceNodeConfig rsSurfaceNodeConfig; rsSurfaceNodeConfig.SurfaceNodeName = "WindowSessionTestSurfaceNode"; auto surfaceNode = RSSurfaceNode::Create(rsSurfaceNodeConfig); if (surfaceNode == nullptr) { GTEST_LOG_(INFO) << "SessionLayoutTest::CreateRSSurfaceNode surfaceNode is nullptr"; } return surfaceNode; } int32_t SessionLayoutTest::GetTaskCount() { std::string dumpInfo = session_->handler_->GetEventRunner()->GetEventQueue()->DumpCurrentQueueSize(); std::regex pattern("\\d+"); std::smatch matches; int32_t taskNum = 0; while (std::regex_search(dumpInfo, matches, pattern)) { taskNum += std::stoi(matches.str()); dumpInfo = matches.suffix(); } return taskNum; } namespace { /** * @tc.name: UpdateRect01 * @tc.desc: update rect * @tc.type: FUNC * @tc.require: #I6JLSI */ HWTEST_F(SessionLayoutTest, UpdateRect01, Function | SmallTest | Level2) { bool preBackgroundUpdateRectNotifyEnabled = Session::IsBackgroundUpdateRectNotifyEnabled(); Session::SetBackgroundUpdateRectNotifyEnabled(true); sptr mockSessionStage = sptr::MakeSptr(); session_->sessionStage_ = mockSessionStage; EXPECT_CALL(*(mockSessionStage), UpdateRect(_, _, _, _)).Times(AtLeast(1)).WillOnce(Return(WSError::WS_OK)); WSRect rect = {0, 0, 0, 0}; ASSERT_EQ(WSError::WS_ERROR_INVALID_SESSION, session_->UpdateRect(rect, SizeChangeReason::UNDEFINED, "SessionLayoutTest")); sptr mockEventChannel = sptr::MakeSptr(mockSessionStage); SystemSessionConfig sessionConfig; sptr property = sptr::MakeSptr(); ASSERT_EQ(WSError::WS_OK, session_->Connect(mockSessionStage, mockEventChannel, nullptr, sessionConfig, property)); rect = {0, 0, 100, 100}; ASSERT_EQ(WSError::WS_ERROR_INVALID_SESSION, session_->UpdateRect(rect, SizeChangeReason::UNDEFINED, "SessionLayoutTest")); ASSERT_EQ(rect, session_->winRect_); rect = {0, 0, 200, 200}; session_->UpdateSessionState(SessionState::STATE_ACTIVE); ASSERT_EQ(WSError::WS_OK, session_->UpdateRect(rect, SizeChangeReason::UNDEFINED, "SessionLayoutTest")); ASSERT_EQ(rect, session_->winRect_); rect = {0, 0, 300, 300}; session_->sessionStage_ = nullptr; ASSERT_EQ(WSError::WS_OK, session_->UpdateRect(rect, SizeChangeReason::UNDEFINED, "SessionLayoutTest")); ASSERT_EQ(rect, session_->winRect_); Session::SetBackgroundUpdateRectNotifyEnabled(preBackgroundUpdateRectNotifyEnabled); } /** * @tc.name: UpdateRect_TestForeground * @tc.desc: update rect * @tc.type: FUNC * @tc.require: #I6JLSI */ HWTEST_F(SessionLayoutTest, UpdateRect_TestForeground, Function | SmallTest | Level2) { bool preBackgroundUpdateRectNotifyEnabled = Session::IsBackgroundUpdateRectNotifyEnabled(); Session::SetBackgroundUpdateRectNotifyEnabled(false); sptr mockSessionStage = sptr::MakeSptr(); session_->sessionStage_ = mockSessionStage; WSRect rect = { 0, 0, 100, 100 }; session_->UpdateSessionState(SessionState::STATE_ACTIVE); ASSERT_EQ(WSError::WS_OK, session_->UpdateRect(rect, SizeChangeReason::UNDEFINED, "SessionLayoutTest")); session_->UpdateSessionState(SessionState::STATE_BACKGROUND); ASSERT_EQ(WSError::WS_DO_NOTHING, session_->UpdateRect(rect, SizeChangeReason::UNDEFINED, "SessionLayoutTest")); Session::SetBackgroundUpdateRectNotifyEnabled(preBackgroundUpdateRectNotifyEnabled); } /** * @tc.name: UpdateSessionRect01 * @tc.desc: UpdateSessionRect * @tc.type: FUNC */ HWTEST_F(SessionLayoutTest, UpdateSessionRect01, Function | SmallTest | Level2) { SessionInfo info; info.abilityName_ = "testSession1"; info.bundleName_ = "testSession3"; sptr sceneSession = sptr::MakeSptr(info, nullptr); WSRect rect = {0, 0, 320, 240}; // width: 320, height: 240 auto result = sceneSession->UpdateSessionRect(rect, SizeChangeReason::RESIZE); ASSERT_EQ(result, WSError::WS_OK); result = sceneSession->UpdateSessionRect(rect, SizeChangeReason::RESIZE); ASSERT_EQ(result, WSError::WS_OK); } /** * @tc.name: SetSingleHandTransform * @tc.desc: SetSingleHandTransform * @tc.type: FUNC */ HWTEST_F(SessionLayoutTest, SetSingleHandTransform, Function | SmallTest | Level2) { SessionInfo info; info.abilityName_ = "SetSingleHandTransform"; info.bundleName_ = "SetSingleHandTransform"; sptr session = sptr::MakeSptr(info); SingleHandTransform transform; session->SetSingleHandTransform(transform); ASSERT_EQ(transform, session->GetSingleHandTransform()); } } } // namespace Rosen } // namespace OHOS