test_updateparams.js
Go to the documentation of this file.
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is mozilla.org code.
15  *
16  * The Initial Developer of the Original Code is
17  * Mozilla
18  *
19  * Portions created by the Initial Developer are Copyright (C) 2007
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK *****
37  */
38 
39 /*
40  * Original test file pulled from the mozilla source:
41  * mozilla/toolkit/mozapps/extensions/test/unit/test_bug384052.js
42  * and modified by:
43  * John Gaunt <redfive@songbirdnest.com>
44  */
45 
46 //const Cc = Components.classes;
47 //const Ci = Components.interfaces;
48 
49 const CLASS_ID = Components.ID("{12345678-1234-1234-1234-123456789abc}");
50 //const CONTRACT_ID = "@mozilla.org/test-parameter-source;1";
51 const CONTRACT_ID = "@songbirdnest.com/moz/sburlformatter;1";
52 
53 var gTestURL = "http://127.0.0.1:4444/?itemID=%ITEM_ID%&custom1=%CUSTOM1%&custom2=%CUSTOM2%";
55 var gSeenExpectedURL = false;
56 
57 var gComponentRegistrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
58 var gObserverService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
59 var gCategoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
60 
61 // Factory for our parameter handler
63  QueryInterface: function(iid) {
64  if (iid.equals(Ci.nsIFactory) || iid.equals(Ci.nsISupports))
65  return this;
66 
67  throw Components.results.NS_ERROR_NO_INTERFACE;
68  },
69 
70  createInstance: function(outer, iid) {
71  var bag = Cc["@mozilla.org/hash-property-bag;1"]
72  .createInstance(Ci.nsIWritablePropertyBag);
73  bag.setProperty("CUSTOM1", "custom_parameter_1");
74  bag.setProperty("CUSTOM2", "custom_parameter_2");
75  return bag.QueryInterface(iid);
76  }
77 };
78 
79 // Observer that should recognize when extension manager requests our URL
80 var observer = {
81  QueryInterface: function(iid) {
82  if (iid.equals(Ci.nsIObserver) || iid.equals(Ci.nsISupports))
83  return this;
84 
85  throw Components.results.NS_ERROR_NO_INTERFACE;
86  },
87 
88  observe: function(subject, topic, data) {
89  if (topic == "http-on-modify-request") {
90  var channel = subject.QueryInterface(Ci.nsIChannel);
91  if (channel.originalURI.spec == gExpectedURL) {
92  log("got notification,\n exp: " + gExpectedURL + "\n got: " + channel.originalURI.spec + "\n");
93  gSeenExpectedURL = true;
94  }
95  }
96  }
97 }
98 
99 function initTest()
100 {
101  // Setup extension manager
102  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9");
103  startupEM();
104 
105  // Register our parameter handlers
106 // gComponentRegistrar.registerFactory(CLASS_ID, "Test component", CONTRACT_ID, paramHandlerFactory);
107 // gCategoryManager.addCategoryEntry("extension-update-params", "CUSTOM1", CONTRACT_ID, false, false);
108 // gCategoryManager.addCategoryEntry("extension-update-params", "CUSTOM2", CONTRACT_ID, false, false);
109 
110  // Register observer to get notified when URLs are requested
111  gObserverService.addObserver(observer, "http-on-modify-request", false);
112 }
113 
114 function shutdownTest()
115 {
116  shutdownEM();
117 
118 // gComponentRegistrar.unregisterFactory(CLASS_ID, paramHandlerFactory);
119 // gCategoryManager.deleteCategoryEntry("extension-update-params", "CUSTOM1", false);
120 // gCategoryManager.deleteCategoryEntry("extension-update-params", "CUSTOM2", false);
121 
122  gObserverService.removeObserver(observer, "http-on-modify-request");
123 }
124 
125 //function run_test()
126 function runTest()
127 {
128  initTest();
129 
130  var item = Cc["@mozilla.org/updates/item;1"].createInstance(Ci.nsIUpdateItem);
131  item.init("test@mozilla.org", "1.0", "app-profile", "0.0", "100.0", "Test extension",
132  null, null, "", null, null, item.TYPE_EXTENSION, "xpcshell@tests.mozilla.org");
133 
134  gExpectedURL = gTestURL.replace(/%ITEM_ID%/, item.id)
135  .replace(/%CUSTOM1%/, "custom_parameter_1")
136  .replace(/%CUSTOM2%/, "custom_parameter_2");
137 
138  // Replace extension update URL
139  var origURL = null;
140  try {
141  origURL = prefs.getCharPref("extensions.update.url");
142  }
143  catch (e) {}
144  gPrefs.setCharPref("extensions.update.url", gTestURL);
145 
146  // Initiate update
147  gEM.update([item], 1, 2, null);
148 
149  //do_check_true(gSeenExpectedURL);
150  assertTrue(gSeenExpectedURL, "gSeenExpectedURL is FALSE");
151 
152  if (origURL)
153  gPrefs.setCharPref("extensions.update.url", origURL);
154  else
155  gPrefs.clearUserPref("extensions.update.url");
156 
157  shutdownTest();
158 }
const CONTRACT_ID
const Cc
const CLASS_ID
function log(s)
var gExpectedURL
sidebarFactory createInstance
Definition: nsSidebar.js:351
sbOSDControlService prototype QueryInterface
function assertTrue(aTest, aMessage)
function startupEM()
var gPrefs
var paramHandlerFactory
var gEM
var gCategoryManager
var gObserverService
var gSeenExpectedURL
return null
Definition: FeedWriter.js:1143
var prefs
Definition: FeedWriter.js:1169
function initTest()
function createAppInfo(id, name, version, platformVersion)
function shutdownEM()
observe topic
Definition: FeedWriter.js:1326
const Ci
observe data
Definition: FeedWriter.js:1329
var gTestURL
function runTest()
let observer
var gComponentRegistrar
sbDeviceFirmwareAutoCheckForUpdate prototype observe