~boiko/dialer-app/fix_call_duration

« back to all changes in this revision

Viewing changes to tests/autopilot/dialer_app/tests/test_calls.py

  • Committer: Tarmac
  • Author(s): Martin Pitt, Martin Pitt
  • Date: 2013-10-10 19:14:09 UTC
  • mfrom: (50.5.16 dialer-app)
  • Revision ID: tarmac-20131010191409-ubzi4gwlovfha81m
Add some incoming/outgoing call tests, using ofono-phonesim.

Approved by Gustavo Pichorim Boiko, PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
# Copyright 2013 Canonical
 
3
# Author: Martin Pitt <martin.pitt@ubuntu.com>
 
4
#
 
5
# This file is part of dialer-app.
 
6
#
 
7
# dialer-app is free software: you can redistribute it and/or modify it
 
8
# under the terms of the GNU General Public License version 3, as published
 
9
# by the Free Software Foundation.
 
10
 
 
11
"""Tests for the Dialer App using ofono-phonesim"""
 
12
 
 
13
from __future__ import absolute_import
 
14
 
 
15
import subprocess
 
16
import os
 
17
import time
 
18
 
 
19
from autopilot.matchers import Eventually
 
20
from testtools.matchers import Equals, NotEquals
 
21
from testtools import skipUnless
 
22
 
 
23
from dialer_app.tests import DialerAppTestCase
 
24
 
 
25
# determine whether we are running with phonesim
 
26
try:
 
27
    out = subprocess.check_output(["/usr/share/ofono/scripts/list-modems"],
 
28
                                  stderr=subprocess.PIPE)
 
29
    have_phonesim = out.startswith("[ /phonesim ]")
 
30
except subprocess.CalledProcessError:
 
31
    have_phonesim = False
 
32
 
 
33
 
 
34
@skipUnless(have_phonesim,
 
35
            "this test needs to run under with-ofono-phonesim")
 
36
class TestCalls(DialerAppTestCase):
 
37
    """Tests for simulated phone calls."""
 
38
 
 
39
    def setUp(self):
 
40
        # provide clean history
 
41
        self.history = os.path.expanduser(
 
42
            "~/.local/share/history-service/history.sqlite")
 
43
        if os.path.exists(self.history):
 
44
            subprocess.call(["pkill", "history-daemon"])
 
45
            os.rename(self.history, self.history + ".orig")
 
46
 
 
47
        super(TestCalls, self).setUp()
 
48
        self.entry = self.main_view.dialer_page.get_keypad_entry()
 
49
        self.call_button = self.main_view.dialer_page.get_call_button()
 
50
        self.hangup_button = None
 
51
 
 
52
        # should have an empty history at the beginning of each test
 
53
        self.history_list = self.app.select_single(objectName="historyList")
 
54
        self.assertThat(self.history_list.visible, Equals(False))
 
55
        self.assertThat(self.history_list.count, Equals(0))
 
56
 
 
57
        self.keys = []
 
58
        for i in range(10):
 
59
            self.keys.append(self.main_view.dialer_page.get_keypad_key(str(i)))
 
60
 
 
61
    def tearDown(self):
 
62
        super(TestCalls, self).tearDown()
 
63
 
 
64
        # ensure that there are no leftover calls in case of failed tests
 
65
        subprocess.call(["/usr/share/ofono/scripts/hangup-all"])
 
66
 
 
67
        # restore history
 
68
        if os.path.exists(self.history + ".orig"):
 
69
            subprocess.call(["pkill", "history-daemon"])
 
70
            os.rename(self.history + ".orig", self.history)
 
71
 
 
72
    def test_outgoing_noanswer(self):
 
73
        """Outgoing call to a normal number, no answer"""
 
74
 
 
75
        self.keypad_dial("144")
 
76
        self.wait_live_call_page("144")
 
77
        self.hangup()
 
78
 
 
79
        # log should show call to "Unknown"
 
80
        self.assertThat(self.history_list.count, Equals(1))
 
81
        self.assertThat(self.history_list.select_single(
 
82
            "Label", text="Unknown"), NotEquals(None))
 
83
 
 
84
    def test_outgoing_answer_local_hangup(self):
 
85
        """Outgoing call, remote answers, local hangs up"""
 
86
 
 
87
        # 06123xx causes accept after xx seconds
 
88
        self.keypad_dial("0612302")
 
89
        self.wait_live_call_page("0612302")
 
90
 
 
91
        # stop watch should start counting
 
92
        stop_watch = self.app.select_single(objectName="stopWatch")
 
93
        self.assertIn("00:0", stop_watch.elapsed)
 
