OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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('selection state', function() { | 5 suite('selection state', function() { |
6 var selection; | 6 var selection; |
7 var action; | 7 var action; |
8 | 8 |
9 function select(items, anchor, add) { | 9 function select(items, anchor, clear, toggle) { |
10 return { | 10 return { |
11 name: 'select-items', | 11 name: 'select-items', |
12 add: add, | 12 clear: clear, |
13 anchor: anchor, | 13 anchor: anchor, |
14 items: items, | 14 items: items, |
| 15 toggle: toggle, |
15 }; | 16 }; |
16 } | 17 } |
17 | 18 |
18 setup(function() { | 19 setup(function() { |
19 selection = { | 20 selection = { |
20 anchor: null, | 21 anchor: null, |
21 items: {}, | 22 items: new Set(), |
22 }; | 23 }; |
23 }); | 24 }); |
24 | 25 |
25 test('can select an item', function() { | 26 test('can select an item', function() { |
26 action = select(['1'], '1', false); | 27 action = select(['1'], '1', true, false); |
27 selection = bookmarks.SelectionState.updateSelection(selection, action); | 28 selection = bookmarks.SelectionState.updateSelection(selection, action); |
28 | 29 |
29 assertDeepEquals(['1'], normalizeSet(selection.items)); | 30 assertDeepEquals(['1'], normalizeSet(selection.items)); |
30 assertEquals('1', selection.anchor); | 31 assertEquals('1', selection.anchor); |
31 | 32 |
32 // Replace current selection. | 33 // Replace current selection. |
33 action = select(['2'], '2', false); | 34 action = select(['2'], '2', true, false); |
34 selection = bookmarks.SelectionState.updateSelection(selection, action); | 35 selection = bookmarks.SelectionState.updateSelection(selection, action); |
35 assertDeepEquals(['2'], normalizeSet(selection.items)); | 36 assertDeepEquals(['2'], normalizeSet(selection.items)); |
36 assertEquals('2', selection.anchor); | 37 assertEquals('2', selection.anchor); |
37 | 38 |
38 // Add to current selection. | 39 // Add to current selection. |
39 action = select(['3'], '3', true); | 40 action = select(['3'], '3', false, false); |
40 selection = bookmarks.SelectionState.updateSelection(selection, action); | 41 selection = bookmarks.SelectionState.updateSelection(selection, action); |
41 assertDeepEquals(['2', '3'], normalizeSet(selection.items)); | 42 assertDeepEquals(['2', '3'], normalizeSet(selection.items)); |
42 assertEquals('3', selection.anchor); | 43 assertEquals('3', selection.anchor); |
43 }); | 44 }); |
44 | 45 |
45 test('can select multiple items', function() { | 46 test('can select multiple items', function() { |
46 action = select(['1', '2', '3'], '3', false); | 47 action = select(['1', '2', '3'], '3', true, false); |
47 selection = bookmarks.SelectionState.updateSelection(selection, action); | 48 selection = bookmarks.SelectionState.updateSelection(selection, action); |
48 assertDeepEquals(['1', '2', '3'], normalizeSet(selection.items)); | 49 assertDeepEquals(['1', '2', '3'], normalizeSet(selection.items)); |
49 | 50 |
50 action = select(['3', '4'], '4', true); | 51 action = select(['3', '4'], '4', false, false); |
51 selection = bookmarks.SelectionState.updateSelection(selection, action); | 52 selection = bookmarks.SelectionState.updateSelection(selection, action); |
52 assertDeepEquals(['1', '2', '3', '4'], normalizeSet(selection.items)); | 53 assertDeepEquals(['1', '2', '3', '4'], normalizeSet(selection.items)); |
53 }); | 54 }); |
54 | 55 |
55 test('is cleared when selected folder changes', function() { | 56 test('is cleared when selected folder changes', function() { |
56 action = select(['1', '2', '3'], '3', false); | 57 action = select(['1', '2', '3'], '3', true, false); |
57 selection = bookmarks.SelectionState.updateSelection(selection, action); | 58 selection = bookmarks.SelectionState.updateSelection(selection, action); |
58 | 59 |
59 action = bookmarks.actions.selectFolder('2'); | 60 action = bookmarks.actions.selectFolder('2'); |
60 selection = bookmarks.SelectionState.updateSelection(selection, action); | 61 selection = bookmarks.SelectionState.updateSelection(selection, action); |
61 assertDeepEquals({}, selection.items); | 62 assertDeepEquals({}, selection.items); |
62 }); | 63 }); |
63 | 64 |
64 test('is cleared when search finished', function() { | 65 test('is cleared when search finished', function() { |
65 action = select(['1', '2', '3'], '3', false); | 66 action = select(['1', '2', '3'], '3', true, false); |
66 selection = bookmarks.SelectionState.updateSelection(selection, action); | 67 selection = bookmarks.SelectionState.updateSelection(selection, action); |
67 | 68 |
68 action = bookmarks.actions.setSearchResults(['2']); | 69 action = bookmarks.actions.setSearchResults(['2']); |
69 selection = bookmarks.SelectionState.updateSelection(selection, action); | 70 selection = bookmarks.SelectionState.updateSelection(selection, action); |
70 assertDeepEquals({}, selection.items); | 71 assertDeepEquals({}, selection.items); |
71 }); | 72 }); |
72 | 73 |
73 test('is cleared when search cleared', function() { | 74 test('is cleared when search cleared', function() { |
74 action = select(['1', '2', '3'], '3', false); | 75 action = select(['1', '2', '3'], '3', true, false); |
75 selection = bookmarks.SelectionState.updateSelection(selection, action); | 76 selection = bookmarks.SelectionState.updateSelection(selection, action); |
76 | 77 |
77 action = bookmarks.actions.clearSearch(); | 78 action = bookmarks.actions.clearSearch(); |
78 selection = bookmarks.SelectionState.updateSelection(selection, action); | 79 selection = bookmarks.SelectionState.updateSelection(selection, action); |
79 assertDeepEquals({}, selection.items); | 80 assertDeepEquals({}, selection.items); |
80 }); | 81 }); |
81 | 82 |
82 test('deselect items', function() { | 83 test('deselect items', function() { |
83 action = select(['1', '2', '3'], '3', false); | 84 action = select(['1', '2', '3'], '3', true, false); |
84 selection = bookmarks.SelectionState.updateSelection(selection, action); | 85 selection = bookmarks.SelectionState.updateSelection(selection, action); |
85 | 86 |
86 action = bookmarks.actions.deselectItems(); | 87 action = bookmarks.actions.deselectItems(); |
87 selection = bookmarks.SelectionState.updateSelection(selection, action); | 88 selection = bookmarks.SelectionState.updateSelection(selection, action); |
88 assertDeepEquals({}, selection.items); | 89 assertDeepEquals({}, selection.items); |
89 }); | 90 }); |
90 | 91 |
| 92 test('toggle an item', function() { |
| 93 action = select(['1', '2', '3'], '3', true, false); |
| 94 selection = bookmarks.SelectionState.updateSelection(selection, action); |
| 95 |
| 96 action = select(['1'], '3', false, true); |
| 97 selection = bookmarks.SelectionState.updateSelection(selection, action); |
| 98 assertDeepEquals(['2', '3'], normalizeSet(selection.items)); |
| 99 }); |
| 100 |
91 test('deselects items when they are deleted', function() { | 101 test('deselects items when they are deleted', function() { |
92 var nodeMap = testTree(createFolder('0', [ | 102 var nodeMap = testTree(createFolder('0', [ |
93 createFolder( | 103 createFolder( |
94 '1', | 104 '1', |
95 [ | 105 [ |
96 createItem('2'), | 106 createItem('2'), |
97 createItem('3'), | 107 createItem('3'), |
98 createItem('4'), | 108 createItem('4'), |
99 ]), | 109 ]), |
100 createItem('5'), | 110 createItem('5'), |
101 ])); | 111 ])); |
102 | 112 |
103 action = select(['2', '4', '5'], '4', false); | 113 action = select(['2', '4', '5'], '4', true, false); |
104 selection = bookmarks.SelectionState.updateSelection(selection, action); | 114 selection = bookmarks.SelectionState.updateSelection(selection, action); |
105 | 115 |
106 action = bookmarks.actions.removeBookmark('1', '0', 0, nodeMap); | 116 action = bookmarks.actions.removeBookmark('1', '0', 0, nodeMap); |
107 selection = bookmarks.SelectionState.updateSelection(selection, action); | 117 selection = bookmarks.SelectionState.updateSelection(selection, action); |
108 | 118 |
109 assertDeepEquals(['5'], normalizeSet(selection.items)); | 119 assertDeepEquals(['5'], normalizeSet(selection.items)); |
110 assertEquals(null, selection.anchor); | 120 assertEquals(null, selection.anchor); |
111 }); | 121 }); |
112 }); | 122 }); |
113 | 123 |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 state = bookmarks.reduceAction(state, action); | 434 state = bookmarks.reduceAction(state, action); |
425 | 435 |
426 action = bookmarks.actions.removeBookmark('2', '1', 0, state.nodes); | 436 action = bookmarks.actions.removeBookmark('2', '1', 0, state.nodes); |
427 state = bookmarks.reduceAction(state, action); | 437 state = bookmarks.reduceAction(state, action); |
428 | 438 |
429 // 2 and 3 should be removed, since 2 was deleted and 3 was a descendant of | 439 // 2 and 3 should be removed, since 2 was deleted and 3 was a descendant of |
430 // 2. | 440 // 2. |
431 assertDeepEquals(['1'], state.search.results); | 441 assertDeepEquals(['1'], state.search.results); |
432 }); | 442 }); |
433 }); | 443 }); |
OLD | NEW |