test_viewandcfslisteners.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 
27 function runTest () {
28 
29  Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
30  Components.utils.import("resource://app/jsmodules/sbLibraryUtils.jsm");
31 
32  var library = createLibrary("test_viewandcfslisteners", null, false);
33  library.clear();
34 
35  var playlist1 = library.createMediaList("simple");
36  var playlist2 = library.createMediaList("simple");
37 
38  var items = [
39  ["The Beatles", "Abbey Road", "Come Together", "ROCK"],
40  ["The Beatles", "Abbey Road", "Sun King", "ROCK"],
41  ["The Beatles", "Let It Be", "Get Back", "POP"],
42  ["The Beatles", "Let It Be", "Two Of Us", "POP"],
43  ["The Doors", "L.A. Woman", "L.A. Woman", "ROCK"],
44  ["The Doors", "L.A. Woman", "Love Her Madly", "ROCK"]
45  ];
46 
47  function makeItem(i) {
48  var item = library.createMediaItem(
49  newURI("http://foo/" + i + ".mp3"),
50  SBProperties.createArray([
51  [SBProperties.artistName,items[i][0]],
52  [SBProperties.albumName, items[i][1]],
53  [SBProperties.trackName, items[i][2]],
54  [SBProperties.genre, items[i][3]]
55  ]));
56  return item;
57  }
58 
59  var item1 = makeItem(0);
60  var item2 = makeItem(1);
61  var item3 = makeItem(2);
62  var item4 = makeItem(3);
63  var item5 = makeItem(4);
64  var item6 = makeItem(5);
65 
66  var view = library.createView();
67 
68  var listener = new TestMediaListViewListener();
69  view.addListener(listener, false);
70 
71  // Set up root libtary display filters
72  view.filterConstraint = LibraryUtils.createConstraint([
73  [
74  [SBProperties.isList, ["0"]]
75  ],
76  [
77  [SBProperties.hidden, ["0"]]
78  ]
79  ]);
80 
81  assertTrue(listener.filterChanged);
82  assertFalse(listener.searchChanged);
83  assertFalse(listener.sortChanged);
84  listener.reset();
85 
86  view.filterConstraint = null;
87 
88  assertTrue(listener.filterChanged);
89  assertFalse(listener.searchChanged);
90  assertFalse(listener.sortChanged);
91  listener.reset();
92 
93  view.searchConstraint = LibraryUtils.createConstraint([
94  [
95  [SBProperties.artistName, ["The Doors"]]
96  ]
97  ]);
98 
99  assertFalse(listener.filterChanged);
100  assertTrue(listener.searchChanged);
101  assertFalse(listener.sortChanged);
102  listener.reset();
103 
104  view.searchConstraint = null;
105 
106  assertFalse(listener.filterChanged);
107  assertTrue(listener.searchChanged);
108  assertFalse(listener.sortChanged);
109  listener.reset();
110 
111  view.setSort(SBProperties.createArray([
112  [SBProperties.artistName, "d"]
113  ]));
114 
115  assertFalse(listener.filterChanged);
116  assertFalse(listener.searchChanged);
117  assertTrue(listener.sortChanged);
118  listener.reset();
119 
120  view.clearSort();
121 
122  assertFalse(listener.filterChanged);
123  assertFalse(listener.searchChanged);
124  assertTrue(listener.sortChanged);
125  listener.reset();
126 
127  var cfs = view.cascadeFilterSet;
128  // Set up cfs
129  cfs.appendSearch(["*"], 1);
130  cfs.appendFilter(SBProperties.genre);
131  cfs.appendFilter(SBProperties.artistName);
132  cfs.appendFilter(SBProperties.albumName);
133 
134  // Set a filter
135  cfs.set(1, ["ROCK"], 1);
136 
137  assertTrue(listener.filterChanged);
138  assertFalse(listener.searchChanged);
139  assertFalse(listener.sortChanged);
140  listener.reset();
141 
142  cfs.clearAll();
143 
144  assertTrue(listener.filterChanged);
145  assertTrue(listener.searchChanged);
146  assertFalse(listener.sortChanged);
147  listener.reset();
148 
149  // Set a search
150  cfs.set(0, ["Beat"], 1);
151 
152  assertFalse(listener.filterChanged);
153  assertTrue(listener.searchChanged);
154  assertFalse(listener.sortChanged);
155  listener.reset();
156 
157  cfs.clearAll();
158 
159  assertTrue(listener.filterChanged);
160  assertTrue(listener.searchChanged);
161  assertFalse(listener.sortChanged);
162  listener.reset();
163 
164  view.removeListener(listener);
165 
166  // Test weak refences
167  view.addListener(listener, true);
168 
169  // This shouldn't actually do anything
170  view.addListener(listener);
171 
172  view.filterConstraint = LibraryUtils.createConstraint([
173  [
174  [SBProperties.isList, ["0"]]
175  ],
176  [
177  [SBProperties.hidden, ["0"]]
178  ]
179  ]);
180 
181  assertTrue(listener.filterChanged);
182  assertFalse(listener.searchChanged);
183  assertFalse(listener.sortChanged);
184  listener.reset();
185 
186  listener = null;
187  Components.utils.forceGC();
188 
189  // This had better not crash.
190  view.filterConstraint = null;
191 }
192 
var playlist2
function assertTrue(aTest, aMessage)
return null
Definition: FeedWriter.js:1143
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
function newURI(aURLString)
function assertFalse(aTest, aMessage)
function runTest()
Advanced DataRemote unit tests.
Javascript wrappers for common library tasks.
_getSelectedPageStyle s i