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