summaryrefslogtreecommitdiff
path: root/tests/functional-tests/14-signals.py
blob: 44806e8a2cef2c6f60a57878ba3eb6154421146a (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
#!/usr/bin/python3
#
# Copyright (C) 2010, Nokia <ivan.frade@nokia.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.
#
"""
Test that after insertion/remove/updates in the store, the signals
are emitted. Theses tests are not extensive (only few selected signals
are tested)
"""

import unittest as ut
from common.utils.storetest import CommonTrackerStoreTest as CommonTrackerStoreTest
from common.utils import configuration as cfg

from gi.repository import Gio
from gi.repository import GLib
import time

GRAPH_UPDATED_SIGNAL = "GraphUpdated"

SIGNALS_PATH = "/org/freedesktop/Tracker1/Resources"
SIGNALS_IFACE = "org.freedesktop.Tracker1.Resources"

CONTACT_CLASS_URI = "http://www.semanticdesktop.org/ontologies/2007/03/22/nco#PersonContact"

REASONABLE_TIMEOUT = 10  # Time waiting for the signal to be emitted


class TrackerStoreSignalsTests (CommonTrackerStoreTest):
    """
    Insert/update/remove instances from nco:PersonContact
    and check that the signals are emitted
    """

    def setUp(self):
        self.clean_up_list = []

        self.loop = GLib.MainLoop()
        self.timeout_id = 0

        self.bus = Gio.bus_get_sync(Gio.BusType.SESSION, None)

        self.results_classname = None
        self.results_deletes = None
        self.results_inserts = None

    def tearDown(self):
        for uri in self.clean_up_list:
            self.tracker.update("DELETE { <%s> a rdfs:Resource }" % uri)

        self.clean_up_list = []

    def __connect_signal(self):
        """
        After connecting to the signal, call self.__wait_for_signal.
        """
        self.cb_id = self.bus.signal_subscribe(
            sender=cfg.TRACKER_BUSNAME,
            interface_name=SIGNALS_IFACE,
            member=GRAPH_UPDATED_SIGNAL,
            object_path=SIGNALS_PATH,
            arg0=CONTACT_CLASS_URI,
            flags=Gio.DBusSignalFlags.NONE,
            callback=self.__signal_received_cb)

    def __wait_for_signal(self):
        """
        In the callback of the signals, there should be a self.loop.quit ()
        """
        self.timeout_id = GLib.timeout_add_seconds(
            REASONABLE_TIMEOUT, self.__timeout_on_idle)
        self.loop.run()

    def __timeout_on_idle(self):
        self.loop.quit()
        self.fail("Timeout, the signal never came!")

    def __pretty_print_array(self, array):
        for g, s, o, p in array:
            uri, prop, value = self.tracker.query(
                "SELECT tracker:uri (%s), tracker:uri (%s), tracker:uri (%s) WHERE {}" % (s, o, p))
            print(" - (", "-".join([g, uri, prop, value]), ")")

    def __signal_received_cb(self, connection, sender_name, object_path, interface_name, signal_name, parameters):
        """
        Save the content of the signal and disconnect the callback
        """
        classname, deletes, inserts = parameters.unpack()

        self.results_classname = classname
        self.results_deletes = deletes
        self.results_inserts = inserts

        if (self.timeout_id != 0):
            GLib.source_remove(self.timeout_id)
            self.timeout_id = 0
        self.loop.quit()
        self.bus.signal_unsubscribe(self.cb_id)

    def test_01_insert_contact(self):
        self.clean_up_list.append("test://signals-contact-add")
        CONTACT = """
        INSERT {
        <test://signals-contact-add> a nco:PersonContact ;
             nco:nameGiven 'Contact-name added';
             nco:nameFamily 'Contact-family added';
             nie:generator 'test-14-signals' ;
             nco:contactUID '1321321312312';
             nco:hasPhoneNumber <tel:555555555> .
        }
        """
        self.__connect_signal()
        self.tracker.update(CONTACT)
        time.sleep(1)
        self.__wait_for_signal()

        # validate results
        self.assertEqual(len(self.results_deletes), 0)
        self.assertEqual(len(self.results_inserts), 6)

    def test_02_remove_contact(self):
        CONTACT = """
        INSERT {
         <test://signals-contact-remove> a nco:PersonContact ;
             nco:nameGiven 'Contact-name removed';
             nco:nameFamily 'Contact-family removed'.
        }
        """
        self.__connect_signal()
        self.tracker.update(CONTACT)
        self.__wait_for_signal()

        self.__connect_signal()
        self.tracker.update ("""
            DELETE { <test://signals-contact-remove> a rdfs:Resource }
            """)
        self.__wait_for_signal()

        # Validate results:
        self.assertEqual(len(self.results_deletes), 1)
        self.assertEqual(len(self.results_inserts), 0)

    def test_03_update_contact(self):
        self.clean_up_list.append("test://signals-contact-update")

        self.__connect_signal()
        self.tracker.update(
            "INSERT { <test://signals-contact-update> a nco:PersonContact }")
        self.__wait_for_signal()

        self.__connect_signal()
        self.tracker.update(
            "INSERT { <test://signals-contact-update> nco:fullname 'wohoo'}")
        self.__wait_for_signal()

        self.assertEqual(len(self.results_deletes), 0)
        self.assertEqual(len(self.results_inserts), 1)

    def test_04_fullupdate_contact(self):
        self.clean_up_list.append("test://signals-contact-fullupdate")

        self.__connect_signal()
        self.tracker.update(
            "INSERT { <test://signals-contact-fullupdate> a nco:PersonContact; nco:fullname 'first value' }")
        self.__wait_for_signal()

        self.__connect_signal()
        self.tracker.update ("""
               DELETE { <test://signals-contact-fullupdate> nco:fullname ?x }
               WHERE { <test://signals-contact-fullupdate> a nco:PersonContact; nco:fullname ?x }
               
               INSERT { <test://signals-contact-fullupdate> nco:fullname 'second value'}
               """)
        self.__wait_for_signal()

        self.assertEqual(len(self.results_deletes), 1)
        self.assertEqual(len(self.results_inserts), 1)


if __name__ == "__main__":
    ut.main()