blob: d70e26da28aaab5ddd848e09b2cb3eeda2ffa89e [file] [log] [blame]
Borjan Tchakaloffa4bdae12017-11-21 14:58:12 +01001#!/usr/bin/env python3
2
3import time
4from uiautomator import Device
5import sys
6import subprocess
7import pathlib
8
9# Prepare the DUT
10def prepare_dut(serial, scenarios_dir, data_dir, apps):
11 # Copy the scenarios
12 ret = subprocess.run(['adb', '-s', serial, 'push', scenarios_dir,
13 '/sdcard/viser'], stdout=subprocess.PIPE, stderr=subprocess.PIPE,
14 universal_newlines=True)
15 if 0 < ret.returncode:
16 print('Could not push the scenarios, error was: {}'.format(ret.stderr),
17 file=sys.stderr)
18 else:
19 print('Scenarios pushed.')
20
21 # Copy the scenarios data
22 ret = subprocess.run(['adb', '-s', serial, 'push', data_dir,
23 '/sdcard/viser/data'], stdout=subprocess.PIPE,
24 stderr=subprocess.PIPE, universal_newlines=True)
25 if 0 < ret.returncode:
26 print('Could not push the scenarios data, error was: {}'.format(ret.stderr),
27 file=sys.stderr)
28 else:
29 print('Scenarios data pushed.')
30
31 # Install the smartviser apps
32 for app in apps:
33 ret = subprocess.run(['adb', '-s', serial, 'install', '-r', app],
34 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
35 universal_newlines=True)
36 if 0 < ret.returncode:
37 print('Could not install app `{}`, error was: {}'.format(app,
38 ret.stderr), file=sys.stderr)
39 else:
40 print('App `{}` installed.'.format(app))
41
42# Grant the permissions through the UI
43def configure_perms(dut):
Borjan Tchakaloff6dad85d2018-04-11 13:55:25 +020044 dut() \
Borjan Tchakaloffa4bdae12017-11-21 14:58:12 +010045 .child_by_text('You need to grant access for the Permissions Group '
46 'Calendar Camera Contacts Microphone SMS Storage Telephone Location',
Borjan Tchakaloff6dad85d2018-04-11 13:55:25 +020047 resourceId='android:id/content') \
48 .child(text='OK', className='android.widget.Button') \
49 .click()
Borjan Tchakaloffa4bdae12017-11-21 14:58:12 +010050
51 # Accept any permission that is asked for
52 prompt = dut(resourceId='com.android.packageinstaller:id/permission_allow_button',
53 className='android.widget.Button')
54 while prompt.exists:
55 prompt.click()
56
57 # Input the credentials
58 dut(resourceId='android:id/content') \
59 .child(text='Username') \
60 .child(className='android.widget.EditText') \
61 .set_text('borjan@fairphone.com')
62 dut(resourceId='android:id/content') \
63 .child(text='Password') \
64 .child(className='android.widget.EditText') \
65 .set_text('Rickisalso1niceguy!')
66
67 # Sign in
68 dut(resourceId='android:id/content') \
69 .child(text='Sign in', className='android.widget.Button') \
70 .click()
71
72def configure_sms(dut):
73 # TODO wait for the connection to be established and time-out
74 prompt = dut(resourceId='android:id/content') \
75 .child(text='Viser must be your SMS app to send messages')
76 while not prompt.exists:
77 time.sleep(1)
78
79 # Make viser the default SMS app
80 dut(resourceId='android:id/content') \
81 .child_by_text('Viser must be your SMS app to send messages',
82 className='android.widget.LinearLayout') \
83 .child(text='OK', className='android.widget.Button') \
84 .click()
85
86 dut(resourceId='android:id/content') \
87 .child_by_text('Change SMS app?', className='android.widget.LinearLayout') \
88 .child(text='Yes', className='android.widget.Button') \
89 .click()
90
91def configure_settings(dut):
92 # Set the e-mail account
93 dut(text='Settings', className='android.widget.TextView') \
94 .click()
95 dut(resourceId='android:id/list') \
96 .child_by_text('User settings', className='android.widget.LinearLayout') \
97 .click()
98 dut(resourceId='android:id/list') \
99 .child_by_text('Email account', className='android.widget.LinearLayout') \
100 .click()
101 prompt = dut(resourceId='android:id/content') \
102 .child_by_text('Email account', className='android.widget.LinearLayout')
103 prompt.child(resourceId='android:id/edit') \
104 .set_text('fairphone.viser@gmail.com')
105 prompt.child(text='OK', className='android.widget.Button') \
106 .click()
107 dut(resourceId='android:id/list') \
108 .child_by_text('Email password', className='android.widget.LinearLayout') \
109 .click()
110 dut(text='Password :') \
111 .child(className='android.widget.EditText') \
112 .set_text('fairphoneviser2017')
113 dut(description='OK', className='android.widget.TextView') \
114 .click()
115 dut.press.back()
116 dut.press.back()
117
Borjan Tchakaloffa4bdae12017-11-21 14:58:12 +0100118
Franz-Xaver Geigerd1079e22018-02-19 14:34:15 +0100119def deploy():
120 serials = []
Borjan Tchakaloffa4bdae12017-11-21 14:58:12 +0100121
Franz-Xaver Geigerd1079e22018-02-19 14:34:15 +0100122 if len(sys.argv) > 1:
123 serials.append(sys.argv[1])
124 else:
125 # List devices attached to adb
126 ret = subprocess.run(
127 "adb devices | grep -E '^[a-z0-9-]+\s+device$' | awk '{{print $1}}'",
128 shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
129 universal_newlines=True)
Borjan Tchakaloffa4bdae12017-11-21 14:58:12 +0100130
Franz-Xaver Geigerd1079e22018-02-19 14:34:15 +0100131 if 0 < ret.returncode:
132 # TODO handle the error
133 raise Exception('Failed to list adb devices')
134 elif ret.stdout:
135 for line in ret.stdout.splitlines():
136 serial = line.strip()
137 if serial:
138 serials.append(serial)
Borjan Tchakaloffa4bdae12017-11-21 14:58:12 +0100139
Franz-Xaver Geigerd1079e22018-02-19 14:34:15 +0100140 for serial in serials:
141 print('Configuring device {}'.format(serial))
Borjan Tchakaloffa4bdae12017-11-21 14:58:12 +0100142
Franz-Xaver Geigerd1079e22018-02-19 14:34:15 +0100143 dut = Device(serial)
Borjan Tchakaloffa4bdae12017-11-21 14:58:12 +0100144
Franz-Xaver Geigerd1079e22018-02-19 14:34:15 +0100145 # Push the scenarios, their data, and install the apps
146 prepare_dut(serial, '../scenarios', '../scenarios-data', [
147 '../../vendor/smartviser/viser/prebuilts/apk/com.smartviser.demogame.apk',
148 '../../vendor/smartviser/viser/prebuilts/apk/com.lunarlabs.panda.proxy.apk',
149 '../../vendor/smartviser/viser/prebuilts/apk/com.lunarlabs.panda.apk'
150 ])
Borjan Tchakaloffa4bdae12017-11-21 14:58:12 +0100151
Franz-Xaver Geigerd1079e22018-02-19 14:34:15 +0100152 # Start the viser app
153 ret = subprocess.run(['adb', '-s', serial, 'shell', 'monkey', '-p',
154 'com.lunarlabs.panda', '-c', 'android.intent.category.LAUNCHER',
155 '1'], stdout=subprocess.PIPE, stderr=subprocess.PIPE,
156 universal_newlines=True)
157 if 0 < ret.returncode:
158 print('Could not start the viser app, error was: {}'.format(app,
159 ret.stderr), file=sys.stderr)
160 exit(-1)
Borjan Tchakaloffa4bdae12017-11-21 14:58:12 +0100161
Franz-Xaver Geigerd1079e22018-02-19 14:34:15 +0100162 configure_perms(dut)
Borjan Tchakaloffa4bdae12017-11-21 14:58:12 +0100163
Franz-Xaver Geigerd1079e22018-02-19 14:34:15 +0100164 # TODO DO NOT DO THE FOLLOWING IF NO SIM CARD IS IN THE DUT
165 # time.sleep(10)
166 # configure_sms(dut)
167
168 configure_settings(dut)
169
170
171if __name__ == '__main__':
172 deploy()