OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 /** @fileoverview Suite of tests for route-controls. */ |
| 6 cr.define('route_controls', function() { |
| 7 function registerTests() { |
| 8 suite('RouteControls', function() { |
| 9 /** |
| 10 * Route Controls created before each test. |
| 11 * @type {RouteControls} |
| 12 */ |
| 13 var controls; |
| 14 |
| 15 /** |
| 16 * First fake route created before each test. |
| 17 * @type {media_router.Route} |
| 18 */ |
| 19 var fakeRouteOne; |
| 20 |
| 21 /** |
| 22 * Second fake route created before each test. |
| 23 * @type {media_router.Route} |
| 24 */ |
| 25 var fakeRouteTwo; |
| 26 |
| 27 var checkText = function(expected, elementId) { |
| 28 assertEquals(expected, |
| 29 controls.$[elementId].innerText); |
| 30 }; |
| 31 |
| 32 var isElementShown = function(elementId) { |
| 33 return !controls.$[elementId].hasAttribute('hidden'); |
| 34 } |
| 35 |
| 36 // Creates an instance of RouteStatus with the given parameters. If a |
| 37 // parameter is not set, it defaults to an empty string, zero, or false. |
| 38 var createRouteStatus = function(params = {}) { |
| 39 return new media_router.RouteStatus( |
| 40 params.title ? params.title : "", |
| 41 params.status ? params.status : "", |
| 42 !!params.canPlayPause, |
| 43 !!params.canMute, |
| 44 !!params.canSetVolume, |
| 45 !!params.canSeek, |
| 46 !!params.isPaused, |
| 47 !!params.isMuted, |
| 48 params.volume ? params.volume : 0, |
| 49 params.duration ? params.duration : 0, |
| 50 params.currentTime ? params.currentTime : 0 |
| 51 ); |
| 52 } |
| 53 |
| 54 // Mock the browser API. |
| 55 var overrideBrowserApi = function() { |
| 56 cr.define('media_router.browserApi', function() { |
| 57 'use strict'; |
| 58 |
| 59 function pauseCurrentMedia() { |
| 60 controls.fire('mock-pause-current-media'); |
| 61 } |
| 62 |
| 63 function playCurrentMedia() { |
| 64 controls.fire('mock-play-current-media'); |
| 65 } |
| 66 |
| 67 function seekCurrentMedia(time) { |
| 68 controls.fire('mock-seek-current-media', {time: time}); |
| 69 } |
| 70 |
| 71 function setCurrentMediaMute(mute) { |
| 72 controls.fire('mock-set-current-media-mute', {mute: mute}); |
| 73 } |
| 74 |
| 75 function setCurrentMediaVolume(volume) { |
| 76 controls.fire('mock-set-current-media-volume', {volume: volume}); |
| 77 } |
| 78 |
| 79 return { |
| 80 pauseCurrentMedia: pauseCurrentMedia, |
| 81 playCurrentMedia: playCurrentMedia, |
| 82 seekCurrentMedia: seekCurrentMedia, |
| 83 setCurrentMediaMute: setCurrentMediaMute, |
| 84 setCurrentMediaVolume: setCurrentMediaVolume, |
| 85 }; |
| 86 }); |
| 87 } |
| 88 |
| 89 // Import route_controls.html before running suite. |
| 90 suiteSetup(function() { |
| 91 return PolymerTest.importHtml( |
| 92 'chrome://media-router/elements/route_controls/' + |
| 93 'route_controls.html'); |
| 94 }); |
| 95 |
| 96 // Initialize a route-controls before each test. |
| 97 setup(function(done) { |
| 98 PolymerTest.clearBody(); |
| 99 controls = document.createElement('route-controls'); |
| 100 document.body.appendChild(controls); |
| 101 overrideBrowserApi(); |
| 102 |
| 103 // Initialize routes and sinks. |
| 104 fakeRouteOne = new media_router.Route('route id 1', 'sink id 1', |
| 105 'Video 1', 1, true, false, |
| 106 'chrome-extension://123/custom_view.html'); |
| 107 fakeRouteTwo = new media_router.Route('route id 2', 'sink id 2', |
| 108 'Video 2', 2, false, true); |
| 109 |
| 110 // Allow for the route controls to be created and attached. |
| 111 setTimeout(done); |
| 112 }); |
| 113 |
| 114 // Tests the initial expected text. |
| 115 test('initial text setting', function() { |
| 116 // Set |route|. |
| 117 controls.route = fakeRouteOne; |
| 118 assertEquals(fakeRouteOne, controls.route); |
| 119 checkText(loadTimeData.getStringF('castingActivityStatus', |
| 120 fakeRouteOne.description), 'route-display-name'); |
| 121 |
| 122 // Set |route| to a different route. |
| 123 controls.route = fakeRouteTwo; |
| 124 assertEquals(fakeRouteTwo, controls.route); |
| 125 checkText(loadTimeData.getStringF('castingActivityStatus', |
| 126 fakeRouteTwo.description), 'route-display-name'); |
| 127 }); |
| 128 |
| 129 // Tests that the route title and status are shown when RouteStatus is |
| 130 // updated. |
| 131 test('update route text', function() { |
| 132 var title = "test title"; |
| 133 var status = "test status"; |
| 134 controls.updateRouteStatus(createRouteStatus({ |
| 135 title: title, |
| 136 status: status |
| 137 })); |
| 138 |
| 139 checkText(title, 'route-display-name'); |
| 140 checkText(status, 'route-description'); |
| 141 }); |
| 142 |
| 143 // Tests that media controls are shown and hidden when RouteStatus is |
| 144 // updated. |
| 145 test('media controls visibility', function() { |
| 146 // Create a RouteStatus with no controls. |
| 147 controls.updateRouteStatus(createRouteStatus()); |
| 148 assertFalse(isElementShown('route-play-pause-button')); |
| 149 assertFalse(isElementShown('route-time-controls')); |
| 150 assertFalse(isElementShown('route-volume-button')); |
| 151 assertFalse(isElementShown('route-volume-slider')); |
| 152 |
| 153 controls.updateRouteStatus(createRouteStatus({ |
| 154 canPlayPause: true, |
| 155 canSeek: true |
| 156 })); |
| 157 |
| 158 assertTrue(isElementShown('route-play-pause-button')); |
| 159 assertTrue(isElementShown('route-time-controls')); |
| 160 assertFalse(isElementShown('route-volume-button')); |
| 161 assertFalse(isElementShown('route-volume-slider')); |
| 162 |
| 163 controls.updateRouteStatus(createRouteStatus({ |
| 164 canMute: true, |
| 165 canSetVolume: true |
| 166 })); |
| 167 |
| 168 assertFalse(isElementShown('route-play-pause-button')); |
| 169 assertFalse(isElementShown('route-time-controls')); |
| 170 assertTrue(isElementShown('route-volume-button')); |
| 171 assertTrue(isElementShown('route-volume-slider')); |
| 172 }); |
| 173 |
| 174 // Tests that the play button sends a command to the browser API. |
| 175 test('send play command', function(done) { |
| 176 controls.addEventListener('mock-play-current-media', function(data) { |
| 177 done(); |
| 178 }); |
| 179 |
| 180 controls.updateRouteStatus(createRouteStatus({ |
| 181 canPlayPause: true, |
| 182 isPaused: true |
| 183 })); |
| 184 MockInteractions.tap(controls.$['route-play-pause-button']); |
| 185 }); |
| 186 |
| 187 // Tests that the pause button sends a command to the browser API. |
| 188 test('send pause command', function(done) { |
| 189 controls.addEventListener('mock-pause-current-media', function(data) { |
| 190 done(); |
| 191 }); |
| 192 |
| 193 controls.updateRouteStatus(createRouteStatus({ |
| 194 canPlayPause: true, |
| 195 isPaused: false |
| 196 })); |
| 197 MockInteractions.tap(controls.$['route-play-pause-button']); |
| 198 }); |
| 199 |
| 200 // Tests that the mute button sends a command to the browser API. |
| 201 test('send mute command', function(done) { |
| 202 controls.addEventListener( |
| 203 'mock-set-current-media-mute', function(data) { |
| 204 if (data.detail.mute) { |
| 205 done(); |
| 206 } else { |
| 207 done('Expected the "Mute" command but received "Unmute".'); |
| 208 } |
| 209 }); |
| 210 |
| 211 controls.updateRouteStatus(createRouteStatus({ |
| 212 canMute: true, |
| 213 isMuted: false |
| 214 })); |
| 215 MockInteractions.tap(controls.$['route-volume-button']); |
| 216 }); |
| 217 |
| 218 // Tests that the unmute button sends a command to the browser API. |
| 219 test('send unmute command', function(done) { |
| 220 controls.addEventListener( |
| 221 'mock-set-current-media-mute', function(data) { |
| 222 if (data.detail.mute) { |
| 223 done('Expected the "Unmute" command but received "Mute".'); |
| 224 } else { |
| 225 done(); |
| 226 } |
| 227 }); |
| 228 |
| 229 controls.updateRouteStatus(createRouteStatus({ |
| 230 canMute: true, |
| 231 isMuted: true |
| 232 })); |
| 233 MockInteractions.tap(controls.$['route-volume-button']); |
| 234 }); |
| 235 |
| 236 // // Tests that the seek slider sends a command to the browser API. |
| 237 test('send seek command', function(done) { |
| 238 var sliderRatio = 0.75; |
| 239 var duration = 12000; |
| 240 var expectedTime = duration * sliderRatio; |
| 241 controls.addEventListener('mock-seek-current-media', function(data) { |
| 242 if (data.detail.time == expectedTime) { |
| 243 done(); |
| 244 } else { |
| 245 done('Expected the time to be ' + expectedTime + |
| 246 ' but instead got ' + data.detail.time); |
| 247 } |
| 248 }); |
| 249 |
| 250 controls.updateRouteStatus(createRouteStatus({ |
| 251 canSeek: true, |
| 252 duration: duration |
| 253 })); |
| 254 |
| 255 // In actual usage, the change event gets fired when the user interacts |
| 256 // with the slider. |
| 257 controls.$['route-time-slider'].value = sliderRatio; |
| 258 controls.$['route-time-slider'].fire('change'); |
| 259 }); |
| 260 |
| 261 // Tests that the volume slider sends a command to the browser API. |
| 262 test('send set volume command', function(done) { |
| 263 var volume = 0.45; |
| 264 controls.addEventListener( |
| 265 'mock-set-current-media-volume', function(data) { |
| 266 if (data.detail.volume == volume) { |
| 267 done(); |
| 268 } else { |
| 269 done('Expected the volume to be ' + volume + ' but instead got ' + |
| 270 data.detail.volume); |
| 271 } |
| 272 }); |
| 273 |
| 274 controls.updateRouteStatus(createRouteStatus({canSetVolume: true})); |
| 275 |
| 276 // In actual usage, the change event gets fired when the user interacts |
| 277 // with the slider. |
| 278 controls.$['route-volume-slider'].value = volume; |
| 279 controls.$['route-volume-slider'].fire('change'); |
| 280 }); |
| 281 }); |
| 282 } |
| 283 |
| 284 return { |
| 285 registerTests: registerTests, |
| 286 }; |
| 287 }); |
OLD | NEW |