include/vcl/uitest/uitest.hxx | 2 ++ offapi/com/sun/star/ui/test/XUITest.idl | 6 ++++++ vcl/source/uitest/uitest.cxx | 12 ++++++++++++ vcl/source/uitest/uno/uitest_uno.cxx | 11 +++++++++++ 4 files changed, 31 insertions(+)
New commits: commit d2d1e8221f8225db50a7c51a4537126076eb4e0d Author: Neil Roberts <[email protected]> AuthorDate: Wed Oct 29 22:29:35 2025 +0100 Commit: Xisco Fauli <[email protected]> CommitDate: Thu Oct 30 11:30:43 2025 +0100 UITest: Add a method to get the currently focused window I think this will be useful when you want to manipulate a window that doesn’t have a unique ID and is difficult to get access to but you can make it get the keyboard focus via UNO commands or keyboard shortcuts. Change-Id: Iafc41dda7c3fdc1b15a62875fe3aa7efc88dcbe5 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/193172 Tested-by: Jenkins Reviewed-by: Xisco Fauli <[email protected]> diff --git a/include/vcl/uitest/uitest.hxx b/include/vcl/uitest/uitest.hxx index 189df1de55e8..488cf4fc4b6f 100644 --- a/include/vcl/uitest/uitest.hxx +++ b/include/vcl/uitest/uitest.hxx @@ -30,6 +30,8 @@ namespace UITest std::unique_ptr<UIObject> getFocusTopWindow(); + std::unique_ptr<UIObject> getFocusWindow(); + std::unique_ptr<UIObject> getFloatWindow(); }; diff --git a/offapi/com/sun/star/ui/test/XUITest.idl b/offapi/com/sun/star/ui/test/XUITest.idl index e4fd1d30aa19..c551e8ebac89 100644 --- a/offapi/com/sun/star/ui/test/XUITest.idl +++ b/offapi/com/sun/star/ui/test/XUITest.idl @@ -27,6 +27,12 @@ interface XUITest @since LibreOffice 25.8 */ XUIObject getWindow([in] ::com::sun::star::awt::XWindow xWindow); + + /** Get the XUIObject for the window that currently has focus. + + @since LibreOffice 26.2 + */ + XUIObject getFocusWindow(); }; }; }; }; }; }; diff --git a/vcl/source/uitest/uitest.cxx b/vcl/source/uitest/uitest.cxx index ba2cef12dd3f..67c8d93dd7db 100644 --- a/vcl/source/uitest/uitest.cxx +++ b/vcl/source/uitest/uitest.cxx @@ -65,6 +65,18 @@ std::unique_ptr<UIObject> UITest::getFocusTopWindow() return pSVData->maFrameData.mpFirstFrame->GetUITestFactory()(pSVData->maFrameData.mpFirstFrame); } +std::unique_ptr<UIObject> UITest::getFocusWindow() +{ + ImplSVData* pSVData = ImplGetSVData(); + ImplSVWinData& rWinData = *pSVData->mpWinData; + + VclPtr<vcl::Window> pFocusWin = rWinData.mpFocusWin; + if (pFocusWin) + return pFocusWin->GetUITestFactory()(pFocusWin); + + return nullptr; +} + std::unique_ptr<UIObject> UITest::getFloatWindow() { ImplSVData* pSVData = ImplGetSVData(); diff --git a/vcl/source/uitest/uno/uitest_uno.cxx b/vcl/source/uitest/uno/uitest_uno.cxx index c49422877e8a..1a3329a7ba1c 100644 --- a/vcl/source/uitest/uno/uitest_uno.cxx +++ b/vcl/source/uitest/uno/uitest_uno.cxx @@ -43,6 +43,8 @@ public: css::uno::Reference<css::ui::test::XUIObject> SAL_CALL getTopFocusWindow() override; + css::uno::Reference<css::ui::test::XUIObject> SAL_CALL getFocusWindow() override; + css::uno::Reference<css::ui::test::XUIObject> SAL_CALL getFloatWindow() override; css::uno::Reference<css::ui::test::XUIObject> @@ -89,6 +91,15 @@ css::uno::Reference<css::ui::test::XUIObject> SAL_CALL UITestUnoObj::getTopFocus return new UIObjectUnoObj(std::move(pObj)); } +css::uno::Reference<css::ui::test::XUIObject> SAL_CALL UITestUnoObj::getFocusWindow() +{ + SolarMutexGuard aGuard; + std::unique_ptr<UIObject> pObj = UITest::getFocusWindow(); + if (!pObj) + throw css::uno::RuntimeException(u"UITest::getFocusWindow did not find a window"_ustr); + return new UIObjectUnoObj(std::move(pObj)); +} + css::uno::Reference<css::ui::test::XUIObject> SAL_CALL UITestUnoObj::getFloatWindow() { SolarMutexGuard aGuard;
