From 1b583f39d375a6e4ab6cf88694205550a08e5f32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Wed, 2 Aug 2023 12:22:28 +0200 Subject: [PATCH] tst_foreignwindow: Add basic test of foreign window reparenting Change-Id: I008fad0f6527503a13ded4818eec5cb280f65cf4 Reviewed-by: Timur Pocheptsov (cherry picked from commit 546d4c9d33ffe9c66b69798fef04a0355fe3f0b8) Reviewed-by: Qt Cherry-pick Bot --- .../gui/kernel/qwindow/tst_foreignwindow.cpp | 19 +++++++++++++++++++ tests/shared/nativewindow.h | 11 +++++++++++ 2 files changed, 30 insertions(+) diff --git a/tests/auto/gui/kernel/qwindow/tst_foreignwindow.cpp b/tests/auto/gui/kernel/qwindow/tst_foreignwindow.cpp index dc82d8b8336..04c70e5feec 100644 --- a/tests/auto/gui/kernel/qwindow/tst_foreignwindow.cpp +++ b/tests/auto/gui/kernel/qwindow/tst_foreignwindow.cpp @@ -23,6 +23,8 @@ private slots: void fromWinId(); void initialState(); + + void embedForeignWindow(); }; void tst_ForeignWindow::fromWinId() @@ -67,5 +69,22 @@ void tst_ForeignWindow::initialState() QCOMPARE(foreignWindow->geometry(), initialGeometry); } +void tst_ForeignWindow::embedForeignWindow() +{ + // A foreign window embedded into a Qt UI requires that the rest of Qt + // is to be able to treat the foreign child window as any other window + // that it can show, hide, stack, and move around. + + QWindow parentWindow; + + NativeWindow nativeWindow; + QVERIFY(nativeWindow); + + // As a prerequisite to that, we must be able to reparent the foreign window + std::unique_ptr foreignWindow(QWindow::fromWinId(nativeWindow)); + foreignWindow.release()->setParent(&parentWindow); + QCOMPARE(nativeWindow.parentWinId(), parentWindow.winId()); +} + #include QTEST_MAIN(tst_ForeignWindow) diff --git a/tests/shared/nativewindow.h b/tests/shared/nativewindow.h index 6b3ffe08976..4b2aff0cdd2 100644 --- a/tests/shared/nativewindow.h +++ b/tests/shared/nativewindow.h @@ -18,6 +18,7 @@ public: ~NativeWindow(); operator WId() const { return reinterpret_cast(m_handle); } + WId parentWinId() const; void setGeometry(const QRect &rect); QRect geometry() const; @@ -69,6 +70,11 @@ QRect NativeWindow::geometry() const return QRectF::fromCGRect(m_handle.frame).toRect(); } +WId NativeWindow::parentWinId() const +{ + return WId(m_handle.superview); +} + #elif defined(Q_OS_WIN) NativeWindow::NativeWindow() @@ -107,6 +113,11 @@ QRect NativeWindow::geometry() const return {}; } +WId NativeWindow::parentWinId() const +{ + return WId(GetAncestor(m_handle, GA_PARENT)); +} + #endif #endif // NATIVEWINDOW_H