From 9db9f172bc15198d803372532e6abb4914f0206a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Mon, 14 Apr 2014 10:31:37 +0200 Subject: [PATCH] [Xcb::Wrapper] Add a unit test for Xcb::Tree Based on the test Tree::children() is extended for a test case on no children which used to return a garbage pointer. --- autotests/test_xcb_wrapper.cpp | 34 ++++++++++++++++++++++++++++++++++ xcbutils.h | 3 +++ 2 files changed, 37 insertions(+) diff --git a/autotests/test_xcb_wrapper.cpp b/autotests/test_xcb_wrapper.cpp index 815b342d3c..2558e2bba7 100644 --- a/autotests/test_xcb_wrapper.cpp +++ b/autotests/test_xcb_wrapper.cpp @@ -44,6 +44,7 @@ private Q_SLOTS: void assignmentBeforeRetrieve(); void assignmentAfterRetrieve(); void discard(); + void testQueryTree(); private: void testEmpty(WindowGeometry &geometry); void testGeometry(WindowGeometry &geometry, const QRect &rect); @@ -210,5 +211,38 @@ void TestXcbWrapper::discard() delete geometry; } +void TestXcbWrapper::testQueryTree() +{ + m_testWindow = createWindow(); + QVERIFY(m_testWindow != noneWindow()); + Tree tree(m_testWindow); + // should have root as parent + QCOMPARE(tree.parent(), static_cast(QX11Info::appRootWindow())); + // shouldn't have any children + QCOMPARE(tree->children_len, uint16_t(0)); + QVERIFY(!tree.children()); + + // query for root + Tree root(QX11Info::appRootWindow()); + // shouldn't have a parent + QCOMPARE(root.parent(), xcb_window_t(XCB_WINDOW_NONE)); + QVERIFY(root->children_len > 0); + xcb_window_t *children = root.children(); + bool found = false; + for (int i = 0; i < xcb_query_tree_children_length(root.data()); ++i) { + if (children[i] == tree.window()) { + found = true; + break; + } + } + QVERIFY(found); + + // query for not existing window + Tree doesntExist(XCB_WINDOW_NONE); + QCOMPARE(doesntExist.parent(), xcb_window_t(XCB_WINDOW_NONE)); + QVERIFY(doesntExist.isNull()); + QVERIFY(doesntExist.isRetrieved()); +} + KWIN_TEST_MAIN(TestXcbWrapper) #include "test_xcb_wrapper.moc" diff --git a/xcbutils.h b/xcbutils.h index 3f2bd6c0ad..73caa1fab2 100644 --- a/xcbutils.h +++ b/xcbutils.h @@ -243,6 +243,9 @@ public: explicit Tree(WindowId window) : Wrapper(window) {} inline WindowId *children() { + if (data()->children_len == 0) { + return nullptr; + } return xcb_query_tree_children(data()); } inline xcb_window_t parent() {