/* * 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 "root_scene_session.h" #include "scene_session_manager.h" #include "session_info.h" using namespace testing; using namespace testing::ext; namespace OHOS { namespace Rosen { class RootSceneSessionTest : public testing::Test { public: static void SetUpTestCase(); static void TearDownTestCase(); void SetUp() override; void TearDown() override; static sptr ssm_; }; sptr RootSceneSessionTest::ssm_ = nullptr; void LoadContentFuncTest(const std::string&, napi_env, napi_value, AbilityRuntime::Context*) {} void RootSceneSessionTest::SetUpTestCase() { ssm_ = &SceneSessionManager::GetInstance(); } void RootSceneSessionTest::TearDownTestCase() { ssm_ = nullptr; } void RootSceneSessionTest::SetUp() {} void RootSceneSessionTest::TearDown() {} namespace { /** * @tc.name: SetLoadContentFunc * @tc.desc: test function : SetLoadContentFunc * @tc.type: FUNC */ HWTEST_F(RootSceneSessionTest, SetLoadContentFunc, TestSize.Level1) { RootSceneSession rootSceneSession; RootSceneSession::LoadContentFunc loadContentFunc_ = LoadContentFuncTest; rootSceneSession.SetLoadContentFunc(loadContentFunc_); ASSERT_FALSE(rootSceneSession.IsVisible()); } /** * @tc.name: LoadContentFunc * @tc.desc: test function : LoadContentFunc * @tc.type: FUNC */ HWTEST_F(RootSceneSessionTest, LoadContentFunc, TestSize.Level1) { RootSceneSession rootSceneSession; std::string strTest("LoadContentFuncTest"); napi_env nativeEnv_ = nullptr; napi_value nativeValue_ = nullptr; AbilityRuntime::Context* conText_ = nullptr; rootSceneSession.LoadContent(strTest, nativeEnv_, nativeValue_, conText_); ASSERT_FALSE(rootSceneSession.IsVisible()); } /** * @tc.name: LoadContent * @tc.desc: test function : LoadContent * @tc.type: FUNC */ HWTEST_F(RootSceneSessionTest, LoadContent, TestSize.Level1) { RootSceneSession rootSceneSession; std::string strTest("LoadContentFuncTest"); napi_env nativeEnv_ = nullptr; napi_value nativeValue_ = nullptr; AbilityRuntime::Context* conText_ = nullptr; RootSceneSession::LoadContentFunc loadContentFunc_ = LoadContentFuncTest; rootSceneSession.SetLoadContentFunc(loadContentFunc_); rootSceneSession.LoadContent(strTest, nativeEnv_, nativeValue_, conText_); ASSERT_FALSE(rootSceneSession.IsVisible()); } /** * @tc.name: GetSystemAvoidAreaForRoot * @tc.desc: test function : GetSystemAvoidAreaForRoot * @tc.type: FUNC */ HWTEST_F(RootSceneSessionTest, GetSystemAvoidAreaForRoot, TestSize.Level1) { RootSceneSession rootSceneSession; AvoidArea avoidArea; WSRect rect; rootSceneSession.GetSystemAvoidAreaForRoot(rect, avoidArea); EXPECT_TRUE(avoidArea.isEmptyAvoidArea()); } /** * @tc.name: GetKeyboardAvoidAreaForRoot * @tc.desc: test function : GetKeyboardAvoidAreaForRoot * @tc.type: FUNC */ HWTEST_F(RootSceneSessionTest, GetKeyboardAvoidAreaForRoot, TestSize.Level1) { RootSceneSession rootSceneSession; AvoidArea avoidArea; WSRect rect; rootSceneSession.GetKeyboardAvoidAreaForRoot(rect, avoidArea); EXPECT_TRUE(avoidArea.isEmptyAvoidArea()); } /** * @tc.name: GetAINavigationBarAreaForRoot * @tc.desc: test function : GetAINavigationBarAreaForRoot * @tc.type: FUNC */ HWTEST_F(RootSceneSessionTest, GetAINavigationBarAreaForRoot, TestSize.Level1) { RootSceneSession rootSceneSession; AvoidArea avoidArea; WSRect rect; rootSceneSession.GetAINavigationBarAreaForRoot(rect, avoidArea); EXPECT_TRUE(avoidArea.isEmptyAvoidArea()); } /** * @tc.name: GetCutoutAvoidAreaForRoot * @tc.desc: test function : GetCutoutAvoidAreaForRoot * @tc.type: FUNC */ HWTEST_F(RootSceneSessionTest, GetCutoutAvoidAreaForRoot, TestSize.Level1) { RootSceneSession rootSceneSession; AvoidArea avoidArea; WSRect rect; rootSceneSession.GetCutoutAvoidAreaForRoot(rect, avoidArea); EXPECT_TRUE(avoidArea.isEmptyAvoidArea()); } /** * @tc.name: SetRootSessionRect * @tc.desc: test function : SetRootSessionRect * @tc.type: FUNC */ HWTEST_F(RootSceneSessionTest, SetRootSessionRect, TestSize.Level1) { RootSceneSession rootSceneSession; AvoidArea avoidArea; WSRect rect; rootSceneSession.SetRootSessionRect(rect); ASSERT_EQ(rootSceneSession.GetSessionRect(), rect); } /** * @tc.name: UpdateAvoidArea * @tc.desc: test function : UpdateAvoidArea * @tc.type: FUNC */ HWTEST_F(RootSceneSessionTest, UpdateAvoidArea, TestSize.Level1) { RootSceneSession rootSceneSession; AvoidArea avoidArea; auto ret = rootSceneSession.UpdateAvoidArea(sptr::MakeSptr(avoidArea), AvoidAreaType::TYPE_SYSTEM); ASSERT_EQ(WSError::WS_ERROR_NULLPTR, ret); } /** * @tc.name: GetAvoidAreaByType * @tc.desc: test function : GetAvoidAreaByType * @tc.type: FUNC */ HWTEST_F(RootSceneSessionTest, GetAvoidAreaByType, TestSize.Level1) { ASSERT_NE(nullptr, ssm_); ssm_->rootSceneSession_ = sptr::MakeSptr(); auto avoidArea = ssm_->rootSceneSession_->GetAvoidAreaByType(AvoidAreaType::TYPE_SYSTEM, { 0, 0, 0, 0 }, 1); EXPECT_TRUE(avoidArea.isEmptyAvoidArea()); avoidArea = ssm_->rootSceneSession_->GetAvoidAreaByType(AvoidAreaType::TYPE_CUTOUT, { 0, 0, 0, 0 }, 1); EXPECT_TRUE(avoidArea.isEmptyAvoidArea()); avoidArea = ssm_->rootSceneSession_->GetAvoidAreaByType(AvoidAreaType::TYPE_SYSTEM_GESTURE, { 0, 0, 0, 0 }, 1); EXPECT_TRUE(avoidArea.isEmptyAvoidArea()); avoidArea = ssm_->rootSceneSession_->GetAvoidAreaByType(AvoidAreaType::TYPE_KEYBOARD, { 0, 0, 0, 0 }, 1); EXPECT_TRUE(avoidArea.isEmptyAvoidArea()); avoidArea = ssm_->rootSceneSession_->GetAvoidAreaByType(AvoidAreaType::TYPE_NAVIGATION_INDICATOR, { 0, 0, 0, 0 }, 1); EXPECT_TRUE(avoidArea.isEmptyAvoidArea()); avoidArea = ssm_->rootSceneSession_->GetAvoidAreaByType(AvoidAreaType::TYPE_END, { 0, 0, 0, 0 }, 1); EXPECT_TRUE(avoidArea.isEmptyAvoidArea()); } /** * @tc.name: GetSystemAvoidAreaForRoot_01 * @tc.desc: test function : GetSystemAvoidAreaForRoot_01 * @tc.type: FUNC */ HWTEST_F(RootSceneSessionTest, GetSystemAvoidAreaForRoot_01, TestSize.Level1) { ASSERT_NE(nullptr, ssm_); auto specificCb = sptr::MakeSptr(); specificCb->onGetSceneSessionVectorByTypeAndDisplayId_ = [](WindowType type, uint64_t displayId) { return ssm_->GetSceneSessionVectorByTypeAndDisplayId(type, displayId); }; ssm_->rootSceneSession_ = sptr::MakeSptr(specificCb); ssm_->rootSceneSession_->GetLayoutController()->SetSessionRect({ 0, 0, 1260, 2720 }); ssm_->rootSceneSession_->onGetStatusBarAvoidHeightFunc_ = [](DisplayId displayId, WSRect& barArea) {}; SessionInfo statusBarSessionInfo; statusBarSessionInfo.abilityName_ = "statusBar"; statusBarSessionInfo.bundleName_ = "statusBar"; statusBarSessionInfo.screenId_ = 0; sptr statusBarSession = sptr::MakeSptr(statusBarSessionInfo, nullptr); statusBarSession->property_->SetPersistentId(2); statusBarSession->property_->type_ = WindowType::WINDOW_TYPE_STATUS_BAR; statusBarSession->GetLayoutController()->SetSessionRect({ 0, 0, 1260, 123 }); statusBarSession->isVisible_ = true; ssm_->sceneSessionMap_.insert({ statusBarSession->GetPersistentId(), statusBarSession }); AvoidArea avoidArea; ssm_->rootSceneSession_->GetSystemAvoidAreaForRoot(ssm_->rootSceneSession_->GetSessionRect(), avoidArea); Rect rect = { 0, 0, 1260, 123 }; ASSERT_EQ(avoidArea.topRect_, rect); statusBarSession->isVisible_ = false; avoidArea.topRect_ = { 0, 0, 0, 0 }; ssm_->rootSceneSession_->GetSystemAvoidAreaForRoot(ssm_->rootSceneSession_->GetSessionRect(), avoidArea); EXPECT_TRUE(avoidArea.isEmptyAvoidArea()); } /** * @tc.name: GetKeyboardAvoidAreaForRoot_01 * @tc.desc: test function : GetKeyboardAvoidAreaForRoot_01 * @tc.type: FUNC */ HWTEST_F(RootSceneSessionTest, GetKeyboardAvoidAreaForRoot_01, TestSize.Level1) { ASSERT_NE(nullptr, ssm_); auto specificCb = sptr::MakeSptr(); specificCb->onGetSceneSessionVectorByType_ = [](WindowType type) { return ssm_->GetSceneSessionVectorByType(type); }; ssm_->rootSceneSession_ = sptr::MakeSptr(specificCb); ssm_->rootSceneSession_->GetLayoutController()->SetSessionRect({ 0, 0, 1260, 2720 }); ssm_->rootSceneSession_->isKeyboardPanelEnabled_ = false; SessionInfo keyboardSessionInfo; keyboardSessionInfo.abilityName_ = "keyboard"; keyboardSessionInfo.bundleName_ = "keyboard"; keyboardSessionInfo.screenId_ = 0; sptr keyboardSession = sptr::MakeSptr(keyboardSessionInfo, nullptr); ssm_->systemConfig_.windowUIType_ = WindowUIType::PHONE_WINDOW; keyboardSession->state_ = SessionState::STATE_FOREGROUND; keyboardSession->keyboardAvoidAreaActive_ = true; keyboardSession->property_->type_ = WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT; keyboardSession->GetLayoutController()->SetSessionRect({ 0, 1700, 1260, 1020 }); keyboardSession->property_->SetPersistentId(2); ssm_->sceneSessionMap_.insert({ keyboardSession->GetPersistentId(), keyboardSession }); AvoidArea avoidArea; ssm_->rootSceneSession_->GetKeyboardAvoidAreaForRoot(ssm_->rootSceneSession_->GetSessionRect(), avoidArea); Rect rect = { 0, 1700, 1260, 1020 }; EXPECT_EQ(avoidArea.bottomRect_, rect); avoidArea.bottomRect_ = { 0, 0, 0, 0 }; ssm_->rootSceneSession_->GetKeyboardAvoidAreaForRoot(ssm_->rootSceneSession_->GetSessionRect(), avoidArea); EXPECT_TRUE(!avoidArea.isEmptyAvoidArea()); ssm_->rootSceneSession_->isKeyboardPanelEnabled_ = true; keyboardSession->state_ = SessionState::STATE_BACKGROUND; avoidArea.bottomRect_ = { 0, 0, 0, 0 }; ssm_->rootSceneSession_->GetKeyboardAvoidAreaForRoot(ssm_->rootSceneSession_->GetSessionRect(), avoidArea); EXPECT_TRUE(avoidArea.isEmptyAvoidArea()); } /** * @tc.name: GetCutoutAvoidAreaForRoot_01 * @tc.desc: test function : GetCutoutAvoidAreaForRoot_01 * @tc.type: FUNC */ HWTEST_F(RootSceneSessionTest, GetCutoutAvoidAreaForRoot_01, TestSize.Level1) { ASSERT_NE(nullptr, ssm_); ssm_->rootSceneSession_ = sptr::MakeSptr(); ssm_->rootSceneSession_->GetLayoutController()->SetSessionRect({ 0, 0, 1260, 2720 }); ssm_->rootSceneSession_->property_->displayId_ = -1; AvoidArea avoidArea; ssm_->rootSceneSession_->GetCutoutAvoidAreaForRoot(ssm_->rootSceneSession_->GetSessionRect(), avoidArea); EXPECT_TRUE(avoidArea.isEmptyAvoidArea()); ssm_->rootSceneSession_->property_->displayId_ = 0; ssm_->rootSceneSession_->GetCutoutAvoidAreaForRoot(ssm_->rootSceneSession_->GetSessionRect(), avoidArea); } /** * @tc.name: GetAINavigationBarAreaForRoot_01 * @tc.desc: test function : GetAINavigationBarAreaForRoot_01 * @tc.type: FUNC */ HWTEST_F(RootSceneSessionTest, GetAINavigationBarAreaForRoot_01, TestSize.Level1) { ASSERT_NE(nullptr, ssm_); auto specificCb = sptr::MakeSptr(); specificCb->onGetAINavigationBarArea_ = [](uint64_t displayId) { return ssm_->GetAINavigationBarArea(displayId); }; ssm_->rootSceneSession_ = sptr::MakeSptr(specificCb); ssm_->rootSceneSession_->GetLayoutController()->SetSessionRect({ 0, 0, 1260, 2720 }); AvoidArea avoidArea; ssm_->rootSceneSession_->GetAINavigationBarAreaForRoot(ssm_->rootSceneSession_->GetSessionRect(), avoidArea); EXPECT_TRUE(avoidArea.isEmptyAvoidArea()); ssm_->currAINavigationBarAreaMap_[0] = { 409, 2629, 442, 91 }; ssm_->rootSceneSession_->GetAINavigationBarAreaForRoot(ssm_->rootSceneSession_->GetSessionRect(), avoidArea); Rect rect = { 409, 2629, 442, 91 }; ASSERT_EQ(avoidArea.bottomRect_, rect); } /** * @tc.name: SetRootSessionRect_01 * @tc.desc: test function : SetRootSessionRect_01 * @tc.type: FUNC */ HWTEST_F(RootSceneSessionTest, SetRootSessionRect_01, TestSize.Level1) { ASSERT_NE(nullptr, ssm_); ssm_->rootSceneSession_ = sptr::MakeSptr(); ssm_->rootSceneSession_->GetLayoutController()->SetSessionRect({ 0, 0, 1260, 2720 }); ssm_->rootSceneSession_->SetRootSessionRect({ 0, 0, 1260, 2720 }); WSRect rect = { 0, 0, 1260, 2720 }; ASSERT_EQ(ssm_->rootSceneSession_->GetSessionRect(), rect); ssm_->rootSceneSession_->GetLayoutController()->SetSessionRect({ 0, 0, 2720, 1260 }); ssm_->rootSceneSession_->SetRootSessionRect({ 0, 0, 1260, 2720 }); rect = { 0, 0, 1260, 2720 }; ASSERT_EQ(ssm_->rootSceneSession_->GetSessionRect(), rect); } /** * @tc.name: UpdateAvoidArea_01 * @tc.desc: test function : UpdateAvoidArea_01 * @tc.type: FUNC */ HWTEST_F(RootSceneSessionTest, UpdateAvoidArea_01, TestSize.Level1) { ASSERT_NE(nullptr, ssm_); auto specificCb = sptr::MakeSptr(); specificCb->onNotifyAvoidAreaChange_ = [](const sptr& avoidArea, AvoidAreaType type) {}; ssm_->rootSceneSession_ = sptr::MakeSptr(specificCb); AvoidArea avoidArea; auto ret = ssm_->rootSceneSession_->UpdateAvoidArea(new AvoidArea(avoidArea), AvoidAreaType::TYPE_SYSTEM); ASSERT_EQ(ret, WSError::WS_OK); } /** * @tc.name: GetStatusBarHeight * @tc.desc: normal function * @tc.type: FUNC */ HWTEST_F(RootSceneSessionTest, GetStatusBarHeight, TestSize.Level1) { ASSERT_NE(nullptr, ssm_); ssm_->rootSceneSession_ = sptr::MakeSptr(); auto height = ssm_->rootSceneSession_->GetStatusBarHeight(); EXPECT_EQ(0, height); auto specificCb = sptr::MakeSptr(); ASSERT_NE(specificCb, nullptr); specificCb->onGetSceneSessionVectorByTypeAndDisplayId_ = [](WindowType type, uint64_t displayId) { return ssm_->GetSceneSessionVectorByTypeAndDisplayId(type, displayId); }; ssm_->rootSceneSession_ = sptr::MakeSptr(specificCb); ssm_->rootSceneSession_->GetLayoutController()->SetSessionRect({ 0, 0, 1260, 2720 }); SessionInfo statusBarSessionInfo; statusBarSessionInfo.abilityName_ = "statusBar"; statusBarSessionInfo.bundleName_ = "statusBar"; statusBarSessionInfo.screenId_ = 0; sptr statusBarSession = sptr::MakeSptr(statusBarSessionInfo, nullptr); statusBarSession->property_->SetPersistentId(2); statusBarSession->property_->type_ = WindowType::WINDOW_TYPE_STATUS_BAR; statusBarSession->GetLayoutController()->SetSessionRect({ 0, 0, 1260, 123 }); statusBarSession->isVisible_ = true; ssm_->sceneSessionMap_.insert({ statusBarSession->GetPersistentId(), statusBarSession }); height = ssm_->rootSceneSession_->GetStatusBarHeight(); EXPECT_EQ(123, height); ssm_->rootSceneSession_->onGetStatusBarAvoidHeightFunc_ = [](DisplayId displayId, WSRect& barArea) { barArea.height_ = 100; }; height = ssm_->rootSceneSession_->GetStatusBarHeight(); EXPECT_EQ(100, height); } } // namespace } // namespace Rosen } // namespace OHOS