test_viewandcfs.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_viewandcfs", null, false);
37  library.clear();
38 
39  var playlist1 = library.createMediaList("simple");
40  var playlist2 = library.createMediaList("simple");
41 
42  var items = [
43  ["The Beatles", "Abbey Road", "Come Together", "ROCK"],
44  ["The Beatles", "Abbey Road", "Sun King", "ROCK"],
45  ["The Beatles", "Let It Be", "Get Back", "POP"],
46  ["The Beatles", "Let It Be", "Two Of Us", "POP"],
47  ["The Doors", "L.A. Woman", "L.A. Woman", "ROCK"],
48  ["The Doors", "L.A. Woman", "Love Her Madly", "ROCK"]
49  ];
50 
51  function makeItem(i) {
52  var item = library.createMediaItem(
53  newURI("http://foo/" + i + ".mp3"),
54  SBProperties.createArray([
55  [SBProperties.artistName,items[i][0]],
56  [SBProperties.albumName, items[i][1]],
57  [SBProperties.trackName, items[i][2]],
58  [SBProperties.genre, items[i][3]]
59  ]));
60  return item;
61  }
62 
63  var item1 = makeItem(0);
64  var item2 = makeItem(1);
65  var item3 = makeItem(2);
66  var item4 = makeItem(3);
67  var item5 = makeItem(4);
68  var item6 = makeItem(5);
69 
70  var view = library.createView();
71  var cfs = view.cascadeFilterSet;
72 
73  assertView(view, [
74  playlist1,
75  playlist2,
76  item1,
77  item2,
78  item3,
79  item4,
80  item5,
81  item6
82  ]);
83 
84 
85  assertEqual(view.searchConstraint, null);
86  assertEqual(view.filterConstraint, null);
87 
88  var filter = LibraryUtils.createConstraint([
89  [
90  [SBProperties.isList, ["0"]]
91  ],
92  [
93  [SBProperties.hidden, ["0"]]
94  ]
95  ]);
96 
97  // Set up root libtary display filters
98  view.filterConstraint = filter;
99 
100  // Set up cfs
101  cfs.appendSearch(["*"], 1);
102  cfs.appendFilter(SBProperties.genre);
103  cfs.appendFilter(SBProperties.artistName);
104  cfs.appendFilter(SBProperties.albumName);
105 
106  assertView(view, [
107  item1,
108  item2,
109  item3,
110  item4,
111  item5,
112  item6
113  ]);
114 
115  assertEqual(view.searchConstraint, null);
116  assertTrue(view.filterConstraint.equals(filter));
117 
118  // Set a filter
119  cfs.set(1, ["ROCK"], 1);
120 
121  assertView(view, [
122  item1,
123  item2,
124  item5,
125  item6
126  ]);
127 
128  assertEqual(view.searchConstraint, null);
129  assertTrue(view.filterConstraint.equals(LibraryUtils.createConstraint([
130  [
131  [SBProperties.isList, ["0"]]
132  ],
133  [
134  [SBProperties.hidden, ["0"]]
135  ],
136  [
137  [SBProperties.genre, ["ROCK"]]
138  ]
139  ])));
140 
141  cfs.set(2, ["The Beatles"], 1);
142 
143  assertView(view, [
144  item1,
145  item2
146  ]);
147 
148  assertEqual(view.searchConstraint, null);
149  assertTrue(view.filterConstraint.equals(LibraryUtils.createConstraint([
150  [
151  [SBProperties.isList, ["0"]]
152  ],
153  [
154  [SBProperties.hidden, ["0"]]
155  ],
156  [
157  [SBProperties.genre, ["ROCK"]]
158  ],
159  [
160  [SBProperties.artistName, ["The Beatles"]]
161  ]
162  ])));
163 
164  // Clear the filter set. This should revert to the root library display
165  // filters
166  cfs.clearAll();
167 
168  assertView(view, [
169  item1,
170  item2,
171  item3,
172  item4,
173  item5,
174  item6
175  ]);
176 
177  assertEqual(view.searchConstraint, null);
178  assertTrue(view.filterConstraint.equals(filter));
179  // Set a search
180  cfs.set(0, ["Beat"], 1);
181 
182  assertView(view, [
183  item1,
184  item2,
185  item3,
186  item4
187  ]);
188 
189  assertTrue(view.searchConstraint.equals(LibraryUtils.createConstraint([
190  [
191  ["*", ["Beat"]]
192  ]
193  ])));
194  assertTrue(view.filterConstraint.equals(filter));
195 
196  // Clearing the filters and searches on the view should remove all filters
197  // and views
198  view.filterConstraint = null;
199 
200  assertView(view, [
201  item1,
202  item2,
203  item3,
204  item4
205  ]);
206 
207  assertTrue(view.searchConstraint.equals(LibraryUtils.createConstraint([
208  [
209  ["*", ["Beat"]]
210  ]
211  ])));
212  assertEqual(view.filterConstraint, null);
213 
214  view.searchConstraint = null;
215 
216  assertView(view, [
217  playlist1,
218  playlist2,
219  item1,
220  item2,
221  item3,
222  item4,
223  item5,
224  item6
225  ]);
226 
227  assertEqual(view.searchConstraint, null);
228  assertEqual(view.filterConstraint, null);
229 }
230 
231 function assertView(view, list) {
232  if (view.length != list.length) {
233  fail("View length not equal to list length, " + view.length +
234  " != " + list.length);
235  }
236 
237  for(var i = 0; i < view.length; i++) {
238  if (!view.getItemByIndex(i).equals(list[i])) {
239  fail("View is different than list at index " + i + ", " +
240  view.getItemByIndex(i).guid + " != " + list[i].guid);
241  }
242  }
243 
244 }
var playlist2
function fail(aMessage)
function assertTrue(aTest, aMessage)
function assertEqual(aExpected, aActual, aMessage)
function runTest()
Test file.
return null
Definition: FeedWriter.js:1143
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
function newURI(aURLString)
function assertView(view, list)
Javascript wrappers for common library tasks.
_getSelectedPageStyle s i
Array filter(tab.attributes, function(aAttr){return(_this.xulAttributes.indexOf(aAttr.name) >-1);}).forEach(tab.removeAttribute