1# Copyright 2014 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from multiprocessing import Process
16import os
17import os.path
18import tempfile
19import subprocess
20import time
21import string
22import sys
23import textwrap
24import its.device
25
26def main():
27    """
28        device0: device serial number for camera 0 testing
29        device1: device serial number for camera 1 testing
30        chart: [Experimental] another android device served as test chart
31               display. When this argument presents, change of test scene will
32               be handled automatically. Note that this argument requires
33               special physical/hardware setup to work and may not work on
34               all android devices.
35    """
36    auto_scenes = ["0", "1", "2", "3", "4"]
37
38    device0_id = None
39    device1_id = None
40    chart_host_id = None
41    scenes = None
42
43    for s in sys.argv[1:]:
44        if s[:8] == "device0=" and len(s) > 8:
45            device0_id = s[8:]
46        elif s[:8] == "device1=" and len(s) > 8:
47            device1_id = s[8:]
48        elif s[:7] == "scenes=" and len(s) > 7:
49            scenes = s[7:].split(',')
50        elif s[:6] == 'chart=' and len(s) > 6:
51            chart_host_id = s[6:]
52
53    #Sanity Check for camera 0 & 1 parallel testing
54    device0_bfp = its.device.get_device_fingerprint(device0_id)
55    device1_bfp = its.device.get_device_fingerprint(device1_id)
56    chart_host_bfp = its.device.get_device_fingerprint(chart_host_id)
57
58    assert device0_bfp is not None, "Can not connect to the device0"
59    assert device0_bfp == device1_bfp, \
60        "Not the same build: %s vs %s" % (device0_bfp, device1_bfp)
61    assert chart_host_bfp is not None, "Can not connect to the chart device"
62
63    if scenes is None:
64        scenes = auto_scenes
65
66    print ">>> Start the at %s" % time.strftime('%Y/%m/%d %H:%M:%S')
67    for scene in scenes:
68        cmds = []
69        cmds.append(build_cmd(device0_id, chart_host_id, device1_id, 0, scene))
70        cmds.append(build_cmd(device1_id, chart_host_id, device0_id, 1, scene))
71
72        procs = []
73        for cmd in cmds:
74            print "running: ", cmd
75            proc = Process(target=run_cmd, args=(cmd,))
76            procs.append(proc)
77            proc.start()
78
79        for proc in procs:
80            proc.join()
81
82    shut_down_device_screen(device0_id)
83    shut_down_device_screen(device1_id)
84    shut_down_device_screen(chart_host_id)
85
86    print ">>> End the test at %s" % time.strftime('%Y/%m/%d %H:%M:%S')
87
88def build_cmd(device_id, chart_host_id, result_device_id, camera_id, scene_id):
89    """ Create a cmd list for run_all_tests.py
90    Return a list of cmd & parameters
91    """
92    cmd = ['python',
93            os.path.join(os.getcwd(),'tools/run_all_tests.py'),
94            'device=%s' % device_id,
95            'result=%s' % result_device_id,
96            'camera=%i' % camera_id,
97            'scenes=%s' % scene_id]
98
99    # scene 5 is not automated and no chart is needed
100    if scene_id != '5':
101        cmd.append('chart=%s' % chart_host_id)
102
103    return cmd
104
105def run_cmd(cmd):
106    """ Run shell command on a subprocess
107    """
108    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
109    output, error = proc.communicate()
110    print output, error
111
112def shut_down_device_screen(device_id):
113    """ Shut Down Device Screen
114
115    Returns:
116        None
117    """
118
119    print 'Shutting down chart screen: ', device_id
120    screen_id_arg = ('screen=%s' % device_id)
121    cmd = ['python', os.path.join(os.environ['CAMERA_ITS_TOP'], 'tools',
122                                  'turn_off_screen.py'), screen_id_arg]
123    retcode = subprocess.call(cmd)
124    assert retcode == 0
125
126if __name__ == '__main__':
127    main()
128