blob: 31495d63663da30da3520af71b9a8b6731102ac4 [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):
44 prompt = dut(resourceId='android:id/content') \
45 .child_by_text('You need to grant access for the Permissions Group '
46 'Calendar Camera Contacts Microphone SMS Storage Telephone Location',
47 className='android.widget.LinearLayout') \
48 .child(text='OK', className='android.widget.Button').click()
49
50 # Accept any permission that is asked for
51 prompt = dut(resourceId='com.android.packageinstaller:id/permission_allow_button',
52 className='android.widget.Button')
53 while prompt.exists:
54 prompt.click()
55
56 # Input the credentials
57 dut(resourceId='android:id/content') \
58 .child(text='Username') \
59 .child(className='android.widget.EditText') \
60 .set_text('borjan@fairphone.com')
61 dut(resourceId='android:id/content') \
62 .child(text='Password') \
63 .child(className='android.widget.EditText') \
64 .set_text('Rickisalso1niceguy!')
65
66 # Sign in
67 dut(resourceId='android:id/content') \
68 .child(text='Sign in', className='android.widget.Button') \
69 .click()
70
71def configure_sms(dut):
72 # TODO wait for the connection to be established and time-out
73 prompt = dut(resourceId='android:id/content') \
74 .child(text='Viser must be your SMS app to send messages')
75 while not prompt.exists:
76 time.sleep(1)
77
78 # Make viser the default SMS app
79 dut(resourceId='android:id/content') \
80 .child_by_text('Viser must be your SMS app to send messages',
81 className='android.widget.LinearLayout') \
82 .child(text='OK', className='android.widget.Button') \
83 .click()
84
85 dut(resourceId='android:id/content') \
86 .child_by_text('Change SMS app?', className='android.widget.LinearLayout') \
87 .child(text='Yes', className='android.widget.Button') \
88 .click()
89
90def configure_settings(dut):
91 # Set the e-mail account
92 dut(text='Settings', className='android.widget.TextView') \
93 .click()
94 dut(resourceId='android:id/list') \
95 .child_by_text('User settings', className='android.widget.LinearLayout') \
96 .click()
97 dut(resourceId='android:id/list') \
98 .child_by_text('Email account', className='android.widget.LinearLayout') \
99 .click()
100 prompt = dut(resourceId='android:id/content') \
101 .child_by_text('Email account', className='android.widget.LinearLayout')
102 prompt.child(resourceId='android:id/edit') \
103 .set_text('fairphone.viser@gmail.com')
104 prompt.child(text='OK', className='android.widget.Button') \
105 .click()
106 dut(resourceId='android:id/list') \
107 .child_by_text('Email password', className='android.widget.LinearLayout') \
108 .click()
109 dut(text='Password :') \
110 .child(className='android.widget.EditText') \
111 .set_text('fairphoneviser2017')
112 dut(description='OK', className='android.widget.TextView') \
113 .click()
114 dut.press.back()
115 dut.press.back()
116
Borjan Tchakaloffa4bdae12017-11-21 14:58:12 +0100117
Franz-Xaver Geigerd1079e22018-02-19 14:34:15 +0100118def deploy():
119 serials = []
Borjan Tchakaloffa4bdae12017-11-21 14:58:12 +0100120
Franz-Xaver Geigerd1079e22018-02-19 14:34:15 +0100121 if len(sys.argv) > 1:
122 serials.append(sys.argv[1])
123 else:
124 # List devices attached to adb
125 ret = subprocess.run(
126 "adb devices | grep -E '^[a-z0-9-]+\s+device$' | awk '{{print $1}}'",
127 shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
128 universal_newlines=True)
Borjan Tchakaloffa4bdae12017-11-21 14:58:12 +0100129
Franz-Xaver Geigerd1079e22018-02-19 14:34:15 +0100130 if 0 < ret.returncode:
131 # TODO handle the error
132 raise Exception('Failed to list adb devices')
133 elif ret.stdout:
134 for line in ret.stdout.splitlines():
135 serial = line.strip()
136 if serial:
137 serials.append(serial)
Borjan Tchakaloffa4bdae12017-11-21 14:58:12 +0100138
Franz-Xaver Geigerd1079e22018-02-19 14:34:15 +0100139 for serial in serials:
140 print('Configuring device {}'.format(serial))
Borjan Tchakaloffa4bdae12017-11-21 14:58:12 +0100141
Franz-Xaver Geigerd1079e22018-02-19 14:34:15 +0100142 dut = Device(serial)
Borjan Tchakaloffa4bdae12017-11-21 14:58:12 +0100143
Franz-Xaver Geigerd1079e22018-02-19 14:34:15 +0100144 # Push the scenarios, their data, and install the apps
145 prepare_dut(serial, '../scenarios', '../scenarios-data', [
146 '../../vendor/smartviser/viser/prebuilts/apk/com.smartviser.demogame.apk',
147 '../../vendor/smartviser/viser/prebuilts/apk/com.lunarlabs.panda.proxy.apk',
148 '../../vendor/smartviser/viser/prebuilts/apk/com.lunarlabs.panda.apk'
149 ])
Borjan Tchakaloffa4bdae12017-11-21 14:58:12 +0100150
Franz-Xaver Geigerd1079e22018-02-19 14:34:15 +0100151 # Start the viser app
152 ret = subprocess.run(['adb', '-s', serial, 'shell', 'monkey', '-p',
153 'com.lunarlabs.panda', '-c', 'android.intent.category.LAUNCHER',
154 '1'], stdout=subprocess.PIPE, stderr=subprocess.PIPE,
155 universal_newlines=True)
156 if 0 < ret.returncode:
157 print('Could not start the viser app, error was: {}'.format(app,
158 ret.stderr), file=sys.stderr)
159 exit(-1)
Borjan Tchakaloffa4bdae12017-11-21 14:58:12 +0100160
Franz-Xaver Geigerd1079e22018-02-19 14:34:15 +0100161 configure_perms(dut)
Borjan Tchakaloffa4bdae12017-11-21 14:58:12 +0100162
Franz-Xaver Geigerd1079e22018-02-19 14:34:15 +0100163 # TODO DO NOT DO THE FOLLOWING IF NO SIM CARD IS IN THE DUT
164 # time.sleep(10)
165 # configure_sms(dut)
166
167 configure_settings(dut)
168
169
170if __name__ == '__main__':
171 deploy()