DBusTest.cpp 8.09 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
    Copyright 2010 by Kurt Hindenburg <kurt.hindenburg@gmail.com>

    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.

    This program 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, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301  USA.
*/

// Own
#include "DBusTest.h"
Kurt Hindenburg's avatar
Kurt Hindenburg committed
22
#include "../Session.h"
23 24 25

using namespace Konsole;

26 27
/* Exec a new Konsole and grab its dbus */
void DBusTest::initTestCase()
28 29 30
{
    const QString interfaceName = "org.kde.konsole";
    QDBusConnectionInterface* bus = 0;
31
    QStringList konsoleServices;
32 33 34 35 36 37 38 39 40

    if (!QDBusConnection::sessionBus().isConnected() ||
            !(bus = QDBusConnection::sessionBus().interface()))
        kFatal() << "Session bus not found";

    QDBusReply<QStringList> serviceReply = bus->registeredServiceNames();
    if (!serviceReply.isValid())
        kFatal() << "SessionBus interfaces not available";

41 42
    // Find all current Konsoles' services running
    QStringList allServices = serviceReply;
43 44
    for (QStringList::const_iterator it = allServices.constBegin(),
            end = allServices.constEnd(); it != end; ++it) {
45 46
        const QString service = *it;
        if (service.startsWith(interfaceName))
47 48 49
            konsoleServices << service;
    }

50 51
    // Create a new Konsole with a seperate process id
    int result = KProcess::execute("konsole");
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    if (result)
        kFatal() << "Unable to exec a new Konsole : " << result;

    // Wait for above Konsole to finish starting
#ifdef HAVE_USLEEP
    usleep(5*1000);
#else
    sleep(5);
#endif

    serviceReply = bus->registeredServiceNames();
    if (!serviceReply.isValid())
        kFatal() << "SessionBus interfaces not available";

    // Find dbus service of above Konsole
    allServices = serviceReply;
68 69
    for (QStringList::const_iterator it = allServices.constBegin(),
            end = allServices.constEnd(); it != end; ++it) {
70 71 72 73
        const QString service = *it;
        if (service.startsWith(interfaceName))
            if (!konsoleServices.contains(service))
                _interfaceName = service;
74 75
    }

Kurt Hindenburg's avatar
Kurt Hindenburg committed
76 77 78
    if (_interfaceName.isEmpty()) {
        kFatal() << "This test will only work in a Konsole window with a new PID.  A new Konsole PID can't be found.";
    }
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    //kDebug()<< "Using service: " + _interfaceName.toLatin1();

    _iface = new QDBusInterface(_interfaceName,
                                QLatin1String("/Konsole"),
                                QLatin1String("org.kde.konsole.Konsole"),
                                QDBusConnection::sessionBus(), this);
    QVERIFY(_iface);
    QVERIFY(_iface->isValid());
}

/* Close the Konsole window that was opened to test the DBus interface
 */
void DBusTest::cleanupTestCase()
{
    QVERIFY(_iface);
    QVERIFY(_iface->isValid());

96 97 98
    // Need to take care of when user has CloseAllTabs=False otherwise
    // they will get a popup dialog when we try to close this.
 
99
    QDBusInterface iface(_interfaceName,
100
                         QLatin1String("/konsole/MainWindow_1"),
101 102 103 104 105 106
                         QLatin1String("com.trolltech.Qt.QWidget"));
    if (!iface.isValid())
        kFatal() << "Unable to get a dbus interface to Konsole!";

    QDBusReply<void> instanceReply = iface.call("close");
    if (!instanceReply.isValid())
107
        kFatal() << "Unable to close Konsole :" << instanceReply.error();
108 109 110 111
}

