OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 function verifyGetInfoOutput(outputs) { | 5 /** |
6 chrome.test.assertEq("30001", outputs[0].id); | 6 * Asserts that device property values match properties in |expectedProperties|. |
7 chrome.test.assertEq("Jabra Speaker: Jabra Speaker 1", outputs[0].name); | 7 * The method will *not* assert that the device contains *only* properties |
8 | 8 * specified in expected properties. |
9 chrome.test.assertEq("30002", outputs[1].id); | 9 * @param {Object} expectedProperties Expected device properties. |
10 chrome.test.assertEq("Jabra Speaker: Jabra Speaker 2", outputs[1].name); | 10 * @param {Object} device Device object to test. |
11 | 11 */ |
12 chrome.test.assertEq("30003", outputs[2].id); | 12 function assertDeviceMatches(expectedProperties, device) { |
13 chrome.test.assertEq("HDMI output: HDA Intel MID", outputs[2].name); | 13 Object.keys(expectedProperties).forEach(function(key) { |
| 14 chrome.test.assertEq(expectedProperties[key], device[key], |
| 15 'Property ' + key + ' of device ' + device.id); |
| 16 }); |
14 } | 17 } |
15 | 18 |
16 function verifyGetInfoInput(inputs) { | 19 /** |
17 chrome.test.assertEq("40001", inputs[0].id); | 20 * Verifies that list of devices contains all and only devices from set of |
18 chrome.test.assertEq("Jabra Mic: Jabra Mic 1", inputs[0].name); | 21 * expected devices. If will fail the test if an unexpected device is found. |
19 | 22 * |
20 chrome.test.assertEq("40002", inputs[1].id); | 23 * @param {Object.<string, Object>} expectedDevices Expected set of test |
21 chrome.test.assertEq("Jabra Mic: Jabra Mic 2", inputs[1].name); | 24 * devices. Maps device ID to device properties. |
22 | 25 * @param {Array.<Object>} devices List of input devices. |
23 chrome.test.assertEq("40003", inputs[2].id); | 26 */ |
24 chrome.test.assertEq("Webcam Mic: Logitech Webcam", inputs[2].name); | 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 }); |
25 } | 43 } |
26 | 44 |
27 function verifyActiveDevices(output_id, input_id) { | 45 /** |
28 chrome.audio.getInfo( | 46 * |
29 chrome.test.callbackPass(function(outputInfo, inputInfo) { | 47 * @param {Array.<Object>} devices List of devices returned by getInfo |
30 var outputFound = false; | 48 * @returns {Object.<string, Object>} List of devices formatted as map of |
31 var inputFound = false; | 49 * expected devices used to assert devices match expectation. |
32 for (var i = 0; i < outputInfo.length; ++i) { | 50 */ |
33 if (outputInfo[i].isActive) { | 51 function deviceListToExpectedDevicesMap(devices) { |
34 chrome.test.assertTrue(outputInfo[i].id == output_id); | 52 var expectedDevicesMap = {}; |
35 outputFound = true; | 53 devices.forEach(function(device) { |
36 } | 54 expectedDevicesMap[device.id] = device; |
37 } | 55 }); |
38 for (var i = 0; i < inputInfo.length; ++i) { | 56 return expectedDevicesMap; |
39 if (inputInfo[i].isActive) { | |
40 chrome.test.assertTrue(inputInfo[i].id == input_id); | |
41 inputFound = true; | |
42 } | |
43 } | |
44 chrome.test.assertTrue(outputFound); | |
45 chrome.test.assertTrue(inputFound); | |
46 })); | |
47 } | |
48 | |
49 | |
50 function verifyDeviceProperties(output_id, input_id, | |
51 output_props, input_props) { | |
52 chrome.audio.getInfo( | |
53 chrome.test.callbackPass(function(outputInfo, inputInfo) { | |
54 var outputFound = false; | |
55 var inputFound = false; | |
56 for (var i = 0; i < outputInfo.length; ++i) { | |
57 if (outputInfo[i].id == output_id) { | |
58 chrome.test.assertEq(output_props.volume, outputInfo[i].volume); | |
59 chrome.test.assertEq(output_props.isMuted, outputInfo[i].isMuted); | |
60 outputFound = true; | |
61 } | |
62 } | |
63 for (var i = 0; i < inputInfo.length; ++i) { | |
64 if (inputInfo[i].id == input_id) { | |
65 chrome.test.assertEq(input_props.gain, inputInfo[i].gain); | |
66 chrome.test.assertEq(input_props.isMuted, inputInfo[i].isMuted); | |
67 inputFound = true; | |
68 } | |
69 } | |
70 chrome.test.assertTrue(outputFound); | |
71 chrome.test.assertTrue(inputFound); | |
72 })); | |
73 } | |
74 | |
75 | |
76 function setActiveDevicesAndVerify(output_id, input_id) { | |
77 chrome.audio.setActiveDevices([output_id, input_id], | |
78 chrome.test.callbackPass(function() { | |
79 verifyActiveDevices(output_id, input_id); | |
80 })); | |
81 } | |
82 | |
83 function setPropertiesAndVerify(outputInfo, inputInfo) { | |
84 var outputProps = new Object; | |
85 outputProps.isMuted = true; | |
86 outputProps.volume = 55; | |
87 chrome.audio.setProperties(outputInfo.id, outputProps, | |
88 chrome.test.callbackPass(function() { | |
89 // Once the output properties have been set, set input properties, then | |
90 // verify. | |
91 var inputProps = new Object; | |
92 inputProps.isMuted = true; | |
93 inputProps.gain = 35; | |
94 chrome.audio.setProperties(inputInfo.id, inputProps, | |
95 chrome.test.callbackPass(function() { | |
96 verifyDeviceProperties(outputInfo.id, inputInfo.id, | |
97 outputProps, inputProps); | |
98 })); | |
99 })); | |
100 } | 57 } |
101 | 58 |
102 chrome.test.runTests([ | 59 chrome.test.runTests([ |
103 function getInfoTest() { | 60 function getInfoTest() { |
| 61 // Test output devices. Maps device ID -> tested device properties. |
| 62 var kTestOutputDevices = { |
| 63 '30001': { |
| 64 id: '30001', |
| 65 name: 'Jabra Speaker: Jabra Speaker 1' |
| 66 }, |
| 67 '30002': { |
| 68 id: '30002', |
| 69 name: 'Jabra Speaker: Jabra Speaker 2' |
| 70 }, |
| 71 '30003': { |
| 72 id: '30003', |
| 73 name: 'HDMI output: HDA Intel MID' |
| 74 } |
| 75 }; |
| 76 |
| 77 // Test input devices. Maps device ID -> tested device properties. |
| 78 var kTestInputDevices = { |
| 79 '40001': { |
| 80 id: '40001', |
| 81 name: 'Jabra Mic: Jabra Mic 1' |
| 82 }, |
| 83 '40002': { |
| 84 id: '40002', |
| 85 name: 'Jabra Mic: Jabra Mic 2' |
| 86 }, |
| 87 '40003': { |
| 88 id: '40003', |
| 89 name: 'Webcam Mic: Logitech Webcam' |
| 90 } |
| 91 }; |
| 92 |
104 chrome.audio.getInfo( | 93 chrome.audio.getInfo( |
105 chrome.test.callbackPass(function(outputInfo, inputInfo) { | 94 chrome.test.callbackPass(function(outputInfo, inputInfo) { |
106 verifyGetInfoOutput(outputInfo); | 95 assertDevicesMatch(kTestOutputDevices, outputInfo); |
107 verifyGetInfoInput(inputInfo); | 96 assertDevicesMatch(kTestInputDevices, inputInfo); |
| 97 })); |
| 98 }, |
| 99 |
| 100 function setActiveDevicesTest() { |
| 101 //Test output devices. Maps device ID -> tested device properties. |
| 102 var kTestOutputDevices = { |
| 103 '30001': { |
| 104 id: '30001', |
| 105 isActive: false |
| 106 }, |
| 107 '30002': { |
| 108 id: '30002', |
| 109 isActive: false |
| 110 }, |
| 111 '30003': { |
| 112 id: '30003', |
| 113 isActive: true |
| 114 } |
| 115 }; |
| 116 |
| 117 // Test input devices. Maps device ID -> tested device properties. |
| 118 var kTestInputDevices = { |
| 119 '40001': { |
| 120 id: '40001', |
| 121 isActive: false |
| 122 }, |
| 123 '40002': { |
| 124 id: '40002', |
| 125 isActive: true |
| 126 }, |
| 127 '40003': { |
| 128 id: '40003', |
| 129 isActive: false |
| 130 } |
| 131 }; |
| 132 |
| 133 chrome.audio.setActiveDevices([ |
| 134 '30003', |
| 135 '40002' |
| 136 ], chrome.test.callbackPass(function() { |
| 137 chrome.audio.getInfo( |
| 138 chrome.test.callbackPass(function(outputInfo, inputInfo) { |
| 139 assertDevicesMatch(kTestOutputDevices, outputInfo); |
| 140 assertDevicesMatch(kTestInputDevices, inputInfo); |
| 141 })); |
108 })); | 142 })); |
109 }, | 143 }, |
110 | 144 |
111 function setActiveDevicesTest() { | 145 function setPropertiesTest() { |
112 chrome.audio.getInfo( | 146 chrome.audio.getInfo(function(originalOutputInfo, originalInputInfo) { |
113 chrome.test.callbackPass(function(outputInfo, inputInfo) { | 147 chrome.test.assertNoLastError(); |
114 setActiveDevicesAndVerify(outputInfo[2].id, inputInfo[1].id); | 148 |
115 })); | 149 var expectedInput = deviceListToExpectedDevicesMap(originalInputInfo); |
| 150 // Update expected input devices with values that should be changed in |
| 151 // test. |
| 152 var updatedInput = expectedInput['40002']; |
| 153 chrome.test.assertFalse(updatedInput.isMuted); |
| 154 chrome.test.assertFalse(updatedInput.gain === 55); |
| 155 updatedInput.isMuted = true; |
| 156 updatedInput.gain = 55; |
| 157 |
| 158 var expectedOutput = deviceListToExpectedDevicesMap(originalOutputInfo); |
| 159 // Update expected output devices with values that should be changed in |
| 160 // test. |
| 161 var updatedOutput = expectedOutput['30001']; |
| 162 chrome.test.assertFalse(updatedOutput.isMuted); |
| 163 chrome.test.assertFalse(updatedOutput.volume === 35); |
| 164 updatedOutput.isMuted = true; |
| 165 updatedOutput.volume = 35; |
| 166 |
| 167 chrome.audio.setProperties('30001', { |
| 168 isMuted: true, |
| 169 volume: 35 |
| 170 }, chrome.test.callbackPass(function() { |
| 171 chrome.audio.setProperties('40002', { |
| 172 isMuted: true, |
| 173 gain: 55 |
| 174 }, chrome.test.callbackPass(function() { |
| 175 chrome.audio.getInfo( |
| 176 chrome.test.callbackPass(function(outputInfo, inputInfo) { |
| 177 assertDevicesMatch(expectedInput, inputInfo); |
| 178 assertDevicesMatch(expectedOutput, outputInfo); |
| 179 })); |
| 180 })); |
| 181 })); |
| 182 }); |
116 }, | 183 }, |
117 | 184 |
118 function setPropertiesTest() { | 185 function setPropertiesInvalidValuesTest() { |
119 chrome.audio.getInfo( | 186 chrome.audio.getInfo(function(originalOutputInfo, originalInputInfo) { |
120 chrome.test.callbackPass(function(outputInfo, inputInfo) { | 187 chrome.test.assertNoLastError(); |
121 setPropertiesAndVerify(outputInfo[0], inputInfo[1]); | 188 var expectedInput = deviceListToExpectedDevicesMap(originalInputInfo); |
122 })); | 189 var expectedOutput = deviceListToExpectedDevicesMap(originalOutputInfo); |
| 190 |
| 191 chrome.audio.setProperties('30001', { |
| 192 isMuted: true, |
| 193 // Output device - should have volume set. |
| 194 gain: 55 |
| 195 }, chrome.test.callbackFail('Could not set properties', function() { |
| 196 chrome.audio.setProperties('40002', { |
| 197 isMuted: true, |
| 198 // Input device - should have gain set. |
| 199 volume:55 |
| 200 }, chrome.test.callbackFail('Could not set properties', function() { |
| 201 // Assert that device properties haven't changed. |
| 202 chrome.audio.getInfo( |
| 203 chrome.test.callbackPass(function(outputInfo, inputInfo) { |
| 204 assertDevicesMatch(expectedOutput, outputInfo); |
| 205 assertDevicesMatch(expectedInput, inputInfo); |
| 206 })); |
| 207 })); |
| 208 })); |
| 209 }); |
123 } | 210 } |
124 ]); | 211 ]); |
OLD | NEW |