test_playlist_selection.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  var url = "data:application/vnd.mozilla.xul+xml," +
33  "<?xml-stylesheet href='chrome://global/skin' type='text/css'?>" +
34  "<?xml-stylesheet href='chrome://songbird/content/bindings/bindings.css' type='text/css'?>" +
35  "<window xmlns='http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul'/>";
36 
38 }
39 
40 function setupPlaylist() {
41 
42  // We need our tree to be visible in order to get the correct events sent
43  // to the view
44  testWindow.resizeTo(200, 200);
45 
46  var databaseGUID = "test_playlist_selection";
47  var library = createLibrary(databaseGUID, false);
48 
49  const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
50 
51  var document = testWindow.document;
52 
53  var playlist = document.createElementNS(XUL_NS, "sb-playlist");
54  playlist.setAttribute("flex", "1");
55  document.documentElement.appendChild(playlist);
56  playlist.bind(library.createView(), null);
57 
58  // kick it off
59  continueWindowTest(testSelectAll, [library, playlist]);
60 
61 }
62 
63 function testSelectAll(library, playlist) {
64 
65  log("testSelectAll");
66 
67  setSort(playlist, "created", "a");
68 
69  var view = playlist.mediaListView;
70  var guids = [];
71  for (var i = 0; i < library.length; i ++) {
72  guids.push(view.getItemByIndex(i).guid);
73  }
74 
75  safeSetTimeout(function() {
76  var treeView = playlist.tree.view;
77  treeView.selection.selectAll();
78  var selected = getSelected(playlist);
79  assertArraySimilar(guids, selected);
80 
81  // continue
82  continueWindowTest(testSelectAllResort, [library, playlist, selected]);
83  }, 500);
84 
85 }
86 
87 function testSelectAllResort(library, playlist, previouslySelected) {
88 
89  log("testSelectAllResort");
90  var treeView = playlist.tree.view;
91 
92  setSort(playlist, "created", "d");
93 
94  safeSetTimeout(function() {
95  treeView.selection.selectAll();
96  var selected = getSelected(playlist);
97  assertArraySimilar(previouslySelected.reverse(), selected);
98 
99  // continue
100  continueWindowTest(testSelectSome, [library, playlist]);
101  }, 500);
102 
103 }
104 
105 function testSelectSome(library, playlist) {
106 
107  log("testSelectSome");
108  var treeView = playlist.tree.view;
109 
110  treeView.selection.clearSelection();
111 
112  var guids = [];
113  for (var i = 0; i < library.length; i += 2) {
114  treeView.selection.toggleSelect(i);
115  guids.push(playlist.mediaListView.getItemByIndex(i).guid);
116  }
117 
118  var selected = getSelected(playlist);
119  assertArraySimilar(guids, selected);
120 
121  // continue
122  continueWindowTest(testSelectSomeResort, [library, playlist, selected]);
123 }
124 
125 function testSelectSomeResort(library, playlist, previouslySelected) {
126 
127  log("testSelectSomeResort");
128  setSort(playlist, "created", "a");
129 
130  safeSetTimeout(function() {
131  var selected = getSelected(playlist);
132  assertArraySimilar(previouslySelected.reverse(), selected);
133 
134  // continue
135  continueWindowTest(testSelectAllThenAdd, [library, playlist]);
136  }, 1000);
137 }
138 
139 function testSelectAllThenAdd(library, playlist) {
140 
141  log("testSelectAllThenAdd");
142 
143  var treeView = playlist.tree.view;
144  treeView.selection.selectAll();
145 
146  // Newly added item should be added into the selection
147  library.createMediaItem(newURI("file:///foo"));
148  var view = playlist.mediaListView;
149  var guids = [];
150  for (var i = 0; i < library.length; i ++) {
151  guids.push(view.getItemByIndex(i).guid);
152  }
153 
154  safeSetTimeout(function() {
155  var selected = getSelected(playlist);
156  assertArraySimilar(guids, selected);
157 
158  // continue
159  continueWindowTest(testSelectRange, [library, playlist]);
160  }, 500);
161 }
162 
163 function testSelectRange(library, playlist) {
164 
165  log("testSelectRange");
166 
167  // Set the sort so the cache gets cleared
168  setSort(playlist, "created", "a");
169 
170  var view = playlist.mediaListView;
171  var guids = [];
172  for (var i = 1; i < library.length - 1; i ++) {
173  guids.push(view.getItemByIndex(i).guid);
174  }
175 
176  // Select everything but the first and last. To make this realistic, we
177  // need to make sure the first and last pages of the tree are made visible
178  // to emulate how a user would make this selection
179  var bo = playlist.tree.boxObject.QueryInterface(Ci.nsITreeBoxObject);
180  bo.ensureRowIsVisible(0);
181 
182  safeSetTimeout(function() {
183  bo.ensureRowIsVisible(library.length - 1);
184 
185  safeSetTimeout(function() {
186  var treeView = playlist.tree.view;
187  treeView.selection.clearSelection();
188 
189  // Use an augmented select here so we can test selecting a range over
190  // non-cached pages. Note that this really does not work right now since
191  // this test data is smaller than the fetch size so there are never non-
192  // cached pages. But I promise I've tested this by altering the fetch
193  // size to something small.
194  treeView.selection.rangedSelect(1, library.length - 2, true);
195  var selected = getSelected(playlist);
196  assertArraySimilar(guids, selected);
197 
198  // continue
199  continueWindowTest(testSelectRangeResort, [library, playlist, selected]);
200  }, 500);
201  }, 500);
202 
203 }
204 
205 function testSelectRangeResort(library, playlist, previouslySelected) {
206 
207  log("testSelectRangeResort");
208 
209  setSort(playlist, "created", "d");
210 
211  safeSetTimeout(function() {
212  var selected = getSelected(playlist);
213  assertArraySimilar(previouslySelected.reverse(), selected);
214 
215  // done
216  endWindowTest();
217  }, 500);
218 }
219 
220 function assertArraySimilar(a1, a2) {
221 
222  if (a1.length != a2.length) {
223  fail("compare failed, length wrong, got " + a1.length + " expected " + a2.length);
224  }
225 
226  for (var i = 0; i < a1.length; i++) {
227  if (a1[i] != a2[i]) {
228  fail("arrays don't match at index " + i + " '" + a1[i] + "' != '" + a2[i] + "'");
229  }
230  }
231 
232 }
233 
234 function setSort(playlist, property, direction) {
235  var view = playlist.mediaListView;
236  var pa = createPropertyArray();
237  pa.strict = false;
238  pa.appendProperty("http://songbirdnest.com/data/1.0#" + property, direction);
239  view.setSort(pa);
240 }
241 
242 function getSelected(playlist) {
243  var enumerator = playlist.mediaListView.selection.selectedIndexedMediaItems;
244  var selected = [];
245  while (enumerator.hasMoreElements()) {
246  selected.push(enumerator.getNext().mediaItem.guid);
247  }
248  return selected;
249 }
250 
252  return Cc["@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1"]
253  .createInstance(Ci.sbIMutablePropertyArray);
254 }
function safeSetTimeout(closure, timeout)
function testSelectRangeResort(library, playlist, previouslySelected)
const Cc
function endWindowTest(e)
function fail(aMessage)
const XUL_NS
Definition: FeedWriter.js:83
function log(s)
function setSort(playlist, property, direction)
function beginWindowTest(url, continueFunction)
function testSelectSomeResort(library, playlist, previouslySelected)
function testSelectSome(library, playlist)
function testSelectAllThenAdd(library, playlist)
return null
Definition: FeedWriter.js:1143
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
function newURI(aURLString)
function testSelectRange(library, playlist)
var testWindow
Test file.
function url(spec)
function setupPlaylist()
const Ci
function getSelected(playlist)
function testSelectAllResort(library, playlist, previouslySelected)
_getSelectedPageStyle s i
function assertArraySimilar(a1, a2)
function createPropertyArray()
var treeView
function continueWindowTest(fn, parameters)
function runTest()
Test file.
function testSelectAll(library, playlist)