test_medialistviewselection.js
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
31 function runTest () {
32 
33  Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
34  Components.utils.import("resource://app/jsmodules/sbLibraryUtils.jsm");
35 
36  var library = createLibrary("test_medialistviewselection");
37  var view = library.createView();
38  var selection = view.selection;
39 
40  assertEqual(selection.count, 0);
41 
42  selection.currentIndex = 0;
43  assertEqual(selection.currentIndex, 0);
44 
45  var item = selection.currentMediaItem;
46  assertTrue(item.equals(view.getItemByIndex(0)));
47 
48  selection.currentIndex = -1;
49  item = selection.currentMediaItem;
50  assertEqual(item, null);
51 
52  selection.select(0);
53  assertTrue(selection.isIndexSelected(0));
54  assertEqual(selection.count, 1);
55 
56  selection.select(1);
57  assertTrue(selection.isIndexSelected(0));
58  assertTrue(selection.isIndexSelected(1));
59  assertEqual(selection.count, 2);
60 
61  selection.toggle(0);
62  assertFalse(selection.isIndexSelected(0));
63  assertEqual(selection.count, 1);
64 
65  selection.selectRange(3, 5);
66  assertEqual(selection.count, 4);
67 
68  selection.clearRange(1, 4);
69  assertEqual(selection.count, 1);
70 
71  selection.selectNone();
72  assertEqual(selection.count, 0);
73 
74  selection.selectAll();
75  assertEqual(selection.count, library.length);
76 
77  selection.clear(3);
78  assertEqual(selection.count, library.length - 1);
79 
80  selection.select(3);
81  assertEqual(selection.count, library.length);
82 
83  selection.selectOnly(2);
84  assertTrue(selection.isIndexSelected(2));
85  assertEqual(selection.count, 1);
86 
87  // Test notifications
88  var listener = {
89  countSelectionChanged: 0,
90  countCurrentIndexChanged: 0,
91  onSelectionChanged: function() {
92  this.countSelectionChanged++;
93  },
94  onCurrentIndexChanged: function() {
95  this.countCurrentIndexChanged++;
96  },
97  reset: function() {
98  this.countCurrentIndexChanged = 0;
99  this.countSelectionChanged = 0;
100  }
101  }
102 
103  selection.addListener(listener);
104  selection.select(0);
105  assertEqual(listener.countSelectionChanged, 1);
106  selection.selectionNotificationsSuppressed = true;
107  selection.select(0);
108  assertEqual(listener.countSelectionChanged, 1);
109  selection.selectionNotificationsSuppressed = false;
110 
111  selection.currentIndex = 2;
112  assertEqual(listener.countCurrentIndexChanged, 1);
113 
114  selection.removeListener(listener);
115 
116  // Test selectedMediaItems
117  // some
118  var items = [];
119  items.push(library.getItemByIndex(2));
120  items.push(library.getItemByIndex(10));
121  items.push(library.getItemByIndex(20));
122  items.push(library.getItemByIndex(21));
123 
124  selection.selectNone();
125  selection.select(2);
126  selection.toggle(10);
127  selection.toggle(20);
128  selection.toggle(21);
129 
130  assertSelectedItems(selection, items);
131 
132  // all
133  var allItems = [];
134  for (var i = 0; i < library.length; i++) {
135  allItems.push(library.getItemByIndex(i));
136  }
137  selection.selectAll();
138  assertSelectedItems(selection, allItems);
139 
140  // all but one
141  allItems.splice(10, 1);
142  selection.toggle(10);
143  assertSelectedItems(selection, allItems);
144 
145  // all but a range
146  allItems.splice(10, 2);
147  selection.selectAll();
148  selection.clearRange(10, 12);
149  assertSelectedItems(selection, allItems);
150 }
151 
152 function assertSelectedItems(selection, items) {
153 
154  var allGuids = {};
155  for (var i = 0; i < items.length; i++) {
156  allGuids[items[i].guid] = 1;
157  }
158 
159  var allItems = selection.selectedIndexedMediaItems;
160  var count = 0;
161  while(allItems.hasMoreElements()) {
162  var item = allItems.getNext().mediaItem;
163  assertTrue(item.guid in allGuids);
164  delete allGuids[item.guid];
165  count++;
166  }
167 
168  assertEqual(count, items.length);
169 }
function runTest()
Test file.
function assertTrue(aTest, aMessage)
function assertEqual(aExpected, aActual, aMessage)
function assertSelectedItems(selection, items)
var count
Definition: test_bug7406.js:32
return null
Definition: FeedWriter.js:1143
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
function assertFalse(aTest, aMessage)
_getSelectedPageStyle s i