mainshelltest.cpp 21.7 KB
Newer Older
1 2 3 4 5 6 7 8 9
/***************************************************************************
 *   Copyright (C) 2014 by Albert Astals Cid <aacid@kde.org>               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

10 11
#include <QtTest>

12 13
#include <qprintdialog.h>
#include <qwidget.h>
Laurent Montel's avatar
Laurent Montel committed
14
#include <qtabwidget.h>
15
#include <QStandardPaths>
16
#include <kconfiggroup.h>
17 18 19 20 21 22 23 24 25 26

#include "../shell/okular_main.h"
#include "../shell/shell.h"
#include "../shell/shellutils.h"
#include "../core/document_p.h"
#include "../ui/presentationwidget.h"
#include "../part.h"
#include "../settings.h"

#include <sys/types.h>
27
#ifndef Q_OS_WIN
28
#include <unistd.h>
29 30 31
#else
#include <process.h>
#endif
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

namespace Okular {
class PartTest
{
public:
    Okular::Document *partDocument(Okular::Part *part) const {
        return part->m_document;
    }
    QWidget *presentationWidget(Okular::Part *part) const {
        return part->m_presentationWidget;
    }
};
}

class ClosePrintDialogHelper : public QObject
{
    Q_OBJECT

public:
    ClosePrintDialogHelper(int expectedTab) : foundDialog(false), m_expectedTab(expectedTab) { }
    bool foundDialog;

private slots:
    void closePrintDialog();

private:
    int m_expectedTab;
};

class MainShellTest : public QObject, public Okular::PartTest
{
    Q_OBJECT

public:
Laurent Montel's avatar
Laurent Montel committed
66
    static QTabWidget* tabWidget(Shell *s)
67 68 69 70 71 72 73 74 75 76 77 78
    {
        return s->m_tabWidget;
    }

private slots:
    void initTestCase();
    void cleanupTestCase();
    void init();
    void cleanup();

    void testShell_data();
    void testShell();
79 80
    void testFileRemembersPagePosition_data();
    void testFileRemembersPagePosition();
81 82
    void test2FilesError_data();
    void test2FilesError();
83 84
    void testSessionRestore_data();
    void testSessionRestore();
85 86 87 88

private:
};

89 90 91 92 93 94 95 96 97 98 99 100 101 102
QList<Shell*> getShells()
{
    QList<Shell*> shells;
    foreach( KMainWindow* kmw, KMainWindow::memberList() )
    {
        Shell* shell = qobject_cast<Shell*>( kmw );
        if( shell )
        {
            shells.append( shell );
        }
    }
    return shells;
}

103 104 105 106 107 108 109 110 111 112 113 114 115
Shell *findShell(Shell *ignore = 0)
{
    foreach (QWidget *widget, QApplication::topLevelWidgets())
    {
        Shell *s = qobject_cast<Shell*>(widget);
        if (s && s != ignore)
            return s;
    }
    return 0;
}

void MainShellTest::initTestCase()
{
116
    QStandardPaths::setTestModeEnabled(true);
117
    // Don't pollute people's okular settings
Laurent Montel's avatar
Laurent Montel committed
118
    Okular::Settings::instance( QStringLiteral("mainshelltest") );
119 120 121 122

    // Register in bus as okular
    QDBusConnectionInterface *bus = QDBusConnection::sessionBus().interface();
    const QString myPid = QString::number( getpid() );
123
    const QString serviceName = QStringLiteral("org.kde.okular-")+ myPid;
124 125 126
    QVERIFY( bus->registerService(serviceName) == QDBusConnectionInterface::ServiceRegistered );

    // Tell the presentationWidget to not be annoying
127
    KSharedConfigPtr c = KSharedConfig::openConfig();
128 129 130 131 132 133 134 135 136 137 138 139 140 141
    KConfigGroup cg = c->group("Notification Messages");
    cg.writeEntry("presentationInfo", false);
}

void MainShellTest::cleanupTestCase()
{
}

void MainShellTest::init()
{
    // Default settings for every test
    Okular::Settings::self()->setDefaults();

    // Clean docdatas
Lukáš Tinkl's avatar
Lukáš Tinkl committed
142
    QList<QUrl> urls;
Laurent Montel's avatar
Laurent Montel committed
143 144 145
    urls << QUrl::fromUserInput(QStringLiteral("file://" KDESRCDIR "data/file1.pdf"));
    urls << QUrl::fromUserInput(QStringLiteral("file://" KDESRCDIR "data/tocreload.pdf"));
    urls << QUrl::fromUserInput(QStringLiteral("file://" KDESRCDIR "data/contents.epub"));
146

Lukáš Tinkl's avatar
Lukáš Tinkl committed
147
    foreach (const QUrl &url, urls)
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
    {
        QFileInfo fileReadTest( url.toLocalFile() );
        const QString docDataPath = Okular::DocumentPrivate::docDataFileName(url, fileReadTest.size());
        QFile::remove(docDataPath);
    }
}

void MainShellTest::cleanup()
{
    Shell *s;
    while ((s = findShell()))
    {
        delete s;
    }
}

void MainShellTest::testShell_data()
{
    QTest::addColumn<QStringList>("paths");
    QTest::addColumn<QString>("serializedOptions");
    QTest::addColumn<bool>("useTabs");
    QTest::addColumn<QString>("externalProcessPath");
    QTest::addColumn<uint>("expectedPage");
    QTest::addColumn<bool>("expectPresentation");
    QTest::addColumn<bool>("expectPrintDialog");
    QTest::addColumn<bool>("unique");
    QTest::addColumn<uint>("externalProcessExpectedPage");
    QTest::addColumn<bool>("externalProcessExpectPresentation");
    QTest::addColumn<bool>("externalProcessExpectPrintDialog");

178 179
    const QStringList contentsEpub = QStringList(QStringLiteral(KDESRCDIR "data/contents.epub"));
    const QStringList file1 = QStringList(QStringLiteral(KDESRCDIR "data/file1.pdf"));
180
    QStringList file1AndToc;
181 182 183
    file1AndToc << QStringLiteral(KDESRCDIR "data/file1.pdf");
    file1AndToc << QStringLiteral(KDESRCDIR "data/tocreload.pdf");
    const QString tocReload = QStringLiteral(KDESRCDIR "data/tocreload.pdf");
184

Laurent Montel's avatar
Laurent Montel committed
185 186
    const QString optionsPage2 = ShellUtils::serializeOptions(false, false, false, false, QStringLiteral("2"));
    const QString optionsPage2Presentation = ShellUtils::serializeOptions(true, false, false, false, QStringLiteral("2"));
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
    const QString optionsPrint = ShellUtils::serializeOptions(false, true, false, false, QString());
    const QString optionsUnique = ShellUtils::serializeOptions(false, false, true, false, QString());

    QTest::newRow("just show shell") << QStringList() << QString() << false << QString() << 0u << false << false << false << 0u << false << false;
    QTest::newRow("open file") << file1 << QString() << false << QString() << 0u << false << false << false << 0u << false << false;
    QTest::newRow("two files no tabs") << file1AndToc << QString() << false << QString() << 0u << false << false << false << 0u << false << false;
    QTest::newRow("two files with tabs") << file1AndToc << QString() << true << QString() << 0u << false << false << false << 0u << false << false;
    QTest::newRow("two files sequence no tabs") << file1 << QString() << false << tocReload << 0u << false << false << false << 0u << false << false;
    QTest::newRow("two files sequence with tabs") << file1 << QString() << true << tocReload << 0u << false << false << false << 0u << false << false;
    QTest::newRow("open file page number") << contentsEpub << optionsPage2 << false << QString() << 1u << false << false << false << 0u << false << false;
    QTest::newRow("open file page number and presentation") << contentsEpub << optionsPage2Presentation << false << QString() << 1u << true << false << false << 0u << false << false;
    QTest::newRow("open file print") << file1 << optionsPrint << false << QString() << 0u << false << true << false << 0u << false << false;
    QTest::newRow("open two files unique") << file1 << optionsUnique << false << tocReload << 0u << false << false << true << 0u << false << false;
    QTest::newRow("open two files unique tabs") << file1 << optionsUnique << true << tocReload << 0u << false << false << true << 0u << false << false;
    QTest::newRow("page number attach tabs") << file1 << QString() << true << contentsEpub[0] << 0u << false << false << false << 2u << false << false;
    QTest::newRow("presentation attach tabs") << file1 << QString() << true << contentsEpub[0] << 0u << false << false << false << 2u << true << false;
    QTest::newRow("print attach tabs") << file1 << QString() << true << contentsEpub[0] << 0u << false << true << false << 2u << false << true;
    QTest::newRow("page number attach unique") << file1 << optionsUnique << false << contentsEpub[0] << 0u << false << false << true << 3u << false << false;
    QTest::newRow("presentation attach unique") << file1 << optionsUnique << false << contentsEpub[0] << 0u << false << false << true << 2u << true << false;
    QTest::newRow("print attach unique") << file1 << optionsUnique << false << contentsEpub[0] << 0u << false << false << true << 2u << false << true;
    QTest::newRow("page number attach unique tabs") << file1 << optionsUnique << true << contentsEpub[0] << 0u << false << false << true << 3u << false << false;
    QTest::newRow("presentation attach unique tabs") << file1 << optionsUnique << true << contentsEpub[0] << 0u << false << false << true << 2u << true << false;
    QTest::newRow("print attach unique tabs") << file1 << optionsUnique << true << contentsEpub[0] << 0u << false << false << true << 2u << false << true;
}

void MainShellTest::testShell()
{
    QFETCH(QStringList, paths);
    QFETCH(QString, serializedOptions);
    QFETCH(bool, useTabs);
    QFETCH(QString, externalProcessPath);
    QFETCH(uint, expectedPage);
    QFETCH(bool, expectPresentation);
    QFETCH(bool, expectPrintDialog);
    QFETCH(bool, unique);
    QFETCH(uint, externalProcessExpectedPage);
    QFETCH(bool, externalProcessExpectPresentation);
    QFETCH(bool, externalProcessExpectPrintDialog);

    QScopedPointer<ClosePrintDialogHelper> helper;

    Okular::Settings::self()->setShellOpenFileInTabs(useTabs);

    if (expectPrintDialog || externalProcessExpectPrintDialog) {
        const int expectedTab = externalProcessExpectPrintDialog && !unique ? 1 : 0;
        helper.reset(new ClosePrintDialogHelper(expectedTab));
        QTimer::singleShot(0, helper.data(), SLOT(closePrintDialog()));
    }

    Okular::Status status = Okular::main(paths, serializedOptions);
    QCOMPARE(status, Okular::Success);
    Shell *s = findShell();
    QVERIFY(s);

    if (paths.count() == 1)
    {
        QCOMPARE(s->m_tabs.count(), 1);
        Okular::Part *part = s->findChild<Okular::Part*>();
        QVERIFY(part);
246
        QCOMPARE(part->url().url(), QStringLiteral("file://%1").arg(paths[0]));
247 248 249 250 251 252 253 254 255 256 257 258
        QCOMPARE(partDocument(part)->currentPage(), expectedPage);
    }
    else if (paths.count() == 2)
    {
        if (useTabs)
        {
            QSet<QString> openUrls;
            Shell *s = findShell();
            QVERIFY(s);
            Okular::Part *part = dynamic_cast<Okular::Part*>(s->m_tabs[0].part);
            Okular::Part *part2 = dynamic_cast<Okular::Part*>(s->m_tabs[1].part);
            QCOMPARE(s->m_tabs.count(), 2);
259 260
            QCOMPARE(part->url().url(), QStringLiteral("file://%1").arg(paths[0]));
            QCOMPARE(part2->url().url(), QStringLiteral("file://%1").arg(paths[1]));
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
            QCOMPARE(partDocument(part)->currentPage(), expectedPage);
            QCOMPARE(partDocument(part2)->currentPage(), expectedPage);
        }
        else
        {
            QSet<QString> openUrls;
            Shell *s = findShell();
            QVERIFY(s);
            QCOMPARE(s->m_tabs.count(), 1);
            Okular::Part *part = s->findChild<Okular::Part*>();
            QVERIFY(part);
            QCOMPARE(partDocument(part)->currentPage(), expectedPage);
            openUrls << part->url().url();

            Shell *s2 = findShell(s);
            QVERIFY(s2);
            QCOMPARE(s2->m_tabs.count(), 1);
            Okular::Part *part2 = s2->findChild<Okular::Part*>();
            QVERIFY(part2);
            QCOMPARE(partDocument(part2)->currentPage(), expectedPage);
            openUrls << part2->url().url();

            foreach(const QString &path, paths)
            {
285
                QVERIFY(openUrls.contains(QStringLiteral("file://%1").arg(path)));
286 287 288 289 290 291 292 293 294
            }
        }
    }

    if (!externalProcessPath.isEmpty())
    {
        Okular::Part *part = s->findChild<Okular::Part*>();

        QProcess p;
295 296
        QStringList args;
        args << externalProcessPath;
297
        if (unique)
Laurent Montel's avatar
Laurent Montel committed
298
            args << QStringLiteral("-unique");
299
        if (externalProcessExpectedPage != 0)
300
            args << QStringLiteral("-page") << QString::number(externalProcessExpectedPage + 1);
301
        if (externalProcessExpectPresentation)
302
            args << QStringLiteral("-presentation");
303
        if (externalProcessExpectPrintDialog)
304
            args << QStringLiteral("-print");
305
        p.start(QStringLiteral(OKULAR_BINARY), args);
306 307 308 309 310 311
        p.waitForStarted();
        QCOMPARE(p.state(), QProcess::Running);

        if (useTabs || unique)
        {
            // It is attaching to us, so will eventually stop
Albert Astals Cid's avatar
Albert Astals Cid committed
312
            QTRY_COMPARE_WITH_TIMEOUT(p.state(), QProcess::NotRunning, 20000);
313 314 315 316 317 318 319
            QCOMPARE(p.exitStatus(), QProcess::NormalExit);
            QCOMPARE(p.exitCode(), 0);

            if (unique)
            {
                // It is unique so part got "overriten"
                QCOMPARE(s->m_tabs.count(), 1);
320
                QCOMPARE(part->url().url(), QStringLiteral("file://%1").arg(externalProcessPath));
321 322 323 324 325 326 327
                QCOMPARE(partDocument(part)->currentPage(), externalProcessExpectedPage);
            }
            else
            {
                // It is attaching to us so a second tab is there
                QCOMPARE(s->m_tabs.count(), 2);
                Okular::Part *part2 = dynamic_cast<Okular::Part*>(s->m_tabs[1].part);
328
                QCOMPARE(part2->url().url(), QStringLiteral("file://%1").arg(externalProcessPath));
329 330 331 332 333 334 335 336 337 338 339
                QCOMPARE(partDocument(part2)->currentPage(), externalProcessExpectedPage);
            }
        }
        else
        {
            QTest::qWait(750);

            // It opened on a new process, so it is still running, we need to kill it
            QCOMPARE(p.state(), QProcess::Running);
            p.terminate();
            p.waitForFinished();
340
            QVERIFY(p.state() != QProcess::Running);
341 342
            // It opened on a new process, so no change for us
            QCOMPARE(s->m_tabs.count(), 1);
343
            QCOMPARE(part->url().url(), QStringLiteral("file://%1").arg(paths[0]));
344 345 346 347 348 349 350 351
            QCOMPARE(partDocument(part)->currentPage(), externalProcessExpectedPage);
        }
    }

    if (expectPresentation)
    {
        QCOMPARE(paths.count(), 1);
        Okular::Part *part = s->findChild<Okular::Part*>();
352
        QTRY_VERIFY(presentationWidget(part) != 0);
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    }

    if (externalProcessExpectPresentation)
    {
        Okular::Part *part;
        if (unique)
        {
            QCOMPARE(s->m_tabs.count(), 1);
            part = dynamic_cast<Okular::Part*>(s->m_tabs[0].part);
        }
        else
        {
            QCOMPARE(s->m_tabs.count(), 2);
            part = dynamic_cast<Okular::Part*>(s->m_tabs[1].part);
        }

369
        QTRY_VERIFY(presentationWidget(part) != 0);
370 371 372 373 374 375 376 377 378 379 380 381 382
    }

    if (helper)
    {
        QVERIFY(helper->foundDialog);
    }
}

void ClosePrintDialogHelper::closePrintDialog()
{
    Shell *s = findShell();
    QPrintDialog *dialog = s->findChild<QPrintDialog*>();
    if (!dialog) {
Laurent Montel's avatar
Laurent Montel committed
383
        QTimer::singleShot(0, this, &ClosePrintDialogHelper::closePrintDialog);
384 385 386 387 388 389 390 391
        return;
    }
    QVERIFY(dialog);
    QCOMPARE(MainShellTest::tabWidget(s)->currentIndex(), m_expectedTab);
    dialog->close();
    foundDialog = true;
}

392 393 394 395 396 397 398 399 400 401 402 403 404
void MainShellTest::testFileRemembersPagePosition_data()
{
    QTest::addColumn<int>("mode");

    QTest::newRow("normal") << 1;
    QTest::newRow("unique") << 2;
    QTest::newRow("tabs") << 3;
}

void MainShellTest::testFileRemembersPagePosition()
{
    QFETCH(int, mode);

405
    const QStringList paths = QStringList(QStringLiteral(KDESRCDIR "data/contents.epub"));
406 407 408 409 410 411 412 413 414 415 416 417 418 419
    QString serializedOptions;
    if (mode == 1 || mode == 3)
        serializedOptions = ShellUtils::serializeOptions(false, false, false, false, QString());
    else
        serializedOptions = ShellUtils::serializeOptions(false, false, true, false, QString());

    Okular::Settings::self()->setShellOpenFileInTabs(mode == 3);

    Okular::Status status = Okular::main(paths, serializedOptions);
    QCOMPARE(status, Okular::Success);
    Shell *s = findShell();
    QVERIFY(s);
    Okular::Part *part = s->findChild<Okular::Part*>();
    QVERIFY(part);
420
    QCOMPARE(part->url().url(), QStringLiteral("file://%1").arg(paths[0]));
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
    QCOMPARE(partDocument(part)->currentPage(), 0u);
    partDocument(part)->setViewportPage(3);
    QCOMPARE(partDocument(part)->currentPage(), 3u);
    s->closeUrl();
    QCOMPARE(part->url().url(), QString());

    if (mode == 1)
    {
        delete s;
        status = Okular::main(paths, serializedOptions);
        QCOMPARE(status, Okular::Success);
    }
    else
    {
        QProcess p;
436 437
        QStringList args;
        args << paths[0];
438
        if (mode == 2)
Laurent Montel's avatar
Laurent Montel committed
439
            args << QStringLiteral("-unique");
440
        p.start(QStringLiteral(OKULAR_BINARY), args);
441 442 443 444
        p.waitForStarted();
        QCOMPARE(p.state(), QProcess::Running);

        // It is attaching to us, so will eventually stop
Albert Astals Cid's avatar
Albert Astals Cid committed
445
        QTRY_COMPARE_WITH_TIMEOUT((int)p.state(), (int)QProcess::NotRunning, 20000);
446
        QCOMPARE((int)p.exitStatus(), (int)QProcess::NormalExit);
447 448 449 450 451 452
        QCOMPARE(p.exitCode(), 0);
    }
    s = findShell();
    QVERIFY(s);
    part = s->findChild<Okular::Part*>();
    QVERIFY(part);
453
    QCOMPARE(part->url().url(), QStringLiteral("file://%1").arg(paths[0]));
454 455 456
    QCOMPARE(partDocument(part)->currentPage(), 3u);
}

457
void MainShellTest::test2FilesError_data()
458
{
459 460 461 462 463
    QTest::addColumn<QString>("serializedOptions");

    QTest::newRow("startInPresentation") << ShellUtils::serializeOptions(true, false, false, false, QString());
    QTest::newRow("showPrintDialog") << ShellUtils::serializeOptions(false, true, false, false, QString());
    QTest::newRow("unique") << ShellUtils::serializeOptions(false, false, true, false, QString());
Laurent Montel's avatar
Laurent Montel committed
464
    QTest::newRow("pageNumger") << ShellUtils::serializeOptions(false, false, false, false, QStringLiteral("3"));
465 466 467 468 469 470
}

void MainShellTest::test2FilesError()
{
    QFETCH(QString, serializedOptions);

471
    QStringList paths;
472
    paths << QStringLiteral(KDESRCDIR "data/file1.pdf") << QStringLiteral(KDESRCDIR "data/tocreload.pdf");
473 474 475 476 477 478 479 480
    Okular::Status status = Okular::main(paths, serializedOptions);
    QCOMPARE(status, Okular::Error);

    QSet<QString> openUrls;
    Shell *s = findShell();
    QVERIFY(!s);
}

481 482 483 484 485 486 487
void MainShellTest::testSessionRestore_data()
{
    QTest::addColumn<QStringList>("paths");
    QTest::addColumn<QString>("options");
    QTest::addColumn<bool>("useTabsOpen");
    QTest::addColumn<bool>("useTabsRestore");

488
    QStringList oneDocPaths(QStringLiteral( KDESRCDIR "data/file1.pdf" ) );
489
    QStringList twoDocPaths( oneDocPaths );
490
    twoDocPaths << QStringLiteral(KDESRCDIR "data/formSamples.pdf");
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519

    const QString options = ShellUtils::serializeOptions(false, false, false, false, QString());

    QTest::newRow("1 doc, 1 window, tabs")      << oneDocPaths << options << true  << true;
    QTest::newRow("2 docs, 1 window, tabs")     << twoDocPaths << options << true  << true;
    QTest::newRow("2 docs, 2 windows, tabs")    << twoDocPaths << options << false << true;
    QTest::newRow("2 docs, 2 windows, no tabs") << twoDocPaths << options << false << false;
    QTest::newRow("2 docs, 1 window, no tabs")  << twoDocPaths << options << true  << false;
}

void MainShellTest::testSessionRestore()
{
    QFETCH( QStringList, paths );
    QFETCH( QString, options );
    QFETCH( bool, useTabsOpen );
    QFETCH( bool, useTabsRestore );

    Okular::Settings::self()->setShellOpenFileInTabs( useTabsOpen );

    Okular::Status status = Okular::main( paths, options );
    QCOMPARE( status, Okular::Success );

    // Gather some information about the state
    // Verify that the correct number of windows/tabs were opened
    QList<Shell*> shells = getShells();
    QVERIFY( !shells.isEmpty() );
    int numDocs = 0;
    foreach( Shell* shell, shells )
    {
520
        QTest::qWaitForWindowExposed( shell );
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
        numDocs += shell->m_tabs.size();
    }

    QCOMPARE( numDocs, paths.size() );
    QCOMPARE( shells.size(), useTabsOpen ? 1 : paths.size() );
    QTest::qWait( 100 );

    // Simulate session shutdown. The actual shutdown path comes through
    // QSessionManager XSMP handlers, then KApplication::commitData/saveState,
    // then KMWSessionManager::commitData/saveState. Without simulating an X
    // session manager, the best we can do here is to make a temporary Config
    // and call KMainWindows save functions directly.
    QTemporaryFile configFile;
    QVERIFY( configFile.open() );

    int numWindows = 0;
    {   // Scope for config so that we can reconstruct from file
        KConfig config( configFile.fileName(), KConfig::SimpleConfig );
        foreach( Shell* shell, shells )
        {
            shell->savePropertiesInternal( &config, ++numWindows );
            // Windows aren't necessarily closed on shutdown, but we'll use
            // this as a way to trigger the destructor code, which is normally
            // connected to the aboutToQuit signal
            shell->close();
        }
    }

    // Wait for shells to delete themselves. QTest::qWait doesn't do deferred
    // deletions so we'll set up a full event loop to do that.
    QEventLoop eventLoop;
Laurent Montel's avatar
Laurent Montel committed
552
    QTimer::singleShot( 100, &eventLoop, &QEventLoop::quit );
553
    eventLoop.exec( QEventLoop::AllEvents );
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
    shells = getShells();
    QVERIFY( shells.isEmpty() );

    Okular::Settings::self()->setShellOpenFileInTabs( useTabsRestore );

    // Simulate session restore. We can't call KMainWindow::restore() directly
    // because it asks for info from the session manager, which doesn't know
    // about our temporary config. But the logic here mostly mirrors restore().
    KConfig config( configFile.fileName(), KConfig::SimpleConfig );
    for( int i = 1; i <= numWindows; ++i )
    {
        Shell* shell = new Shell;
        shell->readPropertiesInternal( &config, i );
        shell->show();
    }

    // Verify that the restore state is reasonable
    shells = getShells();
    QVERIFY( !shells.isEmpty() );
    numDocs = 0;
    foreach( Shell* shell, shells )
    {
576
        QTest::qWaitForWindowExposed( shell );
577 578 579 580 581 582
        numDocs += shell->m_tabs.size();
    }

    QCOMPARE( numDocs, paths.size() );
    QCOMPARE( shells.size(), useTabsRestore ? numWindows : paths.size() );
}
583

584
QTEST_MAIN( MainShellTest )
585
#include "mainshelltest.moc"