1# Copyright 2016 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
15import os
16import os.path
17import subprocess
18import sys
19import tempfile
20import time
21
22import its.device
23# from its.device import ItsSession
24
25NUM_RUNS = 2
26SCENE_NAME = 'sensor_fusion'
27SKIP_RET_CODE = 101
28TEST_NAME = 'test_sensor_fusion'
29TEST_DIR = os.path.join(os.getcwd(), 'tests', SCENE_NAME)
30
31
32def main():
33    """Run all the test_sensor_fusion NUM_RUNS times.
34
35    Save intermediate files, and producing a summary/report of the results.
36
37    Script should be run from the top-level CameraITS directory.
38
39    Command line Arguments:
40        camera:  the camera(s) to be tested. Use comma to separate multiple
41                 camera Ids. Ex: 'camera=0,1' or 'camera=1'
42        device:  the device id for adb
43        rotator: string for rotator id in for vid:pid:ch
44    """
45
46    camera_id = '0'
47    rotator_ids = 'default'
48    for s in sys.argv[1:]:
49        if s[:7] == 'camera=' and len(s) > 7:
50            camera_id = s[7:]
51        elif s[:8] == 'rotator=' and len(s) > 8:
52            rotator_ids = s[8:]
53
54    if camera_id not in ['0', '1']:
55        print 'Need to specify camera 0 or 1'
56        sys.exit()
57
58    # Make output directories to hold the generated files.
59    tmpdir = tempfile.mkdtemp()
60    print 'Saving output files to:', tmpdir, '\n'
61
62    device_id = its.device.get_device_id()
63    device_id_arg = 'device=' + device_id
64    print 'Testing device ' + device_id
65
66    camera_id_arg = 'camera=' + camera_id
67    if rotator_ids:
68        rotator_id_arg = 'rotator=' + rotator_ids
69    print 'Preparing to run sensor_fusion on camera', camera_id
70
71    os.mkdir(os.path.join(tmpdir, camera_id))
72
73    # Run test multiple times, capturing stdout and stderr.
74    numpass = 0
75    numfail = 0
76    numskip = 0
77    for i in range(NUM_RUNS):
78        os.mkdir(os.path.join(tmpdir, camera_id, SCENE_NAME+'_'+str(i)))
79        cmd = ('python tools/rotation_rig.py rotator=%s' % rotator_ids)
80        subprocess.Popen(cmd.split())
81        cmd = ['python', os.path.join(TEST_DIR, TEST_NAME+'.py'),
82               device_id_arg, camera_id_arg, rotator_id_arg]
83        outdir = os.path.join(tmpdir, camera_id, SCENE_NAME+'_'+str(i))
84        outpath = os.path.join(outdir, TEST_NAME+'_stdout.txt')
85        errpath = os.path.join(outdir, TEST_NAME+'_stderr.txt')
86        t0 = time.time()
87        with open(outpath, 'w') as fout, open(errpath, 'w') as ferr:
88            retcode = subprocess.call(
89                cmd, stderr=ferr, stdout=fout, cwd=outdir)
90        t1 = time.time()
91
92        if retcode == 0:
93            retstr = 'PASS '
94            numpass += 1
95        elif retcode == SKIP_RET_CODE:
96            retstr = 'SKIP '
97            numskip += 1
98        else:
99            retstr = 'FAIL '
100            numfail += 1
101        msg = '%s %s/%s [%.1fs]' % (retstr, SCENE_NAME, TEST_NAME, t1-t0)
102        print msg
103
104    test_result = '%d / %d tests passed (%.1f%%)' % (
105        numpass+numskip, NUM_RUNS, 100.0*float(numpass+numskip)/NUM_RUNS)
106    print test_result
107
108if __name__ == '__main__':
109    main()
110
111