1# Copyright (c) 2013 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 shutil
8
9
10from autotest_lib.client.common_lib import error
11from autotest_lib.server import autotest
12from autotest_lib.server import test
13from autotest_lib.server.cros import sonic_extension_downloader
14
15
16class sonic_AppTest(test.test):
17    """Tests that a sonic device can start its apps."""
18    version = 1
19
20
21    def initialize(self, sonic_host, extension_dir=None):
22        """Download the latest extension, or use a local path if specified."""
23        # TODO: crbug.com/337708
24        if not extension_dir:
25            logging.info('Downloading ToT extension for test since no local '
26                         'extension specified.')
27            extension_path = os.path.join(self.job.clientdir, 'deps',
28                                          'sonic_extension')
29            sonic_extension_downloader.setup_extension(extension_path)
30            self.extension_path = extension_path
31        else:
32            logging.info('Using local extension for test %s.',
33                         self.extension_dir)
34            self.extension_path = None
35
36
37    def run_once(self, cros_host, sonic_host, app='ChromeCast', payload=None,
38                 extension_dir=None):
39        """Sonic test to start an app.
40
41        By default this test will test tab cast by installing an extension
42        on the cros host and using chromedriver to cast a tab. If another app
43        is specified, like YouTube or Netflix, the app is tested directly
44        through the server running on port 8080 on the sonic device.
45
46        @param app: The name of the application to start.
47            eg: YouTube
48        @param payload: The payload to send to the app.
49            eg: http://www.youtube.com
50        @param extension_dir: The directory to load a custom extension from.
51
52        @raises CmdExecutionError: If a command failed to execute on the host.
53        @raises TestError: If the app didn't start, or the app was unrecognized,
54            or the payload is invalid.
55        """
56        logging.info('Testing app %s, sonic_host %s and chromeos device %s ',
57                     app, sonic_host.hostname, cros_host.hostname)
58        if app == 'ChromeCast':
59            sonic_host.enable_test_extension()
60            client_at = autotest.Autotest(cros_host)
61            client_at.run_test('desktopui_SonicExtension',
62                               chromecast_ip=sonic_host.hostname,
63                               extension_dir=extension_dir)
64        elif payload and (app == 'Netflix' or app == 'YouTube'):
65            sonic_host.run('logcat -c')
66            sonic_host.client.start_app(app, payload)
67            log = sonic_host.run('logcat -d').stdout
68            app_started_confirmation = 'App started:'
69            for line in log.split('\n'):
70                if app_started_confirmation in line:
71                    logging.info('Successfully started app: %s', line)
72                    break
73            else:
74                logging.error(log)
75                raise error.TestError('App %s failed to start' % app)
76        else:
77            raise error.TestError('Cannot start app %s with payload %s' %
78                                  (app, payload))
79
80
81    def cleanup(self, cros_host, sonic_host, app='ChromeCast'):
82        sonic_host.client.stop_app(app)
83        if self.extension_path:
84            shutil.rmtree(self.extension_path, ignore_errors=True)
85