test_cascadefilterset.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 
30 function CascadeListener()
31 {
32  this.indexes = [];
33 }
34 
35 CascadeListener.prototype.onValuesChanged = function(index)
36 {
37  for (var i = 0; i < this.indexes.length; i++)
38  {
39  if (this.indexes[i] == index)
40  return;
41  }
42  this.indexes.push(index);
43  this.indexes.sort(function numberorder(a, b) { return a - b; });
44 }
45 
46 CascadeListener.prototype.onChange = function()
47 {
48 }
49 
50 CascadeListener.prototype.reset = function()
51 {
52  this.indexes = [];
53 }
54 
55 function runTest () {
56 
57  var library = createLibrary("test_cascademediaset");
58  var view = library.createView();
59  var cfs = view.cascadeFilterSet;
60 
61  var listener = new CascadeListener();
62  cfs.addListener(listener, false);
63  // A simulation of an everything search, artist, album cascase
64  cfs.appendSearch(["*"], 1);
65 
66  cfs.appendFilter("http://songbirdnest.com/data/1.0#artistName", false);
67 
68  cfs.appendFilter("http://songbirdnest.com/data/1.0#albumName", false);
69 
70  // With no filtering applied, we should have 8 artists and 8 albums
71  assertEqual(cfs.getValueCount(1), 8);
72  assertEqual(cfs.getValueCount(2), 8);
73 
74  // Test that reading a cached value matches the last read
75  assertEqual(cfs.getValueCount(1, true), 8);
76  assertEqual(cfs.getValueCount(2, true), 8);
77 
78  // Putting AC/DC in the search box should filter down to only one artist
79  // and one album
80  cfs.set(0, ["AC/DC"], 1);
81  assertEqual(cfs.getValueCount(1), 1);
82  assertEqual(cfs.getValueCount(2), 1);
83 
84  // The filters downstream from 0 should have changed
85  assertEqual(arrayEquals(listener.indexes, [1, 2]), true);
86  listener.reset();
87 
88  // Removing the search filter should return everyting to the others
89  cfs.set(0, [], 0);
90  assertEqual(cfs.getValueCount(1), 8);
91  assertEqual(cfs.getValueCount(2), 8);
92 
93  // The filters downstream from 0 should have changed
94  assertEqual(arrayEquals(listener.indexes, [1, 2]), true);
95  listener.reset();
96 
97  // Filter the artist on a-ha should keep only one album
98  cfs.set(1, ["a-ha"], 1);
99  assertEqual(cfs.getValueCount(1), 8);
100  assertEqual(cfs.getValueCount(2), 1);
101 
102  // The filter downstream from 1 should have changed
103  assertEqual(arrayEquals(listener.indexes, [2]), true);
104  listener.reset();
105 
106  // Add AC/DC to the artist filter and we should have two albums
107  cfs.set(1, ["a-ha", "AC/DC"], 2);
108  assertEqual(cfs.getValueCount(1), 8);
109  assertEqual(cfs.getValueCount(2), 2);
110 
111  // The filter downstream from 1 should have changed
112  assertEqual(arrayEquals(listener.indexes, [2]), true);
113  listener.reset();
114 
115  // Setting an additional filter on album should not affect anything
116  cfs.set(2, ["Back In Black"], 1);
117  assertEqual(cfs.getValueCount(1), 8);
118  assertEqual(cfs.getValueCount(2), 2);
119 
120  // This should not notify any filters of changes
121  assertEqual(arrayEquals(listener.indexes, []), true);
122  listener.reset();
123 
124  // Add gibberish to the search filter should make everything empty
125  cfs.set(0, ["dfkjhdfds"], 1);
126  assertEqual(cfs.getValueCount(1), 0);
127  assertEqual(cfs.getValueCount(2), 0);
128 
129  // The filters downstream from 0 should have changed
130  assertEqual(arrayEquals(listener.indexes, [1, 2]), true);
131  listener.reset();
132 
133  // Removing the search filter should restore us to the previous state
134  cfs.remove(0);
135  assertEqual(cfs.getValueCount(0), 8);
136  assertEqual(cfs.getValueCount(1), 8);
137 
138  // This should cause all filters after the removed one to change
139  assertEqual(arrayEquals(listener.indexes, [0, 1]), true);
140  listener.reset();
141 
142  cfs.remove(0);
143  cfs.remove(0);
144  cfs.appendFilter("http://songbirdnest.com/data/1.0#genre", false);
145  assertEqual(cfs.getValueCount(0), 2);
146  cfs.changeFilter(0, "http://songbirdnest.com/data/1.0#artistName");
147  assertEqual(cfs.getValueCount(0), 8);
148  cfs.changeFilter(0, "http://songbirdnest.com/data/1.0#genre");
149  assertEqual(cfs.getValueCount(0), 2);
150 
151  cfs.removeListener(listener);
152 }
153 
154 function arrayEquals(array1, array2) {
155 
156  if (array1.length != array2.length) {
157  return false;
158  }
159 
160  for(var i = 0; i < array1.length; i++) {
161  if (array1[i] != array2[i]) {
162  return false;
163  }
164  }
165 
166  return true;
167 }
168 
function CascadeListener()
Test file.
function assertEqual(aExpected, aActual, aMessage)
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
function runTest()
_getSelectedPageStyle s i