Fixing the hand tests after the changeover
[theonering] / hand_tests / generic.py
1 #!/usr/bin/env python
2
3 import sys
4
5 import gobject
6 import dbus.mainloop.glib
7 dbus.mainloop.glib.DBusGMainLoop(set_as_default = True)
8
9 import telepathy
10
11
12 DBUS_PROPERTIES = 'org.freedesktop.DBus.Properties'
13
14
15 def get_registry():
16         reg = telepathy.client.ManagerRegistry()
17         reg.LoadManagers()
18         return reg
19
20
21 def get_connection_manager(reg):
22         cm = reg.GetManager('theonering')
23         return cm
24
25
26 class Action(object):
27
28         def __init__(self):
29                 self._action = None
30
31         def queue_action(self):
32                 pass
33
34         def append_action(self, action):
35                 assert self._action is None
36                 self._action = action
37
38         def _on_done(self):
39                 if self._action is None:
40                         return
41                 self._action.queue_action()
42
43         def _on_error(self, error):
44                 print error
45
46         def _on_generic_message(self, *args):
47                 pass
48
49
50 class DummyAction(Action):
51
52         def queue_action(self):
53                 gobject.idle_add(self._on_done)
54
55
56 class QuitLoop(Action):
57
58         def __init__(self, loop):
59                 super(QuitLoop, self).__init__()
60                 self._loop = loop
61
62         def queue_action(self):
63                 self._loop.quit()
64
65
66 class DisplayParams(Action):
67
68         def __init__(self, cm):
69                 super(DisplayParams, self).__init__()
70                 self._cm = cm
71
72         def queue_action(self):
73                 self._cm[telepathy.interfaces.CONN_MGR_INTERFACE].GetParameters(
74                         'sip',
75                         reply_handler = self._on_done,
76                         error_handler = self._on_error,
77                 )
78
79         def _on_done(self, params):
80                 print "Connection Parameters:"
81                 for name, flags, signature, default in params:
82                         print "\t%s (%s)" % (name, signature),
83
84                         if flags & telepathy.constants.CONN_MGR_PARAM_FLAG_REQUIRED:
85                                 print "required",
86                         if flags & telepathy.constants.CONN_MGR_PARAM_FLAG_REGISTER:
87                                 print "register",
88                         if flags & telepathy.constants.CONN_MGR_PARAM_FLAG_SECRET:
89                                 print "secret",
90                         if flags & telepathy.constants.CONN_MGR_PARAM_FLAG_DBUS_PROPERTY:
91                                 print "dbus-property",
92                         if flags & telepathy.constants.CONN_MGR_PARAM_FLAG_HAS_DEFAULT:
93                                 print "has-default(%s)" % default,
94
95                         print ""
96                 super(DisplayParams, self)._on_done()
97
98
99 class RequestConnection(Action):
100
101         def __init__(self, cm, username, password, forward):
102                 super(RequestConnection, self).__init__()
103                 self._cm = cm
104
105                 self._conn = None
106                 self._serviceName = None
107
108                 self._username = username
109                 self._password = password
110                 self._forward = forward
111
112         @property
113         def conn(self):
114                 return self._conn
115
116         @property
117         def serviceName(self):
118                 return self._serviceName
119
120         def queue_action(self):
121                 self._cm[telepathy.server.CONNECTION_MANAGER].RequestConnection(
122                         'sip',
123                         {
124                                 'account':  self._username,
125                                 'password': self._password,
126                                 'forward':  self._forward,
127                         },
128                         reply_handler = self._on_done,
129                         error_handler = self._on_error,
130                 )
131
132         def _on_done(self, busName, objectPath):
133                 self._serviceName = busName
134                 self._conn = telepathy.client.Connection(busName, objectPath)
135                 super(RequestConnection, self)._on_done()
136
137
138 class Connect(Action):
139
140         def __init__(self, connAction):
141                 super(Connect, self).__init__()
142                 self._connAction = connAction
143
144         def queue_action(self):
145                 self._connAction.conn[telepathy.server.CONNECTION].connect_to_signal(
146                         'StatusChanged',
147                         self._on_change,
148                 )
149                 self._connAction.conn[telepathy.server.CONNECTION].Connect(
150                         reply_handler = self._on_generic_message,
151                         error_handler = self._on_error,
152                 )
153
154         def _on_done(self):
155                 super(Connect, self)._on_done()
156
157         def _on_change(self, status, reason):
158                 if status == telepathy.constants.CONNECTION_STATUS_DISCONNECTED:
159                         print "Disconnected!"
160                         self._conn = None
161                 elif status == telepathy.constants.CONNECTION_STATUS_CONNECTED:
162                         print "Connected"
163                         self._on_done()
164                 elif status == telepathy.constants.CONNECTION_STATUS_CONNECTING:
165                         print "Connecting"
166                 else:
167                         print "Status: %r" % status
168
169
170 class SimplePresenceOptions(Action):
171
172         def __init__(self, connAction):
173                 super(SimplePresenceOptions, self).__init__()
174                 self._connAction = connAction
175
176         def queue_action(self):
177                 self._connAction.conn[DBUS_PROPERTIES].Get(
178                         telepathy.server.CONNECTION_INTERFACE_SIMPLE_PRESENCE,
179                         'Statuses',
180                         reply_handler = self._on_done,
181                         error_handler = self._on_error,
182                 )
183
184         def _on_done(self, statuses):
185                 print "\tAvailable Statuses"
186                 for (key, value) in statuses.iteritems():
187                         print "\t\t - %s" % key
188                 super(SimplePresenceOptions, self)._on_done()
189
190
191 class NullHandle(object):
192
193         @property
194         def handle(self):
195                 return 0
196
197         @property
198         def handles(self):
199                 return []
200
201
202 class UserHandle(Action):
203
204         def __init__(self, connAction):
205                 super(UserHandle, self).__init__()
206                 self._connAction = connAction
207                 self._handle = None
208
209         @property
210         def handle(self):
211                 return self._handle
212
213         @property
214         def handles(self):
215                 return [self._handle]
216
217         def queue_action(self):
218                 self._connAction.conn[telepathy.server.CONNECTION].GetSelfHandle(
219                         reply_handler = self._on_done,
220                         error_handler = self._on_error,
221                 )
222
223         def _on_done(self, handle):
224                 self._handle = handle
225                 super(UserHandle, self)._on_done()
226
227
228 class RequestHandle(Action):
229
230         def __init__(self, connAction, handleType, handleNames):
231                 super(RequestHandle, self).__init__()
232                 self._connAction = connAction
233                 self._handle = None
234                 self._handleType = handleType
235                 self._handleNames = handleNames
236
237         @property
238         def handle(self):
239                 return self._handle
240
241         @property
242         def handles(self):
243                 return [self._handle]
244
245         def queue_action(self):
246                 self._connAction.conn[telepathy.server.CONNECTION].RequestHandles(
247                         self._handleType,
248                         self._handleNames,
249                         reply_handler = self._on_done,
250                         error_handler = self._on_error,
251                 )
252
253         def _on_done(self, handles):
254                 self._handle = handles[0]
255                 super(RequestHandle, self)._on_done()
256
257
258 class RequestChannel(Action):
259
260         def __init__(self, connAction, handleAction, channelType, handleType):
261                 super(RequestChannel, self).__init__()
262                 self._connAction = connAction
263                 self._handleAction = handleAction
264                 self._channel = None
265                 self._channelType = channelType
266                 self._handleType = handleType
267
268         @property
269         def channel(self):
270                 return self._channel
271
272         def queue_action(self):
273                 self._connAction.conn[telepathy.server.CONNECTION].RequestChannel(
274                         self._channelType,
275                         self._handleType,
276                         self._handleAction.handle,
277                         True,
278                         reply_handler = self._on_done,
279                         error_handler = self._on_error,
280                 )
281
282         def _on_done(self, channelObjectPath):
283                 self._channel = telepathy.client.Channel(self._connAction.serviceName, channelObjectPath)
284                 super(RequestChannel, self)._on_done()
285
286
287 class CloseChannel(Action):
288
289         def __init__(self, connAction, chanAction):
290                 super(CloseChannel, self).__init__()
291                 self._connAction = connAction
292                 self._chanAction = chanAction
293                 self._handles = []
294
295         def queue_action(self):
296                 self._chanAction.channel[telepathy.server.CHANNEL].Close(
297                         reply_handler = self._on_done,
298                         error_handler = self._on_error,
299                 )
300
301         def _on_done(self):
302                 super(CloseChannel, self)._on_done()
303
304
305 class ContactHandles(Action):
306
307         def __init__(self, connAction, chanAction):
308                 super(ContactHandles, self).__init__()
309                 self._connAction = connAction
310                 self._chanAction = chanAction
311                 self._handles = []
312
313         @property
314         def handles(self):
315                 return self._handles
316
317         def queue_action(self):
318                 self._chanAction.channel[DBUS_PROPERTIES].Get(
319                         telepathy.server.CHANNEL_INTERFACE_GROUP,
320                         'Members',
321                         reply_handler = self._on_done,
322                         error_handler = self._on_error,
323                 )
324
325         def _on_done(self, handles):
326                 self._handles = list(handles)
327                 super(ContactHandles, self)._on_done()
328
329
330 class SimplePresenceStatus(Action):
331
332         def __init__(self, connAction, handleAction):
333                 super(SimplePresenceStatus, self).__init__()
334                 self._connAction = connAction
335                 self._handleAction = handleAction
336
337         def queue_action(self):
338                 self._connAction.conn[telepathy.server.CONNECTION_INTERFACE_SIMPLE_PRESENCE].GetPresences(
339                         self._handleAction.handles,
340                         reply_handler = self._on_done,
341                         error_handler = self._on_error,
342                 )
343
344         def _on_done(self, aliases):
345                 print "\tPresences:"
346                 for hid, (presenceType, presence, presenceMessage) in aliases.iteritems():
347                         print "\t\t%s:" % hid, presenceType, presence, presenceMessage
348                 super(SimplePresenceStatus, self)._on_done()
349
350
351 class SetSimplePresence(Action):
352
353         def __init__(self, connAction, status, message):
354                 super(SetSimplePresence, self).__init__()
355                 self._connAction = connAction
356                 self._status = status
357                 self._message = message
358
359         def queue_action(self):
360                 self._connAction.conn[telepathy.server.CONNECTION_INTERFACE_SIMPLE_PRESENCE].SetPresence(
361                         self._status,
362                         self._message,
363                         reply_handler = self._on_done,
364                         error_handler = self._on_error,
365                 )
366
367         def _on_done(self):
368                 super(SetSimplePresence, self)._on_done()
369
370
371 class Aliases(Action):
372
373         def __init__(self, connAction, handleAction):
374                 super(Aliases, self).__init__()
375                 self._connAction = connAction
376                 self._handleAction = handleAction
377
378         def queue_action(self):
379                 self._connAction.conn[telepathy.server.CONNECTION_INTERFACE_ALIASING].RequestAliases(
380                         self._handleAction.handles,
381                         reply_handler = self._on_done,
382                         error_handler = self._on_error,
383                 )
384
385         def _on_done(self, aliases):
386                 print "\tAliases:"
387                 for h, alias in zip(self._handleAction.handles, aliases):
388                         print "\t\t", h, alias
389                 super(Aliases, self)._on_done()
390
391
392 class Call(Action):
393
394         def __init__(self, connAction, chanAction, handleAction):
395                 super(Call, self).__init__()
396                 self._connAction = connAction
397                 self._chanAction = chanAction
398                 self._handleAction = handleAction
399
400         def queue_action(self):
401                 self._chanAction.channel[telepathy.server.CHANNEL_TYPE_STREAMED_MEDIA].RequestStreams(
402                         self._handleAction.handle,
403                         [telepathy.constants.MEDIA_STREAM_TYPE_AUDIO],
404                         reply_handler = self._on_done,
405                         error_handler = self._on_error,
406                 )
407
408         def _on_done(self, handle):
409                 print "Call started"
410                 super(Call, self)._on_done()
411
412
413 class SendText(Action):
414
415         def __init__(self, connAction, chanAction, handleAction, messageType, message):
416                 super(SendText, self).__init__()
417                 self._connAction = connAction
418                 self._chanAction = chanAction
419                 self._handleAction = handleAction
420                 self._messageType = messageType
421                 self._message = message
422
423         def queue_action(self):
424                 self._chanAction.channel[telepathy.server.CHANNEL_TYPE_TEXT].Send(
425                         self._messageType,
426                         self._message,
427                         reply_handler = self._on_done,
428                         error_handler = self._on_error,
429                 )
430
431         def _on_done(self,):
432                 print "Message sent"
433                 super(SendText, self)._on_done()
434
435
436 class Sleep(Action):
437
438         def __init__(self, length):
439                 super(Sleep, self).__init__()
440                 self._length = length
441
442         def queue_action(self):
443                 gobject.timeout_add(self._length, self._on_done)
444
445
446 class Block(Action):
447
448         def __init__(self):
449                 super(Block, self).__init__()
450
451         def queue_action(self):
452                 print "Blocking"
453
454         def _on_done(self):
455                 #super(SendText, self)._on_done()
456                 pass
457
458
459 class Disconnect(Action):
460
461         def __init__(self, connAction):
462                 super(Disconnect, self).__init__()
463                 self._connAction = connAction
464
465         def queue_action(self):
466                 self._connAction.conn[telepathy.server.CONNECTION].Disconnect(
467                         reply_handler = self._on_done,
468                         error_handler = self._on_error,
469                 )
470
471
472 if __name__ == '__main__':
473         loop = gobject.MainLoop()
474
475         reg = get_registry()
476         cm = get_connection_manager(reg)
477
478         nullHandle = NullHandle()
479
480         dummy = DummyAction()
481         firstAction = dummy
482         lastAction = dummy
483
484         if True:
485                 dp = DisplayParams(cm)
486                 lastAction.append_action(dp)
487                 lastAction = dp
488
489         if True:
490                 username = sys.argv[1]
491                 password = sys.argv[2]
492                 forward = sys.argv[3]
493                 reqcon = RequestConnection(cm, username, password, forward)
494                 lastAction.append_action(reqcon)
495                 lastAction = reqcon
496
497                 if False:
498                         reqcon = RequestConnection(cm, username, password, forward)
499                         lastAction.append_action(reqcon)
500                         lastAction = reqcon
501
502                 con = Connect(reqcon)
503                 lastAction.append_action(con)
504                 lastAction = con
505
506                 if True:
507                         spo = SimplePresenceOptions(reqcon)
508                         lastAction.append_action(spo)
509                         lastAction = spo
510
511                 if True:
512                         uh = UserHandle(reqcon)
513                         lastAction.append_action(uh)
514                         lastAction = uh
515
516                         ua = Aliases(reqcon, uh)
517                         lastAction.append_action(ua)
518                         lastAction = ua
519
520                         sps = SimplePresenceStatus(reqcon, uh)
521                         lastAction.append_action(sps)
522                         lastAction = sps
523
524                         if False:
525                                 setdnd = SetSimplePresence(reqcon, "dnd", "")
526                                 lastAction.append_action(setdnd)
527                                 lastAction = setdnd
528
529                                 sps = SimplePresenceStatus(reqcon, uh)
530                                 lastAction.append_action(sps)
531                                 lastAction = sps
532
533                                 setdnd = SetSimplePresence(reqcon, "available", "")
534                                 lastAction.append_action(setdnd)
535                                 lastAction = setdnd
536
537                                 sps = SimplePresenceStatus(reqcon, uh)
538                                 lastAction.append_action(sps)
539                                 lastAction = sps
540
541                 if False:
542                         sl = Sleep(10 * 1000)
543                         lastAction.append_action(sl)
544                         lastAction = sl
545
546                 if False:
547                         rclh = RequestHandle(reqcon, telepathy.HANDLE_TYPE_LIST, ["subscribe"])
548                         lastAction.append_action(rclh)
549                         lastAction = rclh
550
551                         rclc = RequestChannel(
552                                 reqcon,
553                                 rclh,
554                                 telepathy.CHANNEL_TYPE_CONTACT_LIST,
555                                 telepathy.HANDLE_TYPE_LIST,
556                         )
557                         lastAction.append_action(rclc)
558                         lastAction = rclc
559
560                         ch = ContactHandles(reqcon, rclc)
561                         lastAction.append_action(ch)
562                         lastAction = ch
563
564                         ca = Aliases(reqcon, ch)
565                         lastAction.append_action(ca)
566                         lastAction = ca
567
568                 if True:
569                         rch = RequestHandle(reqcon, telepathy.HANDLE_TYPE_CONTACT, ["18005558355"]) #(1-800-555-TELL)
570                         lastAction.append_action(rch)
571                         lastAction = rch
572
573                         # making a phone call
574                         if True:
575                                 smHandle = rch
576                                 smHandleType = telepathy.HANDLE_TYPE_CONTACT
577                         else:
578                                 smHandle = nullHandle
579                                 smHandleType = telepathy.HANDLE_TYPE_NONE
580                         rsmc = RequestChannel(
581                                 reqcon,
582                                 smHandle,
583                                 telepathy.CHANNEL_TYPE_STREAMED_MEDIA,
584                                 smHandleType,
585                         )
586                         lastAction.append_action(rsmc)
587                         lastAction = rsmc
588
589                         if False:
590                                 call = Call(reqcon, rsmc, rch)
591                                 lastAction.append_action(call)
592                                 lastAction = call
593
594                         # sending a text
595                         rtc = RequestChannel(
596                                 reqcon,
597                                 rch,
598                                 telepathy.CHANNEL_TYPE_TEXT,
599                                 smHandleType,
600                         )
601                         lastAction.append_action(rtc)
602                         lastAction = rtc
603
604                         if True:
605                                 closechan = CloseChannel(reqcon, rtc)
606                                 lastAction.append_action(closechan)
607                                 lastAction = closechan
608
609                                 rtc = RequestChannel(
610                                         reqcon,
611                                         rch,
612                                         telepathy.CHANNEL_TYPE_TEXT,
613                                         smHandleType,
614                                 )
615                                 lastAction.append_action(rtc)
616                                 lastAction = rtc
617
618                         if False:
619                                 sendtext = SendText(reqcon, rtc, rch, telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL, "Boo!")
620                                 lastAction.append_action(sendtext)
621                                 lastAction = sendtext
622
623                 if False:
624                         bl = Block()
625                         lastAction.append_action(bl)
626                         lastAction = bl
627
628                 if False:
629                         sl = Sleep(30 * 1000)
630                         lastAction.append_action(sl)
631                         lastAction = sl
632
633                 dis = Disconnect(reqcon)
634                 lastAction.append_action(dis)
635                 lastAction = dis
636
637         quitter = QuitLoop(loop)
638         lastAction.append_action(quitter)
639         lastAction = quitter
640
641         firstAction.queue_action()
642         loop.run()