diff --git a/dm/src/display_manager.cpp b/dm/src/display_manager.cpp index d4c862fa2a4127986bd89a4d3eebb4cf84a87be9..589612949a292d18ac99b4736ac936bcc69c1654 100644 --- a/dm/src/display_manager.cpp +++ b/dm/src/display_manager.cpp @@ -989,6 +989,30 @@ void DisplayManager::RemoveDisplayIdFromAms(const wptr& abilityTo ShowDisplayIdList(false); } +void DisplayManager::UpdateDisplayIdFromAms(DisplayId displayId, const wptr& abilityToken) +{ + TLOGI(WmsLogTag::DMS, "start, displayId: %{public}" PRIu64, displayId); + if (abilityToken == nullptr || displayId == DISPLAY_ID_INVALID) { + TLOGE(WmsLogTag::DMS, "abilityToken is nullptr or display id invalid. displayId: %{public}" PRIu64, displayId); + return; + } + std::lock_guard lock(displayOperateMutex_); + if (displayIdList_.empty()) { + TLOGI(WmsLogTag::DMS, "displayIdList is empty. add displayId directly."); + displayIdList_.push_back(std::make_pair(abilityToken, displayId)); + return; + } + displayIdList_.erase( + std::remove_if(displayIdList_.begin(), displayIdList_.end(), + [abilityToken](const auto &item) -> bool { + return item.first == abilityToken; + }), + displayIdList_.end() + ); + displayIdList_.push_back(std::make_pair(abilityToken, displayId)); + ShowDisplayIdList(false); +} + DisplayId DisplayManager::GetCallingAbilityDisplayId() { DisplayId displayId = DISPLAY_ID_INVALID; diff --git a/dm/test/unittest/display_manager_test.cpp b/dm/test/unittest/display_manager_test.cpp index a33680fa0fb33cfaf6828587e734e7748750b645..b23b8009b262b87f36e86e38bd62d4390a69021c 100644 --- a/dm/test/unittest/display_manager_test.cpp +++ b/dm/test/unittest/display_manager_test.cpp @@ -2828,6 +2828,92 @@ HWTEST_F(DisplayManagerTest, NotifyScreenshot, TestSize.Level1) EXPECT_TRUE(g_errLog.find("NotifyScreenshot trigger") != std::string::npos); g_errLog.clear(); } + +/** + * @tc.name: UpdateDisplayIdFromAms_Null_AbilityToken + * @tc.desc: Test UpdateDisplayIdFromAms with null abilityToken + * @tc.type: FUNC + */ +HWTEST_F(DisplayManagerTest, UpdateDisplayIdFromAms_Null_AbilityToken, TestSize.Level1) +{ + g_errLog.clear(); + LOG_SetCallback(MyLogCallback); + DisplayId displayId = 0; + wptr abilityToken = nullptr; + SingletonContainer::Get().UpdateDisplayIdFromAms(displayId, abilityToken); + EXPECT_TRUE(g_errLog.find("abilityToken is nullptr") != std::string::npos); + g_errLog.clear(); + + sptr obj; + if (SceneBoardJudgement::IsSceneBoardEnabled()) { + ASSERT_NE(SingletonContainer::Get().screenSessionManagerServiceProxy_, nullptr); + obj = SingletonContainer::Get().screenSessionManagerServiceProxy_->AsObject(); + } else { + ASSERT_NE(SingletonContainer::Get().displayManagerServiceProxy_, nullptr); + obj = SingletonContainer::Get().displayManagerServiceProxy_->AsObject(); + } + abilityToken = obj; + displayId = -1; + SingletonContainer::Get().UpdateDisplayIdFromAms(displayId, abilityToken); + EXPECT_TRUE(g_errLog.find("display id invalid") != std::string::npos); + g_errLog.clear(); +} + +/** + * @tc.name: UpdateDisplayIdFromAms_displayIdList_empty + * @tc.desc: Test UpdateDisplayIdFromAms with empty displayIdList + * @tc.type: FUNC + */ +HWTEST_F(DisplayManagerTest, UpdateDisplayIdFromAms_displayIdList_empty, TestSize.Level1) +{ + g_errLog.clear(); + LOG_SetCallback(MyLogCallback); + DisplayManager displayManager; + displayManager.displayIdList_.clear(); + DisplayId displayId = 123; + sptr obj; + if (SceneBoardJudgement::IsSceneBoardEnabled()) { + ASSERT_NE(SingletonContainer::Get().screenSessionManagerServiceProxy_, nullptr); + obj = SingletonContainer::Get().screenSessionManagerServiceProxy_->AsObject(); + } else { + ASSERT_NE(SingletonContainer::Get().displayManagerServiceProxy_, nullptr); + obj = SingletonContainer::Get().displayManagerServiceProxy_->AsObject(); + } + wptr abilityToken = obj; + displayManager.UpdateDisplayIdFromAms(displayId, abilityToken); + EXPECT_TRUE(g_errLog.find("displayIdList_ is empty") != std::string::npos); + EXPECT_EQ(displayManager.GetCallingAbilityDisplayId(), displayId); + g_errLog.clear(); +} + +/** + * @tc.name: UpdateDisplayIdFromAms_Success + * @tc.desc: Test UpdateDisplayIdFromAms success + * @tc.type: FUNC + */ +HWTEST_F(DisplayManagerTest, UpdateDisplayIdFromAms_Success, TestSize.Level1) +{ + g_errLog.clear(); + LOG_SetCallback(MyLogCallback); + DisplayManager displayManager; + displayManager.displayIdList_.clear(); + sptr obj; + if (SceneBoardJudgement::IsSceneBoardEnabled()) { + ASSERT_NE(SingletonContainer::Get().screenSessionManagerServiceProxy_, nullptr); + obj = SingletonContainer::Get().screenSessionManagerServiceProxy_->AsObject(); + } else { + ASSERT_NE(SingletonContainer::Get().displayManagerServiceProxy_, nullptr); + obj = SingletonContainer::Get().displayManagerServiceProxy_->AsObject(); + } + wptr abilityToken = obj; + displayManager.displayIdList_.emplace_back(abilityToken, 111); + displayManager.displayIdList_.emplace_back(abilityToken, 222); + displayManager.displayIdList_.emplace_back(abilityToken, 333); + DisplayId displayId2 = 123; + displayManager.UpdateDisplayIdFromAms(displayId2, abilityToken); + EXPECT_EQ(displayManager.GetCallingAbilityDisplayId(), displayId2); + g_errLog.clear(); +} } } // namespace Rosen } // namespace OHOS diff --git a/interfaces/innerkits/dm/display_manager.h b/interfaces/innerkits/dm/display_manager.h index b1f5f8d605b2ba448ccef84e866538f77e682ae2..91f2d37a68c715afe8c6dd265fa9f683d2590b46 100644 --- a/interfaces/innerkits/dm/display_manager.h +++ b/interfaces/innerkits/dm/display_manager.h @@ -836,6 +836,14 @@ public: */ void RemoveDisplayIdFromAms(const wptr& abilityToken); + /** + * @brief update displayId for current ability through Ability Management. + * + * @param displayId Identifier of the current display. + * @param abilityToken Token of the ability. + */ + void UpdateDisplayIdFromAms(DisplayId displayId, const wptr& abilityToken); + /** * @brief Get primary display object by means of sync. * diff --git a/wm/src/window_session_impl.cpp b/wm/src/window_session_impl.cpp index 8a31c8bd251f827aa1831d1619388e78bd26f0dc..71678e79a04e57a4df22cfba1cc2e93be44a0702 100644 --- a/wm/src/window_session_impl.cpp +++ b/wm/src/window_session_impl.cpp @@ -5334,13 +5334,20 @@ EnableIfSame>> W void WindowSessionImpl::NotifyDisplayMove(DisplayId from, DisplayId to) { WLOGFD("from %{public}" PRIu64 " to %{public}" PRIu64, from, to); - std::lock_guard lockListener(displayMoveListenerMutex_); - auto displayMoveListeners = GetListeners(); - for (auto& listener : displayMoveListeners) { - if (listener != nullptr) { - listener->OnDisplayMove(from, to); + { + std::lock_guard lockListener(displayMoveListenerMutex_); + auto displayMoveListeners = GetListeners(); + for (auto& listener : displayMoveListeners) { + if (listener != nullptr) { + listener->OnDisplayMove(from, to); + } } } + auto context = GetContext(); + if (context != nullptr) { + TLOGI(WmsLogTag::WMS_MAIN, "update display move to dms"); + DisplayManager::GetInstance().UpdateDisplayIdFromAms(to, context->GetToken()); + } } WSError WindowSessionImpl::NotifyCloseExistPipWindow()