| 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 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'); |
| 11 replaceBody(store); | 11 replaceBody(store); |
| 12 store.setupStore_(TEST_TREE); | 12 store.setupStore_(TEST_TREE); |
| 13 } | 13 } |
| 14 | 14 |
| 15 function navigateTo(route) { | 15 function navigateTo(route) { |
| 16 window.history.replaceState({}, '', route); | 16 window.history.replaceState({}, '', route); |
| 17 window.dispatchEvent(new CustomEvent('location-changed')); | 17 window.dispatchEvent(new CustomEvent('location-changed')); |
| 18 } | 18 } |
| 19 | 19 |
| 20 /** | 20 /** |
| 21 * Overrides the chrome.bookmarks.search to pass results into the callback. | 21 * Overrides the chrome.bookmarks.search to pass results into the callback. |
| 22 * @param {Array} results | 22 * @param {Array} results |
| 23 */ | 23 */ |
| 24 function overrideBookmarksSearch(results) { | 24 function overrideBookmarksSearch(results) { |
| 25 chrome.bookmarks.search = function(searchTerm, callback) { | 25 chrome.bookmarks.search = function(searchTerm, callback) { |
| 26 callback(results); | 26 callback(results); |
| 27 }; | 27 }; |
| 28 } | 28 } |
| 29 | 29 |
| 30 /** |
| 31 * Overrides the chrome.bookmarks.getSubTree to pass results into the |
| 32 * callback. |
| 33 * @param {Array} results |
| 34 */ |
| 35 function overrideBookmarksGetSubTree(results) { |
| 36 chrome.bookmarks.getSubTree = function(parentId, callback) { |
| 37 callback(results); |
| 38 }; |
| 39 } |
| 40 |
| 30 setup(function() { | 41 setup(function() { |
| 31 TEST_TREE = createFolder('0', [ | 42 TEST_TREE = createFolder('0', [ |
| 32 createFolder( | 43 createFolder( |
| 33 '1', | 44 '1', |
| 34 [ | 45 [ |
| 35 createItem('2', {url: 'link2'}), | 46 createItem('2', {url: 'link2'}), |
| 36 createFolder('3', []), | 47 createFolder('3', []), |
| 48 createItem('6', {url: 'link4'}), |
| 49 createItem('7', {url: 'link5'}), |
| 37 ]), | 50 ]), |
| 38 createItem('4', {url: 'link4'}), | 51 createItem('4', {url: 'link4'}), |
| 39 createItem('5', {url: 'link5'}), | 52 createItem('5', {url: 'link5'}), |
| 40 createFolder('6', []), | 53 createFolder('8', []), |
| 41 ]); | 54 ]); |
| 42 | 55 |
| 43 replaceStore(); | 56 replaceStore(); |
| 44 }); | 57 }); |
| 45 | 58 |
| 46 teardown(function() { | 59 teardown(function() { |
| 47 // Clean up anything left in URL. | 60 // Clean up anything left in URL. |
| 48 navigateTo('/'); | 61 navigateTo('/'); |
| 49 }); | 62 }); |
| 50 | 63 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 61 }); | 74 }); |
| 62 | 75 |
| 63 test('correct paths generated for nodes', function() { | 76 test('correct paths generated for nodes', function() { |
| 64 var TEST_PATHS = { | 77 var TEST_PATHS = { |
| 65 '0': 'rootNode', | 78 '0': 'rootNode', |
| 66 '1': 'rootNode.children.#0', | 79 '1': 'rootNode.children.#0', |
| 67 '2': 'rootNode.children.#0.children.#0', | 80 '2': 'rootNode.children.#0.children.#0', |
| 68 '3': 'rootNode.children.#0.children.#1', | 81 '3': 'rootNode.children.#0.children.#1', |
| 69 '4': 'rootNode.children.#1', | 82 '4': 'rootNode.children.#1', |
| 70 '5': 'rootNode.children.#2', | 83 '5': 'rootNode.children.#2', |
| 71 '6': 'rootNode.children.#3', | 84 '6': 'rootNode.children.#0.children.#2', |
| 85 '7': 'rootNode.children.#0.children.#3', |
| 86 '8': 'rootNode.children.#3', |
| 72 }; | 87 }; |
| 73 | 88 |
| 74 for (var id in store.idToNodeMap_) | 89 for (var id in store.idToNodeMap_) |
| 75 assertEquals(TEST_PATHS[id], store.idToNodeMap_[id].path); | 90 assertEquals(TEST_PATHS[id], store.idToNodeMap_[id].path); |
| 76 }); | 91 }); |
| 77 | 92 |
| 78 ////////////////////////////////////////////////////////////////////////////// | 93 ////////////////////////////////////////////////////////////////////////////// |
| 79 // editing bookmarks tree tests: | 94 // editing bookmarks tree tests: |
| 80 | 95 |
| 81 test('changing selectedId changes the displayedList', function() { | 96 test('changing selectedId changes the displayedList', function() { |
| 82 store.selectedId = '0'; | 97 store.selectedId = '0'; |
| 83 assertEquals(TEST_TREE.children, store.displayedList); | 98 assertEquals(TEST_TREE.children, store.displayedList); |
| 84 store.selectedId = '1'; | 99 store.selectedId = '1'; |
| 85 assertEquals(TEST_TREE.children[0].children, store.displayedList); | 100 assertEquals(TEST_TREE.children[0].children, store.displayedList); |
| 86 store.selectedId = '3'; | 101 store.selectedId = '3'; |
| 87 assertEquals( | 102 assertEquals( |
| 88 TEST_TREE.children[0].children[1].children, store.displayedList); | 103 TEST_TREE.children[0].children[1].children, store.displayedList); |
| 89 | 104 |
| 90 // Selecting an item selects the default folder. | 105 // Selecting an item selects the default folder. |
| 91 store.selectedId = '5'; | 106 store.selectedId = '5'; |
| 92 assertEquals(TEST_TREE.children[0].children, store.displayedList); | 107 assertEquals(TEST_TREE.children[0].children, store.displayedList); |
| 93 }); | 108 }); |
| 94 | 109 |
| 95 test('store updates on selected event', function() { | 110 test('store updates on selected event', function() { |
| 96 // First child of root is selected by default. | 111 // First child of root is selected by default. |
| 97 assertEquals('1', store.selectedId); | 112 assertEquals('1', store.selectedId); |
| 98 assertTrue(store.idToNodeMap_['1'].isSelected); | 113 assertTrue(store.idToNodeMap_['1'].isSelectedFolder); |
| 99 | 114 |
| 100 // Selecting a selected folder doesn't deselect it. | 115 // Selecting a selected folder doesn't deselect it. |
| 101 store.fire('selected-folder-changed', '1'); | 116 store.fire('selected-folder-changed', '1'); |
| 102 assertEquals('1', store.selectedId); | 117 assertEquals('1', store.selectedId); |
| 103 assertTrue(store.idToNodeMap_['1'].isSelected); | 118 assertTrue(store.idToNodeMap_['1'].isSelectedFolder); |
| 104 | 119 |
| 105 // Select a deeply nested descendant. | 120 // Select a deeply nested descendant. |
| 106 store.fire('selected-folder-changed', '3'); | 121 store.fire('selected-folder-changed', '3'); |
| 107 assertEquals('3', store.selectedId); | 122 assertEquals('3', store.selectedId); |
| 108 assertTrue(store.idToNodeMap_['3'].isSelected); | 123 assertTrue(store.idToNodeMap_['3'].isSelectedFolder); |
| 109 assertFalse(store.idToNodeMap_['1'].isSelected); | 124 assertFalse(store.idToNodeMap_['1'].isSelectedFolder); |
| 110 | 125 |
| 111 // Select a folder in separate subtree. | 126 // Select a folder in separate subtree. |
| 112 store.fire('selected-folder-changed', '6'); | 127 store.fire('selected-folder-changed', '8'); |
| 113 assertEquals('6', store.selectedId); | 128 assertEquals('8', store.selectedId); |
| 114 assertTrue(store.idToNodeMap_['6'].isSelected); | 129 assertTrue(store.idToNodeMap_['8'].isSelectedFolder); |
| 115 assertFalse(store.idToNodeMap_['3'].isSelected); | 130 assertFalse(store.idToNodeMap_['3'].isSelectedFolder); |
| 116 }); | 131 }); |
| 117 | 132 |
| 118 test('store updates on open and close', function() { | 133 test('store updates on open and close', function() { |
| 119 // All folders are open by default. | 134 // All folders are open by default. |
| 120 for (var id in store.idToNodeMap_) { | 135 for (var id in store.idToNodeMap_) { |
| 121 if (store.idToNodeMap_[id].url) | 136 if (store.idToNodeMap_[id].url) |
| 122 continue; | 137 continue; |
| 123 | 138 |
| 124 assertTrue(store.idToNodeMap_[id].isOpen); | 139 assertTrue(store.idToNodeMap_[id].isOpen); |
| 125 } | 140 } |
| 126 | 141 |
| 127 // Closing a folder doesn't close any descendants. | 142 // Closing a folder doesn't close any descendants. |
| 128 store.fire('folder-open-changed', {id: '1', open: false}); | 143 store.fire('folder-open-changed', {id: '1', open: false}); |
| 129 assertFalse(store.idToNodeMap_['1'].isOpen); | 144 assertFalse(store.idToNodeMap_['1'].isOpen); |
| 130 assertTrue(store.idToNodeMap_['3'].isOpen); | 145 assertTrue(store.idToNodeMap_['3'].isOpen); |
| 131 store.fire('folder-open-changed', {id: '1', open: true}); | 146 store.fire('folder-open-changed', {id: '1', open: true}); |
| 132 | 147 |
| 133 // Closing an ancestor folder of a selected folder selects the ancestor. | 148 // Closing an ancestor folder of a selected folder selects the ancestor. |
| 134 store.fire('selected-folder-changed', '3'); | 149 store.fire('selected-folder-changed', '3'); |
| 135 store.fire('folder-open-changed', {id: '1', open: false}); | 150 store.fire('folder-open-changed', {id: '1', open: false}); |
| 136 assertFalse(store.idToNodeMap_['1'].isOpen); | 151 assertFalse(store.idToNodeMap_['1'].isOpen); |
| 137 assertEquals('1', store.selectedId); | 152 assertEquals('1', store.selectedId); |
| 138 assertTrue(store.idToNodeMap_['1'].isSelected); | 153 assertTrue(store.idToNodeMap_['1'].isSelectedFolder); |
| 139 assertFalse(store.idToNodeMap_['3'].isSelected); | 154 assertFalse(store.idToNodeMap_['3'].isSelectedFolder); |
| 140 }); | 155 }); |
| 141 | 156 |
| 142 test('deleting a node updates the tree', function() { | 157 test('deleting a node updates the tree', function() { |
| 158 overrideBookmarksGetSubTree([createFolder('0', [ |
| 159 createFolder( |
| 160 '1', |
| 161 [ |
| 162 createItem('2', {url: 'link2'}), |
| 163 createFolder('3', []), |
| 164 createItem('6', {url: 'link4'}), |
| 165 createItem('7', {url: 'link5'}), |
| 166 ]), |
| 167 createItem('5', {url: 'link5'}), |
| 168 createFolder('8', []), |
| 169 ])]); |
| 143 // Remove an empty folder/bookmark. | 170 // Remove an empty folder/bookmark. |
| 144 store.onBookmarkRemoved_('4', {parentId: '0', index: '1'}); | 171 store.onBookmarkRemoved_('4', {parentId: '0', index: 1}); |
| 145 | 172 |
| 146 // Check the tree is correct. | 173 // Check the tree is correct. |
| 147 assertEquals('5', store.rootNode.children[1].id); | 174 assertEquals('5', store.rootNode.children[1].id); |
| 148 | 175 |
| 149 // idToNodeMap_ has been updated. | 176 // idToNodeMap_ has been updated. |
| 150 assertEquals(undefined, store.idToNodeMap_['4']); | 177 assertEquals(undefined, store.idToNodeMap_['4']); |
| 151 assertEquals(store.rootNode.children[1], store.idToNodeMap_['5']); | 178 assertEquals(store.rootNode.children[1], store.idToNodeMap_['5']); |
| 152 | 179 |
| 153 // Paths have been updated. | 180 // Paths have been updated. |
| 154 var TEST_PATHS = { | 181 var TEST_PATHS = { |
| 155 '0': 'rootNode', | 182 '0': 'rootNode', |
| 156 '1': 'rootNode.children.#0', | 183 '1': 'rootNode.children.#0', |
| 157 '2': 'rootNode.children.#0.children.#0', | 184 '2': 'rootNode.children.#0.children.#0', |
| 158 '3': 'rootNode.children.#0.children.#1', | 185 '3': 'rootNode.children.#0.children.#1', |
| 159 '5': 'rootNode.children.#1', | 186 '5': 'rootNode.children.#1', |
| 160 '6': 'rootNode.children.#2', | 187 '6': 'rootNode.children.#0.children.#2', |
| 188 '7': 'rootNode.children.#0.children.#3', |
| 189 '8': 'rootNode.children.#2', |
| 161 }; | 190 }; |
| 162 | 191 |
| 163 for (var id in store.idToNodeMap_) | 192 for (var id in store.idToNodeMap_) |
| 164 assertEquals(TEST_PATHS[id], store.idToNodeMap_[id].path); | 193 assertEquals(TEST_PATHS[id], store.idToNodeMap_[id].path); |
| 165 | 194 |
| 166 // Remove a folder with children. | 195 // Remove a folder with children. |
| 196 overrideBookmarksGetSubTree([createFolder('0', [ |
| 197 createItem('5', {url: 'link5'}), |
| 198 createFolder('8', []), |
| 199 ])]); |
| 200 |
| 167 store.onBookmarkRemoved_('1', {parentId: '0', index: '0'}); | 201 store.onBookmarkRemoved_('1', {parentId: '0', index: '0'}); |
| 168 | 202 |
| 169 // Check the tree is correct. | 203 // Check the tree is correct. |
| 170 assertEquals('5', store.rootNode.children[0].id); | 204 assertEquals('5', store.rootNode.children[0].id); |
| 171 assertEquals('6', store.rootNode.children[1].id); | 205 assertEquals('8', store.rootNode.children[1].id); |
| 172 | 206 |
| 173 // idToNodeMap_ has been updated. | 207 // idToNodeMap_ has been updated. |
| 174 assertEquals(undefined, store.idToNodeMap_['1']); | 208 assertEquals(undefined, store.idToNodeMap_['1']); |
| 175 assertEquals(undefined, store.idToNodeMap_['2']); | 209 assertEquals(undefined, store.idToNodeMap_['2']); |
| 176 assertEquals(undefined, store.idToNodeMap_['3']); | 210 assertEquals(undefined, store.idToNodeMap_['3']); |
| 177 assertEquals(undefined, store.idToNodeMap_['4']); | 211 assertEquals(undefined, store.idToNodeMap_['4']); |
| 178 assertEquals(store.rootNode.children[0], store.idToNodeMap_['5']); | 212 assertEquals(store.rootNode.children[0], store.idToNodeMap_['5']); |
| 179 assertEquals(store.rootNode.children[1], store.idToNodeMap_['6']); | 213 assertEquals(store.rootNode.children[1], store.idToNodeMap_['8']); |
| 180 | 214 |
| 181 // Paths have been updated. | 215 // Paths have been updated. |
| 182 TEST_PATHS = { | 216 TEST_PATHS = { |
| 183 '0': 'rootNode', | 217 '0': 'rootNode', |
| 184 '5': 'rootNode.children.#0', | 218 '5': 'rootNode.children.#0', |
| 185 '6': 'rootNode.children.#1' | 219 '8': 'rootNode.children.#1' |
| 186 }; | 220 }; |
| 187 | 221 |
| 188 for (var id in store.idToNodeMap_) | 222 for (var id in store.idToNodeMap_) |
| 189 assertEquals(TEST_PATHS[id], store.idToNodeMap_[id].path); | 223 assertEquals(TEST_PATHS[id], store.idToNodeMap_[id].path); |
| 190 }); | 224 }); |
| 191 | 225 |
| 192 test('selectedId updates after removing a selected folder', function() { | 226 test('selectedId updates after removing a selected folder', function() { |
| 193 // Selected folder gets removed. | 227 // Selected folder gets removed. |
| 194 store.selectedId = '2'; | 228 store.selectedId = '3'; |
| 195 store.onBookmarkRemoved_('2', {parentId: '1', index: '0'}); | 229 overrideBookmarksGetSubTree([createFolder('1', [ |
| 196 assertTrue(store.idToNodeMap_['1'].isSelected); | 230 createItem('2', {url: 'link2'}), |
| 231 createItem('6', {url: 'link4'}), |
| 232 createItem('7', {url: 'link5'}), |
| 233 ])]); |
| 234 |
| 235 store.onBookmarkRemoved_('3', {parentId:'1', index:'1'}); |
| 236 assertTrue(store.idToNodeMap_['1'].isSelectedFolder); |
| 197 assertEquals('1', store.selectedId); | 237 assertEquals('1', store.selectedId); |
| 198 | 238 |
| 199 // A folder with selected folder in it gets removed. | 239 // A folder with selected folder in it gets removed. |
| 240 overrideBookmarksGetSubTree([createFolder('0', [ |
| 241 createItem('4', {url: 'link4'}), |
| 242 createItem('5', {url: 'link5'}), |
| 243 ])]); |
| 244 |
| 200 store.selectedId = '3'; | 245 store.selectedId = '3'; |
| 201 store.onBookmarkRemoved_('1', {parentId: '0', index: '0'}); | 246 store.onBookmarkRemoved_('1', {parentId:'0', index:'0'}); |
| 202 assertTrue(store.idToNodeMap_['0'].isSelected); | 247 assertTrue(store.idToNodeMap_['0'].isSelectedFolder); |
| 203 assertEquals('0', store.selectedId); | 248 assertEquals('0', store.selectedId); |
| 204 }); | 249 }); |
| 205 | 250 |
| 206 test('bookmark gets updated after editing', function() { | 251 test('bookmark gets updated after editing', function() { |
| 207 // Edit title updates idToNodeMap_ properly. | 252 // Edit title updates idToNodeMap_ properly. |
| 208 store.onBookmarkChanged_('4', {'title': 'test'}); | 253 store.onBookmarkChanged_('4', {'title': 'test'}); |
| 209 assertEquals('test', store.idToNodeMap_['4'].title); | 254 assertEquals('test', store.idToNodeMap_['4'].title); |
| 210 assertEquals('link4', store.idToNodeMap_['4'].url); | 255 assertEquals('link4', store.idToNodeMap_['4'].url); |
| 211 | 256 |
| 212 // Edit url updates idToNodeMap_ properly. | 257 // Edit url updates idToNodeMap_ properly. |
| 213 store.onBookmarkChanged_('5', {'url': 'http://www.google.com'}); | 258 store.onBookmarkChanged_('5', {'url': 'http://www.google.com'}); |
| 214 assertEquals('', store.idToNodeMap_['5'].title); | 259 assertEquals('', store.idToNodeMap_['5'].title); |
| 215 assertEquals('http://www.google.com', store.idToNodeMap_['5'].url); | 260 assertEquals('http://www.google.com', store.idToNodeMap_['5'].url); |
| 216 | 261 |
| 217 // Edit url and title updates idToNodeMap_ properly. | 262 // Edit url and title updates idToNodeMap_ properly. |
| 218 store.onBookmarkChanged_('2', { | 263 store.onBookmarkChanged_('2', { |
| 219 'title': 'test', | 264 'title': 'test', |
| 220 'url': 'http://www.google.com', | 265 'url': 'http://www.google.com', |
| 221 }); | 266 }); |
| 222 assertEquals('test', store.idToNodeMap_['2'].title); | 267 assertEquals('test', store.idToNodeMap_['2'].title); |
| 223 assertEquals('http://www.google.com', store.idToNodeMap_['2'].url); | 268 assertEquals('http://www.google.com', store.idToNodeMap_['2'].url); |
| 224 }); | 269 }); |
| 225 | 270 |
| 226 ////////////////////////////////////////////////////////////////////////////// | 271 ////////////////////////////////////////////////////////////////////////////// |
| 227 // search tests: | 272 // search tests: |
| 228 | 273 |
| 229 test('displayedList updates after searchTerm changes', function() { | 274 test('displayedList updates after searchTerm changes', function() { |
| 230 var SEARCH_RESULTS = [ | 275 var SEARCH_RESULTS = [ |
| 231 'cat', | 276 createItem('1', {title: 'cat'}), |
| 232 'apple', | 277 createItem('2', {title: 'apple'}), |
| 233 'Paris', | 278 createItem('3', {title: 'paris'}), |
| 234 ]; | 279 ]; |
| 235 overrideBookmarksSearch(SEARCH_RESULTS); | 280 overrideBookmarksSearch(SEARCH_RESULTS); |
| 236 | 281 |
| 237 // Search for a non-empty string. | 282 // Search for a non-empty string. |
| 238 store.searchTerm = 'a'; | 283 store.searchTerm = 'a'; |
| 239 assertFalse(store.rootNode.children[0].isSelected); | 284 assertFalse(store.rootNode.children[0].isSelectedFolder); |
| 240 assertEquals(null, store.selectedId); | 285 assertEquals(null, store.selectedId); |
| 241 assertEquals(SEARCH_RESULTS, store.displayedList); | 286 assertEquals(SEARCH_RESULTS, store.displayedList); |
| 242 | 287 |
| 243 // Clear the searchTerm. | 288 // Clear the searchTerm. |
| 244 store.searchTerm = ''; | 289 store.searchTerm = ''; |
| 245 var defaultFolder = store.rootNode.children[0]; | 290 var defaultFolder = store.rootNode.children[0]; |
| 246 assertTrue(defaultFolder.isSelected); | 291 assertTrue(defaultFolder.isSelectedFolder); |
| 247 assertEquals(defaultFolder.id, store.selectedId); | 292 assertEquals(defaultFolder.id, store.selectedId); |
| 248 assertEquals(defaultFolder.children, store.displayedList); | 293 assertEquals(defaultFolder.children, store.displayedList); |
| 249 | 294 |
| 250 // Search with no bookmarks returned. | 295 // Search with no bookmarks returned. |
| 251 var EMPTY_RESULT = []; | 296 overrideBookmarksSearch([]); |
| 252 overrideBookmarksSearch(EMPTY_RESULT); | |
| 253 store.searchTerm = 'asdf'; | 297 store.searchTerm = 'asdf'; |
| 254 assertEquals(EMPTY_RESULT, store.displayedList); | 298 assertEquals(0, store.displayedList.length); |
| 255 }); | 299 }); |
| 256 | 300 |
| 257 ////////////////////////////////////////////////////////////////////////////// | 301 ////////////////////////////////////////////////////////////////////////////// |
| 258 // router tests: | 302 // router tests: |
| 259 | 303 |
| 260 test('search updates from route', function() { | 304 test('search updates from route', function() { |
| 261 overrideBookmarksSearch([]); | 305 overrideBookmarksSearch([]); |
| 262 searchTerm = 'Pond'; | 306 searchTerm = 'Pond'; |
| 263 navigateTo('/?q=' + searchTerm); | 307 navigateTo('/?q=' + searchTerm); |
| 264 assertEquals(searchTerm, store.searchTerm); | 308 assertEquals(searchTerm, store.searchTerm); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 navigateTo('/?id=' + selectedId); | 345 navigateTo('/?id=' + selectedId); |
| 302 replaceStore(); | 346 replaceStore(); |
| 303 assertEquals(selectedId, store.selectedId); | 347 assertEquals(selectedId, store.selectedId); |
| 304 }); | 348 }); |
| 305 | 349 |
| 306 test('route updates from selectedId', function() { | 350 test('route updates from selectedId', function() { |
| 307 var selectedId = '2'; | 351 var selectedId = '2'; |
| 308 store.selectedId = selectedId; | 352 store.selectedId = selectedId; |
| 309 assertEquals('chrome://bookmarks/?id=' + selectedId, window.location.href); | 353 assertEquals('chrome://bookmarks/?id=' + selectedId, window.location.href); |
| 310 }); | 354 }); |
| 355 |
| 356 ////////////////////////////////////////////////////////////////////////////// |
| 357 // selection tests: |
| 358 |
| 359 test('single select selects the correct bookmark', function() { |
| 360 for (var id in store.idToNodeMap_) { |
| 361 assertFalse(store.idToNodeMap_[id].isSelectedItem); |
| 362 } |
| 363 |
| 364 store.fire('select-item', {item: store.idToNodeMap_['2']}); |
| 365 assertDeepEquals( |
| 366 [true, false, false, false], |
| 367 store.displayedList.map(i => i.isSelectedItem)); |
| 368 assertEquals(0, store.anchorIndex_); |
| 369 |
| 370 // Select other item will remove the previous selection. |
| 371 store.fire('select-item', {item: store.idToNodeMap_['3']}); |
| 372 assertDeepEquals( |
| 373 [false, true, false, false], |
| 374 store.displayedList.map(i => i.isSelectedItem)); |
| 375 assertEquals(1, store.anchorIndex_); |
| 376 |
| 377 // Delete the selected item will select the next item. |
| 378 store.fire('select-item', {item: store.idToNodeMap_['2']}); |
| 379 overrideBookmarksGetSubTree([createFolder('1', [ |
| 380 createFolder('3', []), |
| 381 createItem('6', {url: 'link4'}), |
| 382 createItem('7', {url: 'link5'}), |
| 383 ])]); |
| 384 store.onBookmarkRemoved_('2', {parentId: '1', index: 0}); |
| 385 assertDeepEquals( |
| 386 [true, false, false], |
| 387 store.displayedList.map(i => i.isSelectedItem)); |
| 388 assertEquals(0, store.anchorIndex_); |
| 389 |
| 390 // Delete the selected item at the end of the list will select the previous |
| 391 // item. |
| 392 store.fire('select-item', {item: store.idToNodeMap_['7']}); |
| 393 overrideBookmarksGetSubTree([createFolder('1', [ |
| 394 createFolder('3', []), |
| 395 createItem('6', {url: 'link4'}), |
| 396 ])]); |
| 397 store.onBookmarkRemoved_('7', {parentId: '1', index: 2}); |
| 398 assertDeepEquals( |
| 399 [false, true], |
| 400 store.displayedList.map(i => i.isSelectedItem)); |
| 401 assertEquals(1, store.anchorIndex_); |
| 402 |
| 403 // Changing the selected folder will remove the select status of the |
| 404 // bookmark. |
| 405 store.selectedId = '3'; |
| 406 assertDeepEquals( |
| 407 [false, false], |
| 408 store.idToNodeMap_['1'].children.map(i => i.isSelectedItem)); |
| 409 assertEquals(null, store.anchorIndex_); |
| 410 }); |
| 411 |
| 412 test('shift select selects the correct bookmarks', function() { |
| 413 // When nothing has been selected, it selects a single item. |
| 414 assertEquals(null, store.anchorIndex_); |
| 415 store.fire('select-item', {item: store.idToNodeMap_['6'], range: true}); |
| 416 assertDeepEquals( |
| 417 [false, false, true, false], |
| 418 store.displayedList.map(i => i.isSelectedItem)); |
| 419 assertEquals(2, store.anchorIndex_); |
| 420 |
| 421 // Select an item below the previous selected item. |
| 422 store.fire('select-item', {item: store.idToNodeMap_['7'], range: true}); |
| 423 assertEquals(2, store.anchorIndex_); |
| 424 assertDeepEquals( |
| 425 [false, false, true, true], |
| 426 store.displayedList.map(i => i.isSelectedItem)); |
| 427 |
| 428 // Select an item above the previous selected item. |
| 429 store.fire('select-item', {item: store.idToNodeMap_['2'], range: true}); |
| 430 assertEquals(2, store.anchorIndex_); |
| 431 assertDeepEquals( |
| 432 [true, true, true, false], |
| 433 store.displayedList.map(i => i.isSelectedItem)); |
| 434 }); |
| 435 |
| 436 test('ctrl select selects the correct bookmarks', function() { |
| 437 // When nothing has been selected, it selects a single item. |
| 438 assertEquals(null, store.anchorIndex_); |
| 439 store.fire('select-item', {item: store.idToNodeMap_['6'], add: true}); |
| 440 assertDeepEquals( |
| 441 [false, false, true, false], |
| 442 store.displayedList.map(i => i.isSelectedItem)); |
| 443 assertEquals(2, store.anchorIndex_); |
| 444 |
| 445 // Select a new item will not deselect the previous item, but will update |
| 446 // anchorIndex_. |
| 447 store.fire('select-item', {item: store.idToNodeMap_['2'], add: true}); |
| 448 assertDeepEquals( |
| 449 [true, false, true, false], |
| 450 store.displayedList.map(i => i.isSelectedItem)); |
| 451 assertEquals(0, store.anchorIndex_); |
| 452 }); |
| 453 |
| 454 test('selection in search mode', function() { |
| 455 // Item gets unselected in search. |
| 456 overrideBookmarksSearch([ |
| 457 createItem('4', {url: 'link4'}), |
| 458 createItem('2', {url: 'link2'}), |
| 459 createItem('5', {url: 'link5'}), |
| 460 ]); |
| 461 |
| 462 store.selectedId = '1'; |
| 463 store.fire('select-item', {item: store.idToNodeMap_['3']}); |
| 464 store.searchTerm = 'a'; |
| 465 assertFalse(store.idToNodeMap_['3'].isSelectedItem); |
| 466 assertEquals(null, store.anchorIndex_); |
| 467 |
| 468 // anchorIndex_ gets updated properly in single select. |
| 469 store.fire('select-item', {item: store.displayedList[1]}); |
| 470 assertDeepEquals( |
| 471 [false, true, false], |
| 472 store.displayedList.map(i => i.isSelectedItem)); |
| 473 assertEquals(1, store.anchorIndex_); |
| 474 |
| 475 // anchorIndex_ gets updated properly in ctrl select. |
| 476 store.fire('select-item', {item: store.displayedList[0], add: true}); |
| 477 assertDeepEquals( |
| 478 [true, true, false], |
| 479 store.displayedList.map(i => i.isSelectedItem)); |
| 480 assertEquals(0, store.anchorIndex_); |
| 481 |
| 482 // Delete the selected item will select the next item. |
| 483 store.fire('select-item', {item: store.displayedList[1]}); |
| 484 overrideBookmarksSearch([ |
| 485 createItem('4', {url: 'link4'}), |
| 486 createItem('5', {url: 'link5'}), |
| 487 ]); |
| 488 overrideBookmarksGetSubTree([createFolder('1', [ |
| 489 createFolder('3', []), |
| 490 createItem('6', {url: 'link4'}), |
| 491 createItem('7', {url: 'link5'}), |
| 492 ])]); |
| 493 |
| 494 store.onBookmarkRemoved_('2', {parentId: '1', index: 0}); |
| 495 assertDeepEquals( |
| 496 [false, true], |
| 497 store.displayedList.map(i => i.isSelectedItem)); |
| 498 assertEquals(1, store.anchorIndex_); |
| 499 |
| 500 // Delete the selected item at the end of the list will select the previous |
| 501 // item. |
| 502 store.fire('select-item', {item: store.displayedList[1]}); |
| 503 overrideBookmarksSearch([ |
| 504 createItem('4', {url: 'link4'}), |
| 505 createItem('2', {url: 'link2'}), |
| 506 ]); |
| 507 overrideBookmarksGetSubTree([createFolder('0', [ |
| 508 createFolder( |
| 509 '1', |
| 510 [ |
| 511 createItem('2', {url: 'link2'}), |
| 512 createFolder('3', []), |
| 513 createItem('6', {url: 'link4'}), |
| 514 createItem('7', {url: 'link5'}), |
| 515 ]), |
| 516 createItem('4', {url: 'link4'}), |
| 517 ])]); |
| 518 |
| 519 assertDeepEquals( |
| 520 [false, true], |
| 521 store.displayedList.map(i => i.isSelectedItem)); |
| 522 assertEquals(1, store.anchorIndex_); |
| 523 }); |
| 311 }); | 524 }); |
| OLD | NEW |