~3v1n0/unity-2d/secondary-activate-support

« back to all changes in this revision

Viewing changes to libunity-2d-private/tests/gconnectortest.cpp

  • Committer: Marco Trevisan (Treviño)
  • Date: 2011-07-19 15:35:07 UTC
  • mfrom: (615.1.18 unity-2d)
  • Revision ID: mail@3v1n0.net-20110719153507-vw9w6hfhuezzll77
Merging with upstream...

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of unity-2d
 
3
 *
 
4
 * Copyright 2011 Canonical Ltd.
 
5
 *
 
6
 * Authors:
 
7
 * - Aurélien Gâteau <aurelien.gateau@canonical.com>
 
8
 *
 
9
 * This program is free software; you can redistribute it and/or modify
 
10
 * it under the terms of the GNU General Public License as published by
 
11
 * the Free Software Foundation; version 3.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
 */
 
21
 
 
22
// Local
 
23
#include <debug_p.h>
 
24
#include <gconnector.h>
 
25
 
 
26
// Qt
 
27
#include <QtTest>
 
28
 
 
29
// GLib
 
30
#include <gio/gio.h>
 
31
 
 
32
class CallbackSpy {
 
33
public:
 
34
    CallbackSpy()
 
35
    : count(0)
 
36
    {}
 
37
 
 
38
    static void increase(GCancellable*, gpointer data)
 
39
    {
 
40
        CallbackSpy* spy = reinterpret_cast<CallbackSpy*>(data);
 
41
        ++spy->count;
 
42
    }
 
43
 
 
44
    int count;
 
45
};
 
46
 
 
47
class GConnectorTest : public QObject
 
48
{
 
49
    Q_OBJECT
 
50
private Q_SLOTS:
 
51
    void init()
 
52
    {
 
53
        g_type_init();
 
54
    }
 
55
 
 
56
    void testDisconnectAll()
 
57
    {
 
58
        // Create a cancellable object as it is easy to get it to emit a
 
59
        // signal.
 
60
        GCancellable* object = g_cancellable_new();
 
61
 
 
62
        CallbackSpy spy;
 
63
        GConnector connector;
 
64
        connector.connect(object, "cancelled", G_CALLBACK(CallbackSpy::increase), &spy);
 
65
 
 
66
        // Emit signal, check callback gets called
 
67
        g_cancellable_cancel(object);
 
68
        QCOMPARE(spy.count, 1);
 
69
 
 
70
        // Disconnect and emit signal, check callback does *not* get called
 
71
        connector.disconnectAll();
 
72
        g_cancellable_reset(object);
 
73
        g_cancellable_cancel(object);
 
74
        QCOMPARE(spy.count, 1);
 
75
 
 
76
        g_object_unref(object);
 
77
    }
 
78
 
 
79
    void testDeletedInstance()
 
80
    {
 
81
        GCancellable* object = g_cancellable_new();
 
82
 
 
83
        // Create a dummy connection
 
84
        CallbackSpy spy;
 
85
        GConnector connector;
 
86
        connector.connect(object, "cancelled", G_CALLBACK(CallbackSpy::increase), &spy);
 
87
        g_object_unref(object);
 
88
 
 
89
        // Should not crash because connection should have been removed
 
90
        connector.disconnectAll();
 
91
    }
 
92
};
 
93
 
 
94
QTEST_MAIN(GConnectorTest)
 
95
 
 
96
#include "gconnectortest.moc"