Chromium Code Reviews| 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 Common utilities for extension ui tests. */ | 5 /** @fileoverview Common utilities for extension ui tests. */ |
| 6 cr.define('extension_test_util', function() { | 6 cr.define('extension_test_util', function() { |
| 7 /** | 7 /** |
| 8 * A mock to test that clicking on an element calls a specific method. | 8 * A mock to test that clicking on an element calls a specific method. |
| 9 * @constructor | 9 * @constructor |
| 10 */ | 10 */ |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 var mock = new MockController(); | 22 var mock = new MockController(); |
| 23 var mockMethod = mock.createFunctionMock(this, callName); | 23 var mockMethod = mock.createFunctionMock(this, callName); |
| 24 MockMethod.prototype.addExpectation.apply( | 24 MockMethod.prototype.addExpectation.apply( |
| 25 mockMethod, opt_expectedArgs); | 25 mockMethod, opt_expectedArgs); |
| 26 MockInteractions.tap(element); | 26 MockInteractions.tap(element); |
| 27 mock.verifyMocks(); | 27 mock.verifyMocks(); |
| 28 }, | 28 }, |
| 29 }; | 29 }; |
| 30 | 30 |
| 31 /** | 31 /** |
| 32 * A mock to test receiving expected events and verify that they were called | |
| 33 * with the proper detail values. | |
| 34 */ | |
| 35 function ListenerMock() {} | |
| 36 | |
| 37 ListenerMock.prototype = { | |
| 38 /** @private {Object<{satisfied: boolean, args: !Object}>} */ | |
| 39 listeners_: {}, | |
| 40 | |
| 41 /** | |
| 42 * @param {string} eventName | |
| 43 * @param {Event} e | |
| 44 */ | |
| 45 onEvent_: function(eventName, e) { | |
| 46 assert(this.listeners_.hasOwnProperty(eventName)); | |
| 47 if (this.listeners_[eventName].satisfied) { | |
| 48 // Event was already called and checked. We could always make this | |
| 49 // more intelligent by allowing for subsequent calls, removing the | |
| 50 // listener, etc, but there's no need right now. | |
| 51 return; | |
| 52 } | |
| 53 var expected = this.listeners_[eventName].args || {}; | |
| 54 expectDeepEquals(e.detail, expected); | |
| 55 this.listeners_[eventName].satisfied = true; | |
| 56 }, | |
| 57 | |
| 58 /** | |
| 59 * Adds an expected event. | |
| 60 * @param {!EventTarget} target | |
| 61 * @param {string} eventName | |
| 62 * @param {Object=} eventArgs If omitted, will check that the details are | |
|
michaelpg
2016/07/08 07:12:33
opt_eventArgs
Devlin
2016/07/08 23:14:43
Done.
| |
| 63 * empty (i.e., {}). | |
| 64 */ | |
| 65 addListener: function(target, eventName, eventArgs) { | |
| 66 assert(!this.listeners_.hasOwnProperty(eventName)); | |
| 67 this.listeners_[eventName] = {args: eventArgs || {}, satisfied: false}; | |
| 68 target.addEventListener(eventName, this.onEvent_.bind(this, eventName)); | |
| 69 }, | |
| 70 | |
| 71 /** Verifies the expectations set. */ | |
| 72 verify: function() { | |
| 73 var missingEvents = []; | |
| 74 for (var key in this.listeners_) { | |
| 75 if (!this.listeners_[key].satisfied) | |
| 76 missingEvents.push(key); | |
| 77 } | |
| 78 expectEquals(0, missingEvents.length, JSON.stringify(missingEvents)); | |
| 79 }, | |
| 80 } | |
| 81 | |
| 82 /** | |
| 32 * A mock delegate for the item, capable of testing functionality. | 83 * A mock delegate for the item, capable of testing functionality. |
| 33 * @constructor | 84 * @constructor |
| 34 * @extends {ClickMock} | 85 * @extends {ClickMock} |
| 35 * @implements {extensions.ItemDelegate} | 86 * @implements {extensions.ItemDelegate} |
| 36 */ | 87 */ |
| 37 function MockItemDelegate() {} | 88 function MockItemDelegate() {} |
| 38 | 89 |
| 39 MockItemDelegate.prototype = { | 90 MockItemDelegate.prototype = { |
| 40 __proto__: ClickMock.prototype, | 91 __proto__: ClickMock.prototype, |
| 41 | 92 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 139 function testIronIcons(e) { | 190 function testIronIcons(e) { |
| 140 e.querySelectorAll('* /deep/ iron-icon').forEach(function(icon) { | 191 e.querySelectorAll('* /deep/ iron-icon').forEach(function(icon) { |
| 141 var svg = icon.$$('svg'); | 192 var svg = icon.$$('svg'); |
| 142 expectTrue(!!svg && svg.innerHTML != '', | 193 expectTrue(!!svg && svg.innerHTML != '', |
| 143 'icon "' + icon.icon + '" is not present'); | 194 'icon "' + icon.icon + '" is not present'); |
| 144 }); | 195 }); |
| 145 } | 196 } |
| 146 | 197 |
| 147 return { | 198 return { |
| 148 ClickMock: ClickMock, | 199 ClickMock: ClickMock, |
| 200 ListenerMock: ListenerMock, | |
| 149 MockItemDelegate: MockItemDelegate, | 201 MockItemDelegate: MockItemDelegate, |
| 150 isVisible: isVisible, | 202 isVisible: isVisible, |
| 151 testVisible: testVisible, | 203 testVisible: testVisible, |
| 152 createExtensionInfo: createExtensionInfo, | 204 createExtensionInfo: createExtensionInfo, |
| 153 testIronIcons: testIronIcons, | 205 testIronIcons: testIronIcons, |
| 154 }; | 206 }; |
| 155 }); | 207 }); |
| OLD | NEW |