• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = 2
21    _FEEDBACK_SENT_URL = 'support.google.com/chromebook/answer/3142217'
22
23    def warmup(self):
24        """Test setup."""
25        # Emulate keyboard to open feedback app.
26        # See input_playback. The keyboard is used to play back shortcuts.
27        self._player = input_playback.InputPlayback()
28        self._player.emulate(input_type='keyboard')
29        self._player.find_connected_inputs()
30
31    def _open_feedback(self):
32        """Use keyboard shortcut to emulate input to open feedback app."""
33        self._player.blocking_playback_of_default_file(
34            input_type='keyboard', filename='keyboard_alt+shift+i')
35
36    def _check_feedback_elements(self):
37        """
38        Return whether feedback app is open or not.
39
40        @returns: True if all elements are present, else False.
41
42        """
43        # Verifying feedback app window is open.
44        if not self.feedback_app.EvaluateJavaScript('document.body != null'):
45            logging.info('Window not enabled.')
46            return False
47
48        # Verifying UI elements in window are enabled.
49        elements = ['cancel-button', 'send-report-button',
50                    'description-text']
51        for element in elements:
52            js = "document.getElementById('%s') != null" % element
53            if not self.feedback_app.EvaluateJavaScript(js):
54                logging.info("%s not enabled.", element)
55                return False
56
57        return True
58
59    def _confirm_feedback_state(self):
60        """
61        Fail test if feedback elements have not been found.
62
63        @raises: error.TestFail if feedback app not found.
64
65        """
66        utils.poll_for_condition(
67                lambda: self._check_feedback_elements(),
68                exception=error.TestFail('Feedback elements not enabled.'),
69                timeout=self._FEEDBACK_STATE_TIMEOUT)
70
71    def _enter_feedback_text(self):
72        """Enter Feedback message in the Text field"""
73        time.sleep(self._WAIT)
74        self._player.blocking_playback_of_default_file(
75               input_type='keyboard', filename='keyboard_T+e+s+t')
76
77    def _press_enter(self):
78        """Use keyboard shortcut to press Enter."""
79        self._player.blocking_playback_of_default_file(
80            input_type='keyboard', filename='keyboard_enter')
81
82    def _press_shift_tab(self):
83        """Use keyboard shortcut to press Shift-Tab."""
84        self._player.blocking_playback_of_default_file(
85            input_type='keyboard', filename='keyboard_shift+tab')
86
87    def _submit_feedback(self):
88        """Click on Send button to submit Feedback Report using keyboard input"""
89        self._enter_feedback_text()
90        self._press_shift_tab()
91        self._press_enter()
92
93    def _is_feedback_sent(self, start_time, timeout):
94        """Checks feedback is sent within timeout
95
96        @param start_time: beginning timestamp
97        @param timeout: duration of URL checks
98
99        @returns: True if feedback sent page is present
100        """
101        while True:
102            time.sleep(self._WAIT)
103            for tab in self.cr.browser.tabs:
104                if self._FEEDBACK_SENT_URL in tab.url:
105                    return True
106            if time.time() - start_time >= timeout:
107                break;
108        return False
109
110    def run_once(self):
111        """Run the test."""
112        with chrome.Chrome(disable_default_apps=False) as self.cr:
113            # Open and confirm feedback app is working.
114            self._open_feedback()
115            cr_exts = self.cr.browser.extensions
116            self.feedback_app = None
117            for extension in cr_exts.GetByExtensionId(self._FEEDBACK_ID):
118                url = extension.EvaluateJavaScript('location.href;')
119                if url.endswith('default.html'):
120                    self.feedback_app = extension
121                    break
122
123            if self.feedback_app is None:
124                raise error.TestError("Incorrect feedback id list.")
125            self._confirm_feedback_state()
126            self._submit_feedback()
127
128            start_time = time.time()
129            if not self._is_feedback_sent(start_time, self._WAIT * 30):
130                raise error.TestFail("Feedback NOT sent!")
131
132    def cleanup(self):
133        """Test cleanup."""
134        self._player.close()
135