1# Copyright (c) 2014 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 os
7import os.path
8import time
9
10from autotest_lib.client.bin import test, utils
11from autotest_lib.client.common_lib import error
12from autotest_lib.client.common_lib.cros import chrome
13
14
15class video_VimeoVideo(test.test):
16    """This test verifies Vimeo video.
17
18    - verify video playback.
19    - verify player states.
20
21    """
22    version = 1
23    _PLAYER_PLAY_STATE = 'play'
24    _PLAYER_PAUSE_STATE = 'pause'
25    _PLAYBACK_TEST_TIME_S = 10
26    _WAIT_TIMEOUT_S = 10
27    _WPR_ARCHIVE = '/usr/local/insight.wpr'
28
29
30    def _get_player_status(self):
31        """Returns the player status."""
32        return self._tab.EvaluateJavaScript('vimeo_player.status')
33
34
35    def _wait_for_player(self):
36        """Wait for the player to load."""
37        self._tab.WaitForJavaScriptCondition(
38                'typeof vimeo_player !== \'undefined\'',
39                timeout=self._WAIT_TIMEOUT_S)
40
41    def _wait_for_player_status(self, expected_status):
42        """"Wait for expected player status.
43
44        @param expected_status: expected player status to wait for.
45        """
46        utils.poll_for_condition(
47                lambda: self._get_player_status() == expected_status,
48                exception=error.TestError(
49                        'Vimeo player failed to obtain %s status. '
50                        'Current player status is %s.' %
51                        (expected_status, self._get_player_status())),
52                timeout=self._WAIT_TIMEOUT_S,
53                sleep_interval=1)
54
55
56    def _video_current_time(self):
57        "Returns current video time."""
58        self._tab.WaitForJavaScriptCondition(
59                'typeof vimeo_player.duration == \'number\'',
60                timeout=self._WAIT_TIMEOUT_S)
61        return float(self._tab.EvaluateJavaScript('vimeo_player.duration'))
62
63
64    def run_vimeo_tests(self, browser):
65        """Run Vimeo video sanity tests.
66
67        @param browser: The Browser object to run the test with.
68
69        """
70        self._tab = browser.tabs[0]
71        self._tab.Navigate(browser.platform.http_server.UrlOf(
72                os.path.join(self.bindir, 'vimeo.html')))
73        self._wait_for_player()
74        self._wait_for_player_status(self._PLAYER_PLAY_STATE)
75        # Abort the test if video is not playing.
76        utils.poll_for_condition(
77                lambda: self._video_current_time() > 5.0,
78                exception=error.TestError(
79                        'Init: video isn\'t playing.'),
80                timeout=self._WAIT_TIMEOUT_S,
81                sleep_interval=1)
82
83        #TODO: mussa crbug/445636 Get pausing and playing again to work
84        """ Pausing and Playing again under WPR currently causes vimeo to throw
85        an error and will stop the playback and fail the test.
86        Need to understand if we can make this work with WPR.
87        """
88
89        playback = 0 # seconds
90        prev_playback = 0
91        logging.info('video current time before loop: %s',
92                     self._video_current_time())
93        while (playback < self._PLAYBACK_TEST_TIME_S):
94            if self._video_current_time() <= prev_playback:
95                utils.poll_for_condition(
96                        lambda: self._video_current_time() > prev_playback,
97                        exception=error.TestError(
98                                'Long Wait: Video is not playing.'),
99                        timeout=self._WAIT_TIMEOUT_S,
100                        sleep_interval=1)
101            prev_playback = self._video_current_time()
102            logging.info('video curr time before sleep n prev playback: %s %s',
103                         self._video_current_time(), prev_playback)
104            time.sleep(1)
105            logging.info('video current time after sleep: %s',
106                         self._video_current_time())
107            playback = playback + 1
108
109
110    def run_once(self):
111        with chrome.Chrome(init_network_controller=True) as cr:
112            cr.browser.platform.SetHTTPServerDirectories(self.bindir)
113            self.run_vimeo_tests(cr.browser)
114