test_filterednotifications.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  var sbIML = Ci.sbIMediaList;
34  var SB_NS = "http://songbirdnest.com/data/1.0#";
35 
36  var databaseGUID = "test_simplemedialistnotifications";
37  var library = createLibrary(databaseGUID, null, false);
38 
39  var listener = new TestMediaListListener();
40 
41  // Each individual notification type
42  var flags = sbIML.LISTENER_FLAGS_ALL;
43  library.addListener(listener, false, flags);
45  assertFlagsMatch(flags, listener);
46 
47  library.removeListener(listener);
48  listener.reset();
49 
50  flags = sbIML.LISTENER_FLAGS_ITEMADDED;
51  library.addListener(listener, false, flags);
53  assertFlagsMatch(flags, listener);
54 
55  library.removeListener(listener);
56  listener.reset();
57 
58  flags = sbIML.LISTENER_FLAGS_BEFOREITEMREMOVED;
59  library.addListener(listener, false, flags);
61  assertFlagsMatch(flags, listener);
62 
63  library.removeListener(listener);
64  listener.reset();
65 
66  flags = sbIML.LISTENER_FLAGS_AFTERITEMREMOVED;
67  library.addListener(listener, false, flags);
69  assertFlagsMatch(flags, listener);
70 
71  library.removeListener(listener);
72  listener.reset();
73 
74  flags = sbIML.LISTENER_FLAGS_ITEMUPDATED;
75  library.addListener(listener, false, flags);
77  assertFlagsMatch(flags, listener);
78 
79  library.removeListener(listener);
80  listener.reset();
81 
82  flags = sbIML.LISTENER_FLAGS_LISTCLEARED;
83  library.addListener(listener, false, flags);
85  assertFlagsMatch(flags, listener);
86 
87  library.removeListener(listener);
88  listener.reset();
89 
90  flags = sbIML.LISTENER_FLAGS_BATCHBEGIN;
91  library.addListener(listener, false, flags);
93  assertFlagsMatch(flags, listener);
94 
95  library.removeListener(listener);
96  listener.reset();
97 
98  flags = sbIML.LISTENER_FLAGS_BATCHEND;
99  library.addListener(listener, false, flags);
101  assertFlagsMatch(flags, listener);
102 
103  library.removeListener(listener);
104  listener.reset();
105 
106  // Test multiple
107  flags = sbIML.LISTENER_FLAGS_BATCHBEGIN |
108  sbIML.LISTENER_FLAGS_BATCHEND |
109  sbIML.LISTENER_FLAGS_ITEMUPDATED;
110  library.addListener(listener, false, flags);
112  assertFlagsMatch(flags, listener);
113 
114  library.removeListener(listener);
115  listener.reset();
116 
117  // Test default param
118  library.addListener(listener, false);
120  assertFlagsMatch(sbIML.LISTENER_FLAGS_ALL, listener);
121 
122  library.removeListener(listener);
123  listener.reset();
124 
125  // Test property filter
126  var filter = createPropertyArray();
127  filter.appendProperty(SB_NS + "albumName", null);
128  filter.appendProperty(SB_NS + "trackNumber", null);
129  library.addListener(listener, false, sbIML.LISTENER_FLAGS_ALL, filter);
130 
131  var item = library.createMediaItem(newURI("http://foo.com/"));
132  item.setProperty(SB_NS + "artistName", "foo");
133  assertEqual(listener.updatedItem, null);
134 
135  listener.reset();
136  item.setProperty(SB_NS + "albumName", "foo");
137  assertEqual(listener.updatedItem, item);
138 
139  listener.reset();
140  item.setProperty(SB_NS + "trackNumber", "123");
141  assertEqual(listener.updatedItem, item);
142 
143  // Test notification suppression
144  // Not in batch, onItemUpdated returning true, should be notified
145  listener.reset();
146  listener.retval = true;
147  item.setProperty(SB_NS + "albumName", "foo");
148  assertEqual(listener.updatedItem, item);
149 
150  listener.reset();
151  listener.retval = false;
152 
153  library.runInBatchMode(function() {
154 
155  // In batch, onItemUpdated returning false, should be notified
156  item.setProperty(SB_NS + "albumName", "foo");
157  assertEqual(listener.updatedItem, item);
158 
159  listener.reset();
160  listener.retval = true;
161 
162  // In batch, onItemUpdated returning true, should be notified since the
163  // last call returned true
164  item.setProperty(SB_NS + "albumName", "foo");
165  assertEqual(listener.updatedItem, item);
166 
167  listener.reset();
168  listener.retval = true;
169 
170  // In batch, onItemUpdated returning true, should not be notified
171  item.setProperty(SB_NS + "albumName", "foo");
172  assertEqual(listener.updatedItem, null);
173  });
174 
175  listener.reset();
176  listener.retval = true;
177 
178  // Out of the batch, should be notified
179  item.setProperty(SB_NS + "albumName", "foo");
180  assertEqual(listener.updatedItem, item);
181 
182  listener.reset();
183  listener.retval = true;
184 
185  // Out of batch, should still be notified since we were not in a batch
186  item.setProperty(SB_NS + "albumName", "foo");
187  assertEqual(listener.updatedItem, item);
188 
189  library.removeListener(listener);
190 }
191 
192 function doSomethingThatFiresAllEvents(library) {
193 
194  library.runInBatchMode(function() {});
195  var item = library.createMediaItem(newURI("http://foo.com/"), null, true);
196  item.setProperty(SB_NS + "albumName", "foo");
197  library.remove(item);
198  library.clear();
199 }
200 
202 
203  if (flags & Ci.sbIMediaList.LISTENER_FLAGS_ITEMADDED) {
204  assertNotEqual(listener.added.length, 0);
205  }
206  else {
207  assertEqual(listener.added.length, 0);
208  }
209 
210  if (flags & Ci.sbIMediaList.LISTENER_FLAGS_BEFOREITEMREMOVED) {
211  assertNotEqual(listener.removedBefore.length, 0);
212  }
213  else {
214  assertEqual(listener.removedBefore.length, 0);
215  }
216 
217  if (flags & Ci.sbIMediaList.LISTENER_FLAGS_AFTERITEMREMOVED) {
218  assertNotEqual(listener.removedAfter.length, 0);
219  }
220  else {
221  assertEqual(listener.removedAfter.length, 0);
222  }
223 
224  if (flags & Ci.sbIMediaList.LISTENER_FLAGS_ITEMUPDATED) {
225  assertNotEqual(listener.updatedItem, null);
226  }
227  else {
228  assertEqual(listener.updatedItem, null);
229  }
230 
231  if (flags & Ci.sbIMediaList.LISTENER_FLAGS_LISTCLEARED) {
232  assertTrue(listener.listCleared);
233  }
234  else {
235  assertFalse(listener.listCleared);
236  }
237 
238  if (flags & Ci.sbIMediaList.LISTENER_FLAGS_BATCHBEGIN) {
239  assertNotEqual(listener.batchBeginList, null);
240  }
241  else {
242  assertEqual(listener.batchBeginList, null);
243  }
244 
245  if (flags & Ci.sbIMediaList.LISTENER_FLAGS_BATCHEND) {
246  assertNotEqual(listener.batchEndList, null);
247  }
248  else {
249  assertEqual(listener.batchEndList, null);
250  }
251 }
252 
254  return Cc["@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1"]
255  .createInstance(Ci.sbIMutablePropertyArray);
256 }
const Cc
function assertNotEqual(aExpected, aActual, aMessage)
sbDeviceFirmwareAutoCheckForUpdate prototype flags
function assertTrue(aTest, aMessage)
function createPropertyArray()
function assertEqual(aExpected, aActual, aMessage)
function runTest()
Test file.
function doSomethingThatFiresAllEvents(library)
const SB_NS
return null
Definition: FeedWriter.js:1143
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
function newURI(aURLString)
function assertFalse(aTest, aMessage)
const Ci
function assertFlagsMatch(flags, listener)
Array filter(tab.attributes, function(aAttr){return(_this.xulAttributes.indexOf(aAttr.name) >-1);}).forEach(tab.removeAttribute
function TestMediaListListener()