| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 /** @fileoverview Suite of tests for settings-dropdown-menu. */ | 5 /** @fileoverview Suite of tests for settings-dropdown-menu. */ |
| 6 cr.define('settings_dropdown_menu', function() { | 6 cr.define('settings_dropdown_menu', function() { |
| 7 function registerTests() { | 7 function registerTests() { |
| 8 suite('SettingsDropdownMenu', function() { | 8 suite('SettingsDropdownMenu', function() { |
| 9 /** @type {SettingsDropdownMenu} */ | 9 /** @type {SettingsDropdownMenu} */ |
| 10 var dropdown; | 10 var dropdown; |
| 11 | 11 |
| 12 /** | 12 /** |
| 13 * The <select> used internally by the dropdown menu. | 13 * The <select> used internally by the dropdown menu. |
| 14 * @type {HTMLSelectElement} | 14 * @type {HTMLSelectElement} |
| 15 */ | 15 */ |
| 16 var selectElement; | 16 var selectElement; |
| 17 | 17 |
| 18 /** |
| 19 * The "Custom" option in the <select> menu. |
| 20 * @type {HTMLOptionElement} |
| 21 */ |
| 22 var customOption; |
| 23 |
| 18 function waitUntilDropdownUpdated() { | 24 function waitUntilDropdownUpdated() { |
| 19 return new Promise(function(resolve) { dropdown.async(resolve); }); | 25 return new Promise(function(resolve) { dropdown.async(resolve); }); |
| 20 } | 26 } |
| 21 | 27 |
| 22 function simulateChangeEvent(value) { | 28 function simulateChangeEvent(value) { |
| 23 selectElement.value = value; | 29 selectElement.value = value; |
| 24 selectElement.dispatchEvent(new CustomEvent('change')); | 30 selectElement.dispatchEvent(new CustomEvent('change')); |
| 25 return waitUntilDropdownUpdated(); | 31 return waitUntilDropdownUpdated(); |
| 26 } | 32 } |
| 27 | 33 |
| 28 setup(function() { | 34 setup(function() { |
| 29 PolymerTest.clearBody(); | 35 PolymerTest.clearBody(); |
| 30 dropdown = document.createElement('settings-dropdown-menu'); | 36 dropdown = document.createElement('settings-dropdown-menu'); |
| 31 selectElement = dropdown.$$('select'); | 37 selectElement = assert(dropdown.$$('select')); |
| 38 var options = selectElement.options; |
| 39 customOption = assert(options[options.length - 1]); |
| 32 document.body.appendChild(dropdown); | 40 document.body.appendChild(dropdown); |
| 33 }); | 41 }); |
| 34 | 42 |
| 35 test('with number options', function testNumberOptions() { | 43 test('with number options', function testNumberOptions() { |
| 36 dropdown.pref = { | 44 dropdown.pref = { |
| 37 key: 'test.number', | 45 key: 'test.number', |
| 38 type: chrome.settingsPrivate.PrefType.NUMBER, | 46 type: chrome.settingsPrivate.PrefType.NUMBER, |
| 39 value: 100, | 47 value: 100, |
| 40 }; | 48 }; |
| 41 dropdown.menuOptions = [{value: 100, name: 'Option 100'}, | 49 dropdown.menuOptions = [{value: 100, name: 'Option 100'}, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 return waitUntilDropdownUpdated().then(function() { | 85 return waitUntilDropdownUpdated().then(function() { |
| 78 // Initially selected item. | 86 // Initially selected item. |
| 79 assertEquals( | 87 assertEquals( |
| 80 'CCC', selectElement.selectedOptions[0].textContent.trim()); | 88 'CCC', selectElement.selectedOptions[0].textContent.trim()); |
| 81 | 89 |
| 82 // Selecting an item updates the pref. | 90 // Selecting an item updates the pref. |
| 83 return simulateChangeEvent('a'); | 91 return simulateChangeEvent('a'); |
| 84 }).then(function() { | 92 }).then(function() { |
| 85 assertEquals('a', dropdown.pref.value); | 93 assertEquals('a', dropdown.pref.value); |
| 86 | 94 |
| 87 // Updating the pref selects an item. | 95 // Item remains selected after updating menu items. |
| 88 dropdown.set('pref.value', 'b'); | 96 var newMenuOptions = dropdown.menuOptions.slice().reverse(); |
| 97 dropdown.menuOptions = newMenuOptions; |
| 89 return waitUntilDropdownUpdated(); | 98 return waitUntilDropdownUpdated(); |
| 90 }).then(function() { | 99 }).then(function() { |
| 91 assertEquals('b', selectElement.value); | 100 assertEquals('AAA', |
| 101 selectElement.selectedOptions[0].textContent.trim()); |
| 92 }); | 102 }); |
| 93 }); | 103 }); |
| 94 | 104 |
| 95 test('with custom value', function testCustomValue() { | 105 test('with custom value', function testCustomValue() { |
| 96 dropdown.pref = { | 106 dropdown.pref = { |
| 97 key: 'test.string', | 107 key: 'test.string', |
| 98 type: chrome.settingsPrivate.PrefType.STRING, | 108 type: chrome.settingsPrivate.PrefType.STRING, |
| 99 value: 'f', | 109 value: 'f', |
| 100 }; | 110 }; |
| 101 dropdown.menuOptions = | 111 dropdown.menuOptions = |
| 102 [{value: 'a', name: 'AAA'}, | 112 [{value: 'a', name: 'AAA'}, |
| 103 {value: 'b', name: 'BBB'}, | 113 {value: 'b', name: 'BBB'}, |
| 104 {value: 'c', name: 'CCC'}, | 114 {value: 'c', name: 'CCC'}, |
| 105 {value: 'd', name: 'DDD'}]; | 115 {value: 'd', name: 'DDD'}]; |
| 106 | 116 |
| 107 return waitUntilDropdownUpdated().then(function() { | 117 return waitUntilDropdownUpdated().then(function() { |
| 108 // "Custom" initially selected. | 118 // "Custom" initially selected. |
| 109 assertEquals(dropdown.notFoundValue_, selectElement.value); | 119 assertEquals(dropdown.notFoundValue_, selectElement.value); |
| 120 assertEquals('block', getComputedStyle(customOption).display); |
| 121 assertFalse(customOption.disabled); |
| 122 |
| 110 // Pref should not have changed. | 123 // Pref should not have changed. |
| 111 assertEquals('f', dropdown.pref.value); | 124 assertEquals('f', dropdown.pref.value); |
| 112 }); | 125 }); |
| 113 }); | 126 }); |
| 114 | 127 |
| 115 function waitForTimeout(timeMs) { | 128 function waitForTimeout(timeMs) { |
| 116 return new Promise(function(resolve) { setTimeout(resolve, timeMs); }); | 129 return new Promise(function(resolve) { setTimeout(resolve, timeMs); }); |
| 117 } | 130 } |
| 118 | 131 |
| 119 test('delay setting options', function testDelayedOptions() { | 132 test('delay setting options', function testDelayedOptions() { |
| 120 dropdown.pref = { | 133 dropdown.pref = { |
| 121 key: 'test.number2', | 134 key: 'test.number2', |
| 122 type: chrome.settingsPrivate.PrefType.NUMBER, | 135 type: chrome.settingsPrivate.PrefType.NUMBER, |
| 123 value: 200, | 136 value: 200, |
| 124 }; | 137 }; |
| 125 | 138 |
| 126 console.log('running test'); | |
| 127 return waitForTimeout(100).then(function() { | 139 return waitForTimeout(100).then(function() { |
| 128 return waitUntilDropdownUpdated(); | 140 return waitUntilDropdownUpdated(); |
| 129 }).then(function() { | 141 }).then(function() { |
| 130 assertTrue(dropdown.$.dropdownMenu.disabled); | 142 assertTrue(selectElement.disabled); |
| 131 assertEquals('SETTINGS_DROPDOWN_NOT_FOUND_ITEM', selectElement.value); | 143 assertEquals('SETTINGS_DROPDOWN_NOT_FOUND_ITEM', selectElement.value); |
| 132 | 144 |
| 133 dropdown.menuOptions = [{value: 100, name: 'Option 100'}, | 145 dropdown.menuOptions = [{value: 100, name: 'Option 100'}, |
| 134 {value: 200, name: 'Option 200'}, | 146 {value: 200, name: 'Option 200'}, |
| 135 {value: 300, name: 'Option 300'}, | 147 {value: 300, name: 'Option 300'}, |
| 136 {value: 400, name: 'Option 400'}]; | 148 {value: 400, name: 'Option 400'}]; |
| 137 return waitUntilDropdownUpdated(); | 149 return waitUntilDropdownUpdated(); |
| 138 }).then(function() { | 150 }).then(function() { |
| 139 // Dropdown menu enables itself and selects the new menu option | 151 // Dropdown menu enables itself and selects the new menu option |
| 140 // correpsonding to the pref value. | 152 // correpsonding to the pref value. |
| 141 assertFalse(dropdown.$.dropdownMenu.disabled); | 153 assertFalse(selectElement.disabled); |
| 142 assertEquals('200', selectElement.value); | 154 assertEquals('200', selectElement.value); |
| 155 |
| 156 // The "Custom" option should not show up in the dropdown list or be |
| 157 // reachable via type-ahead. |
| 158 assertEquals('none', getComputedStyle(customOption).display); |
| 159 assertTrue(customOption.disabled); |
| 143 }); | 160 }); |
| 144 }); | 161 }); |
| 145 }); | 162 }); |
| 146 } | 163 } |
| 147 | 164 |
| 148 return { | 165 return { |
| 149 registerTests: registerTests, | 166 registerTests: registerTests, |
| 150 }; | 167 }; |
| 151 }); | 168 }); |
| OLD | NEW |