blob: 9b069c995e15ab52244b7db7711cc2982169e000 [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
117serials = []
118
119if len(sys.argv) > 1:
120 serials.append(sys.argv[1])
121else:
122 # List devices attached to adb
123 ret = subprocess.run(
124 "adb devices | grep -E '^[a-z0-9-]+\s+device$' | awk '{{print $1}}'",
125 shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
126 universal_newlines=True)
127
128 if 0 < ret.returncode:
129 # TODO handle the error
130 raise Exception('Failed to list adb devices')
131 elif ret.stdout:
132 for line in ret.stdout.splitlines():
133 serial = line.strip()
134 if serial:
135 serials.append(serial)
136
137for serial in serials:
138 print('Configuring device {}'.format(serial))
139
140 dut = Device(serial)
141
142 # Push the scenarios, their data, and install the apps
143 prepare_dut(serial, '../scenarios', '../scenarios-data', [
144 '../../vendor/smartviser/viser/prebuilts/apk/com.smartviser.demogame.apk',
145 '../../vendor/smartviser/viser/prebuilts/apk/com.lunarlabs.panda.proxy.apk',
146 '../../vendor/smartviser/viser/prebuilts/apk/com.lunarlabs.panda.apk'
147 ])
148
149 # Start the viser app
150 ret = subprocess.run(['adb', '-s', serial, 'shell', 'monkey', '-p',
151 'com.lunarlabs.panda', '-c', 'android.intent.category.LAUNCHER',
152 '1'], stdout=subprocess.PIPE, stderr=subprocess.PIPE,
153 universal_newlines=True)
154 if 0 < ret.returncode:
155 print('Could not start the viser app, error was: {}'.format(app,
156 ret.stderr), file=sys.stderr)
157 exit(-1)
158
159 configure_perms(dut)
160
161 # TODO DO NOT DO THE FOLLOWING IF NO SIM CARD IS IN THE DUT
162 # time.sleep(10)
163 # configure_sms(dut)
164
165 configure_settings(dut)