test_library_statistics.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  // import helpers
33  Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
34 
35  // create a library for the test
36  var databaseGUID = "test_library_statistics";
37  var library = createLibrary(databaseGUID);
38 
39  // create a bunch of items
40  var items = [
41  { artistName: 'A', rating: 1 },
42  { artistName: 'A', rating: 2 },
43  { artistName: 'A', rating: 3 },
44  { artistName: 'A', rating: 4 },
45  { artistName: 'A', rating: 5 },
46  { artistName: 'A', rating: 1 },
47  { artistName: 'A', rating: 1 },
48  { artistName: 'A', rating: 1 },
49  { artistName: 'A', rating: 1 },
50  { artistName: 'A', rating: 2 },
51  { artistName: 'B', rating: 3 },
52  { artistName: 'B', rating: 2 },
53  { artistName: 'B', rating: 2 },
54  { artistName: 'C', rating: 4 },
55  { artistName: 'C', rating: 4 },
56  { artistName: 'C', rating: 5 },
57  { artistName: 'C', rating: 1 },
58  { artistName: 'C', rating: 2 },
59  { artistName: 'D', rating: 3 },
60  { artistName: 'E', rating: 2 },
61  ];
62 
63  for (var i in items) {
64  var props = items[i];
65  var item = library.createMediaItem(newURI('about:test/'+i));
66  for (var p in props) {
67  item.setProperty(SBProperties[p], props[p]);
68  }
69  }
70 
71  // get the library stats interface
72  var sbILibraryStatistics = Components.interfaces.sbILibraryStatistics;
73 
74  // helper to convert an nsIArray of nsIVariants to a JS array
75  // ArrayConverter.JSArray is too generic for this case :(
76  function array2array(a) {
77  var a2 = [];
78  for (var i=0; i<a.length; i++) {
79  a2.push(a.queryElementAt(i, Components.interfaces.nsIVariant));
80  }
81  return a2;
82  }
83 
84  // test SUM in ascending order, high limit
85  assertArraysEqual(array2array(
86  library.collectDistinctValues(SBProperties.artistName,
87  sbILibraryStatistics.COLLECT_SUM, SBProperties.rating, true, 100)),
88  ["D", "B", "C", "A"])
89 
90  // test SUM in descending order, high limit
91  assertArraysEqual(array2array(
92  library.collectDistinctValues(SBProperties.artistName,
93  sbILibraryStatistics.COLLECT_SUM, SBProperties.rating, false, 100)),
94  ["A", "C", "B", "D"])
95 
96  // test SUM in descending order, low limit
97  assertArraysEqual(array2array(
98  library.collectDistinctValues(SBProperties.artistName,
99  sbILibraryStatistics.COLLECT_SUM, SBProperties.rating, false, 2)),
100  ["A", "C"])
101 }
dataSBHighestRatedArtists SBProperties rating
Definition: tuner2.js:867
Extract statistics from a media library.
function assertArraysEqual(a1, a2)
function runTest()
Test file.
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
function newURI(aURLString)
_getSelectedPageStyle s i