test_BaseMediacoreEventTarget.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 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
32 Components.utils.import("resource://app/jsmodules/ArrayConverter.jsm");
33 Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
34 Components.utils.import("resource://app/jsmodules/sbLibraryUtils.jsm");
35 
36 function testListener() {
37  this.wrappedJSObject = this;
38  this.log = [];
39  this.onEnd = null;
40 }
41 
42 testListener.prototype = {
43  QueryInterface: XPCOMUtils.generateQI([Ci.sbIMediacoreEventListener]),
44  onMediacoreEvent: function(event) {
45  this.log.push(event);
46  if (this.onEnd && event.type == Ci.sbIMediacoreEvent.STREAM_END) {
47  this.onEnd();
48  testFinished();
49  }
50  }
51 }
52 
53 function dummyCore() {
54  this.wrappedJSObject = this;
55 }
56 
57 dummyCore.prototype = {
58  QueryInterface: XPCOMUtils.generateQI([Ci.sbIMediacore]),
59 }
60 
61 var eventIDs = [Ci.sbIMediacoreEvent.METADATA_CHANGE,
62  Ci.sbIMediacoreEvent.URI_CHANGE,
63  Ci.sbIMediacoreEvent.DURATION_CHANGE,
64  Ci.sbIMediacoreEvent.VOLUME_CHANGE,
65  Ci.sbIMediacoreEvent.MUTE_CHANGE,
66  Ci.sbIMediacoreEvent.STREAM_FOUND,
67  Ci.sbIMediacoreEvent.BUFFERING,
68  Ci.sbIMediacoreEvent.BUFFER_UNDERRUN,
69  Ci.sbIMediacoreEvent.STREAM_START,
70  Ci.sbIMediacoreEvent.STREAM_PAUSE,
71  Ci.sbIMediacoreEvent.STREAM_END];
72 
73 function createEventTarget() {
74  return Cc["@songbirdnest.com/mediacore/sbTestDummyMediacoreManager;1"]
75  .createInstance(Ci.sbIMediacoreEventTarget);
76 }
77 
78 function createEvent(type, error, data) {
79  var creator = Cc["@songbirdnest.com/Songbird/Mediacore/Manager;1"]
80  .getService(Ci.sbIMediacoreManager);
81  return creator.createEvent(type, new dummyCore(), error, data);
82 }
83 
87 function runTest () {
88  log("Synchronous\n");
89  testSimpleListener(false);
90 
91  log("Asynchronous\n");
92  testSimpleListener(true);
93  testPending();
94 
95  eventTarget.removeListener(listener);
96 
97  listener = null;
98  eventTarget = null;
99 }
100 
103 
104 function testSimpleListener(async) {
105 
106  if(!eventTarget) {
107  eventTarget = createEventTarget();
108  }
109 
110  if(!listener) {
111  listener = new testListener();
112  }
113 
114  eventTarget.addListener(listener);
115 
116  for (index = 0; index < eventIDs.length; ++index) {
117  var event = createEvent(eventIDs[index], null, null);
118  eventTarget.dispatchEvent(event, async);
119  }
120  var testValues = function() {
121  assertEqual(listener.log.length, eventIDs.length, "event received count not equal to events sent");
122  for (index = 0; index < eventIDs.length; ++index) {
123  assertEqual(listener.log[index].type, eventIDs[index], "Event type doesn't match event ID sent");
124  }
125  }
126  if (async) {
127  listener.onEnd = testValues;
128  }
129  else {
130  testValues();
131  listener.log = [];
132  eventTarget.removeListener(listener);
133  }
134 }
135 
const Cc
function log(s)
function testFinished()
function createEventTarget()
var event
sbOSDControlService prototype QueryInterface
function assertEqual(aExpected, aActual, aMessage)
function createEvent(type, error, data)
return null
Definition: FeedWriter.js:1143
function testSimpleListener(async)
const Ci
function runTest()
Advanced DataRemote unit tests.
observe data
Definition: FeedWriter.js:1329
function testListener()
function testPending()