diff --git a/bundle.json b/bundle.json index c2157db404..b97fdc0f61 100755 --- a/bundle.json +++ b/bundle.json @@ -152,7 +152,8 @@ "//foundation/window/window_manager/wm:test", "//foundation/window/window_manager/wmserver:test", "//foundation/window/window_manager/test:test", - "//foundation/window/window_manager/extension/window_extension:test" + "//foundation/window/window_manager/extension/window_extension:test", + "//foundation/window/window_manager/window_scene:test" ] } } diff --git a/window_scene/BUILD.gn b/window_scene/BUILD.gn index 88dfdd594c..01fb8ff581 100644 --- a/window_scene/BUILD.gn +++ b/window_scene/BUILD.gn @@ -19,3 +19,8 @@ group("window_scene") { "session_manager:scene_session_manager", ] } + +group("test") { + testonly = true + deps = [ "test/unittest:unittest" ] +} diff --git a/window_scene/session/host/src/session.cpp b/window_scene/session/host/src/session.cpp index 1993311843..5e4b9a68ec 100644 --- a/window_scene/session/host/src/session.cpp +++ b/window_scene/session/host/src/session.cpp @@ -179,14 +179,15 @@ WSError Session::SetActive(bool active) WLOGFD("Session active do not change: [%{public}d]", active); return WSError::WS_DO_NOTHING; } - isActive_ = active; if (active && GetSessionState() == SessionState::STATE_FOREGROUND) { sessionStage_->SetActive(true); UpdateSessionState(SessionState::STATE_ACTIVE); + isActive_ = active; } if (!active && GetSessionState() == SessionState::STATE_ACTIVE) { sessionStage_->SetActive(false); UpdateSessionState(SessionState::STATE_INACTIVE); + isActive_ = active; } return WSError::WS_OK; } diff --git a/window_scene/test/mock/mock_session_stage.h b/window_scene/test/mock/mock_session_stage.h new file mode 100644 index 0000000000..29c0d2f151 --- /dev/null +++ b/window_scene/test/mock/mock_session_stage.h @@ -0,0 +1,40 @@ +/* + * 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 "interfaces/include/ws_common.h" +#include "session/container/include/session_stage.h" +#include "session/host/include/session.h" +#include + +namespace OHOS { +namespace Rosen { +class SessionStageMocker : public SessionStage { +public: + SessionStageMocker(const sptr& session) : SessionStage(session) {}; + ~SessionStageMocker() {}; + MOCK_METHOD0(Connect, WSError()); + MOCK_METHOD0(Foreground, WSError(void)); + MOCK_METHOD0(Background, WSError(void)); + MOCK_METHOD0(Disconnect, WSError(void)); + MOCK_METHOD1(PendingSessionActivation, WSError(const SessionInfo& info)); + + MOCK_METHOD0(Recover, WSError(void)); + MOCK_METHOD0(Maximum, WSError(void)); + + MOCK_METHOD1(SetActive, WSError(bool active)); + MOCK_METHOD2(UpdateRect, WSError(const WSRect& rect, SizeChangeReason reason)); +}; +} // namespace Rosen +} // namespace OHOS diff --git a/window_scene/test/mock/mock_window_event_channel.h b/window_scene/test/mock/mock_window_event_channel.h new file mode 100644 index 0000000000..1fd6785a90 --- /dev/null +++ b/window_scene/test/mock/mock_window_event_channel.h @@ -0,0 +1,31 @@ +/* + * 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 "interfaces/include/ws_common.h" +#include "session/container/include/session_stage.h" +#include "session/container/include/window_event_channel.h" +#include + +namespace OHOS { +namespace Rosen { +class WindowEventChannelMocker : public WindowEventChannel { +public: + WindowEventChannelMocker(sptr sessionStage) : WindowEventChannel(sessionStage) {}; + ~WindowEventChannelMocker() {}; + MOCK_METHOD1(TransferKeyEvent, WSError(const std::shared_ptr& keyEvent)); + MOCK_METHOD1(TransferPointerEvent, WSError(const std::shared_ptr& pointerEvent)); +}; +} // namespace Rosen +} // namespace OHOS diff --git a/window_scene/test/unittest/BUILD.gn b/window_scene/test/unittest/BUILD.gn new file mode 100644 index 0000000000..2f2f310045 --- /dev/null +++ b/window_scene/test/unittest/BUILD.gn @@ -0,0 +1,68 @@ +# 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. + +import("//build/test.gni") +import("//foundation/window/window_manager/windowmanager_aafwk.gni") +module_out_path = "window_manager/window_scene" + +group("unittest") { + testonly = true + + deps = [ ":ws_session_test" ] +} + +ohos_unittest("ws_session_test") { + module_out_path = module_out_path + + sources = [ "session_test.cpp" ] + + deps = [ ":ws_unittest_common" ] +} + +## Build ws_unittest_common.a {{{ +config("ws_unittest_common_public_config") { + include_dirs = [ + "${window_base_path}/window_scene", + "${window_base_path}/window_scene/test", + "//third_party/googletest/googlemock/include", + ] +} + +ohos_static_library("ws_unittest_common") { + visibility = [ ":*" ] + testonly = true + + public_configs = [ + ":ws_unittest_common_public_config", + "${window_base_path}/resources/config/build:coverage_flags", + "${window_base_path}/resources/config/build:testcase_flags", + ] + + deps = [ + "${window_base_path}/window_scene/session:scene_session", + "${window_base_path}/window_scene/session_manager:scene_session_manager", + "//third_party/googletest:gmock", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:configuration", + "ability_base:want", + "c_utils:utils", + "ipc:ipc_core", + ] + + part_name = "window_manager" + subsystem_name = "window" +} +## Build ws_unittest_common.a }}} diff --git a/window_scene/test/unittest/session_test.cpp b/window_scene/test/unittest/session_test.cpp new file mode 100644 index 0000000000..c7df54314d --- /dev/null +++ b/window_scene/test/unittest/session_test.cpp @@ -0,0 +1,114 @@ +/* + * 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 "session/host/include/session.h" +#include "session/host/include/extension_session.h" +#include "mock/mock_session_stage.h" +#include "mock/mock_window_event_channel.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace Rosen { +class WindowSessionTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() override; + void TearDown() override; +private: + sptr session_ = nullptr; +}; + +void WindowSessionTest::SetUpTestCase() +{ +} + +void WindowSessionTest::TearDownTestCase() +{ +} + +void WindowSessionTest::SetUp() +{ + SessionInfo info; + info.abilityName_ = "testSession1"; + info.bundleName_ = "testSession2"; + session_ = new (std::nothrow) Session(info); + EXPECT_NE(nullptr, session_); +} + +void WindowSessionTest::TearDown() +{ + session_ = nullptr; +} + +namespace { +/** + * @tc.name: SetActive01 + * @tc.desc: set session active + * @tc.type: FUNC + * @tc.require: #I6JLSI + */ +HWTEST_F(WindowSessionTest, SetActive01, Function | SmallTest | Level2) +{ + sptr sessionToken = nullptr; + sptr mockSessionStage = new(std::nothrow) SessionStageMocker(sessionToken); + EXPECT_NE(nullptr, mockSessionStage); + EXPECT_CALL(*(mockSessionStage), SetActive(_)).WillOnce(Return(WSError::WS_OK)); + EXPECT_CALL(*(mockSessionStage), UpdateRect(_, _)).Times(1).WillOnce(Return(WSError::WS_OK)); + session_->sessionStage_ = mockSessionStage; + ASSERT_EQ(WSError::WS_ERROR_INVALID_SESSION, session_->SetActive(true)); + + sptr mockEventChannel = new(std::nothrow) WindowEventChannelMocker(mockSessionStage); + EXPECT_NE(nullptr, mockEventChannel); + ASSERT_EQ(WSError::WS_OK, session_->Connect(mockSessionStage, mockEventChannel)); + ASSERT_EQ(WSError::WS_OK, session_->SetActive(true)); + ASSERT_EQ(false, session_->isActive_); + + session_->UpdateSessionState(SessionState::STATE_FOREGROUND); + ASSERT_EQ(WSError::WS_OK, session_->SetActive(true)); + ASSERT_EQ(true, session_->isActive_); +} + +/** + * @tc.name: UpdateRect01 + * @tc.desc: update rect + * @tc.type: FUNC + * @tc.require: #I6JLSI + */ +HWTEST_F(WindowSessionTest, UpdateRect01, Function | SmallTest | Level2) +{ + sptr sessionToken = nullptr; + sptr mockSessionStage = new(std::nothrow) SessionStageMocker(sessionToken); + EXPECT_NE(nullptr, mockSessionStage); + session_->sessionStage_ = mockSessionStage; + EXPECT_CALL(*(mockSessionStage), UpdateRect(_, _)).Times(1).WillOnce(Return(WSError::WS_OK)); + + WSRect rect = {0, 0, 0, 0}; + ASSERT_EQ(WSError::WS_ERROR_INVALID_SESSION, session_->UpdateRect(rect, SizeChangeReason::SHOW)); + sptr mockEventChannel = new(std::nothrow) WindowEventChannelMocker(mockSessionStage); + EXPECT_NE(nullptr, mockEventChannel); + ASSERT_EQ(WSError::WS_OK, session_->Connect(mockSessionStage, mockEventChannel)); + + rect = {0, 0, 100, 100}; + EXPECT_CALL(*(mockSessionStage), UpdateRect(_, _)).Times(1).WillOnce(Return(WSError::WS_OK)); + ASSERT_EQ(WSError::WS_OK, session_->UpdateRect(rect, SizeChangeReason::SHOW)); + ASSERT_EQ(rect, session_->winRect_); +} +} +} // namespace Rosen +} // namespace OHOS