blob: 9f78c4da9cda01112581613e08977144ed33105b [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
13
14
15class ChartFixture:
16 """Sets up chart tablet to display dummy scene image."""
17 DISPLAY_SCRIPT = '/usr/local/autotest/bin/display_chart.py'
18
19 def __init__(self, chart_host, scene_uri):
20 self.host = chart_host
21 self.scene_uri = scene_uri
22 self.display_pid = None
23
24 def initialize(self):
25 """Prepare scene file and display it on chart host."""
26 logging.info('Prepare scene file')
27 chart_dir = self.host.get_tmp_dir()
28 scene_path = os.path.join(
29 chart_dir, self.scene_uri[self.scene_uri.rfind('/') + 1:])
30 self.host.run('wget', args=('-O', scene_path, self.scene_uri))
31 self.host.run('chmod', args=('-R', '755', chart_dir))
32
33 logging.info('Display scene file')
34 self.display_pid = self.host.run_background(
35 'python %s %s' % (self.DISPLAY_SCRIPT, scene_path))
36 # TODO(inker): Suppose chart should be displayed very soon. Or require
37 # of waiting until chart actually displayed.
38
39 def cleanup(self):
40 """Cleanup display script."""
41 if self.display_pid is not None:
42 self.host.run('kill', args=('-2', str(self.display_pid)))
43
44
45def get_chart_address(host_address, args):
Kuo Jen Wei3e89c442019-01-29 14:27:26 +080046 """Get address of chart tablet from commandline args or mapping logic in
47 test lab.
48
49 @param host_address: a list of hostname strings.
50 @param args: a dict parse from commandline args.
51 @return:
52 A list of strings for chart tablet addresses.
53 """
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080054 address = utils.args_to_dict(args).get('chart')
55 if address is not None:
56 return address.split(',')
57 elif utils.is_in_container():
58 return [
Kuo Jen Wei3e89c442019-01-29 14:27:26 +080059 utils.get_lab_chart_address(host)
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080060 for host in host_address
61 ]
62 else:
63 return None
64
65
66class DUTFixture:
67 """Sets up camera filter for target camera facing on DUT."""
68 TEST_CONFIG_PATH = '/var/cache/camera/test_config.json'
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080069 CAMERA_PROFILE_PATH = ('/mnt/stateful_partition/encrypted/var/cache/camera'
70 '/media_profiles.xml')
71
72 def __init__(self, test, host, facing):
73 self.test = test
74 self.host = host
75 self.facing = facing
76
77 @contextlib.contextmanager
78 def _set_selinux_permissive(self):
79 selinux_mode = self.host.run_output('getenforce')
80 self.host.run('setenforce 0')
81 yield
82 self.host.run('setenforce', args=(selinux_mode, ))
83
84 def _filter_camera_profile(self, content, facing):
85 """Filter camera profile of target facing from content of camera
86 profile.
87
88 @return:
89 New camera profile with only target facing, camera ids are
90 renumbered from 0.
91 """
92 tree = etree.parse(
93 StringIO.StringIO(content),
94 parser=etree.XMLParser(compact=False))
95 root = tree.getroot()
96 profiles = root.findall('CamcorderProfiles')
97 logging.debug('%d number of camera(s) found in camera profile',
98 len(profiles))
99 assert 1 <= len(profiles) <= 2
100 if len(profiles) == 2:
101 cam_id = 0 if facing == 'back' else 1
102 for p in profiles:
103 if cam_id == int(p.attrib['cameraId']):
104 p.attrib['cameraId'] = '0'
105 else:
106 root.remove(p)
107 else:
108 with self.test._login_chrome(
109 board=self.test._get_board_name(),
110 reboot=False), self._set_selinux_permissive():
111 has_front_camera = (
112 'feature:android.hardware.camera.front' in self.host.
113 run_output('android-sh -c "pm list features"'))
114 logging.debug('has_front_camera=%s', has_front_camera)
115 if (facing == 'front') != has_front_camera:
116 root.remove(profiles[0])
117 return etree.tostring(
118 tree, xml_declaration=True, encoding=tree.docinfo.encoding)
119
120 def _read_file(self, filepath):
121 """Read content of filepath from host."""
122 tmp_path = os.path.join(self.test.tmpdir, os.path.basename(filepath))
123 self.host.get_file(filepath, tmp_path, delete_dest=True)
124 with open(tmp_path) as f:
125 return f.read()
126
127 def _write_file(self, filepath, content, permission=None, owner=None):
128 """Write content to filepath on remote host.
129 @param permission: set permission to 0xxx octal number of remote file.
130 @param owner: set owner of remote file.
131 """
132 tmp_path = os.path.join(self.test.tmpdir, os.path.basename(filepath))
133 with open(tmp_path, 'w') as f:
134 f.write(content)
135 if permission is not None:
136 os.chmod(tmp_path, permission)
137 self.host.send_file(tmp_path, filepath, delete_dest=True)
138 if owner is not None:
139 self.host.run('chown', args=(owner, filepath))
140
141 def initialize(self):
142 """Filter out camera other than target facing on DUT."""
143 logging.info('Restart camera service with filter option')
144 self._write_file(
145 self.TEST_CONFIG_PATH,
146 json.dumps({
147 'enable_back_camera': self.facing == 'back',
148 'enable_front_camera': self.facing == 'front',
149 'enable_external_camera': False
150 }),
151 owner='arc-camera')
152 self.host.run('restart cros-camera')
153
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +0800154 logging.info('Replace camera profile in ARC++ container')
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +0800155 profile = self._read_file(self.CAMERA_PROFILE_PATH)
156 new_profile = self._filter_camera_profile(profile, self.facing)
157 self._write_file(self.CAMERA_PROFILE_PATH, new_profile)
158 self.host.run('restart ui')
159
160 def cleanup(self):
161 """Cleanup camera filter."""
162 logging.info('Remove filter option and restore camera service')
163 self.host.run('rm', args=('-f', self.TEST_CONFIG_PATH))
164 self.host.run('restart cros-camera')
165
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +0800166 logging.info('Restore camera profile in ARC++ container')
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +0800167 self.host.run('restart ui')