File: tst_SessionUtilsTests.cpp

package info (click to toggle)
morph-browser 1.0.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,004 kB
  • sloc: cpp: 11,940; javascript: 570; xml: 102; makefile: 43
file content (135 lines) | stat: -rw-r--r-- 3,222 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
/*
 * Copyright 2014-2015 Canonical Ltd.
 *
 * This file is part of webbrowser-app.
 *
 * webbrowser-app 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; version 3.
 *
 * webbrowser-app is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// Qt
#include <QtCore/QByteArray>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QStandardPaths>
#include <QtTest/QtTest>

// local
#include "session-utils.h"

using namespace SessionUtils;

class SessionUtilsTests : public QObject
{
    Q_OBJECT

public:
    SessionUtilsTests();
    void clearXdgRuntimeDir();
    void startNewSession();

private Q_SLOTS:
    void initTestCase();
    void cleanup();
    void testNoSession();
    void testSingleSession();
    void testSessionRestart();

private:
    QByteArray m_xdgRuntimeDir;
};

SessionUtilsTests::SessionUtilsTests():
    QObject(),
    m_xdgRuntimeDir("/tmp/session-utils-test")
{
}

void SessionUtilsTests::clearXdgRuntimeDir()
{
    QDir xdgRuntimeDir(m_xdgRuntimeDir);
    xdgRuntimeDir.removeRecursively();
    xdgRuntimeDir.mkpath(".");
}

void SessionUtilsTests::startNewSession()
{
    QDir upstartSessionDir(QString(m_xdgRuntimeDir + "/upstart/sessions"));
    upstartSessionDir.mkpath(".");

    QString sessionFile = QString("%1.session").arg(qrand());
    QFile file(upstartSessionDir.filePath(sessionFile));
    file.open(QIODevice::WriteOnly);
    file.close();

    /* Wait for some time, because the times on the files are only as
     * accurate as a second. */
    QTest::qWait(1001);
}

void SessionUtilsTests::initTestCase()
{
    qputenv("XDG_RUNTIME_DIR", m_xdgRuntimeDir);
    clearXdgRuntimeDir();
}

void SessionUtilsTests::cleanup()
{
    clearXdgRuntimeDir();
}

void SessionUtilsTests::testNoSession()
{
    QVERIFY(firstRun("myapp"));
    QVERIFY(firstRun("yourapp"));

    // If the Unity session never started, firstRun() should always return true
    QVERIFY(firstRun("myapp"));
    QVERIFY(firstRun("yourapp"));
}

/* FIXME: The following 2 tests fail when building on Debian bookworm. */
void SessionUtilsTests::testSingleSession()
{
    QSKIP("Test fails when building on Debian bookworm.");

    startNewSession();

    QVERIFY(firstRun("myapp"));
    QVERIFY(firstRun("yourapp"));

    QVERIFY(!firstRun("myapp"));
    QVERIFY(!firstRun("yourapp"));
}

void SessionUtilsTests::testSessionRestart()
{
    QSKIP("Test fails when building on Debian bookworm.");

    startNewSession();

    QVERIFY(firstRun("myapp"));
    QVERIFY(!firstRun("myapp"));

    QTest::qWait(1001);
    startNewSession();
    QVERIFY(firstRun("myapp"));
    QVERIFY(firstRun("yourapp"));

    QVERIFY(!firstRun("myapp"));
    QVERIFY(!firstRun("yourapp"));
}


QTEST_MAIN(SessionUtilsTests)
#include "tst_SessionUtilsTests.moc"