File: test_workingsets.cpp

package info (click to toggle)
kdevelop 4%3A22.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 70,096 kB
  • sloc: cpp: 284,635; javascript: 3,558; python: 3,422; sh: 1,319; ansic: 685; xml: 331; php: 95; lisp: 66; makefile: 39; sed: 12
file content (275 lines) | stat: -rw-r--r-- 10,480 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
    SPDX-FileCopyrightText: 2021 Christoph Roick <chrisito@gmx.de>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "test_workingsets.h"

#include "../core.h"
#include "../documentcontroller.h"
#include "../session.h"
#include "../uicontroller.h"
#include "../workingsetcontroller.h"
#include "../workingsets/workingset.h"

#include <sublime/area.h>
#include <sublime/document.h>
#include <sublime/view.h>

#include <tests/autotestshell.h>
#include <tests/testcore.h>

#include <KParts/MainWindow>

#include <QTest>

#include <QLayout>
#include <QMainWindow>
#include <QMenuBar>
#include <QSplitter>

#include <algorithm>

using namespace KDevelop;

const QString setName = QStringLiteral("TestSet");
const QString setName2 = QStringLiteral("TestSet2");
const QString setName3 = QStringLiteral("TestSet3");
const QString text = QStringLiteral("Test");

void TestWorkingSetController::initTestCase()
{
    AutoTestShell::init({{}});
    TestCore::initialize();
    m_workingSetCtrl = Core::self()->workingSetControllerInternal();
    auto uiController = Core::self()->uiControllerInternal();
    m_area = uiController->activeArea();
    m_area_debug = nullptr;
    m_documentCtrl = Core::self()->documentController();
    QVERIFY(m_mainWindow = uiController->activeMainWindow());
    auto areaDisplay = m_mainWindow->menuBar()->cornerWidget(Qt::TopRightCorner);
    // active working set + separator + closed working sets + tool button
    QCOMPARE(areaDisplay->layout()->count(), 4);
    // widget that contains the buttons of the currently closed working sets
    m_closedSets = areaDisplay->layout()->itemAt(2)->widget();
}

void TestWorkingSetController::init()
{
    m_file.setFileTemplate(m_tempDir.path() + "/tmp_XXXXXX.txt");
    if(!m_file.open()) {
        QFAIL("Cannot create temp file");
    }
}

void TestWorkingSetController::cleanupTestCase()
{
    m_mainWindow->close();
    TestCore::shutdown();
}

void TestWorkingSetController::restartSession()
{
    // End session and store it for now
    Core::self()->activeSession()->setTemporary(false);
    cleanupTestCase();
    // Reload session and make it temporary again
    initTestCase();
    Core::self()->activeSession()->setTemporary(true);
}


void TestWorkingSetController::createWorkingSet()
{
    // Create a persistent working set
    m_area->setWorkingSet(setName);
    const auto workingSets = m_workingSetCtrl->allWorkingSets();
    QCOMPARE(workingSets.size(), 1);
    const auto id = workingSets.first()->id();
    workingSets.first()->setPersistent(true);
    QVERIFY(workingSets.first()->isPersistent());
    // Open a document such that the working set is not empty
    m_documentCtrl->openDocument(QUrl::fromLocalFile(m_file.fileName()));

    // Create and activate a non-persistent working set
    m_area->setWorkingSet(setName3, false);
    QCOMPARE(m_workingSetCtrl->allWorkingSets().size(), 2);
    m_documentCtrl->openDocument(QUrl::fromLocalFile(m_file.fileName()));

    // Create and activate another non-persistent working set
    m_area->setWorkingSet(setName2, false);
    QCOMPARE(m_workingSetCtrl->allWorkingSets().size(), 3);
    const auto id2 = m_workingSetCtrl->workingSet(m_area->workingSet())->id();
    m_documentCtrl->openDocument(QUrl::fromLocalFile(m_file.fileName()));

    QTRY_COMPARE(m_closedSets->layout()->count(), 2); // working sets 1 + 3 (2 is active)

    restartSession();

    // Check if last non-persistent working set is active and persistent set exists
    QCOMPARE(m_workingSetCtrl->allWorkingSets().size(), 2);
    auto set = m_workingSetCtrl->workingSet(m_area->workingSet());
    QCOMPARE(set->id(), id2);
    QVERIFY(!set->isPersistent());

    // Activate persistent working set
    m_area->setWorkingSet(setName);
    QCOMPARE(m_workingSetCtrl->allWorkingSets().size(), 2);
    set = m_workingSetCtrl->workingSet(m_area->workingSet());
    QCOMPARE(set->id(), id);
    QVERIFY(set->isPersistent());

    QTRY_COMPARE(m_closedSets->layout()->count(), 1); // working set 2 (1 is active)
}

void TestWorkingSetController::deleteWorkingSet()
{
    // Create a persistent working set
    m_area->setWorkingSet(setName2);
    auto set2 = m_workingSetCtrl->workingSet(m_area->workingSet());
    set2->setPersistent(true);
    m_documentCtrl->openDocument(QUrl::fromLocalFile(m_file.fileName()));

    // Create and activate another persistent working set
    m_area->setWorkingSet(setName);
    auto set = m_workingSetCtrl->workingSet(m_area->workingSet());
    set->setPersistent(true);
    m_documentCtrl->openDocument(QUrl::fromLocalFile(m_file.fileName()));

    QTRY_COMPARE(m_closedSets->layout()->count(), 1); // working set 2 (1 is active)

    // Delete the first set
    set2->deleteSet(false);
    // Try deleting the active set
    set->deleteSet(false);

    QCOMPARE(m_closedSets->layout()->count(), 0); // no closed working set

    restartSession();

    const auto sets = m_workingSetCtrl->allWorkingSets();
    QVERIFY(std::any_of(sets.constBegin(), sets.constEnd(), [&](WorkingSet* set){ return set->id() == setName; }));
    QVERIFY(std::none_of(sets.constBegin(), sets.constEnd(), [&](WorkingSet* set){ return set->id() == setName2; }));

    QCOMPARE(m_closedSets->layout()->count(), 0); // no closed working set
}

