1

I wondering if there is a way or if anyone has attempted to automate the setting up of a device for CTS testing?

Basically I have the following scenario

  • I'm using jenkins to flash the devices which puts a modified 4.2 load on the devices
  • After the devices have been flashed I want to run the CTS tests.
  • Without user interaction I want to be able to setup the device and run the CTS.

Is there a way to achieve this? Or is user interaction the only way to setup the device for CTS testing?

user2569315
  • 13
  • 1
  • 4

1 Answers1

3

Yes, there is a way to set up the device for CTS. I used AndroidViewClient to do this. Here are some examples of how you can achieve this:

# This must be imported before MonkeyRunner and MonkeyDevice,
# otherwise the import fails.
from com.dtmilano.android.viewclient import ViewClient, View

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyView

package = 'com.android.settings'
activity = '.Settings'
component_name = package + '/' + activity
EXCLUDE_FROM_RECENTS = 0x00800000
ap_name = "Put acces point name here"

# Connect to device with the IP received as a parameter
device, serialno = ViewClient.connectToDeviceOrExit()

width = int(device.getProperty('display.width'))
height = int(device.getProperty('display.height'))

# Press the HOME button to start the test from the home screen
device.press('KEYCODE_HOME','DOWN_AND_UP')
MonkeyRunner.sleep(2)

# Open the Settings app
device.startActivity(component = component_name, flags = EXCLUDE_FROM_RECENTS)
i = 0
while str(device.getProperty('am.current.package')) != package and i<10:
   MonkeyRunner.sleep(1)
   i = i + 1
if i == 10:
   raise Exception('Cannot open package')

# Create the view client object
vc = ViewClient(device=device, serialno=serialno)

# Enable Wi-Fi
device.shell("svc wifi enable")
i = 0
while not vc.findViewWithText(ap_name) and i<30:
    MonkeyRunner.sleep(1)
    vc.dump()
    i += 1
if i == 30:
    print "Cannot enable Wi-Fi"
for i in range(5):
    device.press('KEYCODE_DPAD_RIGHT', MonkeyDevice.DOWN_AND_UP)
    MonkeyRunner.sleep(0.5)
device.press('KEYCODE_DPAD_LEFT', MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(0.5)
device.press('KEYCODE_ENTER', MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(2)
device.type(ap_name)
vc.dump()
if vc.findViewWithText('Save'):
    vc.findViewWithText('Save').touch()
    MonkeyRunner.sleep(5)

# Set Screen Lock to None
drag_start = (width*1/4, height*9/10)
drag_end = (width*1/4, height*1/10)
device.drag(drag_start, drag_end)
vc.dump()
if vc.findViewWithText('Security'):
    vc.findViewWithText('Security').touch()
MonkeyRunner.sleep(1)
vc.dump()
if vc.findViewWithText('Screen lock'):
    vc.findViewWithText('Screen lock').touch()
MonkeyRunner.sleep(1)
vc.dump()
if vc.findViewWithText('None'):
    vc.findViewWithText('None').touch()
MonkeyRunner.sleep(1)
vc.dump()

# Activate device administrator
if vc.findViewWithText('Device administrators'):
    vc.findViewWithText('Device administrators').touch()
MonkeyRunner.sleep(1)
vc.dump()
admin1 = vc.findViewWithText('android.deviceadmin.cts.CtsDeviceAdminReceiver')
if admin1:
    for aux_view in admin1.parent.parent.children:
        if aux_view['class'] == 'android.widget.CheckBox':
            break
    if aux_view['checked'] == 'false':
        admin1.touch()
        MonkeyRunner.sleep(1)
        vc.dump()
        if vc.findViewWithText('Activate'):
            vc.findViewWithText('Activate').touch()
MonkeyRunner.sleep(1)
vc.dump()
admin2 = vc.findViewWithText('android.deviceadmin.cts.CtsDeviceAdminReceiver2')
if admin2:
    for aux_view in admin2.parent.parent.children:
        if aux_view['class'] == 'android.widget.CheckBox':
            break
    if aux_view['checked'] == 'false':
        admin2.touch()
        MonkeyRunner.sleep(1)
        vc.dump()
        if vc.findViewWithText('Activate'):
            vc.findViewWithText('Activate').touch()
MonkeyRunner.sleep(1)

# Enable Delegating Accessibility Service
MonkeyRunner.sleep(1)
vc.dump()
if vc.findViewWithText('Accessibility'):
    vc.findViewWithText('Accessibility').touch()
MonkeyRunner.sleep(1)
vc.dump()
if vc.findViewWithText('Delegating Accessibility Service'):
    vc.findViewWithText('Delegating Accessibility Service').touch()
MonkeyRunner.sleep(1)
for count in range(5):
    device.press('KEYCODE_DPAD_RIGHT', MonkeyDevice.DOWN_AND_UP)
    MonkeyRunner.sleep(0.5)
device.press('KEYCODE_ENTER', MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1)
vc.dump()
MonkeyRunner.sleep(1)
if vc.findViewWithText('Use Delegating Accessibility Service?'):
    vc.findViewWithText('OK').touch()
else:
    vc.findViewWithText('Cancel').touch()
MonkeyRunner.sleep(1)

These are some of the steps for preparing the device for CTS. Follow the example for each section in the CTS setup guide.

Gabriel Porumb
  • 1,661
  • 1
  • 12
  • 21
  • Great script. This could be very useful to other people running CTS. You may want to add it to AndroidViewClient examples. Just upload it to github and I will gladly merge it. – Diego Torres Milano Jul 12 '13 at 20:15
  • @dtmilano Using AndroidViewClient works fine when I just have a single device connected. However when I have multiple devices connected I continually get errors in the script. For example when i want to connect to a specific device eg device, serialno = ViewClient.connectToDeviceOrExit(serialno="016B756C1001A016") i get the following error: vc = ViewClient(device=device, serialno=serialno) xml.parsers.expat.ExpatError: Content is not allowed in trailing section. Could this perhaps be a defect in the the module the way it handles more than one device. – user2569315 Jul 25 '13 at 17:52
  • @user2569315 it would be interesting to enable `DEBUG_RECEIVED` and check what was there. Content not allowed in trailing session means there are some content after the XML. – Diego Torres Milano Jul 25 '13 at 20:53
  • @dtmilano On a galaxy nexus device when delegating Accessibility is turned on I get "Killed" when uiautomator dump is called eg :"4]" />Killed ". However running the same test on a Nexus 4 it works fine. Therefore this leads me to believe there maybe a bug in the android source it self that is causing the problem on the GN. This issue is very similar to question http://stackoverflow.com/questions/17130500/androidviewclient-content-is-not-allowed-in-trailing-section . Is there a workaround? – user2569315 Jul 26 '13 at 16:10
  • You are right. It's definitely an UiAutomator bug, but AndroidViewClient compensates from it. Have you tried setting `ignoreuiautomatorkilled=True` in https://github.com/dtmilano/AndroidViewClient/blob/master/AndroidViewClient/src/com/dtmilano/android/viewclient.py#L1067 – Diego Torres Milano Jul 26 '13 at 16:16