1# Copyright 2018 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
6
7from autotest_lib.client.bin import utils
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.common_lib.cros import chrome
10from autotest_lib.client.cros import touch_playback_test_base
11
12
13class touch_GestureNav(touch_playback_test_base.touch_playback_test_base):
14    """Test to verify the three finger tab switching touchpad gesture."""
15    version = 1
16
17    _WAIT_FOR_COMPLETE_NAV = 5
18
19    def _is_testable(self):
20        """Returns True if the test can run on this device, else False."""
21        if not self._has_touchpad:
22            raise error.TestError('No touchpad found on this device!')
23
24        # Check if playback files are available on DUT to run test.
25        self._filepaths = self._find_test_files_from_directions('touchpad',
26                'two-finger-longswipe-%s', ['back', 'fwd'])
27        if not self._filepaths:
28            logging.info('Missing gesture files. Aborting test.')
29            return False
30
31        return True
32
33
34    def _check_tab_navigate(self, to_url, direction=''):
35        """
36        Verify tab navigation behavior. Moving two fingers one or
37        another direction will navigate back or forward in the tab.
38
39        @param direction: the swipe direction and the input file suffix.
40        @param to_url: url to verify the swipe navigated to.
41
42        """
43        self.tab.WaitForDocumentReadyStateToBeComplete()
44        fail_msg = 'Incorrect tab navigating %s to %s' % (direction, to_url)
45        utils.poll_for_condition(
46                lambda: self.tab.url.encode('utf8').rstrip('/') == to_url,
47                exception=error.TestFail(fail_msg),
48                timeout=self._WAIT_FOR_COMPLETE_NAV)
49
50
51    def run_once(self):
52        """Entry point of this test."""
53        if not self._is_testable():
54            return
55
56        # Log in and start test.
57        with chrome.Chrome(autotest_ext=True,
58                           init_network_controller=True) as cr:
59            self.tab = cr.browser.tabs[0]
60
61            url_back = 'https://www.youtube.com'
62            url_fwd = 'https://www.google.com'
63
64            # Navigate to two urls in the same tab
65            self.tab.Navigate(url_back)
66            self._check_tab_navigate(url_back)
67            self.tab.Navigate(url_fwd)
68            self._check_tab_navigate(url_fwd)
69
70            # Swipe to navigate back
71            self._blocking_playback(touch_type='touchpad',
72                        filepath=self._filepaths['back'])
73            self._check_tab_navigate(url_back, 'back')
74
75            # Swipe to navigate forward
76            self._blocking_playback(touch_type='touchpad',
77                        filepath=self._filepaths['fwd'])
78            self._check_tab_navigate(url_fwd, 'fwd')
79