void TestWorkingSetController::switchArea()
{
    // Create a persistent working set
    m_area->setWorkingSet(setName2);
    auto set2 = m_workingSetCtrl->workingSet(m_area->workingSet());
    set2->setPersistent(true);
    m_documentCtrl->openDocument(QUrl::fromLocalFile(m_file.fileName()));

    // Create and activate another persistent working set
    m_area->setWorkingSet(setName);
    auto set = m_workingSetCtrl->workingSet(m_area->workingSet());
    set->setPersistent(true);
    m_documentCtrl->openDocument(QUrl::fromLocalFile(m_file.fileName()));

    Core::self()->uiController()->switchToArea(QStringLiteral("debug"), IUiController::ThisWindow);
    m_area_debug = Core::self()->uiControllerInternal()->activeArea();
     // explicitly set the current working set, as in DebugController::addSession
    m_area_debug->setWorkingSet(setName, m_area->workingSetPersistent(), m_area);

    QTRY_COMPARE(m_closedSets->layout()->count(), 1); // working set 2

    m_area_debug->setWorkingSet(setName2);
    QTest::qSleep(1000);
    QCOMPARE(m_closedSets->layout()->count(), 1); // working set 1, BUG 375446

    Core::self()->uiController()->switchToArea(QStringLiteral("code"), IUiController::ThisWindow);
     // explicitly set the current working set, as in DebugController::debuggerStateChanged
    m_area->setWorkingSet(setName2, m_area_debug->workingSetPersistent(), m_area_debug);
    m_area->setWorkingSet(setName);

    QTRY_COMPARE(m_closedSets->layout()->count(), 1); // working set 2, BUG 375446
}

void TestWorkingSetController::restoreSplits()
{
    // we need to show the window to calculate actual widget sizes
    m_mainWindow->show();

    // Create a persistent working set with 4 split views
    m_area->setWorkingSet(setName);
    auto set2 = m_workingSetCtrl->workingSet(m_area->workingSet());
    set2->setPersistent(true);
    auto doc = m_documentCtrl->openDocument(QUrl::fromLocalFile(m_file.fileName()));

    // Split view
    auto view_bottom_left = dynamic_cast<Sublime::Document*>(doc)->createView();
    m_area->addView(view_bottom_left, m_area->activeView(), Qt::Vertical);
    auto view_top_right = dynamic_cast<Sublime::Document*>(doc)->createView();
    m_area->addView(view_top_right, m_area->activeView(), Qt::Horizontal);
    auto view_bottom_right = dynamic_cast<Sublime::Document*>(doc)->createView();
    m_area->addView(view_bottom_right, view_bottom_left, Qt::Horizontal);

    // required to arrange the view widgets in the window
    QApplication::processEvents();

    QSet<Sublime::AreaIndex*> indices;
    for (auto &view : m_area->views()) {
        indices.insert(m_area->indexOf(view));
    }
    QCOMPARE(indices.size(), 4); // number of view containers

    auto *splitter_bottom = qobject_cast<QSplitter*>(view_bottom_left->widget()
                                                         ->parentWidget()
                                                         ->parentWidget()   // view container
                                                         ->parentWidget()   // splitter containing a single container
                                                         ->parentWidget()); // splitter containing lower containers
    QVERIFY(splitter_bottom);
    splitter_bottom->repaint();
    splitter_bottom->setSizes({1, 1000});
    auto sizes_bottom = splitter_bottom->sizes();
    QVERIFY(sizes_bottom.at(0) < sizes_bottom.at(1));

    auto *splitter_mid = qobject_cast<QSplitter*>(splitter_bottom->parentWidget());
    QVERIFY(splitter_mid);
    splitter_mid->setSizes({1, 1000});
    auto sizes_mid = splitter_mid->sizes();
    QVERIFY(sizes_mid.at(0) < sizes_mid.at(1));

    QVERIFY(!qobject_cast<QSplitter*>(splitter_mid->parentWidget()));

    // Create and activate another persistent working set with a single view
    m_area->setWorkingSet(setName2);
    auto set = m_workingSetCtrl->workingSet(m_area->workingSet());
    set->setPersistent(true);
    m_documentCtrl->openDocument(QUrl::fromLocalFile(m_file.fileName()));

    indices.clear();
    for (auto &view : m_area->views()) {
        indices.insert(m_area->indexOf(view));
    }
    QCOMPARE(indices.size(), 1);

    m_area->setWorkingSet(setName);

    QApplication::processEvents();

    indices.clear();
    for (auto &view : m_area->views()) {
        indices.insert(m_area->indexOf(view));
    }
    QCOMPARE(indices.size(), 4);
    view_bottom_left = m_area->views().at(2);

    // check if the splitter sizes are restored
    splitter_bottom = qobject_cast<QSplitter*>(view_bottom_left->widget()
                                                   ->parentWidget()
                                                   ->parentWidget()   // view container
                                                   ->parentWidget()   // splitter containing a single container
                                                   ->parentWidget()); // splitter containing lower containers
    QVERIFY(splitter_bottom);
    QCOMPARE(splitter_bottom->sizes(), sizes_bottom);

    splitter_mid = qobject_cast<QSplitter*>(splitter_bottom->parentWidget());
    QVERIFY(splitter_mid);
    QCOMPARE(splitter_mid->sizes(), sizes_mid);
}

QTEST_MAIN(TestWorkingSetController)