Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
hexiaocong committed Nov 13, 2014
2 parents 968fa4a + 93654d1 commit 0018d64
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
19 changes: 18 additions & 1 deletion test/test_adb.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def test_adb_from_env(self):
self.assertEqual(adb_obj.adb(), adb_path)
exists.assert_called_once_with(adb_path)
self.assertEqual(adb_obj.adb(), adb_path)
exists.assert_called_once_with(adb_path) # the second call will return the __adb_cmd directly
# the second call will return the __adb_cmd directly
exists.assert_called_once_with(adb_path)

os.name = "nt" # linux
adb_obj = Adb()
Expand Down Expand Up @@ -117,6 +118,22 @@ def test_adb_cmd(self):
adb.cmd(*args)
adb.raw_cmd.assert_called_once_with("-s", "'%s'" % adb.device_serial(), *args)

def test_adb_cmd_server_host(self):
adb = Adb(adb_server_host="localhost", adb_server_port=5037)
adb.device_serial = MagicMock()
adb.device_serial.return_value = "ANDROID_SERIAL"
adb.raw_cmd = MagicMock()
args = ["a", "b", "c"]
adb.cmd(*args)
adb.raw_cmd.assert_called_once_with("-H", "localhost", "-P", "5037", "-s", "%s" % adb.device_serial(), *args)

adb = Adb(adb_server_host="localhost")
adb.device_serial = MagicMock()
adb.device_serial.return_value = "ANDROID_SERIAL"
adb.raw_cmd = MagicMock()
args = ["a", "b", "c"]
adb.cmd(*args)
adb.raw_cmd.assert_called_once_with("-H", "localhost", "-s", "%s" % adb.device_serial(), *args)

def test_device_serial(self):
with patch.dict('os.environ', {'ANDROID_SERIAL': "ABCDEF123456"}):
Expand Down
16 changes: 10 additions & 6 deletions test/test_device_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,19 @@ def setUp(self):

def test_child_selector(self):
kwargs = {"text": "child text", "className": "android"}
self.obj.selector.child = MagicMock()
self.obj.child_selector(**kwargs)
self.obj.selector.child.assert_called_once_with(**kwargs)
obj = self.obj.child_selector(**kwargs)
self.assertEqual(len(obj.selector['childOrSibling']), 1)
self.assertEqual(obj.selector['childOrSibling'][0], 'child')
self.assertEqual(len(obj.selector['childOrSiblingSelector']), 1)
self.assertEqual(obj.selector['childOrSiblingSelector'][0], Selector(**kwargs))

def test_from_parent(self):
kwargs = {"text": "parent text", "className": "android"}
self.obj.selector.sibling = MagicMock()
self.obj.from_parent(**kwargs)
self.obj.selector.sibling.assert_called_once_with(**kwargs)
obj = self.obj.from_parent(**kwargs)
self.assertEqual(len(obj.selector['childOrSibling']), 1)
self.assertEqual(obj.selector['childOrSibling'][0], 'sibling')
self.assertEqual(len(obj.selector['childOrSiblingSelector']), 1)
self.assertEqual(obj.selector['childOrSiblingSelector'][0], Selector(**kwargs))

def test_exists(self):
self.jsonrpc.exist = MagicMock()
Expand Down
22 changes: 17 additions & 5 deletions uiautomator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,12 @@ def clone(self):
def child(self, **kwargs):
self[self.__childOrSibling].append("child")
self[self.__childOrSiblingSelector].append(Selector(**kwargs))
return self

def sibling(self, **kwargs):
self[self.__childOrSibling].append("sibling")
self[self.__childOrSiblingSelector].append(Selector(**kwargs))
return self

child_selector, from_parent = child, sibling

Expand All @@ -234,9 +236,11 @@ def point(x=0, y=0):

class Adb(object):

def __init__(self, serial=None):
def __init__(self, serial=None, adb_server_host=None, adb_server_port=None):
self.__adb_cmd = None
self.default_serial = serial if serial else os.environ.get("ANDROID_SERIAL", None)
self.adb_server_host = adb_server_host
self.adb_server_port = adb_server_port

def adb(self):
if self.__adb_cmd is None:
Expand Down Expand Up @@ -264,6 +268,10 @@ def cmd(self, *args):
if serial.find(" ") > 0: # TODO how to include special chars on command line
serial = "'%s'" % serial
cmd_line = ["-s", serial] + list(args)
if self.adb_server_port: # add -P argument
cmd_line = ["-P", str(self.adb_server_port)] + cmd_line
if self.adb_server_host: # add -H argument
cmd_line = ["-H", self.adb_server_host] + cmd_line
return self.raw_cmd(*cmd_line)

def raw_cmd(self, *args):
Expand Down Expand Up @@ -969,13 +977,17 @@ def __init__(self, device, selector):

def child(self, **kwargs):
'''set childSelector.'''
self.selector.child(**kwargs)
return self
return AutomatorDeviceObject(
self.device,
self.selector.clone().child(**kwargs)
)

def sibling(self, **kwargs):
'''set fromParent selector.'''
self.selector.sibling(**kwargs)
return self
return AutomatorDeviceObject(
self.device,
self.selector.clone().sibling(**kwargs)
)

child_selector, from_parent = child, sibling

Expand Down

0 comments on commit 0018d64

Please sign in to comment.