1# Copyright 2016 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 logging
6import os
7import re
8
9from autotest_lib.client.bin import utils
10from autotest_lib.client.common_lib import error
11from autotest_lib.client.common_lib.cros import arc
12from autotest_lib.client.cros.graphics import graphics_utils
13
14_CROS_BIN_DIR = '/usr/local/bin/'
15_SDCARD_DIR = '/sdcard/'
16_EXEC_DIR = '/data/executables/'
17_TEST_COMMAND = 'test -e'
18_POSSIBLE_BINARIES = ['gralloctest_amd64', 'gralloctest_arm', 'gralloctest_x86']
19
20# The tests still can be run individually, though we run with the 'all' option
21# Run ./gralloctest in Android to get a list of options.
22_OPTION = 'all'
23
24# GraphicsTest should go first as it will pass initialize/cleanup function
25# to ArcTest. GraphicsTest initialize would not be called if ArcTest goes first
26class graphics_Gralloc(graphics_utils.GraphicsTest, arc.ArcTest):
27    """gralloc test."""
28    version = 1
29    _executables = []
30
31    def arc_setup(self):
32        super(graphics_Gralloc, self).arc_setup()
33        # Get the executable from CrOS and copy it to Android container. Due to
34        # weird permission issues inside the container, we first have to copy
35        # the test to /sdcard/, then move it to a /data/ subdirectory we create.
36        # The permissions on the exectuable have to be modified as well.
37        arc.adb_root()
38        arc._android_shell('mkdir -p %s' % (_EXEC_DIR))
39        for binary in _POSSIBLE_BINARIES:
40            cros_path = os.path.join(_CROS_BIN_DIR, binary)
41            cros_cmd = '%s %s' % (_TEST_COMMAND, cros_path)
42            job = utils.run(cros_cmd, ignore_status=True)
43            if job.exit_status:
44                continue
45
46            sdcard_path = os.path.join(_SDCARD_DIR, binary)
47            arc.adb_cmd('-e push %s %s' % (cros_path, sdcard_path))
48
49            exec_path = os.path.join(_EXEC_DIR, binary)
50            arc._android_shell('mv %s %s' % (sdcard_path, exec_path))
51            arc._android_shell('chmod o+rwx %s' % (exec_path))
52            self._executables.append(exec_path)
53
54    def arc_teardown(self):
55        for executable in self._executables:
56        # Remove test contents from Android container.
57            arc._android_shell('rm -rf %s' % (executable))
58        super(graphics_Gralloc, self).arc_teardown()
59
60    def run_once(self):
61        gpu_family = utils.get_gpu_family()
62        if not self._executables:
63            raise error.TestFail('Failed: No executables found on %s' %
64                                  gpu_family)
65
66        logging.debug('Running %d executables', len(self._executables))
67        for executable in self._executables:
68            try:
69                cmd = '%s %s' % (executable, _OPTION)
70                stdout = arc._android_shell(cmd)
71            except Exception:
72                logging.error('Exception running %s', cmd)
73                raise error.TestFail('Failed: gralloc on %s' % gpu_family)
74            # Look for the regular expression indicating failure.
75            for line in stdout.splitlines():
76                match = re.search(r'\[  FAILED  \]', stdout)
77                if match:
78                    self.add_failures(line)
79                    logging.error(line)
80                else:
81                    logging.debug(stdout)
82
83        if self.get_failures():
84            raise error.TestFail('Failed: gralloc on %s in %s.' %
85                                 (gpu_family, self.get_failures()))
86