Skip to content

Commit a31a205

Browse files
authored
Support make_gsm_call (appium#360)
* Move const to gsm_signal_strength * Support make_gsm_call * Add make_gsm_call unittest * Move const to gsm class * Move get_dict_const to common.helper * Rename func * Use OrderedDict to keep defined order
1 parent 308fd1b commit a31a205

4 files changed

Lines changed: 72 additions & 28 deletions

File tree

appium/common/helper.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import io
16-
import os
15+
from collections import OrderedDict
1716

1817
from appium import version as appium_version
1918

@@ -38,6 +37,20 @@ def appium_bytes(value, encoding):
3837
return value # Python 2
3938

4039

40+
def extract_const_attributes(cls):
41+
"""
42+
Return dict with constants attributes and values in the class (e.g. {'VAL1': 1, 'VAL2': 2})
43+
44+
:param cls: Class to be extracted constants
45+
:type cls: type
46+
47+
:return: dict with constants attributes and values in the class
48+
:rtype: OrderedDict
49+
"""
50+
return OrderedDict(
51+
[(attr, value) for attr, value in vars(cls).items() if not callable(getattr(cls, attr)) and attr.isupper()])
52+
53+
4154
def library_version():
4255
"""
4356
Return a version of this python library

appium/webdriver/extensions/gsm.py

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,63 @@
1616
from ..mobilecommand import MobileCommand as Command
1717

1818
from appium.common.logger import logger
19+
from appium.common.helper import extract_const_attributes
20+
21+
22+
class GsmCallActions(object):
23+
CALL = 'call'
24+
ACCEPT = 'accept'
25+
CANCEL = 'cancel'
26+
HOLD = 'hold'
27+
28+
29+
class GsmSignalStrength(object):
30+
NONE_OR_UNKNOWN = 0
31+
POOR = 1
32+
MODERATE = 2
33+
GOOD = 3
34+
GREAT = 4
1935

2036

2137
class Gsm(webdriver.Remote):
2238

23-
(
24-
NONE_OR_UNKNOWN,
25-
POOR,
26-
MODERATE,
27-
GOOD,
28-
GREAT
29-
) = range(5)
39+
def make_gsm_call(self, phone_number, action):
40+
"""Make GSM call (Emulator only)
41+
42+
:Args:
43+
- phone_number (str): The phone number to call to.
44+
- action (str): The call action - GsmCallActions.CALL/ACCEPT/CANCEL/HOLD
45+
46+
:Usage:
47+
self.driver.make_gsm_call('5551234567', GsmCallActions.CALL)
48+
"""
49+
constants = extract_const_attributes(GsmCallActions)
50+
if action not in constants.values():
51+
logger.warning('{} is unknown. Consider using one of {} constants. (e.g. {}.CALL)'.format(
52+
action, list(constants.keys()), GsmCallActions.__name__))
53+
self.execute(Command.MAKE_GSM_CALL, {'phoneNumber': phone_number, 'action': action})
54+
return self
3055

3156
def set_gsm_signal(self, strength):
3257
"""Set GSM signal strength (Emulator only)
3358
3459
:Args:
35-
- strength: Signal strength. Can be set Gsm.NONE_OR_UNKNOWN/POOR/MODERATE/GOOD/GREAT
60+
- strength (int): Signal strength - GsmSignalStrength.NONE_OR_UNKNOWN/POOR/MODERATE/GOOD/GREAT
3661
3762
:Usage:
38-
self.driver.set_gsm_signal(Gsm.GOOD)
63+
self.driver.set_gsm_signal(GsmSignalStrength.GOOD)
3964
"""
40-
if strength not in self._dict_signal_strength().values():
41-
logger.warning('{} is out of range. Use the value in {}.'.format(
42-
strength, list(self._dict_signal_strength().keys())))
65+
constants = extract_const_attributes(GsmSignalStrength)
66+
if strength not in constants.values():
67+
logger.warning('{} is out of range. Consider using one of {} constants. (e.g. {}.GOOD)'.format(
68+
strength, list(constants.keys()), GsmSignalStrength.__name__))
4369
self.execute(Command.SET_GSM_SIGNAL, {'signalStrength': strength, 'signalStrengh': strength})
4470
return self
4571

46-
def _dict_signal_strength(self):
47-
return {'{}.{}'.format(Gsm.__name__, attr): value for attr, value in vars(Gsm).items()
48-
if not callable(getattr(Gsm, attr)) and attr.isupper()}
49-
5072
# pylint: disable=protected-access
5173

5274
def _addCommands(self):
75+
self.command_executor._commands[Command.MAKE_GSM_CALL] = \
76+
('POST', '/session/$sessionId/appium/device/gsm_call')
5377
self.command_executor._commands[Command.SET_GSM_SIGNAL] = \
5478
('POST', '/session/$sessionId/appium/device/gsm_signal')

appium/webdriver/mobilecommand.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,4 @@ class MobileCommand(object):
8080
SET_POWER_CAPACITY = 'setPowerCapacity'
8181
SET_POWER_AC = 'setPowerAc'
8282
SET_GSM_SIGNAL = 'setGsmSignal'
83+
MAKE_GSM_CALL = 'makeGsmCall'

test/unit/webdriver/device/gsm_test.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,23 @@
2121
import httpretty
2222

2323
from appium.webdriver.webdriver import WebDriver
24-
from appium.webdriver.extensions.gsm import Gsm
24+
from appium.webdriver.extensions.gsm import GsmCallActions, GsmSignalStrength
2525

2626

2727
class TestWebDriveGsm(object):
2828

29-
def test_gsm_signal_strength(self):
30-
assert Gsm.NONE_OR_UNKNOWN == 0
31-
assert Gsm.POOR == 1
32-
assert Gsm.MODERATE == 2
33-
assert Gsm.GOOD == 3
34-
assert Gsm.GREAT == 4
29+
@httpretty.activate
30+
def test_make_gsm_call(self):
31+
driver = android_w3c_driver()
32+
httpretty.register_uri(
33+
httpretty.POST,
34+
appium_command('/session/1234567890/appium/device/gsm_call'),
35+
)
36+
assert isinstance(driver.make_gsm_call('5551234567', GsmCallActions.CALL), WebDriver)
37+
38+
d = get_httpretty_request_body(httpretty.last_request())
39+
assert d['phoneNumber'] == '5551234567'
40+
assert d['action'] == GsmCallActions.CALL
3541

3642
@httpretty.activate
3743
def test_set_gsm_signal(self):
@@ -40,8 +46,8 @@ def test_set_gsm_signal(self):
4046
httpretty.POST,
4147
appium_command('/session/1234567890/appium/device/gsm_signal'),
4248
)
43-
assert isinstance(driver.set_gsm_signal(Gsm.GREAT), WebDriver)
49+
assert isinstance(driver.set_gsm_signal(GsmSignalStrength.GREAT), WebDriver)
4450

4551
d = get_httpretty_request_body(httpretty.last_request())
46-
assert d['signalStrength'] == Gsm.GREAT
47-
assert d['signalStrengh'] == Gsm.GREAT
52+
assert d['signalStrength'] == GsmSignalStrength.GREAT
53+
assert d['signalStrengh'] == GsmSignalStrength.GREAT

0 commit comments

Comments
 (0)