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 assertElementText = function(expected, elementId) { |
| 28 assertEquals(expected, controls.$$('#' + elementId).innerText); |
| 29 }; |
| 30 |
| 31 var isElementShown = function(elementId) { |
| 32 return !controls.$$('#' + elementId).hasAttribute('hidden'); |
| 33 }; |
| 34 |
| 35 var assertElementShown = function(elementId) { |
| 36 assertTrue(isElementShown(elementId)); |
| 37 }; |
| 38 |
| 39 var assertElementHidden = function(elementId) { |
| 40 assertFalse(isElementShown(elementId)); |
| 41 }; |
| 42 |
| 43 // Creates an instance of RouteStatus with the given parameters. If a |
| 44 // parameter is not set, it defaults to an empty string, zero, or false. |
| 45 var createRouteStatus = function(params = {}) { |
| 46 return new media_router.RouteStatus( |
| 47 params.title ? params.title : '', |
| 48 params.status ? params.status : '', !!params.canPlayPause, |
| 49 !!params.canMute, !!params.canSetVolume, !!params.canSeek, |
| 50 !!params.isPaused, !!params.isMuted, |
| 51 params.volume ? params.volume : 0, |
| 52 params.duration ? params.duration : 0, |
| 53 params.currentTime ? params.currentTime : 0); |
| 54 }; |
| 55 |
| 56 // Import route_controls.html before running suite. |
| 57 suiteSetup(function() { |
| 58 return PolymerTest.importHtml( |
| 59 'chrome://media-router/elements/route_controls/' + |
| 60 'route_controls.html'); |
| 61 }); |
| 62 |
| 63 // Initialize a route-controls before each test. |
| 64 setup(function(done) { |
| 65 PolymerTest.clearBody(); |
| 66 controls = document.createElement('route-controls'); |
| 67 document.body.appendChild(controls); |
| 68 |
| 69 // Initialize routes and sinks. |
| 70 fakeRouteOne = new media_router.Route( |
| 71 'route id 1', 'sink id 1', 'Video 1', 1, true, false); |
| 72 fakeRouteTwo = new media_router.Route( |
| 73 'route id 2', 'sink id 2', 'Video 2', 2, false, true); |
| 74 |
| 75 // Allow for the route controls to be created and attached. |
| 76 setTimeout(done); |
| 77 }); |
| 78 |
| 79 // Tests the initial expected text. |
| 80 test('initial text setting', function() { |
| 81 // Set |route|. |
| 82 controls.onRouteUpdated(fakeRouteOne); |
| 83 assertElementText( |
| 84 loadTimeData.getStringF( |
| 85 'castingActivityStatus', fakeRouteOne.description), |
| 86 'route-description'); |
| 87 |
| 88 // Set |route| to a different route. |
| 89 controls.onRouteUpdated(fakeRouteTwo); |
| 90 assertElementText( |
| 91 loadTimeData.getStringF( |
| 92 'castingActivityStatus', fakeRouteTwo.description), |
| 93 'route-description'); |
| 94 }); |
| 95 |
| 96 // Tests that the route title and status are shown when RouteStatus is |
| 97 // updated. |
| 98 test('update route text', function() { |
| 99 var title = 'test title'; |
| 100 var status = 'test status'; |
| 101 controls.routeStatus = |
| 102 createRouteStatus({title: title, status: status}); |
| 103 |
| 104 assertElementText(title, 'route-title'); |
| 105 assertElementText(status, 'route-description'); |
| 106 }); |
| 107 |
| 108 // Tests that media controls are shown and hidden when RouteStatus is |
| 109 // updated. |
| 110 test('media controls visibility', function() { |
| 111 // Create a RouteStatus with no controls. |
| 112 controls.routeStatus = createRouteStatus(); |
| 113 assertElementHidden('route-play-pause-button'); |
| 114 assertElementHidden('route-time-controls'); |
| 115 assertElementHidden('route-volume-button'); |
| 116 assertElementHidden('route-volume-slider'); |
| 117 |
| 118 controls.routeStatus = |
| 119 createRouteStatus({canPlayPause: true, canSeek: true}); |
| 120 |
| 121 assertElementShown('route-play-pause-button'); |
| 122 assertElementShown('route-time-controls'); |
| 123 assertElementHidden('route-volume-button'); |
| 124 assertElementHidden('route-volume-slider'); |
| 125 |
| 126 controls.routeStatus = |
| 127 createRouteStatus({canMute: true, canSetVolume: true}); |
| 128 |
| 129 assertElementHidden('route-play-pause-button'); |
| 130 assertElementHidden('route-time-controls'); |
| 131 assertElementShown('route-volume-button'); |
| 132 assertElementShown('route-volume-slider'); |
| 133 }); |
| 134 |
| 135 // Tests that the play button sends a command to the browser API. |
| 136 test('send play command', function(done) { |
| 137 document.addEventListener('mock-play-current-media', function(data) { |
| 138 done(); |
| 139 }); |
| 140 |
| 141 controls.routeStatus = |
| 142 createRouteStatus({canPlayPause: true, isPaused: true}); |
| 143 MockInteractions.tap(controls.$$('#route-play-pause-button')); |
| 144 }); |
| 145 |
| 146 // Tests that the pause button sends a command to the browser API. |
| 147 test('send pause command', function(done) { |
| 148 document.addEventListener('mock-pause-current-media', function(data) { |
| 149 done(); |
| 150 }); |
| 151 |
| 152 controls.routeStatus = |
| 153 createRouteStatus({canPlayPause: true, isPaused: false}); |
| 154 MockInteractions.tap(controls.$$('#route-play-pause-button')); |
| 155 }); |
| 156 |
| 157 // Tests that the mute button sends a command to the browser API. |
| 158 test('send mute command', function(done) { |
| 159 function waitForMuteEvent(data) { |
| 160 // Remove the event listener to avoid interfering with other tests. |
| 161 document.removeEventListener( |
| 162 'mock-set-current-media-mute', waitForMuteEvent); |
| 163 if (data.detail.mute) { |
| 164 done(); |
| 165 } else { |
| 166 done('Expected the "Mute" command but received "Unmute".'); |
| 167 } |
| 168 } |
| 169 document.addEventListener( |
| 170 'mock-set-current-media-mute', waitForMuteEvent); |
| 171 |
| 172 controls.routeStatus = |
| 173 createRouteStatus({canMute: true, isMuted: false}); |
| 174 MockInteractions.tap(controls.$$('#route-volume-button')); |
| 175 }); |
| 176 |
| 177 // Tests that the unmute button sends a command to the browser API. |
| 178 test('send unmute command', function(done) { |
| 179 function waitForUnmuteEvent(data) { |
| 180 // Remove the event listener to avoid interfering with other tests. |
| 181 document.removeEventListener( |
| 182 'mock-set-current-media-mute', waitForUnmuteEvent); |
| 183 if (data.detail.mute) { |
| 184 done('Expected the "Unmute" command but received "Mute".'); |
| 185 } else { |
| 186 done(); |
| 187 } |
| 188 } |
| 189 document.addEventListener( |
| 190 'mock-set-current-media-mute', waitForUnmuteEvent); |
| 191 |
| 192 controls.routeStatus = |
| 193 createRouteStatus({canMute: true, isMuted: true}); |
| 194 MockInteractions.tap(controls.$$('#route-volume-button')); |
| 195 }); |
| 196 |
| 197 // // Tests that the seek slider sends a command to the browser API. |
| 198 test('send seek command', function(done) { |
| 199 var currentTime = 500; |
| 200 var duration = 1200; |
| 201 document.addEventListener('mock-seek-current-media', function(data) { |
| 202 if (data.detail.time == currentTime) { |
| 203 done(); |
| 204 } else { |
| 205 done( |
| 206 'Expected the time to be ' + currentTime + ' but instead got ' + |
| 207 data.detail.time); |
| 208 } |
| 209 }); |
| 210 |
| 211 controls.routeStatus = |
| 212 createRouteStatus({canSeek: true, duration: duration}); |
| 213 |
| 214 // In actual usage, the change event gets fired when the user interacts |
| 215 // with the slider. |
| 216 controls.$$('#route-time-slider').value = currentTime; |
| 217 controls.$$('#route-time-slider').fire('change'); |
| 218 }); |
| 219 |
| 220 // Tests that the volume slider sends a command to the browser API. |
| 221 test('send set volume command', function(done) { |
| 222 var volume = 0.45; |
| 223 document.addEventListener( |
| 224 'mock-set-current-media-volume', function(data) { |
| 225 if (data.detail.volume == volume) { |
| 226 done(); |
| 227 } else { |
| 228 done( |
| 229 'Expected the volume to be ' + volume + |
| 230 ' but instead got ' + data.detail.volume); |
| 231 } |
| 232 }); |
| 233 |
| 234 controls.routeStatus = createRouteStatus({canSetVolume: true}); |
| 235 |
| 236 // In actual usage, the change event gets fired when the user interacts |
| 237 // with the slider. |
| 238 controls.$$('#route-volume-slider').value = volume; |
| 239 controls.$$('#route-volume-slider').fire('change'); |
| 240 }); |
| 241 }); |
| 242 } |
| 243 |
| 244 return { |
| 245 registerTests: registerTests, |
| 246 }; |
| 247 }); |
OLD | NEW |