| 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 /** |
| 6 * Asserts that device property values match properties in |expectedProperties|. |
| 7 * The method will *not* assert that the device contains *only* properties |
| 8 * specified in expected properties. |
| 9 * @param {Object} expectedProperties Expected device properties. |
| 10 * @param {Object} device Device object to test. |
| 11 */ |
| 12 function assertDeviceMatches(expectedProperties, device) { |
| 13 Object.keys(expectedProperties).forEach(function(key) { |
| 14 chrome.test.assertEq(expectedProperties[key], device[key], |
| 15 'Property ' + key + ' of device ' + device.id); |
| 16 }); |
| 17 } |
| 18 |
| 19 /** |
| 20 * Verifies that list of devices contains all and only devices from set of |
| 21 * expected devices. If will fail the test if an unexpected device is found. |
| 22 * |
| 23 * @param {Object.<string, Object>} expectedDevices Expected set of test |
| 24 * devices. Maps device ID to device properties. |
| 25 * @param {Array.<Object>} devices List of input devices. |
| 26 */ |
| 27 function assertDevicesMatch(expectedDevices, devices) { |
| 28 var deviceIds = {}; |
| 29 devices.forEach(function(device) { |
| 30 chrome.test.assertFalse(!!deviceIds[device.id], |
| 31 'Duplicated device id: \'' + device.id + '\'.'); |
| 32 deviceIds[device.id] = true; |
| 33 }); |
| 34 |
| 35 function sortedKeys(obj) { |
| 36 return Object.keys(obj).sort(); |
| 37 } |
| 38 chrome.test.assertEq(sortedKeys(expectedDevices), sortedKeys(deviceIds)); |
| 39 |
| 40 devices.forEach(function(device) { |
| 41 assertDeviceMatches(expectedDevices[device.id], device); |
| 42 }); |
| 43 } |
| 44 |
| 45 /** |
| 46 * @param {Array.<Object>} devices List of devices returned by |
| 47 * chrome.audio.getInfo or chrome.audio.getDevices. |
| 48 * @return {Object.<string, Object>} List of devices formatted as map of |
| 49 * expected devices used to assert devices match expectation. |
| 50 */ |
| 51 function deviceListToExpectedDevicesMap(devices) { |
| 52 var expectedDevicesMap = {}; |
| 53 devices.forEach(function(device) { |
| 54 expectedDevicesMap[device.id] = device; |
| 55 }); |
| 56 return expectedDevicesMap; |
| 57 } |
| 58 |
| 59 function EventListener(targetEvent) { |
| 60 this.targetEvent = targetEvent; |
| 61 this.listener = this.handleEvent.bind(this); |
| 62 this.targetEvent.addListener(this.listener); |
| 63 this.eventCount = 0; |
| 64 } |
| 65 |
| 66 EventListener.prototype.handleEvent = function() { |
| 67 ++this.eventCount; |
| 68 } |
| 69 |
| 70 EventListener.prototype.reset = function() { |
| 71 this.targetEvent.removeListener(this.listener); |
| 72 } |
| 73 |
| 74 var deviceChangedListener = null; |
| 75 |
| 76 chrome.test.runTests([ |
| 77 // Sets up a listener for audio.onDeviceChanged event - |
| 78 // |verifyDeviceChangedEvents| test will later verify that |
| 79 // onDeviceChanged events have been observed. |
| 80 function startDeviceChangedListener() { |
| 81 deviceChangedListener = new EventListener(chrome.audio.onDeviceChanged); |
| 82 chrome.test.succeed(); |
| 83 }, |
| 84 |
| 85 function getInfoTest() { |
| 86 // Test output devices. Maps device ID -> tested device properties. |
| 87 var kTestOutputDevices = { |
| 88 '30001': { |
| 89 id: '30001', |
| 90 name: 'Jabra Speaker: Jabra Speaker 1' |
| 91 }, |
| 92 '30002': { |
| 93 id: '30002', |
| 94 name: 'Jabra Speaker: Jabra Speaker 2' |
| 95 }, |
| 96 '30003': { |
| 97 id: '30003', |
| 98 name: 'HDMI output: HDA Intel MID' |
| 99 } |
| 100 }; |
| 101 |
| 102 // Test input devices. Maps device ID -> tested device properties. |
| 103 var kTestInputDevices = { |
| 104 '40001': { |
| 105 id: '40001', |
| 106 name: 'Jabra Mic: Jabra Mic 1' |
| 107 }, |
| 108 '40002': { |
| 109 id: '40002', |
| 110 name: 'Jabra Mic: Jabra Mic 2' |
| 111 }, |
| 112 '40003': { |
| 113 id: '40003', |
| 114 name: 'Webcam Mic: Logitech Webcam' |
| 115 } |
| 116 }; |
| 117 |
| 118 chrome.audio.getInfo(chrome.test.callbackPass( |
| 119 function(outputInfo, inputInfo) { |
| 120 assertDevicesMatch(kTestOutputDevices, outputInfo); |
| 121 assertDevicesMatch(kTestInputDevices, inputInfo); |
| 122 })); |
| 123 }, |
| 124 |
| 125 function setActiveDevicesTest() { |
| 126 //Test output devices. Maps device ID -> tested device properties. |
| 127 var kTestDevices = { |
| 128 '30001': { |
| 129 id: '30001', |
| 130 isActive: false |
| 131 }, |
| 132 '30002': { |
| 133 id: '30002', |
| 134 isActive: false |
| 135 }, |
| 136 '30003': { |
| 137 id: '30003', |
| 138 isActive: true |
| 139 }, |
| 140 '40001': { |
| 141 id: '40001', |
| 142 isActive: false |
| 143 }, |
| 144 '40002': { |
| 145 id: '40002', |
| 146 isActive: true |
| 147 }, |
| 148 '40003': { |
| 149 id: '40003', |
| 150 isActive: false |
| 151 } |
| 152 }; |
| 153 |
| 154 chrome.audio.setActiveDevices([ |
| 155 '30003', |
| 156 '40002' |
| 157 ], chrome.test.callbackPass(function() { |
| 158 chrome.audio.getDevices(chrome.test.callbackPass(function(devices) { |
| 159 assertDevicesMatch(kTestDevices, devices); |
| 160 })); |
| 161 })); |
| 162 }, |
| 163 |
| 164 function setPropertiesTest() { |
| 165 chrome.audio.getInfo(chrome.test.callbackPass(function( |
| 166 initialOutput, initialInput) { |
| 167 var expectedInput = deviceListToExpectedDevicesMap(initialInput); |
| 168 var expectedOutput = deviceListToExpectedDevicesMap(initialOutput); |
| 169 |
| 170 // Update expected input devices with values that should be changed in |
| 171 // test. |
| 172 var updatedInput = expectedInput['40002']; |
| 173 chrome.test.assertFalse(updatedInput.isMuted); |
| 174 chrome.test.assertFalse(updatedInput.gain === 55); |
| 175 updatedInput.isMuted = true; |
| 176 updatedInput.gain = 55; |
| 177 |
| 178 // Update expected output devices with values that should be changed in |
| 179 // test. |
| 180 var updatedOutput = expectedOutput['30001']; |
| 181 chrome.test.assertFalse(updatedOutput.volume === 35); |
| 182 updatedOutput.volume = 35; |
| 183 |
| 184 chrome.audio.setProperties('30001', { |
| 185 volume: 35 |
| 186 }, chrome.test.callbackPass(function() { |
| 187 chrome.audio.setProperties('40002', { |
| 188 isMuted: true, |
| 189 gain: 55 |
| 190 }, chrome.test.callbackPass(function() { |
| 191 chrome.audio.getInfo(chrome.test.callbackPass(function( |
| 192 output, input) { |
| 193 assertDevicesMatch(expectedOutput, output); |
| 194 assertDevicesMatch(expectedInput, input); |
| 195 })); |
| 196 })); |
| 197 })); |
| 198 })); |
| 199 }, |
| 200 |
| 201 function setPropertiesInvalidValuesTest() { |
| 202 chrome.audio.getInfo(chrome.test.callbackPass(function( |
| 203 initialOutput, initialInput) { |
| 204 var expectedOutput = deviceListToExpectedDevicesMap(initialOutput); |
| 205 var expectedInput = deviceListToExpectedDevicesMap(initialInput); |
| 206 var expectedError = 'Could not set volume/gain properties'; |
| 207 |
| 208 chrome.audio.setProperties('30001', { |
| 209 isMuted: true, |
| 210 // Output device - should have volume set. |
| 211 gain: 55 |
| 212 }, chrome.test.callbackFail(expectedError, function() { |
| 213 chrome.audio.setProperties('40002', { |
| 214 isMuted: true, |
| 215 // Input device - should have gain set. |
| 216 volume:55 |
| 217 }, chrome.test.callbackFail(expectedError, function() { |
| 218 // Assert that device properties haven't changed. |
| 219 chrome.audio.getInfo(chrome.test.callbackPass(function( |
| 220 output, input) { |
| 221 assertDevicesMatch(expectedOutput, output); |
| 222 assertDevicesMatch(expectedInput, input); |
| 223 })); |
| 224 })); |
| 225 })); |
| 226 })); |
| 227 }, |
| 228 |
| 229 function verifyDeviceChangedEvents() { |
| 230 chrome.test.assertTrue(!!deviceChangedListener); |
| 231 chrome.test.assertTrue(deviceChangedListener.eventCount > 0); |
| 232 deviceChangedListener.reset(); |
| 233 deviceChangedListener = null; |
| 234 chrome.test.succeed(); |
| 235 }, |
| 236 |
| 237 ]); |
| OLD | NEW |