1// Copyright (c) 2012 The Chromium 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'use strict';
6
7/**
8 * Test API for Chrome OS Video Player and Audio Player.
9 *
10 * To test the Video Player open a tab with the URL:
11 *   chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj/video_player.html
12 *
13 * To test the Audio Player open a tab with the URL:
14 *   chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj/mediaplayer.html
15 *
16 */
17var playerTestAPI = {
18
19  /* Methods common for audio and video players */
20
21  /**
22   * Respond with the path to the current media source.
23   */
24  getSrc: function() {
25    playerTestAPI.respond_(util.extractFilePath(playerTestAPI.getMedia_().src));
26  },
27
28  /**
29   * Respond with a boolean value, true if the media is playing.
30   */
31  isPlaying: function() {
32    playerTestAPI.respond_(playerTestAPI.getControls_().isPlaying());
33  },
34
35  /**
36   * Play the media.
37   */
38  play: function() {
39    playerTestAPI.getControls_().play();
40  },
41
42  /**
43   * Pause the playback.
44   */
45  pause: function() {
46    playerTestAPI.getControls_().pause();
47  },
48
49  /**
50   * Respond with a number, duration of the media in seconds.
51   */
52  getDuration: function() {
53    playerTestAPI.respond_(playerTestAPI.getMedia_().duration);
54  },
55
56  /**
57   * Respond with a number, current media position in seconds.
58   */
59  getCurrentTime: function() {
60    playerTestAPI.respond_(playerTestAPI.getMedia_().currentTime);
61  },
62
63  /**
64   * Change media position.
65   * @param {number} time Media positions.
66   */
67  seekTo: function(time) {
68    playerTestAPI.getMedia_().currentTime = time;
69  },
70
71  /* Video player-specific methods.
72   *
73   * To test the video player open a tab with the url:
74   * chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj/mediaplayer.html
75   *
76   */
77
78  /**
79   * Load the specified file in the video player,
80   * Starts playing immediately.
81   * @param {string} filePath File path.
82   */
83  loadVideo: function(filePath) {
84    var url = util.makeFilesystemUrl(filePath);
85    location.href = location.origin + location.pathname + '?' + url;
86    reload();
87  },
88
89  /**
90   * Respond with a number, current volume [0..100].
91   */
92  getVolume: function() {
93    playerTestAPI.respond_(playerTestAPI.getMedia_().volume * 100);
94  },
95
96  /**
97   * Change volume.
98   * @param {number} volume Volume [0..100].
99   */
100  setVolume: function(volume) {
101    playerTestAPI.respond_(
102        playerTestAPI.getControls_().onVolumeChange_(volume / 100));
103  },
104
105  /**
106   * Respond with a boolean, true if the volume is muted.
107   */
108  isMuted: function() {
109    playerTestAPI.respond_(playerTestAPI.getMedia_().volume == 0);
110  },
111
112  /**
113   * Mute the volume. No-op if already muted.
114   */
115  mute: function() {
116    if (playerTestAPI.getMedia_().volume != 0)
117      playerTestAPI.getControls_().onSoundButtonClick_();
118  },
119
120  /**
121   * Unmute the volume. No-op if not muted.
122   */
123  unmute: function() {
124    if (playerTestAPI.getMedia_().volume == 0)
125      playerTestAPI.getControls_().onSoundButtonClick_();
126  },
127
128  /* Audio player-specific methods. */
129
130  /**
131   * Load a group of tracks into the audio player.
132   * Starts playing one of the tracks immediately.
133   * @param {Array.<string>} filePaths Array of file paths.
134   * @param {number} firstTrack Number of the file to play first (0-based).
135   */
136  loadAudio: function(filePaths, firstTrack) {
137    AudioPlayer.instance.load({
138      items: filePaths.map(util.makeFilesystemUrl),
139      position: firstTrack
140    });
141  },
142
143  /**
144   * Respond with a current track number,
145   */
146  getTrackNumber: function() {
147    playerTestAPI.respond_(AudioPlayer.instance.currentTrack_);
148  },
149
150  /**
151   * Play the next track.
152   */
153  forward: function() {
154    playerTestAPI.getControls_().onAdvanceClick_(true /* forward */);
155  },
156
157  /**
158   * Go back. Will restart the current track if the current position is > 5 sec
159   * or play the previous track otherwise.
160   */
161  back: function() {
162    playerTestAPI.getControls_().onAdvanceClick_(false /* back */);
163  },
164
165  /* Utility methods */
166
167  /**
168   * @return {AudioControls|VideoControls} Media controls.
169   * @private
170   */
171  getControls_: function() {
172    return window.controls || window.AudioPlayer.instance.audioControls_;
173  },
174
175  /**
176   * @return {HTMLVideoElement|HTMLAudioElement} Media element.
177   * @private
178   */
179  getMedia_: function() {
180    return playerTestAPI.getControls_().getMedia();
181  },
182
183  /**
184   * @param {string|boolean|number} value Value to send back.
185   * @private
186   */
187  respond_: function(value) {
188    if (window.domAutomationController)
189      window.domAutomationController.send(value);
190    else
191      console.log('playerTestAPI response: ' + value);
192  }
193};
194