Skip to content

Commit 825f03f

Browse files
committed
Merge pull request dtmilano#12 from dtmilano/viewclient-start-viewserver
Viewclient starts viewserver optionally
2 parents c0b908d + 2dfcc40 commit 825f03f

5 files changed

Lines changed: 55 additions & 12 deletions

File tree

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
############
1212
.*.swp
1313

14-
# other #
15-
#########
14+
# other files #
15+
##########
1616
*~

AndroidViewClient/examples/temperature-converter-get-conversion.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@
3535

3636
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
3737

38-
39-
device, serialno = ViewClient.connectToDeviceOrExit()
38+
print sys.argv
39+
localViewServer = False
40+
if len(sys.argv) > 1 and sys.argv[1] == '--localViewServer':
41+
localViewServer = True
42+
sys.argv.pop(1)
43+
44+
device, serialno = ViewClient.connectToDeviceOrExit(ignoresecuredevice=localViewServer)
4045

4146
FLAG_ACTIVITY_NEW_TASK = 0x10000000
4247
package = 'com.example.i2at.tc'
@@ -50,7 +55,7 @@
5055
device.type("123")
5156
MonkeyRunner.sleep(3)
5257

53-
vc = ViewClient(device, serialno)
58+
vc = ViewClient(device, serialno, startviewserver=(not localViewServer))
5459

5560
# obtain the views by id
5661
celsius = vc.findViewByIdOrRaise("id/celsius")
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#! /usr/bin/env monkeyrunner
2+
'''
3+
Copyright (C) 2012 Diego Torres Milano
4+
Created on Feb 3, 2012
5+
6+
@author: diego
7+
'''
8+
9+
10+
import sys
11+
import os
12+
13+
# This must be imported before MonkeyRunner and MonkeyDevice,
14+
# otherwise the import fails.
15+
# PyDev sets PYTHONPATH, use it
16+
try:
17+
for p in os.environ['PYTHONPATH'].split(':'):
18+
if not p in sys.path:
19+
sys.path.append(p)
20+
except:
21+
pass
22+
23+
try:
24+
sys.path.append(os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
25+
except:
26+
pass
27+
28+
from com.dtmilano.android.viewclient import ViewClient
29+
30+
device, serialno = ViewClient.connectToDeviceOrExit(ignoresecuredevice=True)
31+
vc = ViewClient(device=device, serialno=serialno, startviewserver=False)
32+
vc.findViewWithTextOrRaise("New activity").touch()

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ class ViewClient:
707707
mapping is created.
708708
'''
709709

710-
def __init__(self, device, serialno, adb=None, autodump=True, localport=VIEW_SERVER_PORT, remoteport=VIEW_SERVER_PORT):
710+
def __init__(self, device, serialno, adb=None, autodump=True, localport=VIEW_SERVER_PORT, remoteport=VIEW_SERVER_PORT, startviewserver=True):
711711
'''
712712
Constructor
713713
@@ -724,6 +724,8 @@ def __init__(self, device, serialno, adb=None, autodump=True, localport=VIEW_SER
724724
@type remoteport: int
725725
@param remoteport: the remote port used to start the C{ViewServer} in the device or
726726
emulator
727+
@type startviewserverparam: boolean
728+
@param startviewserverparam: Whether to start the B{global} ViewServer
727729
'''
728730

729731
if not device:
@@ -744,12 +746,13 @@ def __init__(self, device, serialno, adb=None, autodump=True, localport=VIEW_SER
744746
else:
745747
adb = ViewClient.__obtainAdbPath()
746748

747-
if not self.serviceResponse(device.shell('service call window 3')):
748-
try:
749-
self.assertServiceResponse(device.shell('service call window 1 i32 %d' %
749+
if startviewserver:
750+
if not self.serviceResponse(device.shell('service call window 3')):
751+
try:
752+
self.assertServiceResponse(device.shell('service call window 1 i32 %d' %
750753
remoteport))
751-
except:
752-
raise Exception('Cannot start View server.\n'
754+
except:
755+
raise Exception('Cannot start View server.\n'
753756
'This only works on emulator and devices running developer versions.\n'
754757
'Does hierarchyviewer work on your device ?')
755758

@@ -909,8 +912,11 @@ def connectToDeviceOrExit(timeout=60, verbose=False, ignoresecuredevice=False):
909912
@return: the device and serialno used for the connection
910913
'''
911914

912-
serialno = sys.argv[1] if len(sys.argv) > 1 else '.*'
913915
progname = os.path.basename(sys.argv[0])
916+
# eat all the extra options the invoking script may have added
917+
while len(sys.argv) > 1 and sys.argv[1][0] == '-':
918+
sys.argv.pop(1)
919+
serialno = sys.argv[1] if len(sys.argv) > 1 else '.*'
914920
if verbose:
915921
print 'Connecting to a device with serialno=%s with a timeout of %d secs...' % (serialno, timeout)
916922
# Sometimes MonkeyRunner doesn't even timeout (i.e. two connections from same process), so let's
7.11 KB
Binary file not shown.

0 commit comments

Comments
 (0)