blob: 0f222f8b6db926624c59d6990d828cf15ee27eed [file] [log] [blame]
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +08001# 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
5import contextlib
6import json
7import logging
8from lxml import etree
9import os
10import StringIO
Kuo Jen Wei987b7eb2020-05-28 15:39:45 +080011import time
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080012
Kuo Jen Wei987b7eb2020-05-28 15:39:45 +080013from autotest_lib.client.common_lib import error, utils
Kuo Jen Wei377e99b2020-02-25 16:39:42 +080014from autotest_lib.server.cros.tradefed import tradefed_chromelogin as login
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080015
16
17class ChartFixture:
18 """Sets up chart tablet to display dummy scene image."""
19 DISPLAY_SCRIPT = '/usr/local/autotest/bin/display_chart.py'
Kuo Jen Weia10200e2020-03-13 16:03:54 +080020 OUTPUT_LOG = '/tmp/chart_service.log'
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080021
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 Weia10200e2020-03-13 16:03:54 +080026 self.host.run(['rm', '-f', self.OUTPUT_LOG])
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080027
28 def initialize(self):
29 """Prepare scene file and display it on chart host."""
30 logging.info('Prepare scene file')
Kuo Jen Weia10200e2020-03-13 16:03:54 +080031 tmpdir = self.host.get_tmp_dir()
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080032 scene_path = os.path.join(
Kuo Jen Weia10200e2020-03-13 16:03:54 +080033 tmpdir, self.scene_uri[self.scene_uri.rfind('/') + 1:])
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080034 self.host.run('wget', args=('-O', scene_path, self.scene_uri))
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080035
36 logging.info('Display scene file')
37 self.display_pid = self.host.run_background(
Kuo Jen Weia10200e2020-03-13 16:03:54 +080038 'python2 {script} {scene} >{log} 2>&1'.format(
39 script=self.DISPLAY_SCRIPT,
40 scene=scene_path,
41 log=self.OUTPUT_LOG))
Kuo Jen Wei987b7eb2020-05-28 15:39:45 +080042
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 Wei7f00bb32018-11-01 15:35:24 +080057
58 def cleanup(self):
59 """Cleanup display script."""
60 if self.display_pid is not None:
Kuo Jen Weia10200e2020-03-13 16:03:54 +080061 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 Wei7f00bb32018-11-01 15:35:24 +080066
67
68def get_chart_address(host_address, args):
Kuo Jen Wei3e89c442019-01-29 14:27:26 +080069 """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 Wei7f00bb32018-11-01 15:35:24 +080077 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 Wei377e99b2020-02-25 16:39:42 +080081 return [utils.get_lab_chart_address(host) for host in host_address]
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080082 else:
83 return None
84
85
86class DUTFixture:
87 """Sets up camera filter for target camera facing on DUT."""
88 TEST_CONFIG_PATH = '/var/cache/camera/test_config.json'
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080089 CAMERA_PROFILE_PATH = ('/mnt/stateful_partition/encrypted/var/cache/camera'
90 '/media_profiles.xml')
Kuo Jen Wei6f854a02020-05-20 12:01:33 +080091 CAMERA_SCENE_LOG = '/tmp/scene.jpg'
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080092
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 Wei377e99b2020-02-25 16:39:42 +0800129 with login.login_chrome(
130 hosts=[self.host],
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +0800131 board=self.test._get_board_name(),
Kuo Jen Wei377e99b2020-02-25 16:39:42 +0800132 ), self._set_selinux_permissive():
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +0800133 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 Wei9d68af82020-06-23 10:43:21 +0800174 self.host.upstart_restart('cros-camera')
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +0800175
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +0800176 logging.info('Replace camera profile in ARC++ container')
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +0800177 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 Wei6f854a02020-05-20 12:01:33 +0800182 @contextlib.contextmanager
183 def _stop_camera_service(self):
Kuo Jen Wei9d68af82020-06-23 10:43:21 +0800184 self.host.upstart_stop('cros-camera')
Kuo Jen Wei6f854a02020-05-20 12:01:33 +0800185 yield
Kuo Jen Wei9d68af82020-06-23 10:43:21 +0800186 self.host.upstart_restart('cros-camera')
Kuo Jen Wei6f854a02020-05-20 12:01:33 +0800187
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 Wei7f00bb32018-11-01 15:35:24 +0800206 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 Wei9d68af82020-06-23 10:43:21 +0800210 self.host.upstart_restart('cros-camera')
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +0800211
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +0800212 logging.info('Restore camera profile in ARC++ container')
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +0800213 self.host.run('restart ui')