1 /*
2     KWin - the KDE window manager
3     This file is part of the KDE project.
4 
5     SPDX-FileCopyrightText: 2012 Martin Gräßlin <mgraesslin@kde.org>
6 
7     SPDX-License-Identifier: GPL-2.0-or-later
8 */
9 #include "test_tabbox_clientmodel.h"
10 #include "mock_tabboxhandler.h"
11 #include "clientmodel.h"
12 #include "../testutils.h"
13 
14 #include <QtTest>
15 #include <QX11Info>
16 using namespace KWin;
17 
initTestCase()18 void TestTabBoxClientModel::initTestCase()
19 {
20     qApp->setProperty("x11Connection", QVariant::fromValue<void*>(QX11Info::connection()));
21 }
22 
testLongestCaptionWithNullClient()23 void TestTabBoxClientModel::testLongestCaptionWithNullClient()
24 {
25     MockTabBoxHandler tabboxhandler;
26     TabBox::ClientModel *clientModel = new TabBox::ClientModel(&tabboxhandler);
27     clientModel->createClientList();
28     QCOMPARE(clientModel->longestCaption(), QString());
29     // add a window to the mock
30     tabboxhandler.createMockWindow(QString("test"));
31     clientModel->createClientList();
32     QCOMPARE(clientModel->longestCaption(), QString("test"));
33     // delete the one client in the list
34     QModelIndex index = clientModel->index(0, 0);
35     QVERIFY(index.isValid());
36     TabBox::TabBoxClient *client = static_cast<TabBox::TabBoxClient *>(clientModel->data(index, TabBox::ClientModel::ClientRole).value<void*>());
37     client->close();
38     // internal model of ClientModel now contains a deleted pointer
39     // longestCaption should behave just as if the window were not in the list
40     QCOMPARE(clientModel->longestCaption(), QString());
41 }
42 
testCreateClientListNoActiveClient()43 void TestTabBoxClientModel::testCreateClientListNoActiveClient()
44 {
45     MockTabBoxHandler tabboxhandler;
46     tabboxhandler.setConfig(TabBox::TabBoxConfig());
47     TabBox::ClientModel *clientModel = new TabBox::ClientModel(&tabboxhandler);
48     clientModel->createClientList();
49     QCOMPARE(clientModel->rowCount(), 0);
50     // create two windows, rowCount() should go to two
51     QWeakPointer<TabBox::TabBoxClient> client = tabboxhandler.createMockWindow(QString("test"));
52     tabboxhandler.createMockWindow(QString("test2"));
53     clientModel->createClientList();
54     QCOMPARE(clientModel->rowCount(), 2);
55     // let's ensure there is no active client
56     tabboxhandler.setActiveClient(QWeakPointer<TabBox::TabBoxClient>());
57     // now it should still have two members in the list
58     clientModel->createClientList();
59     QCOMPARE(clientModel->rowCount(), 2);
60 }
61 
testCreateClientListActiveClientNotInFocusChain()62 void TestTabBoxClientModel::testCreateClientListActiveClientNotInFocusChain()
63 {
64     MockTabBoxHandler tabboxhandler;
65     tabboxhandler.setConfig(TabBox::TabBoxConfig());
66     TabBox::ClientModel *clientModel = new TabBox::ClientModel(&tabboxhandler);
67     // create two windows, rowCount() should go to two
68     QWeakPointer<TabBox::TabBoxClient> client = tabboxhandler.createMockWindow(QString("test"));
69     client = tabboxhandler.createMockWindow(QString("test2"));
70     clientModel->createClientList();
71     QCOMPARE(clientModel->rowCount(), 2);
72 
73     // simulate that the active client is not in the focus chain
74     // for that we use the closeWindow of the MockTabBoxHandler which
75     // removes the Client from the Focus Chain but leaves the active window as it is
76     QSharedPointer<TabBox::TabBoxClient> clientOwner = client.toStrongRef();
77     tabboxhandler.closeWindow(clientOwner.data());
78     clientModel->createClientList();
79     QCOMPARE(clientModel->rowCount(), 1);
80 }
81 
82 Q_CONSTRUCTOR_FUNCTION(forceXcb)
83 QTEST_MAIN(TestTabBoxClientModel)
84