void DBusTest::testSessions()
{
112 113 114
    QDBusReply<void> voidReply;
    QDBusReply<bool> boolReply;
    QDBusReply<QByteArray> arrayReply;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
115
    QDBusReply<QString> stringReply;
116
    QDBusReply<QStringList> listReply;
117

118 119
    QVERIFY(_iface);
    QVERIFY(_iface->isValid());
120 121 122 123 124 125 126

    QDBusInterface iface(_interfaceName,
                         QLatin1String("/Sessions/1"),
                         QLatin1String("org.kde.konsole.Session"));
    QVERIFY(iface.isValid());

    //****************** Test is/set MonitorActivity
127 128 129
    voidReply = iface.call("setMonitorActivity", false);
    QVERIFY(voidReply.isValid());

130 131 132 133 134
    boolReply = iface.call("isMonitorActivity");
    QVERIFY(boolReply.isValid());
    QCOMPARE(boolReply.value(), false);

    voidReply = iface.call("setMonitorActivity", true);
135
    QVERIFY(voidReply.isValid());
136 137 138 139 140 141

    boolReply = iface.call("isMonitorActivity");
    QVERIFY(boolReply.isValid());
    QCOMPARE(boolReply.value(), true);

    //****************** Test is/set MonitorSilence
142 143 144
    voidReply = iface.call("setMonitorSilence", false);
    QVERIFY(voidReply.isValid());

145 146 147 148 149
    boolReply = iface.call("isMonitorSilence");
    QVERIFY(boolReply.isValid());
    QCOMPARE(boolReply.value(), false);

    voidReply = iface.call("setMonitorSilence", true);
150
    QVERIFY(voidReply.isValid());
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

    boolReply = iface.call("isMonitorSilence");
    QVERIFY(boolReply.isValid());
    QCOMPARE(boolReply.value(), true);

    //****************** Test codec and setCodec
    arrayReply = iface.call("codec");
    QVERIFY(arrayReply.isValid());

    // Obtain a list of system's Codecs
    QList<QByteArray> availableCodecs = QTextCodec::availableCodecs();
    for (int i = 0; i < availableCodecs.count(); ++i) {
        boolReply = iface.call("setCodec", availableCodecs[i]);
        QVERIFY(boolReply.isValid());
        QCOMPARE(boolReply.value(), true);

        arrayReply = iface.call("codec");
        QVERIFY(arrayReply.isValid());
        // Compare result with name due to aliases issue
        // Better way to do this?
        QCOMPARE((QTextCodec::codecForName(arrayReply.value()))->name(),
                (QTextCodec::codecForName(availableCodecs[i]))->name());
    }

175 176 177 178 179 180 181 182 183 184 185 186 187 188
    //****************** Test is/set flowControlEnabled
    voidReply = iface.call("setFlowControlEnabled", true);
    QVERIFY(voidReply.isValid());

    boolReply = iface.call("flowControlEnabled");
    QVERIFY(boolReply.isValid());
    QCOMPARE(boolReply.value(), true);

    voidReply = iface.call("setFlowControlEnabled", false);
    QVERIFY(voidReply.isValid());

    boolReply = iface.call("flowControlEnabled");
    QVERIFY(boolReply.isValid());
    QCOMPARE(boolReply.value(), false);
189

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    //****************** Test is/set environment
    listReply = iface.call("environment");
    QVERIFY(listReply.isValid());

    QStringList prevEnv = listReply.value();
    //for (int i = 0; i < prevEnv.size(); ++i)
    //    kDebug()<< prevEnv.at(i).toLocal8Bit().constData() << endl;

    voidReply = iface.call("setEnvironment", QStringList());
    QVERIFY(voidReply.isValid());

    listReply = iface.call("environment");
    QVERIFY(listReply.isValid());
    QCOMPARE(listReply.value(), QStringList());

    voidReply = iface.call("setEnvironment", prevEnv);
    QVERIFY(voidReply.isValid());

    listReply = iface.call("environment");
    QVERIFY(listReply.isValid());
    QCOMPARE(listReply.value(), prevEnv);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
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

    //****************** Test is/set title
    // TODO: Consider checking what is in Profile
    stringReply = iface.call("title", Session::LocalTabTitle);
    QVERIFY(stringReply.isValid());

    //kDebug()<< stringReply.value();
    QString prevLocalTitle = stringReply.value();

    // set title to,  what title should be
    QMap<QString, QString> titleMap;
    titleMap["Shell"] = "Shell";

    // BUG: It appears that Session::LocalTabTitle is set to Shell and
    // doesn't change.  While RemoteTabTitle is actually the LocalTabTitle
    // TODO: Figure out what's going on...
    QMapIterator<QString, QString> i(titleMap);
    while (i.hasNext()) {
        i.next();
        voidReply = iface.call("setTitle", Session::LocalTabTitle, i.key());
        QVERIFY(voidReply.isValid());

        stringReply = iface.call("title", Session::LocalTabTitle);
        QVERIFY(stringReply.isValid());

        QCOMPARE(stringReply.value(), i.value());

    }
239 240 241 242 243 244
}

QTEST_MAIN(DBusTest)

#include "DBusTest.moc"