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 Runs the Polymer Passwords and Forms tests. */ |
| 6 |
| 7 /** @const {string} Path to root from chrome/test/data/webui/settings/. */ |
| 8 var ROOT_PATH = '../../../../../'; |
| 9 |
| 10 // Polymer BrowserTest fixture. |
| 11 GEN_INCLUDE( |
| 12 [ROOT_PATH + 'chrome/test/data/webui/polymer_browser_test_base.js']); |
| 13 |
| 14 // Fake data generator. |
| 15 GEN_INCLUDE([ROOT_PATH + |
| 16 'chrome/test/data/webui/settings/passwords_and_autofill_fake_data.js']); |
| 17 |
| 18 function PasswordManagerExpectations() {}; |
| 19 PasswordManagerExpectations.prototype = { |
| 20 requested: { |
| 21 passwords: 0, |
| 22 exceptions: 0, |
| 23 plaintextPassword: 0, |
| 24 }, |
| 25 |
| 26 removed: { |
| 27 passwords: 0, |
| 28 exceptions: 0, |
| 29 }, |
| 30 |
| 31 listening: { |
| 32 passwords: 0, |
| 33 exceptions: 0, |
| 34 }, |
| 35 }; |
| 36 |
| 37 /** |
| 38 * Test implementation |
| 39 * @implements {PasswordManager} |
| 40 * @constructor |
| 41 */ |
| 42 function TestPasswordManager() { |
| 43 this.actual_ = new PasswordManagerExpectations(); |
| 44 }; |
| 45 TestPasswordManager.prototype = { |
| 46 /** @override */ |
| 47 addSavedPasswordListChangedListener: function(listener) { |
| 48 this.actual_.listening.passwords++; |
| 49 this.lastCallback.addSavedPasswordListChangedListener = listener; |
| 50 }, |
| 51 |
| 52 /** @override */ |
| 53 removeSavedPasswordListChangedListener: function(listener) { |
| 54 this.actual_.listening.passwords--; |
| 55 }, |
| 56 |
| 57 /** @override */ |
| 58 getSavedPasswordList: function(callback) { |
| 59 this.actual_.requested.passwords++; |
| 60 callback(this.data.passwords); |
| 61 }, |
| 62 |
| 63 /** @override */ |
| 64 removeSavedPassword: function(loginPair) { |
| 65 this.actual_.removed.passwords++; |
| 66 }, |
| 67 |
| 68 /** @override */ |
| 69 addExceptionListChangedListener: function(listener) { |
| 70 this.actual_.listening.exceptions++; |
| 71 this.lastCallback.addExceptionListChangedListener = listener; |
| 72 }, |
| 73 |
| 74 /** @override */ |
| 75 removeExceptionListChangedListener: function(listener) { |
| 76 this.actual_.listening.exceptions--; |
| 77 }, |
| 78 |
| 79 /** @override */ |
| 80 getExceptionList: function(callback) { |
| 81 this.actual_.requested.exceptions++; |
| 82 callback(this.data.exceptions); |
| 83 }, |
| 84 |
| 85 /** @override */ |
| 86 removeException: function(exception) { |
| 87 this.actual_.removed.exceptions++; |
| 88 }, |
| 89 |
| 90 /** @override */ |
| 91 getPlaintextPassword: function(loginPair, callback) { |
| 92 this.actual_.requested.plaintextPassword++; |
| 93 this.lastCallback.getPlaintextPassword = callback; |
| 94 }, |
| 95 |
| 96 /** |
| 97 * Verifies expectations. |
| 98 * @param {!PasswordManagerExpectations} expected |
| 99 */ |
| 100 assertExpectations: function(expected) { |
| 101 var actual = this.actual_; |
| 102 |
| 103 assertEquals(expected.requested.passwords, actual.requested.passwords); |
| 104 assertEquals(expected.requested.exceptions, actual.requested.exceptions); |
| 105 assertEquals(expected.requested.plaintextPassword, |
| 106 actual.requested.plaintextPassword); |
| 107 |
| 108 assertEquals(expected.removed.passwords, actual.removed.passwords); |
| 109 assertEquals(expected.removed.exceptions, actual.removed.exceptions); |
| 110 |
| 111 assertEquals(expected.listening.passwords, actual.listening.passwords); |
| 112 assertEquals(expected.listening.exceptions, actual.listening.exceptions); |
| 113 }, |
| 114 |
| 115 // Set these to have non-empty data. |
| 116 data: { |
| 117 passwords: [], |
| 118 exceptions: [], |
| 119 }, |
| 120 |
| 121 // Holds the last callbacks so they can be called when needed/ |
| 122 lastCallback: { |
| 123 addSavedPasswordListChangedListener: null, |
| 124 addExceptionListChangedListener: null, |
| 125 getPlaintextPassword: null, |
| 126 }, |
| 127 }; |
| 128 |
| 129 function AutofillManagerExpectations() {}; |
| 130 AutofillManagerExpectations.prototype = { |
| 131 requested: { |
| 132 addresses: 0, |
| 133 creditCards: 0, |
| 134 }, |
| 135 |
| 136 listening: { |
| 137 addresses: 0, |
| 138 creditCards: 0, |
| 139 }, |
| 140 }; |
| 141 |
| 142 /** |
| 143 * Test implementation |
| 144 * @implements {AutofillManager} |
| 145 * @constructor |
| 146 */ |
| 147 function TestAutofillManager() { |
| 148 this.actual_ = new AutofillManagerExpectations(); |
| 149 }; |
| 150 TestAutofillManager.prototype = { |
| 151 /** @override */ |
| 152 addAddressListChangedListener: function(listener) { |
| 153 this.actual_.listening.addresses++; |
| 154 this.lastCallback.addAddressListChangedListener = listener; |
| 155 }, |
| 156 |
| 157 /** @override */ |
| 158 removeAddressListChangedListener: function(listener) { |
| 159 this.actual_.listening.addresses--; |
| 160 }, |
| 161 |
| 162 /** @override */ |
| 163 getAddressList: function(callback) { |
| 164 this.actual_.requested.addresses++; |
| 165 callback(this.data.addresses); |
| 166 }, |
| 167 |
| 168 /** @override */ |
| 169 addCreditCardListChangedListener: function(listener) { |
| 170 this.actual_.listening.creditCards++; |
| 171 this.lastCallback.addCreditCardListChangedListener = listener; |
| 172 }, |
| 173 |
| 174 /** @override */ |
| 175 removeCreditCardListChangedListener: function(listener) { |
| 176 this.actual_.listening.creditCards--; |
| 177 }, |
| 178 |
| 179 /** @override */ |
| 180 getCreditCardList: function(callback) { |
| 181 this.actual_.requested.creditCards++; |
| 182 callback(this.data.creditCards); |
| 183 }, |
| 184 |
| 185 /** |
| 186 * Verifies expectations. |
| 187 * @param {!AutofillManagerExpectations} expected |
| 188 */ |
| 189 assertExpectations: function(expected) { |
| 190 var actual = this.actual_; |
| 191 |
| 192 assertEquals(expected.requested.addresses, actual.requested.addresses); |
| 193 assertEquals(expected.requested.creditCards, actual.requested.creditCards); |
| 194 |
| 195 assertEquals(expected.listening.addresses, actual.listening.addresses); |
| 196 assertEquals(expected.listening.creditCards, actual.listening.creditCards); |
| 197 }, |
| 198 |
| 199 // Set these to have non-empty data. |
| 200 data: { |
| 201 addresses: [], |
| 202 creditCards: [], |
| 203 }, |
| 204 |
| 205 // Holds the last callbacks so they can be called when needed/ |
| 206 lastCallback: { |
| 207 addAddressListChangedListener: null, |
| 208 addCreditCardListChangedListener: null, |
| 209 }, |
| 210 }; |
| 211 |
| 212 /** |
| 213 * @constructor |
| 214 * @extends {PolymerTest} |
| 215 */ |
| 216 function PasswordsAndFormsBrowserTest() {} |
| 217 |
| 218 PasswordsAndFormsBrowserTest.prototype = { |
| 219 __proto__: PolymerTest.prototype, |
| 220 |
| 221 /** @override */ |
| 222 browsePreload: 'chrome://md-settings/passwords_and_forms_page/' + |
| 223 'passwords_and_forms_page.html', |
| 224 |
| 225 /** @override */ |
| 226 extraLibraries: PolymerTest.getLibraries(ROOT_PATH), |
| 227 |
| 228 /** @override */ |
| 229 setUp: function() { |
| 230 PolymerTest.prototype.setUp.call(this); |
| 231 |
| 232 // Test is run on an individual element that won't have a page language. |
| 233 this.accessibilityAuditConfig.auditRulesToIgnore.push('humanLangMissing'); |
| 234 |
| 235 // Override the PasswordManagerImpl for testing. |
| 236 this.passwordManager = new TestPasswordManager(); |
| 237 PasswordManagerImpl.instance_ = this.passwordManager; |
| 238 |
| 239 // Override the AutofillManagerImpl for testing. |
| 240 this.autofillManager = new TestAutofillManager(); |
| 241 AutofillManagerImpl.instance_ = this.autofillManager; |
| 242 }, |
| 243 |
| 244 /** |
| 245 * Creates a new passwords and forms element. |
| 246 * @return {!Object} |
| 247 */ |
| 248 createPasswordsAndFormsElement: function() { |
| 249 var element = document.createElement('settings-passwords-and-forms-page'); |
| 250 document.body.appendChild(element); |
| 251 Polymer.dom.flush(); |
| 252 return element; |
| 253 }, |
| 254 |
| 255 /** |
| 256 * Creates PasswordManagerExpectations with the values expected after first |
| 257 * creating the element. |
| 258 * @return {!PasswordManagerExpectations} |
| 259 */ |
| 260 basePasswordExpectations: function() { |
| 261 var expected = new PasswordManagerExpectations(); |
| 262 expected.requested.passwords = 1; |
| 263 expected.requested.exceptions = 1; |
| 264 expected.listening.passwords = 1; |
| 265 expected.listening.exceptions = 1; |
| 266 return expected; |
| 267 }, |
| 268 |
| 269 /** |
| 270 * Creates AutofillManagerExpectations with the values expected after first |
| 271 * creating the element. |
| 272 * @return {!AutofillManagerExpectations} |
| 273 */ |
| 274 baseAutofillExpectations: function() { |
| 275 var expected = new AutofillManagerExpectations(); |
| 276 expected.requested.addresses = 1; |
| 277 expected.requested.creditCards = 1; |
| 278 expected.listening.addresses = 1; |
| 279 expected.listening.creditCards = 1; |
| 280 return expected; |
| 281 }, |
| 282 }; |
| 283 |
| 284 /** |
| 285 * This test will validate that the section is loaded with data. |
| 286 */ |
| 287 TEST_F('PasswordsAndFormsBrowserTest', 'uiTests', function() { |
| 288 var self = this; |
| 289 |
| 290 suite('PasswordsAndForms', function() { |
| 291 test('baseLoadAndRemove', function() { |
| 292 var element = self.createPasswordsAndFormsElement(); |
| 293 |
| 294 var passwordsExpectations = self.basePasswordExpectations(); |
| 295 self.passwordManager.assertExpectations(passwordsExpectations); |
| 296 |
| 297 var autofillExpectations = self.baseAutofillExpectations(); |
| 298 self.autofillManager.assertExpectations(autofillExpectations); |
| 299 |
| 300 element.remove(); |
| 301 Polymer.dom.flush(); |
| 302 |
| 303 passwordsExpectations.listening.passwords = 0; |
| 304 passwordsExpectations.listening.exceptions = 0; |
| 305 self.passwordManager.assertExpectations(passwordsExpectations); |
| 306 |
| 307 autofillExpectations.listening.addresses = 0; |
| 308 autofillExpectations.listening.creditCards = 0; |
| 309 self.autofillManager.assertExpectations(autofillExpectations); |
| 310 }); |
| 311 |
| 312 test('loadPasswordsAsync', function() { |
| 313 var element = self.createPasswordsAndFormsElement(); |
| 314 |
| 315 var list = [FakeDataMaker.passwordEntry(), FakeDataMaker.passwordEntry()]; |
| 316 self.passwordManager.lastCallback.addSavedPasswordListChangedListener( |
| 317 list); |
| 318 Polymer.dom.flush(); |
| 319 |
| 320 assertEquals(list, element.savedPasswords); |
| 321 |
| 322 // The callback is coming from the manager, so the element shouldn't have |
| 323 // additional calls to the manager after the base expectations. |
| 324 self.passwordManager.assertExpectations(self.basePasswordExpectations()); |
| 325 self.autofillManager.assertExpectations(self.baseAutofillExpectations()); |
| 326 }); |
| 327 |
| 328 test('loadExceptionsAsync', function() { |
| 329 var element = self.createPasswordsAndFormsElement(); |
| 330 |
| 331 var list = [FakeDataMaker.exceptionEntry(), |
| 332 FakeDataMaker.exceptionEntry()]; |
| 333 self.passwordManager.lastCallback.addExceptionListChangedListener( |
| 334 list); |
| 335 Polymer.dom.flush(); |
| 336 |
| 337 assertEquals(list, element.passwordExceptions); |
| 338 |
| 339 // The callback is coming from the manager, so the element shouldn't have |
| 340 // additional calls to the manager after the base expectations. |
| 341 self.passwordManager.assertExpectations(self.basePasswordExpectations()); |
| 342 self.autofillManager.assertExpectations(self.baseAutofillExpectations()); |
| 343 }); |
| 344 |
| 345 test('loadAddressesAsync', function() { |
| 346 var element = self.createPasswordsAndFormsElement(); |
| 347 |
| 348 var list = [FakeDataMaker.addressEntry(), FakeDataMaker.addressEntry()]; |
| 349 self.autofillManager.lastCallback.addAddressListChangedListener(list); |
| 350 Polymer.dom.flush(); |
| 351 |
| 352 assertEquals(list, element.addresses); |
| 353 |
| 354 // The callback is coming from the manager, so the element shouldn't have |
| 355 // additional calls to the manager after the base expectations. |
| 356 self.passwordManager.assertExpectations(self.basePasswordExpectations()); |
| 357 self.autofillManager.assertExpectations(self.baseAutofillExpectations()); |
| 358 }); |
| 359 |
| 360 test('loadCreditCardsAsync', function() { |
| 361 var element = self.createPasswordsAndFormsElement(); |
| 362 |
| 363 var list = [FakeDataMaker.creditCardEntry(), |
| 364 FakeDataMaker.creditCardEntry()]; |
| 365 self.autofillManager.lastCallback.addCreditCardListChangedListener(list); |
| 366 Polymer.dom.flush(); |
| 367 |
| 368 assertEquals(list, element.creditCards); |
| 369 |
| 370 // The callback is coming from the manager, so the element shouldn't have |
| 371 // additional calls to the manager after the base expectations. |
| 372 self.passwordManager.assertExpectations(self.basePasswordExpectations()); |
| 373 self.autofillManager.assertExpectations(self.baseAutofillExpectations()); |
| 374 }); |
| 375 }); |
| 376 |
| 377 mocha.run(); |
| 378 }); |
OLD | NEW |