94
 
 
95
        # should still be connected after some time
 
96
        time.sleep(3)
 
97
        self.assertIn("00:0", stop_watch.elapsed)
 
98
 
 
99
        self.hangup()
 
100
 
 
101
    def test_outgoing_answer_remote_hangup(self):
 
102
        """Outgoing call, remote answers and hangs up"""
 
103
 
 
104
        # 05123xx causes immediate accept and hangup after xx seconds
 
105
        self.keypad_dial("0512303")
 
106
        self.wait_live_call_page("0512303")
 
107
 
 
108
        # stop watch should start counting
 
109
        stop_watch = self.app.select_single(objectName="stopWatch")
 
110
        self.assertIn("00:0", stop_watch.elapsed)
 
111
 
 
112
        # after remote hangs up, should switch to call log page and show call
 
113
        # to "Unknown"
 
114
        fn = lambda: self.app.select_single(objectName="hangupButton")
 
115
        self.assertThat(fn, Eventually(Equals(None)))
 
116
        self.assertThat(self.history_list.visible, Eventually(Equals(True)))
 
117
        self.assertThat(self.history_list.count, Equals(1))
 
118
        self.assertThat(self.history_list.select_single(
 
119
            "Label", text="Unknown"), NotEquals(None))
 
120
 
 
121
    def test_incoming(self):
 
122
        """Incoming call"""
 
123
 
 
124
        # magic number 199 will cause a callback from 1234567; dialing 199
 
125
        # itself will fail, so quiesce the error
 
126
        subprocess.call(["/usr/share/ofono/scripts/dial-number", "199"],
 
127
                        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
128
 
 
129
        # wait for incoming call, accept; it would be nicer to fake-click the
 
130
        # popup notification, but as this isn't generated by dialer-app it
 
131
        # isn't exposed to autopilot
 
132
        self.wait_for_incoming_call()
 
133
        subprocess.call(["/usr/share/ofono/scripts/answer-calls"],
 
134
                        stdout=subprocess.PIPE)
 
135
 
 
136
        # call back is from that number
 
137
        self.wait_live_call_page("1234567")
 
138
 
 
139
        # stop watch should start counting
 
140
        stop_watch = self.app.select_single(objectName="stopWatch")
 
141
        self.assertIn("00:0", stop_watch.elapsed)
 
142
 
 
143
        self.hangup()
 
144
 
 
145
    #
 
146
    # Helper methods
 
147
    #
 
148
 
 
149
    def keypad_dial(self, number):
 
150
        """Dial given number (string) on the keypad and call"""
 
151
        for digit in number:
 
152
            self.pointing_device.click_object(self.keys[int(digit)])
 
153
        self.assertThat(self.entry.value, Eventually(Equals(number)))
 
154
 
 
155
        self.pointing_device.click_object(self.call_button)
 
156
 
 
157
    def wait_live_call_page(self, number):
 
158
        """Wait until live call page gets visible
 
159
 
 
160
        Sets self.hangup_button.
 
161
        """
 
162
        fn = lambda: self.app.select_single(objectName="hangupButton")
 
163
        self.assertThat(fn, Eventually(NotEquals(None)))
 
164
        self.hangup_button = self.app.select_single(objectName="hangupButton")
 
165
        self.assertThat(self.hangup_button.visible, Eventually(Equals(True)))
 
166
        self.assertThat(self.call_button.visible, Equals(False))
 
167
 
 
168
        # should show called number in title page
 
169
        lcp = self.app.select_single(objectName="pageLiveCall")
 
170
        self.assertThat(lcp.title, Equals(number))
 
171
 
 
172
    def wait_for_incoming_call(self):
 
173
        """Wait up to 5 s for an incoming phone call"""
 
174
 
 
175
        timeout = 10
 
176
        while timeout >= 0:
 
177
            out = subprocess.check_output(
 
178
                ["/usr/share/ofono/scripts/list-calls"],
 
179
                stderr=subprocess.PIPE)
 
180
            if "State = incoming" in out:
 
181
                break
 
182
            timeout -= 1
 
183
            time.sleep(0.5)
 
184
        else:
 
185
            self.fail("timed out waiting for incoming phonesim call")
 
186
 
 
187
    def hangup(self):
 
188
        self.pointing_device.click_object(self.hangup_button)
 
189
        fn = lambda: self.app.select_single(objectName="hangupButton")
 
190
        self.assertThat(fn, Eventually(Equals(None)))
 
191
 
 
192
        # should switch to call log page
 
193
        self.assertThat(self.history_list.visible, Eventually(Equals(True)))