Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 1 | # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | import contextlib |
| 6 | import json |
| 7 | import logging |
| 8 | from lxml import etree |
| 9 | import os |
| 10 | import StringIO |
Kuo Jen Wei | 987b7eb | 2020-05-28 15:39:45 +0800 | [diff] [blame] | 11 | import time |
Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 12 | |
Kuo Jen Wei | 987b7eb | 2020-05-28 15:39:45 +0800 | [diff] [blame] | 13 | from autotest_lib.client.common_lib import error, utils |
Kuo Jen Wei | 377e99b | 2020-02-25 16:39:42 +0800 | [diff] [blame] | 14 | from autotest_lib.server.cros.tradefed import tradefed_chromelogin as login |
Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 15 | |
| 16 | |
| 17 | class ChartFixture: |
| 18 | """Sets up chart tablet to display dummy scene image.""" |
| 19 | DISPLAY_SCRIPT = '/usr/local/autotest/bin/display_chart.py' |
Kuo Jen Wei | a10200e | 2020-03-13 16:03:54 +0800 | [diff] [blame] | 20 | OUTPUT_LOG = '/tmp/chart_service.log' |
Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 21 | |
| 22 | def __init__(self, chart_host, scene_uri): |
| 23 | self.host = chart_host |
| 24 | self.scene_uri = scene_uri |
| 25 | self.display_pid = None |
Kuo Jen Wei | a10200e | 2020-03-13 16:03:54 +0800 | [diff] [blame] | 26 | self.host.run(['rm', '-f', self.OUTPUT_LOG]) |
Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 27 | |
| 28 | def initialize(self): |
| 29 | """Prepare scene file and display it on chart host.""" |
| 30 | logging.info('Prepare scene file') |
Kuo Jen Wei | a10200e | 2020-03-13 16:03:54 +0800 | [diff] [blame] | 31 | tmpdir = self.host.get_tmp_dir() |
Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 32 | scene_path = os.path.join( |
Kuo Jen Wei | a10200e | 2020-03-13 16:03:54 +0800 | [diff] [blame] | 33 | tmpdir, self.scene_uri[self.scene_uri.rfind('/') + 1:]) |
Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 34 | self.host.run('wget', args=('-O', scene_path, self.scene_uri)) |
Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 35 | |
| 36 | logging.info('Display scene file') |
| 37 | self.display_pid = self.host.run_background( |
Kuo Jen Wei | a10200e | 2020-03-13 16:03:54 +0800 | [diff] [blame] | 38 | 'python2 {script} {scene} >{log} 2>&1'.format( |
| 39 | script=self.DISPLAY_SCRIPT, |
| 40 | scene=scene_path, |
| 41 | log=self.OUTPUT_LOG)) |
Kuo Jen Wei | 987b7eb | 2020-05-28 15:39:45 +0800 | [diff] [blame] | 42 | |
| 43 | logging.info( |
| 44 | 'Poll for "is ready" message for ensuring chart is ready.') |
| 45 | timeout = 30 |
| 46 | poll_time_step = 0.1 |
| 47 | while timeout > 0: |
| 48 | if self.host.run( |
| 49 | 'grep', |
| 50 | args=('-q', 'Chart is ready.', self.OUTPUT_LOG), |
| 51 | ignore_status=True).exit_status == 0: |
| 52 | break |
| 53 | time.sleep(poll_time_step) |
| 54 | timeout -= poll_time_step |
| 55 | else: |
| 56 | raise error.TestError('Timeout waiting for chart ready') |
Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 57 | |
| 58 | def cleanup(self): |
| 59 | """Cleanup display script.""" |
| 60 | if self.display_pid is not None: |
Kuo Jen Wei | a10200e | 2020-03-13 16:03:54 +0800 | [diff] [blame] | 61 | self.host.run( |
| 62 | 'kill', |
| 63 | args=('-2', str(self.display_pid)), |
| 64 | ignore_status=True) |
| 65 | self.host.get_file(self.OUTPUT_LOG, '.') |
Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 66 | |
| 67 | |
| 68 | def get_chart_address(host_address, args): |
Kuo Jen Wei | 3e89c44 | 2019-01-29 14:27:26 +0800 | [diff] [blame] | 69 | """Get address of chart tablet from commandline args or mapping logic in |
| 70 | test lab. |
| 71 | |
| 72 | @param host_address: a list of hostname strings. |
| 73 | @param args: a dict parse from commandline args. |
| 74 | @return: |
| 75 | A list of strings for chart tablet addresses. |
| 76 | """ |
Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 77 | address = utils.args_to_dict(args).get('chart') |
| 78 | if address is not None: |
| 79 | return address.split(',') |
| 80 | elif utils.is_in_container(): |
Kuo Jen Wei | 377e99b | 2020-02-25 16:39:42 +0800 | [diff] [blame] | 81 | return [utils.get_lab_chart_address(host) for host in host_address] |
Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 82 | else: |
| 83 | return None |
| 84 | |
| 85 | |
| 86 | class DUTFixture: |
| 87 | """Sets up camera filter for target camera facing on DUT.""" |
| 88 | TEST_CONFIG_PATH = '/var/cache/camera/test_config.json' |
Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 89 | CAMERA_PROFILE_PATH = ('/mnt/stateful_partition/encrypted/var/cache/camera' |
| 90 | '/media_profiles.xml') |
Kuo Jen Wei | 6f854a0 | 2020-05-20 12:01:33 +0800 | [diff] [blame] | 91 | CAMERA_SCENE_LOG = '/tmp/scene.jpg' |
Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 92 | |
| 93 | def __init__(self, test, host, facing): |
| 94 | self.test = test |
| 95 | self.host = host |
| 96 | self.facing = facing |
| 97 | |
| 98 | @contextlib.contextmanager |
| 99 | def _set_selinux_permissive(self): |
| 100 | selinux_mode = self.host.run_output('getenforce') |
| 101 | self.host.run('setenforce 0') |
| 102 | yield |
| 103 | self.host.run('setenforce', args=(selinux_mode, )) |
| 104 | |
| 105 | def _filter_camera_profile(self, content, facing): |
| 106 | """Filter camera profile of target facing from content of camera |
| 107 | profile. |
| 108 | |
| 109 | @return: |
| 110 | New camera profile with only target facing, camera ids are |
| 111 | renumbered from 0. |
| 112 | """ |
| 113 | tree = etree.parse( |
| 114 | StringIO.StringIO(content), |
| 115 | parser=etree.XMLParser(compact=False)) |
| 116 | root = tree.getroot() |
| 117 | profiles = root.findall('CamcorderProfiles') |
| 118 | logging.debug('%d number of camera(s) found in camera profile', |
| 119 | len(profiles)) |
| 120 | assert 1 <= len(profiles) <= 2 |
| 121 | if len(profiles) == 2: |
| 122 | cam_id = 0 if facing == 'back' else 1 |
| 123 | for p in profiles: |
| 124 | if cam_id == int(p.attrib['cameraId']): |
| 125 | p.attrib['cameraId'] = '0' |
| 126 | else: |
| 127 | root.remove(p) |
| 128 | else: |
Kuo Jen Wei | 377e99b | 2020-02-25 16:39:42 +0800 | [diff] [blame] | 129 | with login.login_chrome( |
| 130 | hosts=[self.host], |
Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 131 | board=self.test._get_board_name(), |
Kuo Jen Wei | 377e99b | 2020-02-25 16:39:42 +0800 | [diff] [blame] | 132 | ), self._set_selinux_permissive(): |
Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 133 | has_front_camera = ( |
| 134 | 'feature:android.hardware.camera.front' in self.host. |
| 135 | run_output('android-sh -c "pm list features"')) |
| 136 | logging.debug('has_front_camera=%s', has_front_camera) |
| 137 | if (facing == 'front') != has_front_camera: |
| 138 | root.remove(profiles[0]) |
| 139 | return etree.tostring( |
| 140 | tree, xml_declaration=True, encoding=tree.docinfo.encoding) |
| 141 | |
| 142 | def _read_file(self, filepath): |
| 143 | """Read content of filepath from host.""" |
| 144 | tmp_path = os.path.join(self.test.tmpdir, os.path.basename(filepath)) |
| 145 | self.host.get_file(filepath, tmp_path, delete_dest=True) |
| 146 | with open(tmp_path) as f: |
| 147 | return f.read() |
| 148 | |
| 149 | def _write_file(self, filepath, content, permission=None, owner=None): |
| 150 | """Write content to filepath on remote host. |
| 151 | @param permission: set permission to 0xxx octal number of remote file. |
| 152 | @param owner: set owner of remote file. |
| 153 | """ |
| 154 | tmp_path = os.path.join(self.test.tmpdir, os.path.basename(filepath)) |
| 155 | with open(tmp_path, 'w') as f: |
| 156 | f.write(content) |
| 157 | if permission is not None: |
| 158 | os.chmod(tmp_path, permission) |
| 159 | self.host.send_file(tmp_path, filepath, delete_dest=True) |
| 160 | if owner is not None: |
| 161 | self.host.run('chown', args=(owner, filepath)) |
| 162 | |
| 163 | def initialize(self): |
| 164 | """Filter out camera other than target facing on DUT.""" |
| 165 | logging.info('Restart camera service with filter option') |
| 166 | self._write_file( |
| 167 | self.TEST_CONFIG_PATH, |
| 168 | json.dumps({ |
| 169 | 'enable_back_camera': self.facing == 'back', |
| 170 | 'enable_front_camera': self.facing == 'front', |
| 171 | 'enable_external_camera': False |
| 172 | }), |
| 173 | owner='arc-camera') |
Kuo Jen Wei | 9d68af8 | 2020-06-23 10:43:21 +0800 | [diff] [blame^] | 174 | self.host.upstart_restart('cros-camera') |
Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 175 | |
Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 176 | logging.info('Replace camera profile in ARC++ container') |
Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 177 | profile = self._read_file(self.CAMERA_PROFILE_PATH) |
| 178 | new_profile = self._filter_camera_profile(profile, self.facing) |
| 179 | self._write_file(self.CAMERA_PROFILE_PATH, new_profile) |
| 180 | self.host.run('restart ui') |
| 181 | |
Kuo Jen Wei | 6f854a0 | 2020-05-20 12:01:33 +0800 | [diff] [blame] | 182 | @contextlib.contextmanager |
| 183 | def _stop_camera_service(self): |
Kuo Jen Wei | 9d68af8 | 2020-06-23 10:43:21 +0800 | [diff] [blame^] | 184 | self.host.upstart_stop('cros-camera') |
Kuo Jen Wei | 6f854a0 | 2020-05-20 12:01:33 +0800 | [diff] [blame] | 185 | yield |
Kuo Jen Wei | 9d68af8 | 2020-06-23 10:43:21 +0800 | [diff] [blame^] | 186 | self.host.upstart_restart('cros-camera') |
Kuo Jen Wei | 6f854a0 | 2020-05-20 12:01:33 +0800 | [diff] [blame] | 187 | |
| 188 | def log_camera_scene(self): |
| 189 | """Capture an image from camera as the log for debugging scene related |
| 190 | problem.""" |
| 191 | |
| 192 | gtest_filter = ( |
| 193 | 'Camera3StillCaptureTest/' |
| 194 | 'Camera3DumpSimpleStillCaptureTest.DumpCaptureResult/0') |
| 195 | with self._stop_camera_service(): |
| 196 | self.host.run( |
| 197 | 'sudo', |
| 198 | args=('--user=arc-camera', 'cros_camera_test', |
| 199 | '--gtest_filter=' + gtest_filter, |
| 200 | '--camera_facing=' + self.facing, |
| 201 | '--dump_still_capture_path=' + |
| 202 | self.CAMERA_SCENE_LOG)) |
| 203 | |
| 204 | self.host.get_file(self.CAMERA_SCENE_LOG, '.') |
| 205 | |
Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 206 | def cleanup(self): |
| 207 | """Cleanup camera filter.""" |
| 208 | logging.info('Remove filter option and restore camera service') |
| 209 | self.host.run('rm', args=('-f', self.TEST_CONFIG_PATH)) |
Kuo Jen Wei | 9d68af8 | 2020-06-23 10:43:21 +0800 | [diff] [blame^] | 210 | self.host.upstart_restart('cros-camera') |
Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 211 | |
Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 212 | logging.info('Restore camera profile in ARC++ container') |
Kuo Jen Wei | 7f00bb3 | 2018-11-01 15:35:24 +0800 | [diff] [blame] | 213 | self.host.run('restart ui') |