aboutsummaryrefslogtreecommitdiff
path: root/tests/ut_mremoteaction/ut_mremoteaction.cpp
blob: 1c4aa3b9bedfb187323d2e29f1f0915af5d5e2c1 (plain)
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
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include "ut_mremoteaction.h"
#include <QDBusInterface>
#include <maction_stub.h>
#include "maction_p.h"

bool Ut_MRemoteAction::captureCalls = false;
QString Ut_MRemoteAction::callServiceName;
QString Ut_MRemoteAction::callObjectPath;
QString Ut_MRemoteAction::callInterface;
QList<QString> Ut_MRemoteAction::callMethods;
QList< QList<QVariant> > Ut_MRemoteAction::callArguments;

// MActionPrivate stubs
MActionPrivate::MActionPrivate()
  : location(MAction::EveryLocation), styleAction(false), q_ptr(0)
{
}

MActionPrivate::~MActionPrivate()
{
}

// QDBusInterface stubs (used by MRemoteAction)
QDBusInterface::QDBusInterface(const QString &service, const QString &path, const QString &interface, const QDBusConnection &connection, QObject *parent) : QDBusAbstractInterface(service, path, interface.toUtf8().constData(), connection, parent)
{
    Ut_MRemoteAction::callServiceName = service;
    Ut_MRemoteAction::callObjectPath = path;
    Ut_MRemoteAction::callInterface = interface;
}

QDBusInterface::~QDBusInterface()
{
}

// QDBusAbstractInterface stubs (used by MRemoteAction)
QDBusMessage QDBusAbstractInterface::callWithArgumentList(QDBus::CallMode, const QString &method, const QList<QVariant> &args)
{
    if (Ut_MRemoteAction::captureCalls) {
        Ut_MRemoteAction::callMethods.append(method);
        Ut_MRemoteAction::callArguments.append(args);
    }

    return QDBusMessage();
}

void Ut_MRemoteAction::init()
{
    captureCalls = false;
    callServiceName = QString();
    callObjectPath = QString();
    callInterface = QString();
    callMethods.clear();
    callArguments.clear();
}

void Ut_MRemoteAction::cleanup()
{
}

void Ut_MRemoteAction::initTestCase()
{
}

void Ut_MRemoteAction::cleanupTestCase()
{
}

void Ut_MRemoteAction::testSerialization()
{
    // Create two arguments as QVariants
    QVariant arg1("arg1");
    QVariant arg2(2);

    // Serialize the QVariants into QBuffers
    QBuffer buffer1;
    QBuffer buffer2;
    buffer1.open(QIODevice::ReadWrite);
    buffer2.open(QIODevice::ReadWrite);
    QDataStream stream1(&buffer1);
    QDataStream stream2(&buffer2);
    stream1 << arg1;
    stream2 << arg2;
    buffer1.close();
    buffer2.close();

    // Encode the contents of the QBuffers in Base64
    QString arg1String(buffer1.buffer().toBase64().data());
    QString arg2String(buffer2.buffer().toBase64().data());

    // Test that an empty action serializes into an empty action
    MRemoteAction action1;
    QCOMPARE(action1.toString(), QString());

    // Test that a class deserialized from a string serializes into the same string
    QString action2String("serviceName objectPath interface methodName ");
    action2String.append(arg1String);
    action2String.append(' ');
    action2String.append(arg2String);
    MRemoteAction action2(action2String);
    QCOMPARE(action2.toString(), action2String);

    // Test that passing the parameters separately will result in the same string
    QList<QVariant> args;
    args.append(arg1);
    args.append(arg2);
    MRemoteAction action3("serviceName", "objectPath", "interface", "methodName", args);
    QCOMPARE(action3.toString(), action2String);
}

void Ut_MRemoteAction::testTrigger()
{
    // Create an action with a method call and some QVariant arguments
    QList<QVariant> args;
    args.append(QVariant("arg1"));
    args.append(QVariant(2));
    MRemoteAction action("serviceName", "objectPath", "interface", "methodName", args);
    captureCalls = true;
    action.trigger();

    // Make sure the corrent method was called with the corrent arguments
    QCOMPARE(callServiceName, QString("serviceName"));
    QCOMPARE(callObjectPath, QString("objectPath"));
    QCOMPARE(callInterface, QString("interface"));
    QCOMPARE(callMethods.count(), 1);
    QCOMPARE(callMethods.at(0), QString("methodName"));
    QCOMPARE(callArguments.count(), 1);
    QCOMPARE(callArguments.at(0).count(), 2);
    QCOMPARE(callArguments.at(0).at(0), QVariant("arg1"));
    QCOMPARE(callArguments.at(0).at(1), QVariant(2));
}

QTEST_MAIN(Ut_MRemoteAction)