add ut&st for session

Signed-off-by: chyyy0213 <chenhaiying3@huawei.com>
Change-Id: I9685cae44702780491fc0a10ca05e5436695a87d
Signed-off-by: chyyy0213 <chenhaiying3@huawei.com>
This commit is contained in:
chyyy0213 2023-03-02 16:51:27 +08:00
parent bdc5f377ef
commit 9fc821a5e5
7 changed files with 262 additions and 2 deletions

View File

@ -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"
]
}
}

View File

@ -19,3 +19,8 @@ group("window_scene") {
"session_manager:scene_session_manager",
]
}
group("test") {
testonly = true
deps = [ "test/unittest:unittest" ]
}

View File

@ -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;
}

View File

@ -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 <gmock/gmock.h>
namespace OHOS {
namespace Rosen {
class SessionStageMocker : public SessionStage {
public:
SessionStageMocker(const sptr<ISession>& 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

View File

@ -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 <gmock/gmock.h>
namespace OHOS {
namespace Rosen {
class WindowEventChannelMocker : public WindowEventChannel {
public:
WindowEventChannelMocker(sptr<SessionStage> sessionStage) : WindowEventChannel(sessionStage) {};
~WindowEventChannelMocker() {};
MOCK_METHOD1(TransferKeyEvent, WSError(const std::shared_ptr<MMI::KeyEvent>& keyEvent));
MOCK_METHOD1(TransferPointerEvent, WSError(const std::shared_ptr<MMI::PointerEvent>& pointerEvent));
};
} // namespace Rosen
} // namespace OHOS

View File

@ -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 }}}

View File

@ -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 <gtest/gtest.h>
#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> 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<ISession> sessionToken = nullptr;
sptr<SessionStageMocker> 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<WindowEventChannelMocker> 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<ISession> sessionToken = nullptr;
sptr<SessionStageMocker> 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<WindowEventChannelMocker> 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