| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 * @fileoverview Suite of tests for shared cr elements. Currently: |
| 7 * <cr-toolbar-search-field>. |
| 8 */ |
| 9 |
| 10 /** @const {string} Path to source root. */ |
| 11 var ROOT_PATH = '../../../../'; |
| 12 |
| 13 // Polymer BrowserTest fixture. |
| 14 GEN_INCLUDE( |
| 15 [ROOT_PATH + 'chrome/test/data/webui/polymer_browser_test_base.js']); |
| 16 |
| 17 /** |
| 18 * Test fixture for cr elements |
| 19 * @constructor |
| 20 * @extends {PolymerTest} |
| 21 */ |
| 22 function CrElementsBrowserTest() {} |
| 23 |
| 24 CrElementsBrowserTest.prototype = { |
| 25 __proto__: PolymerTest.prototype, |
| 26 extraLibraries: PolymerTest.getLibraries(ROOT_PATH), |
| 27 }; |
| 28 |
| 29 TEST_F('CrElementsBrowserTest', 'CrToolbarSearchFieldTest', function() { |
| 30 suite('cr-toolbar-search-field', function() { |
| 31 /** @type {?CrToolbarSearchFieldElement} */ |
| 32 var field; |
| 33 /** @type {?MockSearchFieldDelegate} */ |
| 34 var delegate; |
| 35 |
| 36 /** @param {string} term */ |
| 37 function simulateSearch(term) { |
| 38 field.$.searchInput.bindValue = term; |
| 39 field.onSearchTermSearch_(); |
| 40 } |
| 41 |
| 42 /** |
| 43 * @constructor |
| 44 * @implements {SearchFieldDelegate} |
| 45 */ |
| 46 function MockSearchFieldDelegate() { |
| 47 /** @type {!Array<string>} */ |
| 48 this.searches = []; |
| 49 } |
| 50 |
| 51 MockSearchFieldDelegate.prototype = { |
| 52 /** @override */ |
| 53 onSearchTermSearch: function(term) { |
| 54 this.searches.push(term); |
| 55 } |
| 56 }; |
| 57 |
| 58 suiteSetup(function() { |
| 59 return PolymerTest.importHtml( |
| 60 'chrome://resources/cr_elements/cr_toolbar/' + |
| 61 'cr_toolbar_search_field.html'); |
| 62 }); |
| 63 |
| 64 setup(function() { |
| 65 PolymerTest.clearBody(); |
| 66 // Constructing a new delegate resets the list of searches. |
| 67 delegate = new MockSearchFieldDelegate(); |
| 68 field = document.createElement('cr-toolbar-search-field'); |
| 69 field.setDelegate(delegate); |
| 70 document.body.appendChild(field); |
| 71 }); |
| 72 |
| 73 teardown(function() { |
| 74 field.remove(); |
| 75 field = null; |
| 76 delegate = null; |
| 77 }); |
| 78 |
| 79 test('opens and closes correctly', function() { |
| 80 assertFalse(field.showingSearch); |
| 81 MockInteractions.tap(field); |
| 82 assertTrue(field.showingSearch); |
| 83 assertEquals(field.$.searchInput, field.root.activeElement); |
| 84 |
| 85 MockInteractions.blur(field.$.searchInput); |
| 86 assertFalse(field.showingSearch); |
| 87 |
| 88 MockInteractions.tap(field); |
| 89 assertEquals(field.$.searchInput, field.root.activeElement); |
| 90 |
| 91 MockInteractions.pressAndReleaseKeyOn( |
| 92 field.$.searchInput, 27, '', 'Escape'); |
| 93 assertFalse(field.showingSearch, 'Pressing escape closes field.'); |
| 94 assertNotEquals(field.$.searchInput, field.root.activeElement); |
| 95 }); |
| 96 |
| 97 test('passes searches correctly', function() { |
| 98 MockInteractions.tap(field); |
| 99 simulateSearch('test'); |
| 100 assertEquals('test', field.getValue()); |
| 101 |
| 102 MockInteractions.tap(field.$.clearSearch); |
| 103 assertFalse(field.showingSearch); |
| 104 assertEquals('', field.getValue()); |
| 105 |
| 106 assertEquals(['test', ''].join(), delegate.searches.join()); |
| 107 }); |
| 108 |
| 109 test('blur does not close field when a search is active', function() { |
| 110 MockInteractions.tap(field); |
| 111 simulateSearch('test'); |
| 112 MockInteractions.blur(field.$.searchInput); |
| 113 |
| 114 assertTrue(field.showingSearch); |
| 115 }); |
| 116 }); |
| 117 |
| 118 mocha.run(); |
| 119 }); |
| OLD | NEW |