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 time
7
8from autotest_lib.client.bin import test
9from autotest_lib.client.bin import utils
10from autotest_lib.client.common_lib import error
11from autotest_lib.client.common_lib.cros import chrome
12from autotest_lib.client.cros.input_playback import input_playback
13
14
15class logging_FeedbackReport(test.test):
16    """Tests if feedback report can be opened with no crashes in browser."""
17    version = 1
18    _FEEDBACK_ID = 'gfdkimpbcpahaombhbimeihdjnejgicl'
19    _FEEDBACK_STATE_TIMEOUT = 40
20    _WAIT = 5
21
22    def warmup(self):
23        """Test setup."""
24        # Emulate keyboard to open feedback app.
25        # See input_playback. The keyboard is used to play back shortcuts.
26        self._player = input_playback.InputPlayback()
27        self._player.emulate(input_type='keyboard')
28        self._player.find_connected_inputs()
29
30    def _open_feedback(self):
31        """Use keyboard shortcut to emulate input to open feedback app."""
32        self._player.blocking_playback_of_default_file(
33            input_type='keyboard', filename='keyboard_alt+shift+i')
34
35    def _check_feedback_elements(self):
36        """
37        Return whether feedback app is open or not.
38
39        @returns: True if all elements are present, else False.
40
41        """
42        # Verifying feedback app window is open.
43        if not self.feedback_app.EvaluateJavaScript('document.body != null'):
44            logging.info('Window not enabled.')
45            return False
46
47        # Verifying UI elements in window are enabled.
48        elements = ['cancel-button', 'send-report-button',
49                    'description-text']
50        for element in elements:
51            js = "document.getElementById('%s') != null" % element
52            if not self.feedback_app.EvaluateJavaScript(js):
53                logging.info("%s not enabled.", element)
54                return False
55
56        return True
57
58    def _confirm_feedback_state(self):
59        """
60        Fail test if feedback elements have not been found.
61
62        @raises: error.TestFail if feedback app not found.
63
64        """
65        utils.poll_for_condition(
66                lambda: self._check_feedback_elements(),
67                exception=error.TestFail('Feedback elements not enabled.'),
68                timeout=self._FEEDBACK_STATE_TIMEOUT)
69
70    def run_once(self):
71        """Run the test."""
72        with chrome.Chrome(disable_default_apps=False) as cr:
73            # Open and confirm feedback app is working.
74            time.sleep(self._WAIT)
75            self._open_feedback()
76            time.sleep(self._WAIT)
77            cr_exts = cr.browser.extensions
78            self.feedback_app = None
79            for extension in cr_exts.GetByExtensionId(self._FEEDBACK_ID):
80                url = extension.EvaluateJavaScript('location.href;')
81                if url.endswith('default.html'):
82                    self.feedback_app = extension
83                    break
84
85            if self.feedback_app is None:
86                raise error.TestError("Incorrect feedback id list.")
87            self._confirm_feedback_state()
88
89    def cleanup(self):
90        """Test cleanup."""
91        self._player.close()
92