summaryrefslogtreecommitdiff
path: root/emu_test/test_console/testcase_call.py
blob: 71dbc68da623facb4d2894bf55e2669c37a31e8f (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
"""Tests for call-related commands."""

import inspect
import json
import os
import time
import unittest

import requests
import testcase_base
from utils import util

TESTCASE_CALL_DIR = os.path.dirname(os.path.realpath(__file__))
SERVLET_TELEPHONY = 'http://localhost:8080/TelephonyManagerService'

CALL_STATE_IDLE = 0
CALL_STATE_RINGING = 1
CALL_STATE_OFFHOOK = 2

CALL_NUMBER = '1234567890'
CMD_GSM_CANCEL = 'gsm cancel %s\n' % CALL_NUMBER
CMD_GSM_CALL = 'gsm call %s\n' % CALL_NUMBER
CMD_GSM_ACCEPT = 'gsm accept %s\n' % CALL_NUMBER


class PhoneCallTest(testcase_base.BaseConsoleTest):
  """This class aims to test call-related emulator console commands."""

  def __init__(self, method_name=None, avd=None):
    if method_name:
      super(PhoneCallTest, self).__init__(method_name)
    else:
      super(PhoneCallTest, self).__init__()
    self.avd = avd

  @classmethod
  def setUpClass(cls):
    util.run_script_run_adb_shell(TESTCASE_CALL_DIR)

  @classmethod
  def tearDownClass(cls):
    util.unstall_apps(TESTCASE_CALL_DIR)

  def _process_request_telephony_service(self, payload):
    r = requests.post(SERVLET_TELEPHONY, data=json.dumps(payload))
    if r.raise_for_status():
      print ('Servlet Error: Post request to %s failed' %
             SERVLET_TELEPHONY)
      return False
    r_json = r.json()
    if r_json['isFail']:
      print ('Servlet Error: Failure occurred in servlet side => %s' %
             SERVLET_TELEPHONY)
      return False
    call_state = int(r_json['description'])
    print 'call_state = %d' % call_state
    return call_state

  def _cancel_inbound_call(self):
    self.telnet.write(CMD_GSM_CANCEL)
    time.sleep(util.CMD_WAIT_TIMEOUT_S)

  def _cancel_inbound_call_verification(self):
    is_cmd_successful, output_cancel_inbound = util.execute_console_command(
        self.telnet, CMD_GSM_CANCEL, util.OK)

    self.assert_cmd_successful(is_cmd_successful,
                               'Failed to properly cancel an inbound call',
                               False, '', util.OK, output_cancel_inbound)
    self.assertTrue(
        self._process_request_telephony_service({}) == CALL_STATE_IDLE,
        'Call state idle not matched')
    time.sleep(util.CMD_WAIT_TIMEOUT_S)

  def _make_inbound_call(self):
    self.assertTrue(
        self._process_request_telephony_service({}) == CALL_STATE_IDLE,
        'Call state idle not matched')

    is_cmd_successful, output_inbound_call = util.execute_console_command(
        self.telnet, CMD_GSM_CALL, util.OK)

    self.assert_cmd_successful(is_cmd_successful,
                               'Failed to properly set up an inbound call',
                               False, '', util.OK, output_inbound_call)

    self.assertTrue(
        self._process_request_telephony_service({}) == CALL_STATE_RINGING,
        'Call state ringing not matched')
    time.sleep(util.CMD_WAIT_TIMEOUT_S)

  def _accept_inbound_call(self):
    is_cmd_successful, output_accept_inbound = util.execute_console_command(
        self.telnet, CMD_GSM_ACCEPT, util.OK)
    self.assert_cmd_successful(is_cmd_successful,
                               'Failed to properly accept an inbound call',
                               False, '', util.OK, output_accept_inbound)
    self.assertTrue(
        self._process_request_telephony_service({}) == CALL_STATE_OFFHOOK,
        'Call state offhook not matched')
    time.sleep(util.CMD_WAIT_TIMEOUT_S)

  def test_inbound_call(self):
    """Test for command: gsm call <phonenumber>.

    Test Rail ID: C14595296
    Test steps:
      1. Launch an emulator avd
      2. From command prompt, run: telnet localhost <port>
      3. Copy the auth_token value from ~/.emulator_console_auth_token
      4. Run: auth auth_token
      5. Run: gsm call <phonenumber>, verify 1
      6. Run: gsm cancel <phonenumber>, verify 2
    Verify:
      1. Emulator displays an incoming call from the <phonenumber>
      2. Phone call is terminated.
    """
    print 'Running test: %s' % (inspect.stack()[0][3])
    self._make_inbound_call()
    self._cancel_inbound_call()

  def test_accept_call(self):
    """Test for command: gsm accept <phonenumber>.

    Test Rail ID: C14595296
    Test steps:
      1. Launch an emulator avd
      2. From command prompt, run: telnet localhost <port>
      3. Copy the auth_token value from ~/.emulator_console_auth_token
      4. Run: auth auth_token
      5. Run: gsm call <phonenumber>, verify 1
      6. Run: gsm accept <phonenumber>, verify 2
      7. Run: gsm cancel <phonenumber>
    Verify:
      1. Emulator displays an incoming call from the <phonenumber>
      2. Emulator displays that the incoming call is accepted
    """
    print 'Running test: %s' % (inspect.stack()[0][3])
    self._make_inbound_call()
    self._accept_inbound_call()
    self._cancel_inbound_call()

  def test_terminate_call(self):
    """Test for command: gsm cancel <phonenumber>.

    Test Rail ID: C14595296
    Test steps:
      1. Launch an emulator avd
      2. From command prompt, run: telnet localhost <port>
      3. Copy the auth_token value from ~/.emulator_console_auth_token
      4. Run: auth auth_token
      5. Run: gsm call <phonenumber>, verify 1
      6. Run: gsm accept <phonenumber>, verify 2
      7. Run: gsm cancel <phonenumber>, verify 3
    Verify:
      1. Emulator displays an incoming call from the <phonenumber>
      2. Emulator displays that the incoming call is accepted
      3. Phone call is terminated. The emulator displays the phone
         hang-up icon in the notification bar.
    """
    print 'Running test: %s' % (inspect.stack()[0][3])
    self._make_inbound_call()
    self._accept_inbound_call()
    self._cancel_inbound_call_verification()


if __name__ == '__main__':
  print '======= Call Test ======='
  unittest.main()