test_device_firmware_handler.js
Go to the documentation of this file.
1 /* vim: set sw=2 :miv */
2 /*
3 //
4 // BEGIN SONGBIRD GPL
5 //
6 // This file is part of the Songbird web player.
7 //
8 // Copyright(c) 2005-2008 POTI, Inc.
9 // http://songbirdnest.com
10 //
11 // This file may be licensed under the terms of of the
12 // GNU General Public License Version 2 (the "GPL").
13 //
14 // Software distributed under the License is distributed
15 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
16 // express or implied. See the GPL for the specific language
17 // governing rights and limitations.
18 //
19 // You should have received a copy of the GPL along with this
20 // program. If not, go to http://www.gnu.org/licenses/gpl.html
21 // or write to the Free Software Foundation, Inc.,
22 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 //
24 // END SONGBIRD GPL
25 //
26 */
27 
28 
33 var TEST_FILE_LOCATION = "testharness/devicefirmware/files/";
35 var REMOTE_FILE_PREFIX = "http://localhost:" + PORT_NUMBER + "/files/";
36 
37 
43 function getTempFolder() {
44  if (gTempFolder) {
45  return gTempFolder;
46  }
47  gTempFolder = Components.classes["@mozilla.org/file/directory_service;1"]
48  .getService(Components.interfaces.nsIProperties)
49  .get("TmpD", Components.interfaces.nsIFile);
50  gTempFolder.append("songbird_metadata_tests.tmp");
51  gTempFolder.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0777);
52  return gTempFolder;
53 }
54 
55 
60 function getCopyOfFolder(folder, tempName) {
61  assertNotEqual(folder, null);
62  var tempFolder = getTempFolder();
63  folder.copyTo(tempFolder, tempName);
64  folder = tempFolder.clone();
65  folder.append(tempName);
66  assertEqual(folder.exists(), true);
67  return folder;
68 }
69 
70 
75 function runTest () {
76  // Setup the JS server to serve up the mock test handler files.
78  "_temp_firmware_files"));
79 
80  var jsHttpServer = Cc["@mozilla.org/server/jshttp;1"]
81  .createInstance(Ci.nsIHttpServer);
82  jsHttpServer.start(PORT_NUMBER);
83  jsHttpServer.registerDirectory("/", testFolder.clone());
84 
85  // Set the remote URL's for the firmware files via the JS HTTP server.
86  var handlerURLService = Cc["@songbirdnest.com/mock-firmware-url-handler;1"]
87  .getService(Ci.sbPIMockFirmwareHandlerURLService);
88 
89  handlerURLService.firmwareURL = REMOTE_FILE_PREFIX + "firmware.xml";
90  handlerURLService.resetURL = REMOTE_FILE_PREFIX + "reset.html";
91  handlerURLService.releaseNotesURL = REMOTE_FILE_PREFIX + "release_notes.html";
92  handlerURLService.supportURL = REMOTE_FILE_PREFIX + "support.html";
93  handlerURLService.registerURL = REMOTE_FILE_PREFIX + "register.html";
94 
95  var device = Components.classes["@songbirdnest.com/Songbird/Device/DeviceTester/MockDevice;1"]
96  .createInstance(Components.interfaces.sbIDevice);
97  assertEqual(device.name, "Bob's Mock Device");
98 
99  var updater = Components.classes["@songbirdnest.com/Songbird/Device/Firmware/Updater;1"]
100  .getService(Components.interfaces.sbIDeviceFirmwareUpdater);
101  assertTrue(updater.hasHandler(device, 0, 0));
102 
103  var handler = updater.getHandler(device, 0, 0);
104  assertNotEqual(handler, null);
105 
106  var listener = {
107  op: "",
108  firmwareUpdate: null,
109  onDeviceEvent: function(aEvent) {
110 
111  function eventTypeToString(aType) {
112  for (let type in Ci.sbIDeviceEvent) {
113  if (Ci.sbIDeviceEvent[type] === aType)
114  return type;
115  }
116  return "0x" + (Array(8).join("0") + (type>>>0).toString(16)).substr(-8);
117  }
118 
119  log("event operation: " + this.op);
120  log("event type: " + eventTypeToString(aEvent.type));
121  log("event origin: " + aEvent.origin);
122  log("event target: " + aEvent.target);
123 
124  var data = aEvent.data;
125  log("event data: " + data);
126 
127  switch(this.op) {
128  case "cfu":
129  if(aEvent.type == Ci.sbIDeviceEvent.EVENT_FIRMWARE_CFU_END) {
130  log("is update available: " + data);
131  testFinished();
132  }
133  break;
134 
135  case "du": {
136  if(aEvent.type == Ci.sbIDeviceEvent.EVENT_FIRMWARE_DOWNLOAD_START) {
137  log("firmware update download start");
138  }
139  else if(aEvent.type == Ci.sbIDeviceEvent.EVENT_FIRMWARE_DOWNLOAD_PROGRESS) {
140  log("firmware update download progress: " + data);
141  }
142  if(aEvent.type == Ci.sbIDeviceEvent.EVENT_FIRMWARE_DOWNLOAD_END) {
143  this.firmwareUpdate = data.QueryInterface(Ci.sbIDeviceFirmwareUpdate);
144  log("firmware update version: " + this.firmwareUpdate.firmwareReadableVersion);
145  log("firmware update file path: " + this.firmwareUpdate.firmwareImageFile.path);
146  testFinished();
147  }
148  }
149  break;
150 
151  case "au": {
152  if(aEvent.type == Ci.sbIDeviceEvent.EVENT_FIRMWARE_UPDATE_START) {
153  log("firmware apply update start");
154  }
155  else if(aEvent.type == Ci.sbIDeviceEvent.EVENT_FIRMWARE_WRITE_START) {
156  log("firmware write start");
157  }
158  else if(aEvent.type == Ci.sbIDeviceEvent.EVENT_FIRWMARE_WRITE_PROGRESS) {
159  log("firmware write progress: " + data);
160  }
161  else if(aEvent.type == Ci.sbIDeviceEvent.EVENT_FIRMWARE_WRITE_END) {
162  log("firmware write end");
163  }
164  else if(aEvent.type == Ci.sbIDeviceEvent.EVENT_FIRMWARE_UPDATE_END) {
165  log("firmware apply update end");
166  testFinished();
167  }
168  }
169  break;
170 
171  default:
172  log("unknown operation");
173  testFinished();
174  }
175  }
176  };
177 
178  try {
179  log("Testing 'checkForUpdate'");
180  listener.op = "cfu";
181 
182  var eventTarget = device.QueryInterface(Ci.sbIDeviceEventTarget);
183  eventTarget.addEventListener(listener);
184 
185  updater.checkForUpdate(device, 0, 0, null);
186  testPending();
187 
188  log("Testing 'downloadUpdate'");
189  listener.op = "du";
190 
191  updater.downloadUpdate(device, false, null);
192  testPending();
193 
194  log("Testing 'applyUpdate'");
195  listener.op = "au";
196 
197  updater.applyUpdate(device, listener.firmwareUpdate, null);
198  testPending();
199 
200  updater.finalizeUpdate(device);
201  }
202  finally {
203  eventTarget.removeEventListener(listener);
204  }
205 
206  jsHttpServer.stop(testFinished);
207  testPending();
208 }
const Cc
function newAppRelativeFile(path)
var testFolder
function assertNotEqual(aExpected, aActual, aMessage)
function log(s)
function testFinished()
function assertTrue(aTest, aMessage)
function runTest()
Device Firmware Tests.
function assertEqual(aExpected, aActual, aMessage)
return null
Definition: FeedWriter.js:1143
function getTempFolder()
const Ci
function getCopyOfFolder(folder, tempName)
Copy the given folder to tempName, returning an nsIFile for the new location.
function getTestServerPortNumber()
observe data
Definition: FeedWriter.js:1329
GstMessage gpointer data sbGStreamerMessageHandler * handler
function testPending()