/* * Copyright (c) 2023 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 "ability_context_impl.h" #include "mock_session.h" #include "window_session_impl.h" #include "mock_uicontent.h" using namespace testing; using namespace testing::ext; namespace OHOS { namespace Rosen { class MockWindowChangeListener : public IWindowChangeListener { public: MOCK_METHOD3(OnSizeChange, void(Rect rect, WindowSizeChangeReason reason, const std::shared_ptr& rsTransaction)); }; class MockWindowLifeCycleListener : public IWindowLifeCycle { public: MOCK_METHOD0(AfterForeground, void(void)); MOCK_METHOD0(AfterBackground, void(void)); MOCK_METHOD0(AfterFocused, void(void)); MOCK_METHOD0(AfterUnfocused, void(void)); MOCK_METHOD1(ForegroundFailed, void(int32_t)); MOCK_METHOD0(AfterActive, void(void)); MOCK_METHOD0(AfterInactive, void(void)); }; class WindowSessionImplTest : public testing::Test { public: static void SetUpTestCase(); static void TearDownTestCase(); void SetUp() override; void TearDown() override; std::shared_ptr abilityContext_; }; void WindowSessionImplTest::SetUpTestCase() { } void WindowSessionImplTest::TearDownTestCase() { } void WindowSessionImplTest::SetUp() { abilityContext_ = std::make_shared(); } void WindowSessionImplTest::TearDown() { abilityContext_ = nullptr; } namespace { /** * @tc.name: CreateWindowAndDestroy01 * @tc.desc: Create window and destroy window * @tc.type: FUNC */ HWTEST_F(WindowSessionImplTest, CreateWindowAndDestroy01, Function | SmallTest | Level2) { sptr option = new WindowOption(); option->SetWindowName("CreateWindow01"); sptr window = new WindowSessionImpl(option); SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" }; sptr session = new(std::nothrow) SessionMocker(sessionInfo); ASSERT_NE(nullptr, session); ASSERT_EQ(WMError::WM_OK, window->Create(nullptr, session)); ASSERT_EQ(WMError::WM_OK, window->Create(abilityContext_, session)); ASSERT_EQ(WMError::WM_OK, window->Create(abilityContext_, session)); window->property_->SetPersistentId(1); ASSERT_EQ(WMError::WM_ERROR_INVALID_WINDOW, window->Destroy()); // session is null window = new WindowSessionImpl(option); ASSERT_EQ(WMError::WM_OK, window->Create(abilityContext_, nullptr)); ASSERT_EQ(WMError::WM_ERROR_INVALID_WINDOW, window->Destroy()); } /** * @tc.name: Connect01 * @tc.desc: Connect session * @tc.type: FUNC */ HWTEST_F(WindowSessionImplTest, Connect01, Function | SmallTest | Level2) { sptr option = new WindowOption(); option->SetWindowName("Connect01"); sptr window = new(std::nothrow) WindowSessionImpl(option); ASSERT_NE(nullptr, window); window->property_->SetPersistentId(1); // connect with null session ASSERT_EQ(WMError::WM_ERROR_NULLPTR, window->Connect()); SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" }; sptr session = new(std::nothrow) SessionMocker(sessionInfo); ASSERT_NE(nullptr, session); window->hostSession_ = session; EXPECT_CALL(*(session), Connect(_, _, _, _, _, _)).WillOnce(Return(WSError::WS_ERROR_NULLPTR)); ASSERT_EQ(WMError::WM_ERROR_NULLPTR, window->Connect()); EXPECT_CALL(*(session), Connect(_, _, _, _, _, _)).WillOnce(Return(WSError::WS_OK)); ASSERT_EQ(WMError::WM_OK, window->Connect()); ASSERT_EQ(WMError::WM_OK, window->Destroy()); } /** * @tc.name: Show01 * @tc.desc: Show session * @tc.type: FUNC */ HWTEST_F(WindowSessionImplTest, Show01, Function | SmallTest | Level2) { sptr option = new WindowOption(); option->SetWindowName("Show01"); sptr window = new(std::nothrow) WindowSessionImpl(option); ASSERT_NE(nullptr, window); window->property_->SetPersistentId(1); // show with null session ASSERT_EQ(WMError::WM_ERROR_INVALID_WINDOW, window->Show()); SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" }; sptr session = new(std::nothrow) SessionMocker(sessionInfo); ASSERT_NE(nullptr, session); window->hostSession_ = session; EXPECT_CALL(*(session), Foreground(_)).WillOnce(Return(WSError::WS_OK)); ASSERT_EQ(WMError::WM_OK, window->Show()); ASSERT_EQ(WMError::WM_OK, window->Show()); window->state_ = WindowState::STATE_CREATED; EXPECT_CALL(*(session), Foreground(_)).WillOnce(Return(WSError::WS_ERROR_INVALID_SESSION)); ASSERT_EQ(WMError::WM_ERROR_INVALID_SESSION, window->Show()); ASSERT_EQ(WMError::WM_OK, window->Destroy()); } /** * @tc.name: Hide01 * @tc.desc: Hide session * @tc.type: FUNC */ HWTEST_F(WindowSessionImplTest, Hide01, Function | SmallTest | Level2) { sptr option = new WindowOption(); option->SetWindowName("Hide01"); sptr window = new(std::nothrow) WindowSessionImpl(option); ASSERT_NE(nullptr, window); window->property_->SetPersistentId(1); // show with null session ASSERT_EQ(WMError::WM_ERROR_INVALID_WINDOW, window->Hide()); SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" }; sptr session = new(std::nothrow) SessionMocker(sessionInfo); ASSERT_NE(nullptr, session); window->hostSession_ = session; ASSERT_EQ(WMError::WM_OK, window->Hide()); ASSERT_EQ(WMError::WM_OK, window->Hide()); window->state_ = WindowState::STATE_CREATED; ASSERT_EQ(WMError::WM_OK, window->Hide()); window->state_ = WindowState::STATE_SHOWN; window->property_->type_ = WindowType::WINDOW_TYPE_FLOAT; ASSERT_EQ(WMError::WM_OK, window->Hide()); window->property_->type_ = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW; ASSERT_EQ(WMError::WM_OK, window->Destroy()); } /** * @tc.name: SetResizeByDragEnabled01 * @tc.desc: SetResizeByDragEnabled and check the retCode * @tc.type: FUNC */ HWTEST_F(WindowSessionImplTest, SetResizeByDragEnabled01, Function | SmallTest | Level2) { sptr option = new WindowOption(); option->SetWindowName("SetResizeByDragEnabled01"); sptr window = new(std::nothrow) WindowSessionImpl(option); ASSERT_NE(nullptr, window); WMError retCode = window->SetResizeByDragEnabled(true); ASSERT_EQ(retCode, WMError::WM_ERROR_INVALID_WINDOW); window->property_->SetPersistentId(1); SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" }; sptr session = new(std::nothrow) SessionMocker(sessionInfo); ASSERT_NE(nullptr, session); window->hostSession_ = session; window->state_ = WindowState::STATE_CREATED; retCode = window->SetResizeByDragEnabled(true); ASSERT_EQ(retCode, WMError::WM_DO_NOTHING); } /** * @tc.name: SetRaiseByClickEnabled01 * @tc.desc: SetRaiseByClickEnabled and check the retCode * @tc.type: FUNC */ HWTEST_F(WindowSessionImplTest, SetRaiseByClickEnabled01, Function | SmallTest | Level2) { sptr option = new WindowOption(); option->SetWindowName("SetRaiseByClickEnabled01"); sptr window = new(std::nothrow) WindowSessionImpl(option); ASSERT_NE(nullptr, window); WMError retCode = window->SetRaiseByClickEnabled(true); ASSERT_EQ(retCode, WMError::WM_ERROR_INVALID_WINDOW); window->property_->SetPersistentId(1); SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" }; sptr session = new(std::nothrow) SessionMocker(sessionInfo); ASSERT_NE(nullptr, session); window->hostSession_ = session; window->state_ = WindowState::STATE_CREATED; retCode = window->SetRaiseByClickEnabled(true); ASSERT_EQ(retCode, WMError::WM_DO_NOTHING); } } } // namespace Rosen } // namespace OHOS