Chromium Code Reviews| 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 /** @fileoverview Suite of tests for cr-toolbar-search-field. */ | |
| 6 cr.define('cr_toolbar_search_field', function() { | |
| 7 function registerTests() { | |
| 8 suite('cr-toolbar-search-field', function() { | |
|
michaelpg
2016/05/27 19:23:08
rietveld sucks but everything below here is just a
| |
| 9 /** @type {?CrToolbarSearchFieldElement} */ | |
| 10 var field; | |
| 11 /** @type {?MockSearchFieldDelegate} */ | |
| 12 var delegate; | |
| 13 | |
| 14 /** @param {string} term */ | |
| 15 function simulateSearch(term) { | |
| 16 field.$.searchInput.bindValue = term; | |
| 17 field.onSearchTermSearch_(); | |
| 18 } | |
| 19 | |
| 20 /** | |
| 21 * @constructor | |
| 22 * @implements {SearchFieldDelegate} | |
| 23 */ | |
| 24 function MockSearchFieldDelegate() { | |
| 25 /** @type {!Array<string>} */ | |
| 26 this.searches = []; | |
| 27 } | |
| 28 | |
| 29 MockSearchFieldDelegate.prototype = { | |
| 30 /** @override */ | |
| 31 onSearchTermSearch: function(term) { | |
| 32 this.searches.push(term); | |
| 33 } | |
| 34 }; | |
| 35 | |
| 36 suiteSetup(function() { | |
| 37 return PolymerTest.importHtml( | |
| 38 'chrome://resources/cr_elements/cr_toolbar/' + | |
| 39 'cr_toolbar_search_field.html'); | |
| 40 }); | |
| 41 | |
| 42 setup(function() { | |
| 43 PolymerTest.clearBody(); | |
| 44 // Constructing a new delegate resets the list of searches. | |
| 45 delegate = new MockSearchFieldDelegate(); | |
| 46 field = document.createElement('cr-toolbar-search-field'); | |
| 47 field.setDelegate(delegate); | |
| 48 document.body.appendChild(field); | |
| 49 }); | |
| 50 | |
| 51 teardown(function() { | |
| 52 field.remove(); | |
| 53 field = null; | |
| 54 delegate = null; | |
| 55 }); | |
| 56 | |
| 57 test('opens and closes correctly', function() { | |
| 58 assertFalse(field.showingSearch); | |
| 59 MockInteractions.tap(field); | |
| 60 assertTrue(field.showingSearch); | |
| 61 assertEquals(field.$.searchInput, field.root.activeElement); | |
| 62 | |
| 63 MockInteractions.blur(field.$.searchInput); | |
| 64 assertFalse(field.showingSearch); | |
| 65 | |
| 66 MockInteractions.tap(field); | |
| 67 assertEquals(field.$.searchInput, field.root.activeElement); | |
| 68 | |
| 69 MockInteractions.pressAndReleaseKeyOn( | |
| 70 field.$.searchInput, 27, '', 'Escape'); | |
| 71 assertFalse(field.showingSearch, 'Pressing escape closes field.'); | |
| 72 assertNotEquals(field.$.searchInput, field.root.activeElement); | |
| 73 }); | |
| 74 | |
| 75 test('passes searches correctly', function() { | |
| 76 MockInteractions.tap(field); | |
| 77 simulateSearch('test'); | |
| 78 assertEquals('test', field.getValue()); | |
| 79 | |
| 80 MockInteractions.tap(field.$.clearSearch); | |
| 81 assertFalse(field.showingSearch); | |
| 82 assertEquals('', field.getValue()); | |
| 83 | |
| 84 assertEquals(['test', ''].join(), delegate.searches.join()); | |
| 85 }); | |
| 86 | |
| 87 test('blur does not close field when a search is active', function() { | |
| 88 MockInteractions.tap(field); | |
| 89 simulateSearch('test'); | |
| 90 MockInteractions.blur(field.$.searchInput); | |
| 91 | |
| 92 assertTrue(field.showingSearch); | |
| 93 }); | |
| 94 }); | |
| 95 } | |
| 96 | |
| 97 return { | |
| 98 registerTests: registerTests, | |
| 99 }; | |
| 100 }); | |
| OLD | NEW |