test_asyncguidarray.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 ArrayListener()
32 {
33  this.gotLength = false;
34  this.length = -1;
35 
36  this.gotGuid = false;
37  this.guid = null;
38 
39  this.gotValue = false;
40  this.value = null;
41 
42  this.gotMediaItemId = false;
43  this.mediaItemId = null;
44 
45  this.rv = null;
46 }
47 
48 ArrayListener.prototype.onGetLength = function(length, rv)
49 {
50  this.length = length;
51  this.rv = rv;
52  this.gotLength = true;
53 }
54 
55 ArrayListener.prototype.onGetGuidByIndex = function(index, guid, rv)
56 {
57  this.guid = guid;
58  this.rv = rv;
59  this.gotGuid = true;
60 }
61 
62 ArrayListener.prototype.onGetSortPropertyValueByIndex = function(index, value, rv)
63 {
64  this.value = value;
65  this.rv = rv;
66  this.gotValue = true;
67 }
68 
69 ArrayListener.prototype.onGetMediaItemIdByIndex = function(index, mediaItemId, rv)
70 {
71  this.mediaItemId = mediaItemId;
72  this.rv = rv;
73  this.gotMediaItemId = true;
74 }
75 
76 ArrayListener.prototype.onStateChange = function(state)
77 {
78 }
79 
80 ArrayListener.prototype.QueryInterface = function(iid) {
81  if (!iid.equals(Ci.sbILocalDatabaseAsyncGUIDArrayListener) &&
82  !iid.equals(Ci.nsISupportsWeakReference) &&
83  !iid.equals(Ci.nsISupports))
84  throw Cr.NS_ERROR_NO_INTERFACE;
85  return this;
86 };
87 
88 function doTest(array) {
89 
90  switch(phases[currentPhase]) {
91  case "getLengthAsync":
92 
93  var listener = new ArrayListener();
94  array.addAsyncListener(listener);
95  array.getLengthAsync();
96 
97  var tester = {};
98  tester.listener = listener;
99  tester.func = function() {
100  if (listener.gotLength) {
101  if (listener.rv == Cr.NS_OK && listener.length == 101) {
102  return 1;
103  }
104  else {
105  return -1;
106  }
107  }
108  return 0;
109  };
110 
111  return tester;
112  break;
113 
114  case "getGuidByIndexAsync":
115 
116  dump("------------------------------------- 1\n");
117  var listener = new ArrayListener();
118  array.addAsyncListener(listener);
119  array.getGuidByIndexAsync(0);
120  dump("------------------------------------- 2\n");
121 
122  var tester = {};
123  tester.listener = listener;
124  tester.func = function() {
125  if (listener.gotGuid) {
126  if (listener.rv == Cr.NS_OK &&
127  listener.guid == "3E4FAFDA-AD99-11DB-9321-C22AB7121F49") {
128  return 1;
129  }
130  else {
131  return -1;
132  }
133  }
134  return 0;
135  };
136 
137  dump("------------------------------------- 3\n");
138  return tester;
139  break;
140 
141  case "getSortPropertyValueByIndex":
142 
143  var listener = new ArrayListener();
144  array.addAsyncListener(listener);
145  array.getSortPropertyValueByIndexAsync(0);
146 
147  var tester = {};
148  tester.listener = listener;
149  tester.func = function() {
150  if (listener.gotValue) {
151  if (listener.rv == Cr.NS_OK &&
152  listener.value == "ac black") {
153  return 1;
154  }
155  else {
156  return -1;
157  }
158  }
159  return 0;
160  };
161 
162  return tester;
163  break;
164 
165  case "getMediaItemIdByIndex":
166 
167  var listener = new ArrayListener();
168  array.addAsyncListener(listener);
169  array.getMediaItemIdByIndexAsync(0);
170 
171  var tester = {};
172  tester.listener = listener;
173  tester.func = function() {
174  if (listener.gotMediaItemId) {
175  if (listener.rv == Cr.NS_OK &&
176  listener.mediaItemId == testMediaItemId) {
177  return 1;
178  }
179  else {
180  return -1;
181  }
182  }
183  return 0;
184  };
185 
186  return tester;
187  break;
188  }
189 
190  // Shouldn't get here
191  var tester = {};
192  tester.func = function() { return -1; }
193  return tester;
194 }
195 
196 function TimerLoop(array) {
197  this._array = array;
198  this._timer = null;
199 }
200 
201 TimerLoop.prototype.notify = function(timer)
202 {
203  var shouldContinue = false;
204 
205  if (currentTester) {
206  var result = currentTester.func.apply(this);
207  if (result != 0) {
208  try {
209  this._array.removeAsyncListener(currentTester.listener);
210  }
211  catch (ex) {
212  log("exception: " + ex);
213  result = -1;
214  }
215  }
216 
217  if (result == 1) {
218  log("phase '" + phases[currentPhase] + "' passed");
220  currentPhase++;
221  shouldContinue = true;
222  }
223 
224  if (result == -1) {
226  failed = true;
227  shouldContinue = false;
228  }
229 
230  if (result == 0) {
231  shouldContinue = true;
232  }
233  }
234  else {
235  if (currentPhase < phases.length) {
236  try {
237  currentTester = doTest(this._array);
238  shouldContinue = true;
239  }
240  catch (ex) {
241  log("exception: " + ex);
242  failed = true;
243  shouldContinue = false;
244  }
245  }
246  }
247 
248  if (shouldContinue) {
249  if (iterations < 1000) {
250  iterations++;
251  this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
252  this._timer.initWithCallback(this, 10, Ci.nsITimer.TYPE_ONE_SHOT);
253  }
254  else {
255  this._array = null;
256  this._timer = null;
257  loop = null;
258  testFinished();
259  fail("maximum iterations reached");
260  }
261  }
262  else {
263  this._array = null;
264  this._timer = null;
265  loop = null;
266  testFinished();
267  if (failed) {
268  fail("phase '" + phases[currentPhase] + "'");
269  }
270  }
271 }
272 
273 var phases = ["getLengthAsync",
274  "getGuidByIndexAsync",
275  "getSortPropertyValueByIndex",
276  "getMediaItemIdByIndex"];
277 var currentPhase = 0;
279 var failed = false;
280 var iterations = 0;
281 var loop;
283 
284 function runTest () {
285 
286  var databaseGUID = "test_asyncguidarray";
287  var library = createLibrary(databaseGUID);
288 
289  testMediaItemId = library.QueryInterface(Ci.sbILocalDatabaseLibrary).getMediaItemIdForGuid("3E4FAFDA-AD99-11DB-9321-C22AB7121F49");
290 
291  var array = Cc["@songbirdnest.com/Songbird/Library/LocalDatabase/AsyncGUIDArray;1"]
292  .createInstance(Ci.sbILocalDatabaseAsyncGUIDArray);
293  array.databaseGUID = databaseGUID;
294  array.propertyCache =
295  library.QueryInterface(Ci.sbILocalDatabaseLibrary).propertyCache;
296  array.baseTable = "media_items";
297  array.addSort("http://songbirdnest.com/data/1.0#albumName", true);
298  array.addSort("http://songbirdnest.com/data/1.0#trackNumber", true);
299 
300  currentPhase = 0;
301 
302  loop = new TimerLoop(array);
303  loop.notify();
304  testPending();
305 }
306 
const Cc
function fail(aMessage)
inArray array
function log(s)
function testFinished()
function runTest()
Advanced DataRemote unit tests.
var testMediaItemId
sbDeviceFirmwareAutoCheckForUpdate prototype _timer
var iterations
var currentPhase
var loop
return null
Definition: FeedWriter.js:1143
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
function doTest(array)
var currentTester
countRef value
Definition: FeedWriter.js:1423
const Cr
const Ci
function ArrayListener()
Test file.
var failed
function TimerLoop(array)
function testPending()