head_localdatabaselibrary.js
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
29 var SB_NS = "http://songbirdnest.com/data/1.0#";
30 
31 function StringArrayEnumerator(aArray) {
32  this._array = aArray;
33  this._current = 0;
34 }
35 
36 StringArrayEnumerator.prototype.hasMore = function() {
37  return this._current < this._array.length;
38 }
39 
40 StringArrayEnumerator.prototype.getNext = function() {
41  return this._array[this._current++];
42 }
43 
44 StringArrayEnumerator.prototype.QueryInterface = function(iid) {
45  if (!iid.equals(Components.interfaces.nsIStringEnumerator) &&
46  !iid.equals(Components.interfaces.nsISupports))
47  throw Components.results.NS_ERROR_NO_INTERFACE;
48  return this;
49 };
50 
51 function SimpleArrayEnumerator(aArray) {
52  this._array = aArray;
53  this._current = 0;
54 }
55 
56 SimpleArrayEnumerator.prototype.hasMoreElements = function() {
57  return this._current < this._array.length;
58 }
59 
60 SimpleArrayEnumerator.prototype.getNext = function() {
61  return this._array[this._current++];
62 }
63 
64 SimpleArrayEnumerator.prototype.QueryInterface = function(iid) {
65  if (!iid.equals(Components.interfaces.nsISimpleEnumerator) &&
66  !iid.equals(Components.interfaces.nsISupports))
67  throw Components.results.NS_ERROR_NO_INTERFACE;
68  return this;
69 };
70 
71 function makeArray(library) {
72  var ldl = library.QueryInterface(Ci.sbILocalDatabaseLibrary);
73  var array = Cc["@songbirdnest.com/Songbird/Library/LocalDatabase/GUIDArray;1"]
74  .createInstance(Ci.sbILocalDatabaseGUIDArray);
75  array.databaseGUID = ldl.databaseGuid;
76  array.propertyCache = ldl.propertyCache;
77  return array;
78 }
79 
80 function assertSort(array, dataFile) {
81 
82  var data = readFile(dataFile);
83  var a = data.split("\n");
84 
85  if(a.length - 1 != array.length) {
86  fail("sort failed, length wrong, got " + array.length + " expected " + (a.length - 1));
87  }
88 
89  for(var i = 0; i < a.length - 1; i++) {
90  var b = a[i].split("\t");
91  if(array.getGuidByIndex(i) != b[0]) {
92  fail("sort failed, index " + i + " got " + array.getGuidByIndex(i) + " expected " + b[0]);
93  }
94  }
95 
96 }
97 
98 function assertArray(array, dataFile) {
99 
100  var data = readFile(dataFile);
101  var a = data.split("\n");
102 
103  if(a.length - 1 != array.length) {
104  fail("length wrong, got " + array.length + " expected " + (a.length - 1));
105  }
106 
107  for(var i = 0; i < a.length - 1; i++) {
108  var dataValue = a[i].split("\t")[0];
109  if(array[i] != dataValue) {
110  fail("sort failed, index " + i + " got " + array[i] + " expected " + dataValue);
111  }
112  }
113 }
114 
115 function assertList(list, data) {
116 
117  var a;
118  if (data instanceof Array) {
119  a = data;
120  }
121  else {
122  a = readList(data);
123  }
124 
125  if(a.length != list.length) {
126  fail("compare failed, length wrong, got " + list.length + " expected " + a.length);
127  }
128 
129  var listener = new TestMediaListEnumerationListener();
130 
131  listener.enumItemFunction = function onItem(list, item) {
132  if (item.guid != a[this.count]) {
133  fail("compare failed, index " + this.count + " got " + item.guid +
134  " expected " + a[this.count]);
135  return false;
136  }
137  return true;
138  };
139 
140  list.enumerateAllItems(listener, Ci.sbIMediaList.ENUMERATIONTYPE_LOCKING);
141 }
142 
144 }
145 TestMediaListListener.prototype = {
146  _added: [],
147  _removedBefore: [],
148  _removedAfter: [],
149  _updatedItem: null,
150  _updatedProperties: null,
151  _movedItemFromIndex: [],
152  _movedItemToIndex: [],
153  _batchBeginList: null,
154  _batchEndList: null,
155  _listCleared: false,
156  _retval: false,
157 
158  get added() {
159  log("_added.length=" + this._added.length);
160  return this._added;
161  },
162 
163  get removedBefore() {
164  return this._removedBefore;
165  },
166 
167  get removedAfter() {
168  return this._removedAfter;
169  },
170 
171  get updatedItem() {
172  return this._updatedItem;
173  },
174 
175  get updatedProperties() {
176  return this._updatedProperties;
177  },
178 
179  get movedItemFromIndex() {
180  return this._movedItemFromIndex;
181  },
182 
183  get movedItemToIndex() {
184  return this._movedItemToIndex;
185  },
186 
187  get batchBeginList() {
188  return this._batchBeginList;
189  },
190 
191  get batchEndList() {
192  return this._batchEndList;
193  },
194 
195  get listCleared() {
196  return this._listCleared;
197  },
198 
199  set retval(value) {
200  this._retval = value;
201  },
202  reset: function reset() {
203  this._added = [];
204  this._removedBefore = [];
205  this._removedAfter = [];
206  this._updatedItem = null;
207  this._updatedProperties = null;
208  this._movedItemFromIndex = [];
209  this._movedItemToIndex = [];
210  this._batchBeginList = null;
211  this._batchEndList = null;
212  this._listCleared = false;
213  this._retval = false;
214  },
215 
216  onItemAdded: function onItemAdded(list, item, index) {
217  this._added.push({list: list, item: item, index: index});
218  return this._retval;
219  },
220 
221  onBeforeItemRemoved: function onBeforeItemRemoved(list, item, index) {
222  this._removedBefore.push({list: list, item: item, index: index});
223  return this._retval;
224  },
225 
226  onAfterItemRemoved: function onAfterItemRemoved(list, item, index) {
227  this._removedAfter.push({list: list, item: item, index: index});
228  return this._retval;
229  },
230 
231  onItemUpdated: function onItemUpdated(list, item, properties) {
232  this._updatedItem = item;
233  this._updatedProperties = properties;
234  return this._retval;
235  },
236 
237  onItemMoved: function onItemMoved(list, fromIndex, toIndex) {
238  this._movedItemFromIndex.push(fromIndex);
239  this._movedItemToIndex.push(toIndex);
240  return this._retval;
241  },
242 
243  onBatchBegin: function onBatchBegin(list) {
244  this._batchBeginList = list;
245  },
246 
247  onBatchEnd: function onBatchEnd(list) {
248  this._batchEndList = list;
249  },
250  // XXX: implement the tests for this function
251  onBeforeListCleared: function onBeforeListCleared() {
252  return this._retval;
253  },
254  onListCleared: function onListCleared() {
255  this._listCleared = true;
256  return this._retval;
257  },
258  QueryInterface: function QueryInterface(iid) {
259  if (!iid.equals(Ci.sbIMediaListListener) &&
260  !iid.equals(Ci.nsISupports))
261  throw Cr.NS_ERROR_NO_INTERFACE;
262  return this;
263  }
264 }
265 
266 function TestMediaListEnumerationListener() {
267  this._items = [];
268 }
269 TestMediaListEnumerationListener.prototype = {
270  _result: Cr.NS_OK,
271  _enumBeginFunction: null,
272  _enumItemFunction: null,
273  _enumEndFunction: null,
274  _enumerationComplete: false,
275  _items: null,
276  _index: 0,
277 
278  onEnumerationBegin: function onEnumerationBegin(list) {
279  if (this._enumBeginFunction) {
280  return this._enumBeginFunction(list);
281  }
282 
283  return Ci.sbIMediaListEnumerationListener.CONTINUE;
284  },
285 
286  onEnumeratedItem: function onEnumeratedItem(list, item) {
287  var retval = Ci.sbIMediaListEnumerationListener.CONTINUE;
288  if (this._enumItemFunction)
289  retval = this._enumItemFunction(list, item);
290 
291  if (retval == Ci.sbIMediaListEnumerationListener.CONTINUE) {
292  this._items.push(item);
293  }
294 
295  return retval;
296  },
297 
298  onEnumerationEnd: function onEnumerationEnd(list, result) {
299  if (this._enumEndFunction)
300  this._enumEndFunction(list, result);
301  this._result = result;
302  this._enumerationComplete = true;
303  },
304 
305  reset: function reset() {
306  this._result = Cr.NS_OK;
307  this._enumeratedItemCount = 0;
308  this._enumBeginFunction = null;
309  this._enumItemFunction = null;
310  this._enumEndFunction = null;
311  this._items = [];
312  this._index = 0;
313  this._enumerationComplete = false;
314  },
315 
316  hasMoreElements: function hasMoreElements() {
317  if (!this._enumerationComplete)
318  throw Cr.NS_ERROR_NOT_AVAILABLE;
319 
320  return this._index < this._items.length;
321  },
322 
323  getNext: function getNext() {
324  if (!this._enumerationComplete)
325  throw Cr.NS_ERROR_NOT_AVAILABLE;
326 
327  return this._items[this._index++];
328  },
329 
330  set enumBeginFunction(val) {
331  this._enumBeginFunction = val;
332  },
333 
334  set enumItemFunction(val) {
335  this._enumItemFunction = val;
336  },
337 
338  set enumEndFunction(val) {
339  this._enumEndFunction = val;
340  },
341 
342  get result() {
343  return this._result;
344  },
345 
346  get count() {
347  return this._items.length;
348  },
349 
350  QueryInterface: function QueryInterface(iid) {
351  if (!iid.equals(Ci.sbIMediaListEnumerationListener) &&
352  !iid.equals(Ci.nsISimpleEnumerator) &&
353  !iid.equals(Ci.nsISupports))
354  throw Cr.NS_ERROR_NO_INTERFACE;
355  return this;
356  }
357 }
358 
359 function TestMediaListViewListener() {
360 }
361 TestMediaListViewListener.prototype = {
362  _filterChanged: false,
363  _searchChanged: false,
364  _sortChanged: false,
365 
366  get filterChanged() {
367  return this._filterChanged;
368  },
369 
370  get searchChanged() {
371  return this._searchChanged;
372  },
373 
374  get sortChanged() {
375  return this._sortChanged;
376  },
377 
378  reset: function TMLVL_reset() {
379  this._filterChanged = false;
380  this._searchChanged = false;
381  this._sortChanged = false;
382  },
383 
384  onFilterChanged: function TMLVL_onFilterChanged(view) {
385  this._filterChanged = true;
386  },
387 
388  onSearchChanged: function TMLVL_onSearchChanged(view) {
389  this._searchChanged = true;
390  },
391 
392  onSortChanged: function TMLVL_onSortChanged(view) {
393  this._sortChanged = true;
394  },
395 
396  QueryInterface: function TMLVL_QueryInterface(iid) {
397  if (!iid.equals(Components.interfaces.sbIMediaListViewListener) &&
398  !iid.equals(Components.interfaces.nsISupportsWeakReference))
399  throw Components.results.NS_ERROR_NO_INTERFACE;
400  return this;
401  }
402 }
403 
404 function readList(dataFile, index) {
405 
406  var data = readFile(dataFile);
407  var a = data.split("\n");
408  index = index || 0;
409 
410  var b = [];
411  for(var i = 0; i < a.length - 1; i++) {
412  b.push(a[i].split("\t")[index]);
413  }
414 
415  return b;
416 }
417 
418 function loadMockDatabase() {
419  var data = readFile("media_items.txt");
420  var a = data.split("\n");
421 
422  var sp = [
423  "http://songbirdnest.com/data/1.0#GUID",
424  "http://songbirdnest.com/data/1.0#created",
425  "http://songbirdnest.com/data/1.0#updated",
426  "http://songbirdnest.com/data/1.0#contentURL",
427  "http://songbirdnest.com/data/1.0#contentType",
428  "http://songbirdnest.com/data/1.0#contentLength",
429  "http://songbirdnest.com/data/1.0#hidden",
430  "http://songbirdnest.com/data/1.0#isList"
431  ];
432 
433  var db = {};
434 
435  for(var i = 0; i < a.length - 1; i++) {
436  var b = a[i].split("\t");
437  var item = db[b[1]];
438  if(!item) {
439  item = {};
440  db[b[1]] = item;
441  }
442 
443  for(var j = 0; j < sp.length; j++) {
444  if (sp[j] == "http://songbirdnest.com/data/1.0#isList") {
445  if (!b[j + 1] || b[j + 1] == "0") {
446  item[sp[j]] = "0";
447  }
448  else {
449  item[sp[j]] = "1";
450  item["http://songbirdnest.com/data/1.0#listType"] = "1";
451  }
452  }
453  else {
454  item[sp[j]] = b[j + 1];
455  }
456  }
457  }
458 
459  var props = [
460  "http://songbirdnest.com/data/1.0#trackName",
461  "http://songbirdnest.com/data/1.0#albumName",
462  "http://songbirdnest.com/data/1.0#artistName",
463  "http://songbirdnest.com/data/1.0#duration",
464  "http://songbirdnest.com/data/1.0#genre",
465  "http://songbirdnest.com/data/1.0#trackNumber",
466  "http://songbirdnest.com/data/1.0#year",
467  "http://songbirdnest.com/data/1.0#discNumber",
468  "http://songbirdnest.com/data/1.0#totalDiscs",
469  "http://songbirdnest.com/data/1.0#totalTracks",
470  "http://songbirdnest.com/data/1.0#lastPlayTime",
471  "http://songbirdnest.com/data/1.0#playCount",
472  "http://songbirdnest.com/data/1.0#customType",
473  "http://songbirdnest.com/data/1.0#isSortable"
474  ];
475 
476  data = readFile("resource_properties.txt");
477  a = data.split("\n");
478  for(var i = 0; i < a.length - 1; i++) {
479  var b = a[i].split("\t");
480  var item = db[b[0]];
481  item[props[parseInt(b[1]) - 1]] = b[2];
482  }
483 
484  return db;
485 }
486 
487 function getFile(fileName) {
488  var file = Cc["@mozilla.org/file/directory_service;1"]
489  .getService(Ci.nsIProperties)
490  .get("resource:app", Ci.nsIFile);
491  file = file.clone();
492  file.append("testharness");
493  file.append("localdatabaselibrary");
494  file.append(fileName);
495  return file;
496 }
497 
498 function execQuery(databaseGuid, sql) {
499 
500  var dbq = Cc["@songbirdnest.com/Songbird/DatabaseQuery;1"]
501  .createInstance(Ci.sbIDatabaseQuery);
502 
503  dbq.setDatabaseGUID(databaseGuid);
504  dbq.setAsyncQuery(false);
505  dbq.addQuery(sql);
506  dbq.execute();
507 
508  var result = dbq.getResultObject();
509  var rows = result.getRowCount();
510  var cols = result.getColumnCount();
511 
512  var d = [];
513  for (var i = 0; i < rows; i++) {
514  var r = [];
515  for (var j = 0; j < cols; j++) {
516  r.push(result.getRowCell(i, j));
517  }
518  d.push(r);
519  }
520  return d;
521 }
const Cc
function fail(aMessage)
inArray array
function log(s)
function assertList(list, data)
sbOSDControlService prototype QueryInterface
function d(s)
function getFile(fileName)
var count
Definition: test_bug7406.js:32
this _dialogInput val(dateText)
function assertArray(array, dataFile)
return null
Definition: FeedWriter.js:1143
function SimpleArrayEnumerator(aArray)
SimpleArrayEnumerator prototype hasMoreElements
countRef value
Definition: FeedWriter.js:1423
const Cr
function readList(dataFile)
function StringArrayEnumerator(aArray)
const Ci
function readFile(fileName)
var SB_NS
Some globally useful stuff for the local database library tests.
observe data
Definition: FeedWriter.js:1329
_getSelectedPageStyle s i
function TestMediaListListener()
function assertSort(array, dataFile)
var file
function makeArray(library)