1# Copyright 2015 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
6from autotest_lib.client.cros.video import video_player
7
8
9class NativeHtml5Player(video_player.VideoPlayer):
10    """
11    Provides an interface to interact with native html5 player in chrome.
12
13    """
14
15
16    def inject_source_file(self):
17        """
18        Injects the path to the video file under test into the html doc.
19
20
21        """
22        self.tab.ExecuteJavaScript(
23            'loadVideoSource("%s")' % self.video_src_path)
24
25
26    def is_video_ready(self):
27        """
28        Determines if a native html5 video is ready by using javascript.
29
30        returns: bool, True if video is ready, else False.
31
32        """
33        return self.tab.EvaluateJavaScript('canplay()')
34
35
36    def is_javascript_ready(self):
37        """
38        returns: True if javascript variables and functions have been defined,
39
40        else False.
41
42        """
43        return self.tab.EvaluateJavaScript(
44                    'typeof script_ready!="undefined" && script_ready == true')
45
46
47    def play(self):
48        """
49        Plays the video.
50
51        """
52        self.tab.ExecuteJavaScript('play()')
53
54
55    def pause(self):
56        """
57        Pauses the video.
58
59        """
60        self.tab.ExecuteJavaScript('pause()')
61
62
63    def paused(self):
64        """
65        Checks whether video paused.
66
67        """
68        cmd = "%s.paused" % self.video_id
69        return self.tab.EvaluateJavaScript(cmd)
70
71
72    def ended(self):
73        """
74        Checks whether video paused.
75
76        """
77        cmd = "%s.ended" % self.video_id
78        return self.tab.EvaluateJavaScript(cmd)
79
80
81    def currentTime(self):
82        """
83        Returns the current time of the video element.
84
85        """
86        return self.tab.EvaluateJavaScript('currentTime()')
87
88
89    def seek_to(self, t):
90        """
91        Seeks a video to a time stamp.
92
93        @param t: timedelta, time value to seek to.
94
95        """
96        cmd = "%s.currentTime=%.3f" % (self.video_id, t.total_seconds())
97        self.tab.ExecuteJavaScript(cmd)
98
99
100    def has_video_finished_seeking(self):
101        """
102        Determines if the video has finished seeking.
103
104        """
105        return self.tab.EvaluateJavaScript('finishedSeeking()')
106
107
108    def wait_for_error(self):
109        """
110        Determines if the video has any errors
111
112        """
113        return self.tab.WaitForJavaScriptCondition('errorDetected();',
114                                                   timeout=30)
115
116
117    def reload_page(self):
118        """
119        Reloads current page
120
121        """
122        self.tab.ExecuteJavaScript('location.reload()')
123
124
125    def enable_VideoControls(self):
126        """
127        For enabling controls
128
129        """
130        self.tab.ExecuteJavaScript('setControls()')
131
132
133    def dropped_frame_count(self):
134        """
135        Gets the number of dropped frames.
136
137        @returns: An integer indicates the number of dropped frame.
138
139        """
140        cmd = "%s.webkitDroppedFrameCount" % self.video_id
141        return self.tab.EvaluateJavaScript(cmd)
142
143
144    def duration(self):
145        """
146        Gets the duration of the video.
147
148        @returns: An number indicates the duration of the video.
149
150        """
151        cmd = "%s.duration" % self.video_id
152        return self.tab.EvaluateJavaScript(cmd)
153
154
155    def wait_video_ended(self):
156        """
157        Waits until the video playback is ended.
158
159        """
160        cmd = "%s.ended" % self.video_id
161        self.tab.WaitForJavaScriptCondition(cmd, timeout=(self.duration() * 2))
162