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 /** @override */ |
| 27 validateStartupPage: function(url) { |
| 28 this.methodCalled('validateStartupPage', url); |
| 29 return Promise.resolve(this.urlIsValid_); |
| 30 }, |
22 | 31 |
23 /** @override */ | 32 /** @override */ |
24 loadStartupPages: function() {}, | 33 loadStartupPages: function() { |
| 34 this.methodCalled('loadStartupPages'); |
| 35 }, |
| 36 |
| 37 /** @param {boolean} isValid */ |
| 38 setValidateStartupPage: function(isValid) { |
| 39 this.urlIsValid_ = isValid; |
| 40 }, |
25 | 41 |
26 /** @override */ | 42 /** @override */ |
27 validateStartupPage: function(url) { | 43 useCurrentPages: function() { |
28 this.methodCalled('validateStartupPage'); | 44 this.methodCalled('useCurrentPages'); |
29 var resolver = new PromiseResolver; | |
30 resolver.promise = Promise.resolve(this.urlsAreValid); | |
31 return resolver; | |
32 }, | 45 }, |
33 | 46 |
34 /** @override */ | 47 /** @override */ |
35 addStartupPage: function(url) { | 48 addStartupPage: function(url) { |
36 this.methodCalled('addStartupPage'); | 49 this.methodCalled('addStartupPage', url); |
37 }, | 50 }, |
38 }; | 51 }; |
39 | 52 |
| 53 suite('StartupUrlDialog', function() { |
| 54 /** @type {?SettingsStartupUrlDialogElement} */ |
| 55 var dialog = null; |
| 56 |
| 57 var browserProxy = null; |
| 58 |
| 59 /** |
| 60 * Triggers an 'input' event on the given text input field, which triggers |
| 61 * validation to occur. |
| 62 * @param {!PaperInputElement} element |
| 63 */ |
| 64 function triggerInputEvent(element) { |
| 65 // The actual key code is irrelevant for these tests. |
| 66 MockInteractions.keyEventOn(element, 'input', 32 /* space key code */); |
| 67 } |
| 68 |
| 69 setup(function() { |
| 70 browserProxy = new TestStartupUrlsPageBrowserProxy(); |
| 71 settings.StartupUrlsPageBrowserProxyImpl.instance_ = browserProxy; |
| 72 PolymerTest.clearBody(); |
| 73 dialog = document.createElement('settings-startup-url-dialog'); |
| 74 document.body.appendChild(dialog); |
| 75 }); |
| 76 |
| 77 teardown(function() { dialog.remove(); }); |
| 78 |
| 79 test('Validation', function() { |
| 80 var addButton = dialog.$.add; |
| 81 assertTrue(!!addButton); |
| 82 assertTrue(addButton.disabled); |
| 83 |
| 84 var inputElement = dialog.$.url; |
| 85 assertTrue(!!inputElement); |
| 86 |
| 87 var expectedUrl = "dummy-foo.com"; |
| 88 inputElement.value = expectedUrl; |
| 89 browserProxy.setValidateStartupPage(false); |
| 90 triggerInputEvent(inputElement); |
| 91 |
| 92 return browserProxy.whenCalled('validateStartupPage').then(function(url) { |
| 93 assertEquals(expectedUrl, url); |
| 94 assertTrue(addButton.disabled); |
| 95 |
| 96 browserProxy.setValidateStartupPage(true); |
| 97 browserProxy.resetResolver('validateStartupPage'); |
| 98 triggerInputEvent(inputElement); |
| 99 |
| 100 return browserProxy.whenCalled('validateStartupPage'); |
| 101 }).then(function() { |
| 102 assertFalse(addButton.disabled); |
| 103 MockInteractions.tap(addButton); |
| 104 return browserProxy.whenCalled('addStartupPage'); |
| 105 }).then(function(url) { |
| 106 assertEquals(expectedUrl, url); |
| 107 }); |
| 108 }); |
| 109 }); |
| 110 |
40 suite('StartupUrlsPage', function() { | 111 suite('StartupUrlsPage', function() { |
41 /** @type {?SettingsStartupUrlsPageElement} */ | 112 /** @type {?SettingsStartupUrlsPageElement} */ |
42 var page = null; | 113 var page = null; |
43 | 114 |
44 var browserProxy = null; | 115 var browserProxy = null; |
45 | 116 |
46 setup(function() { | 117 setup(function() { |
47 browserProxy = new TestStartupUrlsPageBrowserProxy(); | 118 browserProxy = new TestStartupUrlsPageBrowserProxy(); |
48 settings.StartupUrlsPageBrowserProxyImpl.instance_ = browserProxy; | 119 settings.StartupUrlsPageBrowserProxyImpl.instance_ = browserProxy; |
49 PolymerTest.clearBody(); | 120 PolymerTest.clearBody(); |
50 page = document.createElement('settings-startup-urls-page'); | 121 page = document.createElement('settings-startup-urls-page'); |
51 document.body.appendChild(page); | 122 document.body.appendChild(page); |
52 }); | 123 }); |
53 | 124 |
54 teardown(function() { page.remove(); }); | 125 teardown(function() { page.remove(); }); |
55 | 126 |
56 test('validate', function() { | 127 // Test that the page is requesting information from the browser. |
57 browserProxy.whenCalled('validateStartupPage').then(function() { | 128 test('Initialization', function() { |
58 var addButton = page.$.add; | 129 return browserProxy.whenCalled('loadStartupPages'); |
59 assertTrue(!!addButton); | 130 }); |
60 assertFalse(browserProxy.urlsAreValid); | |
61 assertTrue(addButton.disabled); | |
62 | 131 |
63 browserProxy.resetResolver('validateStartupPage'); | 132 test('UseCurrentPages', function() { |
64 browserProxy.whenCalled('validateStartupPage').then(function() { | 133 var useCurrentPagesButton = page.$.useCurrentPages; |
65 page.async(function() { | 134 assertTrue(!!useCurrentPagesButton); |
66 assertFalse(addButton.disabled); | 135 MockInteractions.tap(useCurrentPagesButton); |
67 MockInteractions.tap(addButton); | 136 return browserProxy.whenCalled('useCurrentPages'); |
68 }); | 137 }); |
69 }); | |
70 | 138 |
71 browserProxy.urlsAreValid = true; | 139 test('AddPage_OpensDialog', function() { |
72 page.$.newUrl.value = "overriding validation anyway"; | 140 var addPageButton = page.$.addPage; |
73 }); | 141 assertTrue(!!addPageButton); |
| 142 assertFalse(!!page.$$('settings-startup-url-dialog')); |
74 | 143 |
75 return browserProxy.whenCalled('addStartupPage'); | 144 MockInteractions.tap(addPageButton); |
| 145 Polymer.dom.flush(); |
| 146 assertTrue(!!page.$$('settings-startup-url-dialog')); |
76 }); | 147 }); |
77 }); | 148 }); |
78 }); | 149 }); |
OLD | NEW |