Skip to content

Commit

Permalink
Added startviewserver argument to ViewClient
Browse files Browse the repository at this point in the history
- Start the server if specified
- Eat optional command line arguments
- Added UML
  • Loading branch information
dtmilano committed Nov 4, 2012
1 parent 19b3db9 commit 357e6b5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@
# vi files #
############
.*.swp

# other files #
##########
*~
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice


device, serialno = ViewClient.connectToDeviceOrExit()
print sys.argv
localViewServer = False
if len(sys.argv) > 1 and sys.argv[1] == '--localViewServer':
localViewServer = True
sys.argv.pop(1)

device, serialno = ViewClient.connectToDeviceOrExit(ignoresecuredevice=localViewServer)

FLAG_ACTIVITY_NEW_TASK = 0x10000000
package = 'com.example.i2at.tc'
Expand All @@ -50,7 +55,7 @@
device.type("123")
MonkeyRunner.sleep(3)

vc = ViewClient(device, serialno)
vc = ViewClient(device, serialno, startviewserver=(not localViewServer))

# obtain the views by id
celsius = vc.findViewByIdOrRaise("id/celsius")
Expand Down
32 changes: 32 additions & 0 deletions AndroidViewClient/examples/viewserveractivity-new-activity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#! /usr/bin/env monkeyrunner
'''
Copyright (C) 2012 Diego Torres Milano
Created on Feb 3, 2012
@author: diego
'''


import sys
import os

# This must be imported before MonkeyRunner and MonkeyDevice,
# otherwise the import fails.
# PyDev sets PYTHONPATH, use it
try:
for p in os.environ['PYTHONPATH'].split(':'):
if not p in sys.path:
sys.path.append(p)
except:
pass

try:
sys.path.append(os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
except:
pass

from com.dtmilano.android.viewclient import ViewClient

device, serialno = ViewClient.connectToDeviceOrExit(ignoresecuredevice=True)
vc = ViewClient(device=device, serialno=serialno, startviewserver=False)
vc.findViewWithTextOrRaise("New activity").touch()
18 changes: 12 additions & 6 deletions AndroidViewClient/src/com/dtmilano/android/viewclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ class ViewClient:
mapping is created.
'''

def __init__(self, device, serialno='emulator-5554', adb=os.path.join(ANDROID_HOME, 'platform-tools', ADB), autodump=True):
def __init__(self, device, serialno='emulator-5554', adb=os.path.join(ANDROID_HOME, 'platform-tools', ADB), autodump=True, startviewserver=True):
'''
Constructor
Expand All @@ -695,18 +695,21 @@ def __init__(self, device, serialno='emulator-5554', adb=os.path.join(ANDROID_HO
@param adb: the path of the C{adb} executable
@type autodump: boolean
@param autodump: whether an automatic dump is performed at the end of this constructor
@type startviewserverparam: boolean
@param startviewserverparam: Whether to start the B{global} ViewServer
'''

if not device:
raise Exception('Device is not connected')
if not os.access(adb, os.X_OK):
raise Exception('adb="%s" is not executable. Did you forget to set ANDROID_HOME in the environment?' % adb)
if not self.serviceResponse(device.shell('service call window 3')):
try:
self.assertServiceResponse(device.shell('service call window 1 i32 %d' %
if startviewserver:
if not self.serviceResponse(device.shell('service call window 3')):
try:
self.assertServiceResponse(device.shell('service call window 1 i32 %d' %
VIEW_SERVER_PORT))
except:
raise Exception('Cannot start View server.\n'
except:
raise Exception('Cannot start View server.\n'
'This only works on emulator and devices running developer versions.\n'
'Does hierarchyviewer work on your device ?')

Expand Down Expand Up @@ -775,6 +778,9 @@ def connectToDeviceOrExit(timeout=60, verbose=False, ignoresecuredevice=False):
'''

progname = os.path.basename(sys.argv[0])
# eat all the extra options the invoking script may have added
while len(sys.argv) > 1 and sys.argv[1][0] == '-':
sys.argv.pop(1)
serialno = sys.argv[1] if len(sys.argv) > 1 else 'emulator-5554'
if verbose:
print 'Connecting to a device with serialno=%s with a timeout of %d secs...' % (serialno, timeout)
Expand Down
Binary file added AndroidViewClient/uml/AndroidViewClient.zargo
Binary file not shown.

0 comments on commit 357e6b5

Please sign in to comment.