summaryrefslogtreecommitdiff
path: root/tubes/qa/test_manager.cxx
blob: 2e5543ee81d9dffd3c4c8f182ea3ac070d67a60a (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
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * Version: MPL 1.1 / GPLv3+ / LGPLv3+
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License or as specified alternatively below. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Major Contributor(s):
 * Copyright (C) 2012 Red Hat, Inc., Eike Rathke <erack@redhat.com>
 *
 * All Rights Reserved.
 *
 * For minor contributions see the git repository.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
 * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
 * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
 * instead of those above.
 */

#include <sal/types.h>

#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/plugin/TestPlugIn.h>

#include <rtl/bootstrap.hxx>
#include <rtl/string.hxx>
#include <rtl/ustring.hxx>
#include <tubes/collaboration.hxx>
#include <tubes/contact-list.hxx>
#include <tubes/manager.hxx>
#include <unotools/localfilehelper.hxx>

#include <telepathy-glib/telepathy-glib.h>

class TeleConference;

namespace {

class TestTeleTubes: public CppUnit::TestFixture
{
public:

    TestTeleTubes() {}
    ~TestTeleTubes() {}
    // This could happen in costructor wasn't there TestTeleTubes instance for each test:
    void testInitialize();
    void testInitTeleManager();
    void testContactList();
    void testStartBuddySession();
    void testSendPacket();
    void testDestroyTeleTubes();
    void testFailAlways();

    // Order is significant.
    CPPUNIT_TEST_SUITE( TestTeleTubes );
    CPPUNIT_TEST( testInitialize );
    CPPUNIT_TEST( testInitTeleManager );
    CPPUNIT_TEST( testContactList );
    CPPUNIT_TEST( testStartBuddySession );
    CPPUNIT_TEST( testSendPacket );
    CPPUNIT_TEST( testDestroyTeleTubes );
#if 0
    CPPUNIT_TEST( testFailAlways );     // test failure displays SAL_LOG, uncomment for debugging
#endif
    CPPUNIT_TEST_SUITE_END();
};

class TestCollaboration;
// static, not members, so they actually survive cppunit test iteration
static TestCollaboration*   mpCollaboration1 = NULL;
static TestCollaboration*   mpCollaboration2 = NULL;
static TpAccount*           mpOffererAccount = NULL;
static TpContact*           mpAccepterContact = NULL;
//static bool                 mbFileSentSuccess = false;
static bool                 mbPacketReceived = false;
static OUString             maTestConfigIniURL;
static OString              maOffererIdentifier;
static OString              maAccepterIdentifier;

class TestCollaboration : public Collaboration
{
    virtual void EndCollaboration() const {}
    virtual void PacketReceived( const OString& rPacket ) const
    {
        CPPUNIT_ASSERT( rPacket == "from 1 to 2");
        mbPacketReceived = true;
    }
    virtual void SaveAndSendFile( TpContact* ) const {}
    virtual void StartCollaboration( TeleConference* ) {}
};

static gboolean timed_out( void * )
{
    CPPUNIT_ASSERT_MESSAGE( "Test took longer than ten seconds!", false);

    return FALSE;
}

void TestTeleTubes::testInitialize()
{
    utl::LocalFileHelper::ConvertPhysicalNameToURL(
            OUString::createFromAscii( getenv("SRCDIR") ) + "/tubes/qa/test-config.ini",
            maTestConfigIniURL );
    rtl::Bootstrap aTestConfig( maTestConfigIniURL );

    TeleManager::addSuffixToNames( "TeleTest");

    rtl::OUString aOffererIdentifier;
    CPPUNIT_ASSERT_MESSAGE( "See README for how to set up test-config.ini",
        aTestConfig.getFrom("offerer", aOffererIdentifier));
    maOffererIdentifier = OUStringToOString( aOffererIdentifier, RTL_TEXTENCODING_UTF8);

    rtl::OUString aAccepterIdentifier;
    CPPUNIT_ASSERT_MESSAGE( "See README for how to set up test-config.ini",
        aTestConfig.getFrom("accepter", aAccepterIdentifier));
    maAccepterIdentifier = OUStringToOString( aAccepterIdentifier, RTL_TEXTENCODING_UTF8);

    g_timeout_add_seconds (10, timed_out, NULL);
    mpCollaboration1 = new TestCollaboration();
    mpCollaboration2 = new TestCollaboration();
}

void TestTeleTubes::testInitTeleManager()
{
    CPPUNIT_ASSERT( TeleManager::init( true));
}

void TestTeleTubes::testContactList()
{
    AccountContactPairV pairs = TeleManager::getContactList()->getContacts();
    /* Both our accounts are meant to be signed in, and they both should be
     * capable of LibreOffice tubes because this test runs after we register
     * our handler. */
    CPPUNIT_ASSERT_MESSAGE(
        "Make sure both your test accounts are signed in "
        "and are on each other's contact lists",
        pairs.size() > 0 );
    CPPUNIT_ASSERT(!mpAccepterContact);

    for (guint i = 0; i < pairs.size(); i++)
    {
        AccountContactPair pair = pairs[i];

        if (tp_account_get_normalized_name (pair.first) == maOffererIdentifier &&
            tp_contact_get_identifier (pair.second) == maAccepterIdentifier)
        {
            mpOffererAccount = pair.first;
            g_object_ref (mpOffererAccount);
            mpAccepterContact = pair.second;
            g_object_ref (mpAccepterContact);
        }
        g_object_unref (pair.first);
        g_object_unref (pair.second);
    }

    CPPUNIT_ASSERT_MESSAGE(
        "Couldn't find offerer account. "
        "Make sure both your test accounts are signed in "
        "and are on each other's contact lists",
        mpOffererAccount);
    CPPUNIT_ASSERT_MESSAGE(
        "Couldn't find accepter contact. "
        "Make sure both your test accounts are signed in "
        "and are on each other's contact lists",
        mpAccepterContact);
}

/* FIXME: do we need the possibility to pass function to Collaboration::SendFile() ?
static void lcl_FileSent( bool success, void * )
{
    mbFileSentSuccess = success;
}
*/

void TestTeleTubes::testStartBuddySession()
{
    TeleConference* pConference = NULL;
    CPPUNIT_ASSERT( mpOffererAccount != 0);
    CPPUNIT_ASSERT( mpAccepterContact != 0);
    pConference = TeleManager::startBuddySession( mpOffererAccount, mpAccepterContact);
    CPPUNIT_ASSERT( pConference != NULL);
    mpCollaboration1->SetConference( pConference );
    mpCollaboration1->SendFile( mpAccepterContact, maTestConfigIniURL );

    //while (!mbFileSentSuccess)
    //    g_main_context_iteration( NULL, TRUE);

    // This checks that the file was received and msCurrentUUID set (see manager.cxx)
    while (!TeleManager::hasWaitingConference())
        g_main_context_iteration( NULL, TRUE);

    pConference = TeleManager::getConference();
    CPPUNIT_ASSERT( pConference != NULL);
    mpCollaboration2->SetConference( pConference );
}

void TestTeleTubes::testSendPacket()
{
    mpCollaboration1->SendPacket( "from 1 to 2");

    while (!mbPacketReceived)
        g_main_context_iteration( NULL, TRUE);
}

void TestTeleTubes::testDestroyTeleTubes()
{
    if (mpOffererAccount) {
        g_object_unref(mpOffererAccount);
        mpOffererAccount = NULL;
    }
    if (mpAccepterContact) {
        g_object_unref(mpAccepterContact);
        mpAccepterContact = NULL;
    }
    // Closes the TeleConference in destructor:
    delete mpCollaboration1;
    delete mpCollaboration2;

    TeleManager::finalize();
}

void TestTeleTubes::testFailAlways()
{
    CPPUNIT_ASSERT( false);
}

CPPUNIT_TEST_SUITE_REGISTRATION( TestTeleTubes);

}

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */