test_faceplateManager.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 
35 var faceplateManager = Cc['@songbirdnest.com/faceplate/manager;1']
36  .getService(Ci.sbIFaceplateManager);
37 
38 var panes = [];
39 
44 var faceplateManagerListener = {
45 
46  expectCreateID: null,
47  expectShowID: null,
48  expectDestroyID: null,
49 
50  onCreatePane: function(pane) {
51  assertEqual(pane.id, this.expectCreateID);
52  this.expectCreateID = null;
53  },
54 
55  onShowPane: function(pane) {
56  assertEqual(pane.id, this.expectShowID);
57  this.expectShowID = null;
58  },
59 
60  onDestroyPane: function(pane) {
61  assertEqual(pane.id, this.expectDestroyID);
62  this.expectDestroyID = null;
63  },
64 
65  QueryInterface: function(iid) {
66  if (!iid.equals(Components.interfaces.sbIFeathersChangeListener))
67  throw Components.results.NS_ERROR_NO_INTERFACE;
68  return this;
69  }
70 }
71 
72 
73 
77 function runTest () {
78 
79  // Clear out the default panes so that we can test an empty
80  // faceplate manager
81  clearManager();
82 
83  assertEqual(faceplateManager.paneCount, 0);
84 
85  faceplateManager.addListener(faceplateManagerListener);
86 
87  testCreatePanes();
88 
89  testAccessors();
90 
91  testFaceplatePaneData();
92 
93  testShowPane();
94 
95  testDestroyPanes();
96 
97  // Make sure unregistering the listener actually works
98  faceplateManager.removeListener(faceplateManagerListener);
99  faceplateManagerListener.expectCreateID = null;
100  faceplateManagerListener.expectDestroyID = null;
101  var pane = faceplateManager.createPane("test", "test1", "chrome:test1");
102  faceplateManager.destroyPane(pane);
103 
104  assertEqual(faceplateManager.paneCount, 0);
105 
106  return Components.results.NS_OK;
107 }
108 
109 
111 // Tests //
113 
114 
115 function testCreatePanes() {
116  for (var i = 0; i < 3; i++) {
117  var id = "testpane" + i;
118 
119  // Test creation listener
120  faceplateManagerListener.expectCreateID = id;
121 
122  var pane = faceplateManager.createPane(id, "test1", "chrome:test1");
123  assertEqual(pane.id, id);
124  assertEqual(pane.name, "test1");
125  assertEqual(pane.bindingURL, "chrome:test1");
126  panes.push(pane);
127 
128  // Make sure the listener fired
129  assertEqual(faceplateManagerListener.expectCreateID, null);
130  }
131 }
132 
133 
134 function testAccessors() {
135  // Test paneCount
136  assertEqual(faceplateManager.paneCount, panes.length);
137 
138  // Test getPanes()
139  var enumerator = wrapEnumerator(faceplateManager.getPanes(),
140  Ci.sbIFaceplatePane);
141  assertEnumeratorEqualsArray(enumerator, panes);
142 
143  // Test getPane(id)
144  for (var i = 0; i < panes.length; i++) {
145  var pane = faceplateManager.getPane(panes[i].id);
146  assertEqual(pane.id, panes[i].id);
147  }
148 }
149 
150 
151 function testShowPane() {
152  faceplateManagerListener.expectShowID = panes[0].id;
153 
154  faceplateManager.showPane(panes[0]);
155 
156  // Make sure the listener fired
157  assertEqual(faceplateManagerListener.expectShowID, null);
158 
159  // Test getDefaultPane.
160  var pane = faceplateManager.getDefaultPane();
161  assertEqual(pane.id, panes[0].id);
162 }
163 
164 
165 function testDestroyPanes() {
166  for (var i = 0; i < panes.length; i++) {
167  // Test listener
168  faceplateManagerListener.expectDestroyID = panes[i].id;
169 
170  faceplateManager.destroyPane(panes[i]);
171 
172  // Make sure the listener fired
173  assertEqual(faceplateManagerListener.expectDestroyID, null);
174  }
175  assertEqual(faceplateManager.paneCount, 0);
176  panes = [];
177 }
178 
179 
180 function testFaceplatePaneData() {
181  var pane = panes[0];
182 
183  // Observer that asserts expected values
184  var observer = {
185  expectKey: null,
186  expectValue: null,
187  observe: function(subject, topic, data) {
188  assertEqual(subject.id, pane.id);
189  assertEqual(topic, this.expectKey);
190  assertEqual(pane.getData(topic), this.expectValue);
191  }
192  };
193  pane.addObserver(observer);
194 
195  // Test set/get/observe
196  var keys = ["test1", "test2"];
197  observer.expectKey = keys[0];
198  observer.expectValue = "success1";
199  pane.setData(observer.expectKey, observer.expectValue);
200  observer.expectKey = keys[1];
201  observer.expectValue = "success2";
202  pane.setData(observer.expectKey, observer.expectValue);
203 
204  // Test enumerate
205  var enumerator = wrapStringEnumerator(pane.getKeys());
206  assertEnumeratorEqualsArray(enumerator, keys);
207 
208  // Make sure removing the observer works as it should
209  pane.removeObserver(observer);
210  observer.expectKey = null;
211  pane.setData("test", "blah");
212 }
213 
215 // Helpers //
217 
221 function wrapEnumerator(enumerator, iface)
222 {
223  while (enumerator.hasMoreElements()) {
224  yield enumerator.getNext().QueryInterface(iface);
225  }
226 }
227 
231 function wrapStringEnumerator(enumerator)
232 {
233  while (enumerator.hasMore()) {
234  yield enumerator.getNext();
235  }
236 }
237 
238 
243 function assertEnumeratorEqualsArray(enumerator, list) {
244  list = list.concat(); // Clone list before modifying
245  for (var item in enumerator) {
246  assertEqual(list.indexOf(item) > -1, true);
247  list.splice(list.indexOf(item), 1);
248  }
249  assertEqual(list.length, 0);
250 }
251 
255 function clearManager () {
256  var enumerator = wrapEnumerator(faceplateManager.getPanes(), Ci.sbIFaceplatePane);
257  for (pane in enumerator) {
258  faceplateManager.destroyPane(pane);
259  }
260 }
261 
262 
263 
const Cc
menuItem id
Definition: FeedWriter.js:971
var faceplateManager
FaceplateManager test file.
sbOSDControlService prototype QueryInterface
function assertEqual(aExpected, aActual, aMessage)
return null
Definition: FeedWriter.js:1143
observe topic
Definition: FeedWriter.js:1326
const Ci
observe data
Definition: FeedWriter.js:1329
function runTest()
_getSelectedPageStyle s i
let observer
sbDeviceFirmwareAutoCheckForUpdate prototype observe