blob: 491c87e81e0e4a46d5a8ac7c09e66b8234eaaa24 [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
11
12from autotest_lib.client.common_lib import utils
Kuo Jen Wei377e99b2020-02-25 16:39:42 +080013from autotest_lib.server.cros.tradefed import tradefed_chromelogin as login
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080014
15
16class ChartFixture:
17 """Sets up chart tablet to display dummy scene image."""
18 DISPLAY_SCRIPT = '/usr/local/autotest/bin/display_chart.py'
Kuo Jen Weia10200e2020-03-13 16:03:54 +080019 OUTPUT_LOG = '/tmp/chart_service.log'
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080020
21 def __init__(self, chart_host, scene_uri):
22 self.host = chart_host
23 self.scene_uri = scene_uri
24 self.display_pid = None
Kuo Jen Weia10200e2020-03-13 16:03:54 +080025 self.host.run(['rm', '-f', self.OUTPUT_LOG])
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080026
27 def initialize(self):
28 """Prepare scene file and display it on chart host."""
29 logging.info('Prepare scene file')
Kuo Jen Weia10200e2020-03-13 16:03:54 +080030 tmpdir = self.host.get_tmp_dir()
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080031 scene_path = os.path.join(
Kuo Jen Weia10200e2020-03-13 16:03:54 +080032 tmpdir, self.scene_uri[self.scene_uri.rfind('/') + 1:])
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080033 self.host.run('wget', args=('-O', scene_path, self.scene_uri))
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080034
35 logging.info('Display scene file')
36 self.display_pid = self.host.run_background(
Kuo Jen Weia10200e2020-03-13 16:03:54 +080037 'python2 {script} {scene} >{log} 2>&1'.format(
38 script=self.DISPLAY_SCRIPT,
39 scene=scene_path,
40 log=self.OUTPUT_LOG))
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080041 # TODO(inker): Suppose chart should be displayed very soon. Or require
42 # of waiting until chart actually displayed.
43
44 def cleanup(self):
45 """Cleanup display script."""
46 if self.display_pid is not None:
Kuo Jen Weia10200e2020-03-13 16:03:54 +080047 self.host.run(
48 'kill',
49 args=('-2', str(self.display_pid)),
50 ignore_status=True)
51 self.host.get_file(self.OUTPUT_LOG, '.')
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080052
53
54def get_chart_address(host_address, args):
Kuo Jen Wei3e89c442019-01-29 14:27:26 +080055 """Get address of chart tablet from commandline args or mapping logic in
56 test lab.
57
58 @param host_address: a list of hostname strings.
59 @param args: a dict parse from commandline args.
60 @return:
61 A list of strings for chart tablet addresses.
62 """
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080063 address = utils.args_to_dict(args).get('chart')
64 if address is not None:
65 return address.split(',')
66 elif utils.is_in_container():
Kuo Jen Wei377e99b2020-02-25 16:39:42 +080067 return [utils.get_lab_chart_address(host) for host in host_address]
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080068 else:
69 return None
70
71
72class DUTFixture:
73 """Sets up camera filter for target camera facing on DUT."""
74 TEST_CONFIG_PATH = '/var/cache/camera/test_config.json'
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080075 CAMERA_PROFILE_PATH = ('/mnt/stateful_partition/encrypted/var/cache/camera'
76 '/media_profiles.xml')
77
78 def __init__(self, test, host, facing):
79 self.test = test
80 self.host = host
81 self.facing = facing
82
83 @contextlib.contextmanager
84 def _set_selinux_permissive(self):
85 selinux_mode = self.host.run_output('getenforce')
86 self.host.run('setenforce 0')
87 yield
88 self.host.run('setenforce', args=(selinux_mode, ))
89
90 def _filter_camera_profile(self, content, facing):
91 """Filter camera profile of target facing from content of camera
92 profile.
93
94 @return:
95 New camera profile with only target facing, camera ids are
96 renumbered from 0.
97 """
98 tree = etree.parse(
99 StringIO.StringIO(content),
100 parser=etree.XMLParser(compact=False))
101 root = tree.getroot()
102 profiles = root.findall('CamcorderProfiles')
103 logging.debug('%d number of camera(s) found in camera profile',
104 len(profiles))
105 assert 1 <= len(profiles) <= 2
106 if len(profiles) == 2:
107 cam_id = 0 if facing == 'back' else 1
108 for p in profiles:
109 if cam_id == int(p.attrib['cameraId']):
110 p.attrib['cameraId'] = '0'
111 else:
112 root.remove(p)
113 else:
Kuo Jen Wei377e99b2020-02-25 16:39:42 +0800114 with login.login_chrome(
115 hosts=[self.host],
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +0800116 board=self.test._get_board_name(),
Kuo Jen Wei377e99b2020-02-25 16:39:42 +0800117 ), self._set_selinux_permissive():
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +0800118 has_front_camera = (
119 'feature:android.hardware.camera.front' in self.host.
120 run_output('android-sh -c "pm list features"'))
121 logging.debug('has_front_camera=%s', has_front_camera)
122 if (facing == 'front') != has_front_camera:
123 root.remove(profiles[0])
124 return etree.tostring(
125 tree, xml_declaration=True, encoding=tree.docinfo.encoding)
126
127 def _read_file(self, filepath):
128 """Read content of filepath from host."""
129 tmp_path = os.path.join(self.test.tmpdir, os.path.basename(filepath))
130 self.host.get_file(filepath, tmp_path, delete_dest=True)
131 with open(tmp_path) as f:
132 return f.read()
133
134 def _write_file(self, filepath, content, permission=None, owner=None):
135 """Write content to filepath on remote host.
136 @param permission: set permission to 0xxx octal number of remote file.
137 @param owner: set owner of remote file.
138 """
139 tmp_path = os.path.join(self.test.tmpdir, os.path.basename(filepath))
140 with open(tmp_path, 'w') as f:
141 f.write(content)
142 if permission is not None:
143 os.chmod(tmp_path, permission)
144 self.host.send_file(tmp_path, filepath, delete_dest=True)
145 if owner is not None:
146 self.host.run('chown', args=(owner, filepath))
147
148 def initialize(self):
149 """Filter out camera other than target facing on DUT."""
150 logging.info('Restart camera service with filter option')
151 self._write_file(
152 self.TEST_CONFIG_PATH,
153 json.dumps({
154 'enable_back_camera': self.facing == 'back',
155 'enable_front_camera': self.facing == 'front',
156 'enable_external_camera': False
157 }),
158 owner='arc-camera')
159 self.host.run('restart cros-camera')
160
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +0800161 logging.info('Replace camera profile in ARC++ container')
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +0800162 profile = self._read_file(self.CAMERA_PROFILE_PATH)
163 new_profile = self._filter_camera_profile(profile, self.facing)
164 self._write_file(self.CAMERA_PROFILE_PATH, new_profile)
165 self.host.run('restart ui')
166
167 def cleanup(self):
168 """Cleanup camera filter."""
169 logging.info('Remove filter option and restore camera service')
170 self.host.run('rm', args=('-f', self.TEST_CONFIG_PATH))
171 self.host.run('restart cros-camera')
172
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +0800173 logging.info('Restore camera profile in ARC++ container')
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +0800174 self.host.run('restart ui')