OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 cr.define('settings_startup_urls_page', function() { | 5 cr.define('settings_startup_urls_page', function() { |
6 /** | 6 /** |
7 * @constructor | 7 * @constructor |
8 * @implements {settings.StartupUrlsPageBrowserProxy} | 8 * @implements {settings.StartupUrlsPageBrowserProxy} |
9 * @extends {settings.TestBrowserProxy} | 9 * @extends {settings.TestBrowserProxy} |
10 */ | 10 */ |
11 function TestStartupUrlsPageBrowserProxy() { | 11 function TestStartupUrlsPageBrowserProxy() { |
12 settings.TestBrowserProxy.call(this, [ | 12 settings.TestBrowserProxy.call(this, [ |
| 13 'addStartupPage', |
| 14 'loadStartupPages', |
| 15 'useCurrentPages', |
13 'validateStartupPage', | 16 'validateStartupPage', |
14 'addStartupPage', | |
15 ]); | 17 ]); |
| 18 |
| 19 /** @private {boolean} */ |
| 20 this.urlIsValid_ = true; |
16 } | 21 } |
17 | 22 |
18 TestStartupUrlsPageBrowserProxy.prototype = { | 23 TestStartupUrlsPageBrowserProxy.prototype = { |
19 __proto__: settings.TestBrowserProxy.prototype, | 24 __proto__: settings.TestBrowserProxy.prototype, |
20 | 25 |
21 urlsAreValid: false, | 26 /** @param {boolean} isValid */ |
22 | 27 setUrlValidity: function(isValid) { |
23 /** @override */ | 28 this.urlIsValid_ = isValid; |
24 loadStartupPages: function() {}, | |
25 | |
26 /** @override */ | |
27 validateStartupPage: function(url) { | |
28 this.methodCalled('validateStartupPage'); | |
29 var resolver = new PromiseResolver; | |
30 resolver.promise = Promise.resolve(this.urlsAreValid); | |
31 return resolver; | |
32 }, | 29 }, |
33 | 30 |
34 /** @override */ | 31 /** @override */ |
35 addStartupPage: function(url) { | 32 addStartupPage: function(url) { |
36 this.methodCalled('addStartupPage'); | 33 this.methodCalled('addStartupPage', url); |
| 34 return Promise.resolve(this.urlIsValid_); |
| 35 }, |
| 36 |
| 37 /** @override */ |
| 38 loadStartupPages: function() { |
| 39 this.methodCalled('loadStartupPages'); |
| 40 }, |
| 41 |
| 42 /** @override */ |
| 43 useCurrentPages: function() { |
| 44 this.methodCalled('useCurrentPages'); |
| 45 }, |
| 46 |
| 47 /** @override */ |
| 48 validateStartupPage: function(url) { |
| 49 this.methodCalled('validateStartupPage', url); |
| 50 return Promise.resolve(this.urlIsValid_); |
37 }, | 51 }, |
38 }; | 52 }; |
39 | 53 |
| 54 suite('StartupUrlDialog', function() { |
| 55 /** @type {?SettingsStartupUrlDialogElement} */ |
| 56 var dialog = null; |
| 57 |
| 58 var browserProxy = null; |
| 59 |
| 60 /** |
| 61 * Triggers an 'input' event on the given text input field, which triggers |
| 62 * validation to occur. |
| 63 * @param {!PaperInputElement} element |
| 64 */ |
| 65 function pressSpace(element) { |
| 66 // The actual key code is irrelevant for these tests. |
| 67 MockInteractions.keyEventOn(element, 'input', 32 /* space key code */); |
| 68 } |
| 69 |
| 70 setup(function() { |
| 71 browserProxy = new TestStartupUrlsPageBrowserProxy(); |
| 72 settings.StartupUrlsPageBrowserProxyImpl.instance_ = browserProxy; |
| 73 PolymerTest.clearBody(); |
| 74 dialog = document.createElement('settings-startup-url-dialog'); |
| 75 document.body.appendChild(dialog); |
| 76 }); |
| 77 |
| 78 teardown(function() { dialog.remove(); }); |
| 79 |
| 80 // Test that validation occurs as the user is typing, and that the action |
| 81 // button is updated accordingly. |
| 82 test('Validation', function() { |
| 83 assertTrue(dialog.$.dialog.opened); |
| 84 var addButton = dialog.$.add; |
| 85 assertTrue(!!addButton); |
| 86 assertTrue(addButton.disabled); |
| 87 |
| 88 var inputElement = dialog.$.url; |
| 89 assertTrue(!!inputElement); |
| 90 |
| 91 var expectedUrl = "dummy-foo.com"; |
| 92 inputElement.value = expectedUrl; |
| 93 browserProxy.setUrlValidity(false); |
| 94 pressSpace(inputElement); |
| 95 |
| 96 return browserProxy.whenCalled('validateStartupPage').then(function(url) { |
| 97 assertEquals(expectedUrl, url); |
| 98 assertTrue(addButton.disabled); |
| 99 |
| 100 browserProxy.setUrlValidity(true); |
| 101 browserProxy.resetResolver('validateStartupPage'); |
| 102 pressSpace(inputElement); |
| 103 |
| 104 return browserProxy.whenCalled('validateStartupPage'); |
| 105 }).then(function() { |
| 106 assertFalse(addButton.disabled); |
| 107 }); |
| 108 }); |
| 109 |
| 110 test('AddStartupPage', function() { |
| 111 assertTrue(dialog.$.dialog.opened); |
| 112 var addButton = dialog.$.add; |
| 113 addButton.disabled = false; |
| 114 |
| 115 // Test that the dialog remains open if the user somehow manages to submit |
| 116 // an invalid URL. |
| 117 browserProxy.setUrlValidity(false); |
| 118 MockInteractions.tap(addButton); |
| 119 return browserProxy.whenCalled('addStartupPage').then(function() { |
| 120 assertTrue(dialog.$.dialog.opened); |
| 121 |
| 122 // Test that dialog is closed if the user submits a valid URL. |
| 123 browserProxy.setUrlValidity(true); |
| 124 browserProxy.resetResolver('addStartupPage'); |
| 125 MockInteractions.tap(addButton); |
| 126 return browserProxy.whenCalled('addStartupPage'); |
| 127 }).then(function() { |
| 128 assertFalse(dialog.$.dialog.opened); |
| 129 }); |
| 130 }); |
| 131 }); |
| 132 |
40 suite('StartupUrlsPage', function() { | 133 suite('StartupUrlsPage', function() { |
41 /** @type {?SettingsStartupUrlsPageElement} */ | 134 /** @type {?SettingsStartupUrlsPageElement} */ |
42 var page = null; | 135 var page = null; |
43 | 136 |
44 var browserProxy = null; | 137 var browserProxy = null; |
45 | 138 |
46 setup(function() { | 139 setup(function() { |
47 browserProxy = new TestStartupUrlsPageBrowserProxy(); | 140 browserProxy = new TestStartupUrlsPageBrowserProxy(); |
48 settings.StartupUrlsPageBrowserProxyImpl.instance_ = browserProxy; | 141 settings.StartupUrlsPageBrowserProxyImpl.instance_ = browserProxy; |
49 PolymerTest.clearBody(); | 142 PolymerTest.clearBody(); |
50 page = document.createElement('settings-startup-urls-page'); | 143 page = document.createElement('settings-startup-urls-page'); |
51 document.body.appendChild(page); | 144 document.body.appendChild(page); |
52 }); | 145 }); |
53 | 146 |
54 teardown(function() { page.remove(); }); | 147 teardown(function() { page.remove(); }); |
55 | 148 |
56 test('validate', function() { | 149 // Test that the page is requesting information from the browser. |
57 browserProxy.whenCalled('validateStartupPage').then(function() { | 150 test('Initialization', function() { |
58 var addButton = page.$.add; | 151 return browserProxy.whenCalled('loadStartupPages'); |
59 assertTrue(!!addButton); | 152 }); |
60 assertFalse(browserProxy.urlsAreValid); | |
61 assertTrue(addButton.disabled); | |
62 | 153 |
63 browserProxy.resetResolver('validateStartupPage'); | 154 test('UseCurrentPages', function() { |
64 browserProxy.whenCalled('validateStartupPage').then(function() { | 155 var useCurrentPagesButton = page.$.useCurrentPages; |
65 page.async(function() { | 156 assertTrue(!!useCurrentPagesButton); |
66 assertFalse(addButton.disabled); | 157 MockInteractions.tap(useCurrentPagesButton); |
67 MockInteractions.tap(addButton); | 158 return browserProxy.whenCalled('useCurrentPages'); |
68 }); | 159 }); |
69 }); | |
70 | 160 |
71 browserProxy.urlsAreValid = true; | 161 test('AddPage_OpensDialog', function() { |
72 page.$.newUrl.value = "overriding validation anyway"; | 162 var addPageButton = page.$.addPage; |
73 }); | 163 assertTrue(!!addPageButton); |
| 164 assertFalse(!!page.$$('settings-startup-url-dialog')); |
74 | 165 |
75 return browserProxy.whenCalled('addStartupPage'); | 166 MockInteractions.tap(addPageButton); |
| 167 Polymer.dom.flush(); |
| 168 assertTrue(!!page.$$('settings-startup-url-dialog')); |
76 }); | 169 }); |
77 }); | 170 }); |
78 }); | 171 }); |
OLD | NEW |