1# Lint as: python2, python3
2# Copyright 2019 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5import logging
6import time
7
8from autotest_lib.server.cros.audio import audio_test
9from autotest_lib.client.cros.audio import audio_test_data
10from autotest_lib.client.cros.chameleon import chameleon_audio_helper
11from autotest_lib.client.cros.chameleon import chameleon_audio_ids
12from autotest_lib.client.common_lib import error
13from autotest_lib.client.common_lib import utils
14from autotest_lib.server import test
15from autotest_lib.server.cros.multimedia import remote_facade_factory
16
17class audio_AudioBasicAssistant(audio_test.AudioTest):
18    """A basic assistant voice command test."""
19    version = 1
20    DELAY_AFTER_BINDING_SECS = 0.5
21    DELAY_AFTER_COMMAND_SECS = 3
22    GOOGLE_URL = 'google.com'
23
24    def run_once(self, host, enable_dsp_hotword=False):
25        """Run Basic Audio Assistant Test.
26
27        @param host: Device under test CrosHost
28        @param enable_dsp_hotword: A bool to control the usage of dsp for
29                hotword.
30        """
31        # TODO(paulhsia): Use gtts to generate command in runtime
32        hotword_file = audio_test_data.HOTWORD_OPEN_TAB_TEST_FILE
33        chameleon_board = host.chameleon
34        factory = remote_facade_factory.RemoteFacadeFactory(
35                host, results_dir=self.resultsdir)
36
37        # Prepares chameleon speaker resource
38        chameleon_board.setup_and_reset(self.outputdir)
39        widget_factory = chameleon_audio_helper.AudioWidgetFactory(
40                factory, host)
41        source = widget_factory.create_widget(
42                chameleon_audio_ids.ChameleonIds.LINEOUT)
43        sink = widget_factory.create_widget(
44                chameleon_audio_ids.PeripheralIds.SPEAKER)
45        binder = widget_factory.create_binder(source, sink)
46
47        # Restarts Chrome with assistant enabled and enables hotword
48        assistant_facade = factory.create_assistant_facade()
49        assistant_facade.restart_chrome_for_assistant(enable_dsp_hotword)
50        assistant_facade.enable_hotword()
51
52        browser_facade = factory.create_browser_facade()
53
54        # Tests voice command with chameleon
55        with chameleon_audio_helper.bind_widgets(binder):
56            time.sleep(self.DELAY_AFTER_BINDING_SECS)
57            logging.info('Setting hotword playback data on Chameleon')
58            remote_hotword_file_path = source.set_playback_data(hotword_file)
59            source.start_playback_with_path(remote_hotword_file_path)
60            time.sleep(hotword_file.duration_secs)
61        time.sleep(self.DELAY_AFTER_COMMAND_SECS)
62
63        # Checks urls in open tabs
64        urls = browser_facade.get_tab_urls()
65        if len(urls) != 2:
66            raise error.TestFail('There should be an empty tab and a tab with '
67                     'Google.')
68        # Checks if google.com exists in tabs
69        for url in urls:
70            logging.debug(url)
71            if self.GOOGLE_URL in url.lower():
72                break
73        else:
74            raise error.TestFail('There is no google.com opened in tabs.')
75