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 cr.define('settings_main_page', function() { | |
6 /** | |
7 * @implements {SearchManager} | |
8 */ | |
9 var TestSearchManager = function() { | |
10 /** @private {boolean} */ | |
11 this.matchesFound_ = true; | |
12 | |
13 /** @private {?settings.SearchRequest} */ | |
14 this.searchRequest_ = null; | |
15 } | |
16 | |
17 TestSearchManager.prototype = { | |
18 /** | |
19 * @param {boolean} matchesFound | |
20 */ | |
21 setMatchesFound: function(matchesFound) { | |
22 this.matchesFound_ = matchesFound; | |
23 }, | |
24 | |
25 /** @override */ | |
26 search: function(text, page) { | |
27 if (this.searchRequest_ == null || !this.searchRequest_.isSame(text)) { | |
28 this.searchRequest_ = new settings.SearchRequest(text); | |
29 this.searchRequest_.finished = true; | |
30 this.searchRequest_.updateMatches(this.matchesFound_); | |
31 this.searchRequest_.resolver.resolve(this.searchRequest_); | |
32 } | |
33 return this.searchRequest_.resolver.promise; | |
34 }, | |
35 }; | |
36 | |
37 function registerTests() { | |
38 var settingsPrefs = null; | |
39 | |
40 suiteSetup(function() { | |
41 settingsPrefs = document.createElement('settings-prefs'); | |
42 return CrSettingsPrefs.initialized; | |
43 }); | |
44 | |
45 suite('MainPageTests', function() { | |
46 /** @type {?TestSearchManager} */ | |
47 var searchManager = null; | |
48 | |
49 /** @type {?SettingsMainElement} */ | |
50 var settingsMain = null; | |
51 | |
52 setup(function() { | |
53 settings.navigateTo(settings.Route.BASIC); | |
54 searchManager = new TestSearchManager(); | |
55 settings.setSearchManagerForTesting(searchManager); | |
56 PolymerTest.clearBody(); | |
57 settingsMain = document.createElement('settings-main'); | |
58 settingsMain.prefs = settingsPrefs.prefs; | |
59 settingsMain.toolbarSpinnerActive = false; | |
60 document.body.appendChild(settingsMain); | |
61 }); | |
62 | |
63 teardown(function() { settingsMain.remove(); }); | |
64 | |
65 test('no results page shows and hides', function() { | |
66 Polymer.dom.flush(); | |
67 var noSearchResults = settingsMain.$.noSearchResults; | |
68 assertTrue(!!noSearchResults); | |
69 assertTrue(noSearchResults.hidden); | |
70 | |
71 var toggleContainer = settingsMain.$$('#toggleContainer'); | |
72 assertTrue(!!toggleContainer); | |
73 assertNotEquals('none', toggleContainer.style.display); | |
74 | |
75 searchManager.setMatchesFound(false); | |
76 return settingsMain.searchContents('Query1').then(function() { | |
77 assertFalse(noSearchResults.hidden); | |
78 assertEquals('none', toggleContainer.style.display); | |
79 | |
80 searchManager.setMatchesFound(true); | |
81 return settingsMain.searchContents('Query2'); | |
82 }).then(function() { | |
83 assertTrue(noSearchResults.hidden); | |
84 }); | |
85 }); | |
86 | |
87 // Ensure that when the user clears the search box, the "no results" page | |
88 // is hidden and the "advanced page toggle" is visible again. | |
89 test('no results page hides on clear', function() { | |
90 Polymer.dom.flush(); | |
91 var noSearchResults = settingsMain.$.noSearchResults; | |
92 assertTrue(!!noSearchResults); | |
93 assertTrue(noSearchResults.hidden); | |
94 | |
95 var toggleContainer = settingsMain.$$('#toggleContainer'); | |
96 assertTrue(!!toggleContainer); | |
97 assertNotEquals('none', toggleContainer.style.display); | |
98 | |
99 searchManager.setMatchesFound(false); | |
100 // Clearing the search box is effectively a search for the empty string. | |
101 return settingsMain.searchContents('').then(function() { | |
102 Polymer.dom.flush(); | |
103 assertTrue(noSearchResults.hidden); | |
104 assertNotEquals('none', toggleContainer.style.display); | |
105 }); | |
106 }); | |
107 | |
108 /** | |
109 * Asserts the visibility of the basic and advanced pages after exiting | |
110 * search mode. | |
111 * @param {string} Expected 'display' value for the basic page. | |
112 * @param {string} Expected 'display' value for the advanced page. | |
113 * @return {!Promise} | |
114 */ | |
115 function assertPageVisibilityAfterSearch( | |
116 expectedBasic, expectedAdvanced) { | |
117 searchManager.setMatchesFound(true); | |
118 return settingsMain.searchContents('Query1').then(function() { | |
119 searchManager.setMatchesFound(false); | |
120 return settingsMain.searchContents(''); | |
121 }).then(function() { | |
122 Polymer.dom.flush(); | |
123 assertEquals( | |
124 expectedBasic, | |
125 settingsMain.$$('settings-basic-page').style.display); | |
126 assertEquals( | |
127 expectedAdvanced, | |
128 settingsMain.$$('settings-advanced-page').style.display); | |
129 }); | |
130 } | |
131 | |
132 test('exiting search mode, advanced collapsed', function() { | |
133 // Simulating searching while the advanced page is collapsed. | |
134 settingsMain.currentRouteChanged(settings.Route.BASIC); | |
135 Polymer.dom.flush(); | |
136 return assertPageVisibilityAfterSearch('', 'none'); | |
137 }); | |
138 | |
139 // Ensure that clearing the search results restores both "basic" and | |
140 // "advanced" page, when the search has been initiated from a subpage | |
141 // whose parent is the "advanced" page. | |
142 test('exiting search mode, advanced expanded', function() { | |
143 settings.navigateTo(settings.Route.SITE_SETTINGS); | |
144 Polymer.dom.flush(); | |
145 return assertPageVisibilityAfterSearch('', ''); | |
146 }); | |
147 | |
148 // Ensure that searching, then entering a subpage, then going back | |
149 // lands the user in a page where both basic and advanced sections are | |
150 // visible, because the page is still in search mode. | |
151 test('returning from subpage to search results', function() { | |
152 settings.navigateTo(settings.Route.BASIC); | |
153 Polymer.dom.flush(); | |
154 | |
155 searchManager.setMatchesFound(true); | |
156 return settingsMain.searchContents('Query1').then(function() { | |
157 // Simulate navigating into a subpage. | |
158 settings.navigateTo(settings.Route.SEARCH_ENGINES); | |
159 settingsMain.$$('settings-basic-page').fire('subpage-expand'); | |
160 Polymer.dom.flush(); | |
161 | |
162 // Simulate clicking the left arrow to go back to the search results. | |
163 settingsMain.currentRouteChanged(settings.Route.BASIC); | |
164 Polymer.dom.flush(); | |
165 assertEquals( | |
166 '', settingsMain.$$('settings-basic-page').style.display); | |
167 assertEquals( | |
168 '', settingsMain.$$('settings-advanced-page').style.display); | |
169 }); | |
170 }); | |
171 | |
172 test('can collapse advanced on advanced section route', function() { | |
173 settings.navigateTo(settings.Route.PRIVACY); | |
174 Polymer.dom.flush(); | |
175 | |
176 var advancedToggle = settingsMain.$$('#advancedToggle'); | |
177 assertTrue(!!advancedToggle); | |
178 | |
179 MockInteractions.tap(advancedToggle); | |
180 Polymer.dom.flush(); | |
181 | |
182 assertFalse(settingsMain.showPages_.advanced); | |
183 }); | |
184 | |
185 test('navigating to a basic page does not collapse advanced', function() { | |
186 settings.navigateTo(settings.Route.PRIVACY); | |
187 Polymer.dom.flush(); | |
188 | |
189 var advancedToggle = settingsMain.$$('#advancedToggle'); | |
190 assertTrue(!!advancedToggle); | |
191 | |
192 settings.navigateTo(settings.Route.PEOPLE); | |
193 Polymer.dom.flush(); | |
194 | |
195 assertTrue(settingsMain.showPages_.advanced); | |
196 }); | |
197 }); | |
198 } | |
199 | |
200 return { | |
201 registerTests: registerTests, | |
202 }; | |
203 }); | |
OLD | NEW |