Skip to content

Commit ef7dfcc

Browse files
committed
Change PatternObject type comparison
- Added MockViewServer - Added tests
1 parent dded142 commit ef7dfcc

4 files changed

Lines changed: 316 additions & 19 deletions

File tree

AndroidViewClient/src/com/dtmilano/android/viewclient.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import signal
3131
import warnings
3232
import xml.parsers.expat
33+
import org.python.modules.sre.PatternObject
3334
from com.android.monkeyrunner import MonkeyDevice, MonkeyRunner
3435

3536
DEBUG = False
@@ -168,7 +169,7 @@ def __str__(self):
168169

169170
class ViewNotFoundException(Exception):
170171
def __init__(self, attr, value, root):
171-
if type(value).__name__ == 'PatternObject':
172+
if isinstance(value, org.python.modules.sre.PatternObject):
172173
msg = "Couldn't find View with %s that matches '%s' in tree with root=%s" % (attr, value.pattern, root)
173174
else:
174175
msg = "Couldn't find View with %s='%s' in tree with root=%s" % (attr, value, root)
@@ -1569,8 +1570,10 @@ def dump(self, window=-1, sleep=1):
15691570
15701571
@type window: int or str
15711572
@param window: the window id or name of the window to dump.
1572-
The name is the package name or the window name (i.e. StatusBar) for
1573+
The B{name} is the package name or the window name (i.e. StatusBar) for
15731574
system windows.
1575+
The window id can be provided as C{int} or C{str}. The C{str} should represent
1576+
and C{int} in either base 10 or 16.
15741577
Use -1 to dump all windows.
15751578
This parameter only is used when the backend is B{ViewServer} and it's
15761579
ignored for B{UiAutomator}.
@@ -1590,7 +1593,7 @@ def dump(self, window=-1, sleep=1):
15901593
if not output:
15911594
raise RuntimeError('ERROR: Getting UIAutomator dump')
15921595
if not re.search('dumped', output):
1593-
raise RuntimeError("ERROR: UIAutomator dump output doesn't containt 'dumped' (%s)" % output)
1596+
raise RuntimeError("ERROR: UIAutomator dump output doesn't contain 'dumped' (%s)" % output)
15941597
received = self.device.shell('cat %s 2>/dev/null' % windowDump)
15951598
if received:
15961599
received = received.encode('ascii', 'ignore')
@@ -1610,19 +1613,34 @@ def dump(self, window=-1, sleep=1):
16101613
print >>sys.stderr
16111614
self.setViewsFromUiAutomatorDump(received)
16121615
else:
1613-
if type(window).__name__ == 'str':
1616+
if isinstance(window, str):
16141617
self.list(sleep=0)
16151618
found = False
16161619
for wId in self.windows:
16171620
try:
1618-
if window == self.windows[wId] or int(window) == wId:
1621+
if window == self.windows[wId]:
16191622
window = wId
16201623
found = True
16211624
break
1622-
except ValueError:
1625+
except:
1626+
pass
1627+
try:
1628+
if int(window) == wId:
1629+
window = wId
1630+
found = True
1631+
break
1632+
except:
16231633
pass
1634+
try:
1635+
if int(window, 16) == wId:
1636+
window = wId
1637+
found = True
1638+
break
1639+
except:
1640+
pass
1641+
16241642
if not found:
1625-
raise RuntimeError("ERROR: Cannot find window '%s'" % window)
1643+
raise RuntimeError("ERROR: Cannot find window '%s' in %s" % (window, self.windows))
16261644

16271645
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
16281646
try:
@@ -1807,7 +1825,7 @@ def __findViewWithAttributeInTree(self, attr, val, root):
18071825

18081826
if DEBUG: print >>sys.stderr, "__findViewWithAttributeInTree: checking if root=%s has attr=%s == %s" % (root.__smallStr__(), attr, val)
18091827

1810-
if type(val).__name__ == 'PatternObject':
1828+
if isinstance(val, org.python.modules.sre.PatternObject):
18111829
return self.__findViewWithAttributeInTreeThatMatches(attr, val, root)
18121830
else:
18131831
if root and attr in root.map and root.map[attr] == val:
@@ -1884,7 +1902,7 @@ def findViewWithAttributeThatMatches(self, attr, regex, root="ROOT"):
18841902
return self.__findViewWithAttributeInTreeThatMatches(attr, regex, root)
18851903

18861904
def findViewWithText(self, text, root="ROOT"):
1887-
if type(text).__name__ == 'PatternObject':
1905+
if isinstance(text, org.python.modules.sre.PatternObject):
18881906
return self.findViewWithAttributeThatMatches(self.textProperty, text, root)
18891907
#l = self.findViewWithAttributeThatMatches(TEXT_PROPERTY, text)
18901908
#ll = len(l)

AndroidViewClient/tests/allTests.py

100644100755
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
1+
#! /usr/bin/env monkeyrunner
12
'''
23
Copyright (C) 2012 Diego Torres Milano
34
Created on Feb 5, 2012
45
56
@author: diego
67
'''
78
import unittest
9+
import sys
10+
import os
11+
12+
# This must be imported before MonkeyRunner and MonkeyDevice,
13+
# otherwise the import fails.
14+
# PyDev sets PYTHONPATH, use it
15+
try:
16+
for p in os.environ['PYTHONPATH'].split(':'):
17+
if not p in sys.path:
18+
sys.path.append(p)
19+
except:
20+
pass
21+
22+
try:
23+
sys.path.append(os.environ['ANDROID_VIEW_CLIENT_HOME'])
24+
sys.path.append(os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
25+
except:
26+
pass
27+
828

929

1030
if __name__ == "__main__":
11-
import sys
12-
sys.argv = ['', 'ViewTest.testName']
31+
#sys.argv = ['', 'ViewTest.testName']
1332
import tests.com.dtmilano.android.viewclient
14-
unittest.main()
33+
unittest.main()

0 commit comments

Comments
 (0)