blob: 9f78c4da9cda01112581613e08977144ed33105b [file] [log] [blame]
# Copyright 2018 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import contextlib
import json
import logging
from lxml import etree
import os
import StringIO
from autotest_lib.client.common_lib import utils
class ChartFixture:
"""Sets up chart tablet to display dummy scene image."""
DISPLAY_SCRIPT = '/usr/local/autotest/bin/'
def __init__(self, chart_host, scene_uri): = chart_host
self.scene_uri = scene_uri
self.display_pid = None
def initialize(self):
"""Prepare scene file and display it on chart host."""'Prepare scene file')
chart_dir =
scene_path = os.path.join(
chart_dir, self.scene_uri[self.scene_uri.rfind('/') + 1:])'wget', args=('-O', scene_path, self.scene_uri))'chmod', args=('-R', '755', chart_dir))'Display scene file')
self.display_pid =
'python %s %s' % (self.DISPLAY_SCRIPT, scene_path))
# TODO(inker): Suppose chart should be displayed very soon. Or require
# of waiting until chart actually displayed.
def cleanup(self):
"""Cleanup display script."""
if self.display_pid is not None:'kill', args=('-2', str(self.display_pid)))
def get_chart_address(host_address, args):
"""Get address of chart tablet from commandline args or mapping logic in
test lab.
@param host_address: a list of hostname strings.
@param args: a dict parse from commandline args.
A list of strings for chart tablet addresses.
address = utils.args_to_dict(args).get('chart')
if address is not None:
return address.split(',')
elif utils.is_in_container():
return [
for host in host_address
return None
class DUTFixture:
"""Sets up camera filter for target camera facing on DUT."""
TEST_CONFIG_PATH = '/var/cache/camera/test_config.json'
CAMERA_PROFILE_PATH = ('/mnt/stateful_partition/encrypted/var/cache/camera'
def __init__(self, test, host, facing):
self.test = test = host
self.facing = facing
def _set_selinux_permissive(self):
selinux_mode ='getenforce')'setenforce 0')
yield'setenforce', args=(selinux_mode, ))
def _filter_camera_profile(self, content, facing):
"""Filter camera profile of target facing from content of camera
New camera profile with only target facing, camera ids are
renumbered from 0.
tree = etree.parse(
root = tree.getroot()
profiles = root.findall('CamcorderProfiles')
logging.debug('%d number of camera(s) found in camera profile',
assert 1 <= len(profiles) <= 2
if len(profiles) == 2:
cam_id = 0 if facing == 'back' else 1
for p in profiles:
if cam_id == int(p.attrib['cameraId']):
p.attrib['cameraId'] = '0'
with self.test._login_chrome(
reboot=False), self._set_selinux_permissive():
has_front_camera = (
'' in
run_output('android-sh -c "pm list features"'))
logging.debug('has_front_camera=%s', has_front_camera)
if (facing == 'front') != has_front_camera:
return etree.tostring(
tree, xml_declaration=True, encoding=tree.docinfo.encoding)
def _read_file(self, filepath):
"""Read content of filepath from host."""
tmp_path = os.path.join(self.test.tmpdir, os.path.basename(filepath)), tmp_path, delete_dest=True)
with open(tmp_path) as f:
def _write_file(self, filepath, content, permission=None, owner=None):
"""Write content to filepath on remote host.
@param permission: set permission to 0xxx octal number of remote file.
@param owner: set owner of remote file.
tmp_path = os.path.join(self.test.tmpdir, os.path.basename(filepath))
with open(tmp_path, 'w') as f:
if permission is not None:
os.chmod(tmp_path, permission), filepath, delete_dest=True)
if owner is not None:'chown', args=(owner, filepath))
def initialize(self):
"""Filter out camera other than target facing on DUT."""'Restart camera service with filter option')
'enable_back_camera': self.facing == 'back',
'enable_front_camera': self.facing == 'front',
'enable_external_camera': False
owner='arc-camera')'restart cros-camera')'Replace camera profile in ARC++ container')
profile = self._read_file(self.CAMERA_PROFILE_PATH)
new_profile = self._filter_camera_profile(profile, self.facing)
self._write_file(self.CAMERA_PROFILE_PATH, new_profile)'restart ui')
def cleanup(self):
"""Cleanup camera filter."""'Remove filter option and restore camera service')'rm', args=('-f', self.TEST_CONFIG_PATH))'restart cros-camera')'Restore camera profile in ARC++ container')'restart ui')