~iahmad/messaging-app/autopilot_test_helpers

« back to all changes in this revision

Viewing changes to tests/autopilot/messaging_app/helpers.py

  • Committer: Iftikhar Ahmad
  • Date: 2014-07-14 11:50:05 UTC
  • mfrom: (127.2.12 messaging-app)
  • Revision ID: iftikhar.ahmad@canonical.com-20140714115005-92zqol5v9amyhl7p
catch up with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
import os
12
12
import shutil
13
13
import tempfile
 
14
import subprocess
 
15
import sys
 
16
import time
 
17
from dbus import exceptions
14
18
 
15
19
 
16
20
def receive_sms(sender, text):
38
42
    script_proxy.SetPath(script_dir)
39
43
    script_proxy.Run("sms.js")
40
44
    shutil.rmtree(script_dir)
 
45
 
 
46
 
 
47
def get_phonesim():
 
48
    bus = dbus.SystemBus()
 
49
    try:
 
50
        manager = dbus.Interface(bus.get_object('org.ofono', '/'),
 
51
                                 'org.ofono.Manager')
 
52
    except dbus.exceptions.DBusException:
 
53
        return False
 
54
 
 
55
    modems = manager.GetModems()
 
56
 
 
57
    for path, properties in modems:
 
58
        if path == '/phonesim':
 
59
            return properties
 
60
 
 
61
    return None
 
62
 
 
63
 
 
64
def is_phonesim_running():
 
65
    """Determine whether we are running with phonesim."""
 
66
    phonesim = get_phonesim()
 
67
    return phonesim is not None
 
68
 
 
69
 
 
70
def ensure_ofono_account():
 
71
    # oFono modems are now set online by NetworkManager, so for the tests
 
72
    # we need to manually put them online.
 
73
    subprocess.check_call(['/usr/share/ofono/scripts/enable-modem',
 
74
                           '/phonesim'])
 
75
    subprocess.check_call(['/usr/share/ofono/scripts/online-modem',
 
76
                           '/phonesim'])
 
77
 
 
78
    # wait until the modem is actually online
 
79
    for index in range(10):
 
80
        phonesim = get_phonesim()
 
81
        if phonesim['Online'] == 1:
 
82
            break
 
83
        time.sleep(1)
 
84
    else:
 
85
        raise exceptions.RuntimeError("oFono simulator didn't get online.")
 
86
 
 
87
    # this is a bit drastic, but sometimes mission-control-5 won't recognize
 
88
    # clients installed after it was started, so, we make sure it gets
 
89
    # restarted
 
90
    subprocess.check_call(['pkill', '-9', 'mission-control'])
 
91
 
 
92
    if not _is_ofono_account_set():
 
93
        subprocess.check_call(['ofono-setup'])
 
94
        if not _is_ofono_account_set():
 
95
            sys.stderr.write('ofono-setup failed to create ofono account!\n')
 
96
            sys.exit(1)
 
97
 
 
98
 
 
99
def _is_ofono_account_set():
 
100
    mc_tool = subprocess.Popen(
 
101
        [
 
102
            'mc-tool',
 
103
            'list',
 
104
        ], stdout=subprocess.PIPE, universal_newlines=True)
 
105
    mc_accounts = mc_tool.communicate()[0]
 
106
    return 'ofono/ofono/account' in mc_accounts