Chromium Code Reviews| 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() { | |
| 10 store = document.createElement('bookmarks-store'); | |
| 11 replaceBody(store); | |
| 12 store.setupStore_(TEST_TREE); | |
| 13 } | |
| 14 | |
| 15 function navigateTo(route) { | |
| 16 window.history.replaceState({}, '', route); | |
| 17 window.dispatchEvent(new CustomEvent('location-changed')); | |
| 18 } | |
| 19 | |
| 20 /** | |
| 21 * Overrides the chrome.bookmarks.search to pass results into the callback. | |
| 22 * @param {Array} results | |
| 23 */ | |
| 24 function overrideBookmarksSearch(results) { | |
| 25 chrome.bookmarks.search = function(searchTerm, callback) { | |
| 26 callback(results); | |
| 27 }; | |
| 28 } | |
| 29 | |
| 9 setup(function() { | 30 setup(function() { |
| 10 TEST_TREE = createFolder('0', [ | 31 TEST_TREE = createFolder('0', [ |
| 11 createFolder( | 32 createFolder( |
| 12 '1', | 33 '1', |
| 13 [ | 34 [ |
| 14 createItem('2', {url: 'link2'}), | 35 createItem('2', {url: 'link2'}), |
| 15 createFolder('3', []), | 36 createFolder('3', []), |
| 16 ]), | 37 ]), |
| 17 createItem('4', {url: 'link4'}), | 38 createItem('4', {url: 'link4'}), |
| 18 createItem('5', {url: 'link5'}), | 39 createItem('5', {url: 'link5'}), |
| 40 createFolder('6', []), | |
| 19 ]); | 41 ]); |
| 20 | 42 |
| 21 store = document.createElement('bookmarks-store'); | 43 replaceStore(); |
| 22 replaceBody(store); | |
| 23 store.setupStore_(TEST_TREE); | |
| 24 }); | 44 }); |
| 25 | 45 |
| 26 test('initNodes inserts nodes into idToNodeMap', function(){ | 46 teardown(function() { |
| 47 // Clean up anything left in URL. | |
| 48 navigateTo('/'); | |
| 49 }); | |
| 50 | |
| 51 ////////////////////////////////////////////////////////////////////////////// | |
| 52 // store initialization tests: | |
| 53 | |
| 54 test('initNodes inserts nodes into idToNodeMap', function() { | |
| 27 assertEquals(TEST_TREE, store.idToNodeMap_['0']); | 55 assertEquals(TEST_TREE, store.idToNodeMap_['0']); |
| 28 assertEquals(TEST_TREE.children[0], store.idToNodeMap_['1']); | 56 assertEquals(TEST_TREE.children[0], store.idToNodeMap_['1']); |
| 29 assertEquals(TEST_TREE.children[0].children[0], store.idToNodeMap_['2']); | 57 assertEquals(TEST_TREE.children[0].children[0], store.idToNodeMap_['2']); |
| 30 assertEquals(TEST_TREE.children[0].children[1], store.idToNodeMap_['3']); | 58 assertEquals(TEST_TREE.children[0].children[1], store.idToNodeMap_['3']); |
| 31 assertEquals(TEST_TREE.children[1], store.idToNodeMap_['4']); | 59 assertEquals(TEST_TREE.children[1], store.idToNodeMap_['4']); |
| 32 assertEquals(TEST_TREE.children[2], store.idToNodeMap_['5']); | 60 assertEquals(TEST_TREE.children[2], store.idToNodeMap_['5']); |
| 33 }); | 61 }); |
| 34 | 62 |
| 35 test('changing selectedId changes the displayedList', function(){ | |
| 36 store.selectedId = '0'; | |
| 37 assertEquals(TEST_TREE.children, store.displayedList); | |
| 38 store.selectedId = '1'; | |
| 39 assertEquals(TEST_TREE.children[0].children, store.displayedList); | |
| 40 store.selectedId = '2'; | |
| 41 assertEquals( | |
| 42 TEST_TREE.children[0].children[0].children, store.displayedList); | |
| 43 store.selectedId = '3'; | |
| 44 assertEquals( | |
| 45 TEST_TREE.children[0].children[1].children, store.displayedList); | |
| 46 store.selectedId = '4'; | |
| 47 assertEquals(TEST_TREE.children[1].children, store.displayedList); | |
| 48 store.selectedId = '5'; | |
| 49 assertEquals(TEST_TREE.children[2].children, store.displayedList); | |
| 50 }); | |
| 51 | |
| 52 test('correct paths generated for nodes', function() { | 63 test('correct paths generated for nodes', function() { |
| 53 var TEST_PATHS = { | 64 var TEST_PATHS = { |
| 54 '0': 'rootNode', | 65 '0': 'rootNode', |
| 55 '1': 'rootNode.children.#0', | 66 '1': 'rootNode.children.#0', |
| 56 '2': 'rootNode.children.#0.children.#0', | 67 '2': 'rootNode.children.#0.children.#0', |
| 57 '3': 'rootNode.children.#0.children.#1', | 68 '3': 'rootNode.children.#0.children.#1', |
| 58 '4': 'rootNode.children.#1', | 69 '4': 'rootNode.children.#1', |
| 59 '5': 'rootNode.children.#2', | 70 '5': 'rootNode.children.#2', |
| 71 '6': 'rootNode.children.#3', | |
| 60 }; | 72 }; |
| 61 | 73 |
| 62 for (var id in store.idToNodeMap_) | 74 for (var id in store.idToNodeMap_) |
| 63 assertEquals(TEST_PATHS[id], store.idToNodeMap_[id].path); | 75 assertEquals(TEST_PATHS[id], store.idToNodeMap_[id].path); |
| 64 }); | 76 }); |
| 65 | 77 |
| 78 ////////////////////////////////////////////////////////////////////////////// | |
| 79 // editing bookmarks tree tests: | |
| 80 | |
| 81 test('changing selectedId changes the displayedList', function() { | |
| 82 store.selectedId = '0'; | |
| 83 assertEquals(TEST_TREE.children, store.displayedList); | |
| 84 store.selectedId = '1'; | |
| 85 assertEquals(TEST_TREE.children[0].children, store.displayedList); | |
| 86 store.selectedId = '3'; | |
| 87 assertEquals( | |
| 88 TEST_TREE.children[0].children[1].children, store.displayedList); | |
| 89 | |
| 90 // Selecting an item selects the default folder. | |
| 91 store.selectedId = '5'; | |
| 92 assertEquals(TEST_TREE.children[0].children, store.displayedList); | |
| 93 }); | |
| 94 | |
| 66 test('store updates on selected event', function() { | 95 test('store updates on selected event', function() { |
| 67 // First child of root is selected by default. | 96 // First child of root is selected by default. |
| 68 assertEquals('1', store.selectedId); | 97 assertEquals('1', store.selectedId); |
| 69 assertTrue(store.idToNodeMap_['1'].isSelected); | 98 assertTrue(store.idToNodeMap_['1'].isSelected); |
| 70 | 99 |
| 71 // Selecting a selected folder doesn't deselect it. | 100 // Selecting a selected folder doesn't deselect it. |
| 72 store.fire('selected-folder-changed', '1'); | 101 store.fire('selected-folder-changed', '1'); |
| 73 assertEquals('1', store.selectedId); | 102 assertEquals('1', store.selectedId); |
| 74 assertTrue(store.idToNodeMap_['1'].isSelected); | 103 assertTrue(store.idToNodeMap_['1'].isSelected); |
| 75 | 104 |
| 76 // Select a deeply nested descendant. | 105 // Select a deeply nested descendant. |
| 77 store.fire('selected-folder-changed', '3'); | 106 store.fire('selected-folder-changed', '3'); |
| 78 assertEquals('3', store.selectedId); | 107 assertEquals('3', store.selectedId); |
| 79 assertTrue(store.idToNodeMap_['3'].isSelected); | 108 assertTrue(store.idToNodeMap_['3'].isSelected); |
| 80 assertFalse(store.idToNodeMap_['1'].isSelected); | 109 assertFalse(store.idToNodeMap_['1'].isSelected); |
| 81 | 110 |
| 82 // Select a folder in separate subtree. | 111 // Select a folder in separate subtree. |
| 83 store.fire('selected-folder-changed', '5'); | 112 store.fire('selected-folder-changed', '6'); |
| 84 assertEquals('5', store.selectedId); | 113 assertEquals('6', store.selectedId); |
| 85 assertTrue(store.idToNodeMap_['5'].isSelected); | 114 assertTrue(store.idToNodeMap_['6'].isSelected); |
| 86 assertFalse(store.idToNodeMap_['3'].isSelected); | 115 assertFalse(store.idToNodeMap_['3'].isSelected); |
| 87 }); | 116 }); |
| 88 | 117 |
| 89 test('store updates on open and close', function() { | 118 test('store updates on open and close', function() { |
| 90 // All folders are open by default. | 119 // All folders are open by default. |
| 91 for (var id in store.idToNodeMap_) { | 120 for (var id in store.idToNodeMap_) { |
| 92 if (store.idToNodeMap_[id].url) | 121 if (store.idToNodeMap_[id].url) |
| 93 continue; | 122 continue; |
| 94 | 123 |
| 95 assertTrue(store.idToNodeMap_[id].isOpen); | 124 assertTrue(store.idToNodeMap_[id].isOpen); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 121 assertEquals(undefined, store.idToNodeMap_['4']); | 150 assertEquals(undefined, store.idToNodeMap_['4']); |
| 122 assertEquals(store.rootNode.children[1], store.idToNodeMap_['5']); | 151 assertEquals(store.rootNode.children[1], store.idToNodeMap_['5']); |
| 123 | 152 |
| 124 // Paths have been updated. | 153 // Paths have been updated. |
| 125 var TEST_PATHS = { | 154 var TEST_PATHS = { |
| 126 '0': 'rootNode', | 155 '0': 'rootNode', |
| 127 '1': 'rootNode.children.#0', | 156 '1': 'rootNode.children.#0', |
| 128 '2': 'rootNode.children.#0.children.#0', | 157 '2': 'rootNode.children.#0.children.#0', |
| 129 '3': 'rootNode.children.#0.children.#1', | 158 '3': 'rootNode.children.#0.children.#1', |
| 130 '5': 'rootNode.children.#1', | 159 '5': 'rootNode.children.#1', |
| 160 '6': 'rootNode.children.#2', | |
| 131 }; | 161 }; |
| 132 | 162 |
| 133 for (var id in store.idToNodeMap_) | 163 for (var id in store.idToNodeMap_) |
| 134 assertEquals(TEST_PATHS[id], store.idToNodeMap_[id].path); | 164 assertEquals(TEST_PATHS[id], store.idToNodeMap_[id].path); |
| 135 | 165 |
| 136 // Remove a folder with children. | 166 // Remove a folder with children. |
| 137 store.onBookmarkRemoved_('1', {parentId: '0', index: '0'}); | 167 store.onBookmarkRemoved_('1', {parentId: '0', index: '0'}); |
| 138 | 168 |
| 139 // Check the tree is correct. | 169 // Check the tree is correct. |
| 140 assertEquals('5', store.rootNode.children[0].id); | 170 assertEquals('5', store.rootNode.children[0].id); |
| 171 assertEquals('6', store.rootNode.children[1].id); | |
| 141 | 172 |
| 142 // idToNodeMap_ has been updated. | 173 // idToNodeMap_ has been updated. |
| 143 assertEquals(undefined, store.idToNodeMap_['1']); | 174 assertEquals(undefined, store.idToNodeMap_['1']); |
| 144 assertEquals(undefined, store.idToNodeMap_['2']); | 175 assertEquals(undefined, store.idToNodeMap_['2']); |
| 145 assertEquals(undefined, store.idToNodeMap_['3']); | 176 assertEquals(undefined, store.idToNodeMap_['3']); |
| 146 assertEquals(undefined, store.idToNodeMap_['4']); | 177 assertEquals(undefined, store.idToNodeMap_['4']); |
| 147 assertEquals(store.rootNode.children[0], store.idToNodeMap_['5']); | 178 assertEquals(store.rootNode.children[0], store.idToNodeMap_['5']); |
| 179 assertEquals(store.rootNode.children[1], store.idToNodeMap_['6']); | |
| 148 | 180 |
| 149 // Paths have been updated. | 181 // Paths have been updated. |
| 150 TEST_PATHS = { | 182 TEST_PATHS = { |
| 151 '0': 'rootNode', | 183 '0': 'rootNode', |
| 152 '5': 'rootNode.children.#0', | 184 '5': 'rootNode.children.#0', |
| 185 '6': 'rootNode.children.#1' | |
| 153 }; | 186 }; |
| 154 | 187 |
| 155 for (var id in store.idToNodeMap_) | 188 for (var id in store.idToNodeMap_) |
| 156 assertEquals(TEST_PATHS[id], store.idToNodeMap_[id].path); | 189 assertEquals(TEST_PATHS[id], store.idToNodeMap_[id].path); |
| 157 }); | 190 }); |
| 158 | 191 |
| 159 test('selectedId updates after removing a selected folder', function() { | 192 test('selectedId updates after removing a selected folder', function() { |
| 160 // Selected folder gets removed. | 193 // Selected folder gets removed. |
| 161 store.selectedId = '2'; | 194 store.selectedId = '2'; |
| 162 store.onBookmarkRemoved_('2', {parentId:'1', index:'0'}); | 195 store.onBookmarkRemoved_('2', {parentId: '1', index: '0'}); |
| 163 assertTrue(store.idToNodeMap_['1'].isSelected); | 196 assertTrue(store.idToNodeMap_['1'].isSelected); |
| 164 assertEquals('1', store.selectedId); | 197 assertEquals('1', store.selectedId); |
| 165 | 198 |
| 166 // A folder with selected folder in it gets removed. | 199 // A folder with selected folder in it gets removed. |
| 167 store.selectedId = '3'; | 200 store.selectedId = '3'; |
| 168 store.onBookmarkRemoved_('1', {parentId:'0', index:'0'}); | 201 store.onBookmarkRemoved_('1', {parentId: '0', index: '0'}); |
| 169 assertTrue(store.idToNodeMap_['0'].isSelected); | 202 assertTrue(store.idToNodeMap_['0'].isSelected); |
| 170 assertEquals('0', store.selectedId); | 203 assertEquals('0', store.selectedId); |
| 171 }); | 204 }); |
| 172 | 205 |
| 173 test('displayedList updates after searchTerm changes', function() { | |
| 174 var SEARCH_RESULTS = [ | |
| 175 'cat', | |
| 176 'apple', | |
| 177 'Paris', | |
| 178 ]; | |
| 179 | |
| 180 chrome.bookmarks.search = function(searchTerm, callback) { | |
| 181 callback(SEARCH_RESULTS); | |
| 182 }; | |
| 183 | |
| 184 // Search for a non-empty string. | |
| 185 store.searchTerm = 'a'; | |
| 186 assertFalse(store.rootNode.children[0].isSelected); | |
| 187 assertEquals(null, store.selectedId); | |
| 188 assertEquals(SEARCH_RESULTS, store.displayedList); | |
| 189 | |
| 190 // Clear the searchTerm. | |
| 191 store.searchTerm = ''; | |
| 192 var defaultFolder = store.rootNode.children[0]; | |
| 193 assertTrue(defaultFolder.isSelected); | |
| 194 assertEquals(defaultFolder.id, store.selectedId); | |
| 195 assertEquals(defaultFolder.children, store.displayedList); | |
| 196 | |
| 197 // Search with no bookmarks returned. | |
| 198 var EMPTY_RESULT = []; | |
| 199 chrome.bookmarks.search = function(searchTerm, callback) { | |
| 200 callback(EMPTY_RESULT); | |
| 201 }; | |
| 202 store.searchTerm = 'asdf'; | |
| 203 assertEquals(EMPTY_RESULT, store.displayedList); | |
| 204 }); | |
| 205 | |
| 206 test('bookmark gets updated after editing', function() { | 206 test('bookmark gets updated after editing', function() { |
| 207 // Edit title updates idToNodeMap_ properly. | 207 // Edit title updates idToNodeMap_ properly. |
| 208 store.onBookmarkChanged_('4', {'title': 'test'}); | 208 store.onBookmarkChanged_('4', {'title': 'test'}); |
| 209 assertEquals('test', store.idToNodeMap_['4'].title); | 209 assertEquals('test', store.idToNodeMap_['4'].title); |
| 210 assertEquals('link4', store.idToNodeMap_['4'].url); | 210 assertEquals('link4', store.idToNodeMap_['4'].url); |
| 211 | 211 |
| 212 // Edit url updates idToNodeMap_ properly. | 212 // Edit url updates idToNodeMap_ properly. |
| 213 store.onBookmarkChanged_('5', {'url': 'http://www.google.com'}); | 213 store.onBookmarkChanged_('5', {'url': 'http://www.google.com'}); |
| 214 assertEquals('', store.idToNodeMap_['5'].title); | 214 assertEquals('', store.idToNodeMap_['5'].title); |
| 215 assertEquals('http://www.google.com', store.idToNodeMap_['5'].url); | 215 assertEquals('http://www.google.com', store.idToNodeMap_['5'].url); |
| 216 | 216 |
| 217 // Edit url and title updates idToNodeMap_ properly. | 217 // Edit url and title updates idToNodeMap_ properly. |
| 218 store.onBookmarkChanged_('2', { | 218 store.onBookmarkChanged_('2', { |
| 219 'title': 'test', | 219 'title': 'test', |
| 220 'url': 'http://www.google.com', | 220 'url': 'http://www.google.com', |
| 221 }); | 221 }); |
| 222 assertEquals('test', store.idToNodeMap_['2'].title); | 222 assertEquals('test', store.idToNodeMap_['2'].title); |
| 223 assertEquals('http://www.google.com', store.idToNodeMap_['2'].url); | 223 assertEquals('http://www.google.com', store.idToNodeMap_['2'].url); |
| 224 }); | 224 }); |
| 225 | |
| 226 ////////////////////////////////////////////////////////////////////////////// | |
| 227 // search tests: | |
| 228 | |
| 229 test('displayedList updates after searchTerm changes', function() { | |
| 230 var SEARCH_RESULTS = [ | |
| 231 'cat', | |
| 232 'apple', | |
| 233 'Paris', | |
| 234 ]; | |
| 235 overrideBookmarksSearch(SEARCH_RESULTS); | |
| 236 | |
| 237 // Search for a non-empty string. | |
| 238 store.searchTerm = 'a'; | |
| 239 assertFalse(store.rootNode.children[0].isSelected); | |
| 240 assertEquals(null, store.selectedId); | |
| 241 assertEquals(SEARCH_RESULTS, store.displayedList); | |
| 242 | |
| 243 // Clear the searchTerm. | |
| 244 store.searchTerm = ''; | |
| 245 var defaultFolder = store.rootNode.children[0]; | |
| 246 assertTrue(defaultFolder.isSelected); | |
| 247 assertEquals(defaultFolder.id, store.selectedId); | |
| 248 assertEquals(defaultFolder.children, store.displayedList); | |
| 249 | |
| 250 // Search with no bookmarks returned. | |
| 251 var EMPTY_RESULT = []; | |
| 252 overrideBookmarksSearch(EMPTY_RESULT); | |
| 253 store.searchTerm = 'asdf'; | |
| 254 assertEquals(EMPTY_RESULT, store.displayedList); | |
| 255 }); | |
| 256 | |
| 257 ////////////////////////////////////////////////////////////////////////////// | |
| 258 // router tests: | |
| 259 | |
| 260 test('search updates from route', function() { | |
| 261 overrideBookmarksSearch([]); | |
| 262 searchTerm = 'Pond'; | |
| 263 navigateTo('/?q=' + searchTerm); | |
| 264 assertEquals(searchTerm, store.searchTerm); | |
| 265 }); | |
| 266 | |
| 267 test('search updates from route on setup', function() { | |
| 268 overrideBookmarksSearch([]); | |
| 269 var searchTerm = 'Boat24'; | |
| 270 navigateTo('/?q=' + searchTerm); | |
| 271 replaceStore(); | |
| 272 assertEquals(searchTerm, store.searchTerm); | |
| 273 }); | |
| 274 | |
| 275 test('route updates from search', function() { | |
| 276 overrideBookmarksSearch([]); | |
| 277 var searchTerm = 'Boat24'; | |
| 278 store.searchTerm = searchTerm; | |
| 279 assertEquals('chrome://bookmarks/?q=' + searchTerm, window.location.href); | |
| 280 }); | |
| 281 | |
| 282 test('selectedId updates from route', function() { | |
| 283 // Folder id routes to the corresponding folder. | |
| 284 var selectedId = '3'; | |
| 285 navigateTo('/?id=' + selectedId); | |
| 286 assertEquals(selectedId, store.selectedId); | |
| 287 | |
| 288 // Bookmark id routes to the default Bookmarks Bar. | |
| 289 var selectedId = '2'; | |
| 290 navigateTo('/?id=' + selectedId); | |
| 291 assertEquals(store.rootNode.children[0].id, store.selectedId); | |
| 292 | |
| 293 // Invalid id routes to the default Bookmarks Bar. | |
| 294 selectedId = 'foo'; | |
| 295 navigateTo('/?id=' + selectedId); | |
| 296 assertEquals(store.rootNode.children[0].id, store.selectedId); | |
|
calamity
2017/01/23 02:25:13
Add a test for routing to the id of a URL bookmark
| |
| 297 }); | |
| 298 | |
| 299 test('selectedId updates from route on setup', function() { | |
| 300 selectedId = '3'; | |
| 301 navigateTo('/?id=' + selectedId); | |
| 302 replaceStore(); | |
| 303 assertEquals(selectedId, store.selectedId); | |
| 304 }); | |
| 305 | |
| 306 test('route updates from selectedId', function() { | |
| 307 var selectedId = '2'; | |
| 308 store.selectedId = selectedId; | |
| 309 assertEquals('chrome://bookmarks/?id=' + selectedId, window.location.href); | |
| 310 }); | |
| 225 }); | 311 }); |
| OLD | NEW |