• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 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#
5# assistant_util.py is supposed to be called from chrome.py for Assistant
6# specific logic.
7
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.common_lib import utils
10from telemetry.core import exceptions
11
12
13def enable_assistant(autotest_ext):
14    """Enables Google Assistant.
15
16    @param autotest_ext private autotest extension.
17    @raise error.TestFail if failed to start Assistant service within time.
18    """
19    if autotest_ext is None:
20        raise error.TestFail('Could not start Assistant service because '
21                             'autotest extension is not available.')
22
23    try:
24        autotest_ext.ExecuteJavaScript('''
25            window.__assistant_ready = 0;
26            chrome.autotestPrivate.setAssistantEnabled(true,
27                10 * 1000 /* timeout_ms */,
28                () => {
29                    if (chrome.runtime.lastError) {
30                      window.__assistant_ready = -1;
31                      window.__assistant_error_msg =
32                            chrome.runtime.lastError.message;
33                    } else {
34                      window.__assistant_ready = 1;
35                    }
36                });
37        ''')
38    except exceptions.EvaluateException as e:
39        raise error.TestFail('Could not start Assistant "%s".' % e)
40
41    ready = utils.poll_for_condition(
42                lambda: autotest_ext.EvaluateJavaScript(
43                    'window.__assistant_ready'),
44                desc='Wait for the assistant running state to return.')
45
46    if ready == -1:
47        raise error.TestFail(
48                autotest_ext.EvaluateJavaScript(
49                        'window.__assistant_error_msg'))
50
51
52def enable_hotword(autotest_ext):
53    """Enables hotword in Google Assistant.
54
55    @param autotest_ext private autotest extension.
56    @raise error.TestFail if failed to enable hotword feature within time.
57    """
58    try:
59        autotest_ext.ExecuteJavaScript('''
60            window.__assistant_hotword_ready = 0;
61            chrome.autotestPrivate.setWhitelistedPref(
62              'settings.voice_interaction.hotword.enabled', true,
63              function(response) {
64                if (chrome.runtime.lastError) {
65                  window.__assistant_hotword_ready = -1;
66                  window.__assistant_hotword_error_msg =
67                      chrome.runtime.lastError.message;
68                } else {
69                  window.__assistant_hotword_ready = 1;
70                }
71              });
72            ''')
73    except exceptions.EvaluateException as e:
74        raise error.TestFail('Could not enable Hotword "{}".'.format(e))
75
76    ready = utils.poll_for_condition(
77            lambda: autotest_ext.EvaluateJavaScript(
78                    'window.__assistant_hotword_ready'),
79            desc='Wait for the hotword pref change event to return".')
80
81    if ready == -1:
82        raise error.TestFail(
83                autotest_ext.EvaluateJavaScript(
84                        'window.__assistant_hotword_error_msg'))
85
86
87def send_text_query(autotest_ext, text_query):
88    """Sends text query to Assistant and returns response.
89
90    @param autotest_ext private autotest extension.
91    @param text_query text query.
92    @return dictionary containing the information of Assistant query
93            response, mapping from response type to content.
94    """
95    try:
96        autotest_ext.ExecuteJavaScript('''
97            window.__assistant_response_ready = 0;
98            chrome.autotestPrivate.sendAssistantTextQuery('%s', 10 * 1000,
99                function(response) {
100                  if (chrome.runtime.lastError) {
101                    window.__assistant_response_ready = -1;
102                    window.__assistant_error_msg =
103                        chrome.runtime.lastError.message;
104                  } else {
105                    window.__assistant_response_ready = 1;
106                    window.__query_response = response;
107                  }
108                });
109            ''' % text_query)
110    except exceptions.EvaluateException as e:
111        raise error.TestFail('Could not get Assistant response "%s".' % e)
112
113    is_ready = utils.poll_for_condition(
114            lambda: autotest_ext.EvaluateJavaScript(
115                    'window.__assistant_response_ready'),
116            desc='Waiting for Assistant response.')
117
118    if is_ready == -1:
119        raise error.TestFail(
120                autotest_ext.EvaluateJavaScript(
121                        'window.__assistant_error_msg'))
122
123    return autotest_ext.EvaluateJavaScript('window.__query_response')
124