Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(336)

Side by Side Diff: chrome/test/data/webui/md_bookmarks/store_test.js

Issue 2645273002: [MD Bookmarks] Modify search to retain the previously selected folder. (Closed)
Patch Set: Rebase Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 suite('<bookmarks-store>', function() { 5 suite('<bookmarks-store>', function() {
6 var store; 6 var store;
7 var TEST_TREE; 7 var TEST_TREE;
8 8
9 function replaceStore() { 9 function replaceStore() {
10 store = document.createElement('bookmarks-store'); 10 store = document.createElement('bookmarks-store');
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 '8': 'rootNode.children.#3', 86 '8': 'rootNode.children.#3',
87 }; 87 };
88 88
89 for (var id in store.idToNodeMap_) 89 for (var id in store.idToNodeMap_)
90 assertEquals(TEST_PATHS[id], store.idToNodeMap_[id].path); 90 assertEquals(TEST_PATHS[id], store.idToNodeMap_[id].path);
91 }); 91 });
92 92
93 ////////////////////////////////////////////////////////////////////////////// 93 //////////////////////////////////////////////////////////////////////////////
94 // editing bookmarks tree tests: 94 // editing bookmarks tree tests:
95 95
96 test('changing selectedId changes the displayedList', function() {
97 store.selectedId = '0';
98 assertEquals(TEST_TREE.children, store.displayedList);
99 store.selectedId = '1';
100 assertEquals(TEST_TREE.children[0].children, store.displayedList);
101 store.selectedId = '3';
102 assertEquals(
103 TEST_TREE.children[0].children[1].children, store.displayedList);
104
105 // Selecting an item selects the default folder.
106 store.selectedId = '5';
107 assertEquals(TEST_TREE.children[0].children, store.displayedList);
108 });
109
110 test('store updates on selected event', function() { 96 test('store updates on selected event', function() {
111 // First child of root is selected by default. 97 // First child of root is selected by default.
112 assertEquals('1', store.selectedId); 98 assertEquals('1', store.selectedId);
113 assertTrue(store.idToNodeMap_['1'].isSelectedFolder); 99 assertTrue(store.idToNodeMap_['1'].isSelectedFolder);
114 100
115 // Selecting a selected folder doesn't deselect it. 101 // Selecting a selected folder doesn't deselect it.
116 store.fire('selected-folder-changed', '1'); 102 store.fire('selected-folder-changed', {id: '1', clearSearch: true});
117 assertEquals('1', store.selectedId); 103 assertEquals('1', store.selectedId);
118 assertTrue(store.idToNodeMap_['1'].isSelectedFolder); 104 assertTrue(store.idToNodeMap_['1'].isSelectedFolder);
119 105
120 // Select a deeply nested descendant. 106 // Select a deeply nested descendant.
121 store.fire('selected-folder-changed', '3'); 107 store.fire('selected-folder-changed', {id: '3', clearSearch: true});
122 assertEquals('3', store.selectedId); 108 assertEquals('3', store.selectedId);
123 assertTrue(store.idToNodeMap_['3'].isSelectedFolder); 109 assertTrue(store.idToNodeMap_['3'].isSelectedFolder);
124 assertFalse(store.idToNodeMap_['1'].isSelectedFolder); 110 assertFalse(store.idToNodeMap_['1'].isSelectedFolder);
125 111
126 // Select a folder in separate subtree. 112 // Select a folder in separate subtree.
127 store.fire('selected-folder-changed', '8'); 113 store.fire('selected-folder-changed', {id: '8', clearSearch: true});
128 assertEquals('8', store.selectedId); 114 assertEquals('8', store.selectedId);
129 assertTrue(store.idToNodeMap_['8'].isSelectedFolder); 115 assertTrue(store.idToNodeMap_['8'].isSelectedFolder);
130 assertFalse(store.idToNodeMap_['3'].isSelectedFolder); 116 assertFalse(store.idToNodeMap_['3'].isSelectedFolder);
131 }); 117 });
132 118
133 test('store updates on open and close', function() { 119 test('store updates on open and close', function() {
134 // All folders are open by default. 120 // All folders are open by default.
135 for (var id in store.idToNodeMap_) { 121 for (var id in store.idToNodeMap_) {
136 if (store.idToNodeMap_[id].url) 122 if (store.idToNodeMap_[id].url)
137 continue; 123 continue;
138 124
139 assertTrue(store.idToNodeMap_[id].isOpen); 125 assertTrue(store.idToNodeMap_[id].isOpen);
140 } 126 }
141 127
142 // Closing a folder doesn't close any descendants. 128 // Closing a folder doesn't close any descendants.
143 store.fire('folder-open-changed', {id: '1', open: false}); 129 store.fire('folder-open-changed', {id: '1', open: false});
144 assertFalse(store.idToNodeMap_['1'].isOpen); 130 assertFalse(store.idToNodeMap_['1'].isOpen);
145 assertTrue(store.idToNodeMap_['3'].isOpen); 131 assertTrue(store.idToNodeMap_['3'].isOpen);
146 store.fire('folder-open-changed', {id: '1', open: true}); 132 store.fire('folder-open-changed', {id: '1', open: true});
147 133
148 // Closing an ancestor folder of a selected folder selects the ancestor. 134 // Closing an ancestor folder of a selected folder selects the ancestor.
149 store.fire('selected-folder-changed', '3'); 135 store.fire('selected-folder-changed', {id: '3'});
150 store.fire('folder-open-changed', {id: '1', open: false}); 136 store.fire('folder-open-changed', {id: '1', open: false});
151 assertFalse(store.idToNodeMap_['1'].isOpen); 137 assertFalse(store.idToNodeMap_['1'].isOpen);
152 assertEquals('1', store.selectedId); 138 assertEquals('1', store.selectedId);
153 assertTrue(store.idToNodeMap_['1'].isSelectedFolder); 139 assertTrue(store.idToNodeMap_['1'].isSelectedFolder);
154 assertFalse(store.idToNodeMap_['3'].isSelectedFolder); 140 assertFalse(store.idToNodeMap_['3'].isSelectedFolder);
155 }); 141 });
156 142
157 test('deleting a node updates the tree', function() { 143 test('deleting a node updates the tree', function() {
158 removeChild(TEST_TREE, 1); 144 removeChild(TEST_TREE, 1);
159 overrideBookmarksGetSubTree([TEST_TREE]); 145 overrideBookmarksGetSubTree([TEST_TREE]);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 '5': 'rootNode.children.#0', 192 '5': 'rootNode.children.#0',
207 '8': 'rootNode.children.#1' 193 '8': 'rootNode.children.#1'
208 }; 194 };
209 195
210 for (var id in store.idToNodeMap_) 196 for (var id in store.idToNodeMap_)
211 assertEquals(TEST_PATHS[id], store.idToNodeMap_[id].path); 197 assertEquals(TEST_PATHS[id], store.idToNodeMap_[id].path);
212 }); 198 });
213 199
214 test('selectedId updates after removing a selected folder', function() { 200 test('selectedId updates after removing a selected folder', function() {
215 // Selected folder gets removed. 201 // Selected folder gets removed.
216 store.selectedId = '8'; 202 store.fire('selected-folder-changed', {id: '8', clearSearch: true});
217 removeChild(TEST_TREE, 3); 203 removeChild(TEST_TREE, 3);
218 overrideBookmarksGetSubTree([TEST_TREE]); 204 overrideBookmarksGetSubTree([TEST_TREE]);
219 205
220 store.onBookmarkRemoved_('8', {parentId:'0', index:'3'}); 206 store.onBookmarkRemoved_('8', {parentId:'0', index:'3'});
221 assertTrue(store.idToNodeMap_['0'].isSelectedFolder); 207 assertTrue(store.idToNodeMap_['0'].isSelectedFolder);
222 assertEquals('0', store.selectedId); 208 assertEquals('0', store.selectedId);
223 209
224 // A folder with selected folder in it gets removed. 210 // A folder with selected folder in it gets removed.
225 store.selectedId = '3'; 211 store.selectFolder_('3');
226 removeChild(TEST_TREE, 0); 212 removeChild(TEST_TREE, 0);
227 overrideBookmarksGetSubTree([TEST_TREE]); 213 overrideBookmarksGetSubTree([TEST_TREE]);
228 214
229 store.onBookmarkRemoved_('1', {parentId:'0', index:'0'}); 215 store.onBookmarkRemoved_('1', {parentId:'0', index:'0'});
230 assertTrue(store.idToNodeMap_['0'].isSelectedFolder); 216 assertTrue(store.idToNodeMap_['0'].isSelectedFolder);
231 assertEquals('0', store.selectedId); 217 assertEquals('0', store.selectedId);
232 }); 218 });
233 219
234 test('bookmark gets updated after editing', function() { 220 test('bookmark gets updated after editing', function() {
235 // Edit title updates idToNodeMap_ properly. 221 // Edit title updates idToNodeMap_ properly.
(...skipping 18 matching lines...) Expand all
254 ////////////////////////////////////////////////////////////////////////////// 240 //////////////////////////////////////////////////////////////////////////////
255 // search tests: 241 // search tests:
256 242
257 test('displayedList updates after searchTerm changes', function() { 243 test('displayedList updates after searchTerm changes', function() {
258 var SEARCH_RESULTS = [ 244 var SEARCH_RESULTS = [
259 createItem('1', {title: 'cat'}), 245 createItem('1', {title: 'cat'}),
260 createItem('2', {title: 'apple'}), 246 createItem('2', {title: 'apple'}),
261 createItem('3', {title: 'paris'}), 247 createItem('3', {title: 'paris'}),
262 ]; 248 ];
263 overrideBookmarksSearch(SEARCH_RESULTS); 249 overrideBookmarksSearch(SEARCH_RESULTS);
250 var prevFolder = store.idToNodeMap_[store.selectedId];
264 251
265 // Search for a non-empty string. 252 // Search for a non-empty string.
266 store.searchTerm = 'a'; 253 store.searchTerm = 'a';
267 assertFalse(store.rootNode.children[0].isSelectedFolder); 254 assertFalse(prevFolder.isSelectedFolder);
268 assertEquals(null, store.selectedId); 255 assertEquals(prevFolder.id, store.selectedId);
269 assertEquals(SEARCH_RESULTS, store.displayedList); 256 assertEquals(SEARCH_RESULTS, store.displayedList);
270 257
271 // Clear the searchTerm. 258 // Clearing search sets the displayed list to the previous folder.
272 store.searchTerm = ''; 259 store.searchTerm = '';
273 var defaultFolder = store.rootNode.children[0]; 260 assertTrue(prevFolder.isSelectedFolder);
274 assertTrue(defaultFolder.isSelectedFolder); 261 assertEquals(prevFolder.id, store.selectedId);
275 assertEquals(defaultFolder.id, store.selectedId); 262 assertEquals(prevFolder.children, store.displayedList);
276 assertEquals(defaultFolder.children, store.displayedList);
277 263
278 // Search with no bookmarks returned. 264 // Search with no bookmarks returned.
279 overrideBookmarksSearch([]); 265 overrideBookmarksSearch([]);
280 store.searchTerm = 'asdf'; 266 store.searchTerm = 'asdf';
281 assertEquals(0, store.displayedList.length); 267 assertEquals(0, store.displayedList.length);
282 }); 268 });
283 269
284 ////////////////////////////////////////////////////////////////////////////// 270 //////////////////////////////////////////////////////////////////////////////
285 // router tests: 271 // router tests:
286 272
287 test('search updates from route', function() { 273 test('search updates from route', function() {
288 overrideBookmarksSearch([]); 274 overrideBookmarksSearch([]);
275
276 // Route with basic search.
289 searchTerm = 'Pond'; 277 searchTerm = 'Pond';
290 navigateTo('/?q=' + searchTerm); 278 navigateTo('/?q=' + searchTerm);
291 assertEquals(searchTerm, store.searchTerm); 279 assertEquals(searchTerm, store.searchTerm);
280 assertEquals(store.rootNode.children[0].id, store.selectedId);
281
282 // Route with selected folder and search.
283 var selectedId = '3';
284 searchTerm = 'Fish';
285 navigateTo('/?id=' + selectedId + '&q=' + searchTerm);
286 assertEquals(searchTerm, store.searchTerm);
287 assertEquals(selectedId, store.selectedId);
288
289 // Route with invalid selected folder and search.
290 selectedId = 'bar';
291 searchTerm = 'foo';
292 navigateTo('/?q=' + searchTerm + '&id=' + selectedId);
293 assertEquals(searchTerm, store.searchTerm);
294 assertEquals(store.rootNode.children[0].id, store.selectedId);
292 }); 295 });
293 296
294 test('search updates from route on setup', function() { 297 test('search updates from route on setup', function() {
295 overrideBookmarksSearch([]); 298 overrideBookmarksSearch([]);
296 var searchTerm = 'Boat24'; 299 var searchTerm = 'Boat24';
297 navigateTo('/?q=' + searchTerm); 300 navigateTo('/?q=' + searchTerm);
298 replaceStore(); 301 replaceStore();
299 assertEquals(searchTerm, store.searchTerm); 302 assertEquals(searchTerm, store.searchTerm);
300 }); 303 });
301 304
302 test('route updates from search', function() { 305 test('route updates from search', function() {
303 overrideBookmarksSearch([]); 306 overrideBookmarksSearch([]);
307 var prevFolderId = '3';
308 store.fire('selected-folder-changed', {id: prevFolderId});
309
310 // Searching sets the previous folder and searchTerm as the URL parameters.
304 var searchTerm = 'Boat24'; 311 var searchTerm = 'Boat24';
305 store.searchTerm = searchTerm; 312 store.searchTerm = searchTerm;
306 assertEquals('chrome://bookmarks/?q=' + searchTerm, window.location.href); 313 assertEquals(
314 'chrome://bookmarks/?id=' + prevFolderId + '&q=' + searchTerm,
315 window.location.href);
316
317 // Search parameter is removed from the URL when search is cleared.
318 searchTerm = '';
319 store.searchTerm = searchTerm;
320 assertEquals(
321 'chrome://bookmarks/?id=' + prevFolderId, window.location.href);
307 }); 322 });
308 323
309 test('selectedId updates from route', function() { 324 test('selectedId updates from route', function() {
310 // Folder id routes to the corresponding folder. 325 // Folder id routes to the corresponding folder.
311 var selectedId = '3'; 326 var selectedId = '3';
312 navigateTo('/?id=' + selectedId); 327 navigateTo('/?id=' + selectedId);
313 assertEquals(selectedId, store.selectedId); 328 assertEquals(selectedId, store.selectedId);
314 329
315 // Bookmark id routes to the default Bookmarks Bar. 330 // Bookmark id routes to the default Bookmarks Bar.
316 var selectedId = '2'; 331 var selectedId = '2';
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 assertEquals(0, store.anchorIndex_); 365 assertEquals(0, store.anchorIndex_);
351 366
352 // Select other item will remove the previous selection. 367 // Select other item will remove the previous selection.
353 store.fire('select-item', {item: store.idToNodeMap_['3']}); 368 store.fire('select-item', {item: store.idToNodeMap_['3']});
354 assertDeepEquals( 369 assertDeepEquals(
355 [false, true, false, false], 370 [false, true, false, false],
356 store.displayedList.map(i => i.isSelectedItem)); 371 store.displayedList.map(i => i.isSelectedItem));
357 assertEquals(1, store.anchorIndex_); 372 assertEquals(1, store.anchorIndex_);
358 373
359 // Deleting the selected item will unselect everything. 374 // Deleting the selected item will unselect everything.
360 store.selectedId = '1'; 375 store.selectFolder_('1');
361 store.fire('select-item', {item: store.idToNodeMap_['2']}); 376 store.fire('select-item', {item: store.idToNodeMap_['2']});
362 removeChild(TEST_TREE.children[0], 0); 377 removeChild(TEST_TREE.children[0], 0);
363 overrideBookmarksGetSubTree([TEST_TREE.children[0]]); 378 overrideBookmarksGetSubTree([TEST_TREE.children[0]]);
364 store.onBookmarkRemoved_('2', {parentId: '1', index: 0}); 379 store.onBookmarkRemoved_('2', {parentId: '1', index: 0});
365 assertDeepEquals( 380 assertDeepEquals(
366 [false, false, false], 381 [false, false, false],
367 store.displayedList.map(i => i.isSelectedItem)); 382 store.displayedList.map(i => i.isSelectedItem));
368 assertEquals(null, store.anchorIndex_); 383 assertEquals(null, store.anchorIndex_);
369 384
370 // Changing the selected folder will remove the select status of the 385 // Changing the selected folder will remove the select status of the
371 // bookmark. 386 // bookmark.
372 store.selectedId = '3'; 387 store.selectFolder_('3');
373 assertDeepEquals( 388 assertDeepEquals(
374 [false, false, false], 389 [false, false, false],
375 store.idToNodeMap_['1'].children.map(i => i.isSelectedItem)); 390 store.idToNodeMap_['1'].children.map(i => i.isSelectedItem));
376 assertEquals(null, store.anchorIndex_); 391 assertEquals(null, store.anchorIndex_);
377 }); 392 });
378 393
379 test('shift select selects the correct bookmarks', function() { 394 test('shift select selects the correct bookmarks', function() {
380 // When nothing has been selected, it selects a single item. 395 // When nothing has been selected, it selects a single item.
381 assertEquals(null, store.anchorIndex_); 396 assertEquals(null, store.anchorIndex_);
382 store.fire('select-item', {item: store.idToNodeMap_['6'], range: true}); 397 store.fire('select-item', {item: store.idToNodeMap_['6'], range: true});
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 }); 446 });
432 447
433 test('selection in search mode', function() { 448 test('selection in search mode', function() {
434 // Item gets unselected in search. 449 // Item gets unselected in search.
435 overrideBookmarksSearch([ 450 overrideBookmarksSearch([
436 createItem('4', {url: 'link4'}), 451 createItem('4', {url: 'link4'}),
437 createItem('2', {url: 'link2'}), 452 createItem('2', {url: 'link2'}),
438 createItem('5', {url: 'link5'}), 453 createItem('5', {url: 'link5'}),
439 ]); 454 ]);
440 455
441 store.selectedId = '1'; 456 // store.selectFolder_('1');
457 store.fire('selected-folder-changed', {id: '1'});
442 store.fire('select-item', {item: store.idToNodeMap_['3']}); 458 store.fire('select-item', {item: store.idToNodeMap_['3']});
443 store.searchTerm = 'a'; 459 store.searchTerm = 'a';
444 assertFalse(store.idToNodeMap_['3'].isSelectedItem); 460 assertFalse(store.idToNodeMap_['3'].isSelectedItem);
445 assertEquals(null, store.anchorIndex_); 461 assertEquals(null, store.anchorIndex_);
446 462
447 // anchorIndex_ gets updated properly in single select. 463 // anchorIndex_ gets updated properly in single select.
448 store.fire('select-item', {item: store.displayedList[1]}); 464 store.fire('select-item', {item: store.displayedList[1]});
449 assertDeepEquals( 465 assertDeepEquals(
450 [false, true, false], 466 [false, true, false],
451 store.displayedList.map(i => i.isSelectedItem)); 467 store.displayedList.map(i => i.isSelectedItem));
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
486 store.fire( 502 store.fire(
487 'select-item', {item: store.displayedList[2], add: true, range: false}); 503 'select-item', {item: store.displayedList[2], add: true, range: false});
488 store.fire( 504 store.fire(
489 'select-item', {item: store.displayedList[4], add: true, range: true}); 505 'select-item', {item: store.displayedList[4], add: true, range: true});
490 assertDeepEquals( 506 assertDeepEquals(
491 [true, false, true, true, true], 507 [true, false, true, true, true],
492 store.displayedList.map(i => i.isSelectedItem)); 508 store.displayedList.map(i => i.isSelectedItem));
493 assertEquals(2, store.anchorIndex_); 509 assertEquals(2, store.anchorIndex_);
494 }); 510 });
495 }); 511 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698