services/healthreport/modules-testing/utils.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 this.EXPORTED_SYMBOLS = [
michael@0 8 "getAppInfo",
michael@0 9 "updateAppInfo",
michael@0 10 "createFakeCrash",
michael@0 11 "InspectedHealthReporter",
michael@0 12 "getHealthReporter",
michael@0 13 ];
michael@0 14
michael@0 15
michael@0 16 const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
michael@0 17
michael@0 18 Cu.import("resource://gre/modules/Preferences.jsm");
michael@0 19 Cu.import("resource://gre/modules/Promise.jsm");
michael@0 20 Cu.import("resource://gre/modules/FileUtils.jsm");
michael@0 21 Cu.import("resource://gre/modules/osfile.jsm");
michael@0 22 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 23 Cu.import("resource://gre/modules/services-common/utils.js");
michael@0 24 Cu.import("resource://gre/modules/services/datareporting/policy.jsm");
michael@0 25 Cu.import("resource://gre/modules/services/healthreport/healthreporter.jsm");
michael@0 26 Cu.import("resource://gre/modules/services/healthreport/providers.jsm");
michael@0 27
michael@0 28
michael@0 29 let APP_INFO = {
michael@0 30 vendor: "Mozilla",
michael@0 31 name: "xpcshell",
michael@0 32 ID: "xpcshell@tests.mozilla.org",
michael@0 33 version: "1",
michael@0 34 appBuildID: "20121107",
michael@0 35 platformVersion: "p-ver",
michael@0 36 platformBuildID: "20121106",
michael@0 37 inSafeMode: false,
michael@0 38 logConsoleErrors: true,
michael@0 39 OS: "XPCShell",
michael@0 40 XPCOMABI: "noarch-spidermonkey",
michael@0 41 QueryInterface: XPCOMUtils.generateQI([Ci.nsIXULAppInfo, Ci.nsIXULRuntime]),
michael@0 42 invalidateCachesOnRestart: function() {},
michael@0 43 };
michael@0 44
michael@0 45
michael@0 46 /**
michael@0 47 * Obtain a reference to the current object used to define XULAppInfo.
michael@0 48 */
michael@0 49 this.getAppInfo = function () { return APP_INFO; }
michael@0 50
michael@0 51 /**
michael@0 52 * Update the current application info.
michael@0 53 *
michael@0 54 * If the argument is defined, it will be the object used. Else, APP_INFO is
michael@0 55 * used.
michael@0 56 *
michael@0 57 * To change the current XULAppInfo, simply call this function. If there was
michael@0 58 * a previously registered app info object, it will be unloaded and replaced.
michael@0 59 */
michael@0 60 this.updateAppInfo = function (obj) {
michael@0 61 obj = obj || APP_INFO;
michael@0 62 APP_INFO = obj;
michael@0 63
michael@0 64 let id = Components.ID("{fbfae60b-64a4-44ef-a911-08ceb70b9f31}");
michael@0 65 let cid = "@mozilla.org/xre/app-info;1";
michael@0 66 let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
michael@0 67
michael@0 68 // Unregister an existing factory if one exists.
michael@0 69 try {
michael@0 70 let existing = Components.manager.getClassObjectByContractID(cid, Ci.nsIFactory);
michael@0 71 registrar.unregisterFactory(id, existing);
michael@0 72 } catch (ex) {}
michael@0 73
michael@0 74 let factory = {
michael@0 75 createInstance: function (outer, iid) {
michael@0 76 if (outer != null) {
michael@0 77 throw Cr.NS_ERROR_NO_AGGREGATION;
michael@0 78 }
michael@0 79
michael@0 80 return obj.QueryInterface(iid);
michael@0 81 },
michael@0 82 };
michael@0 83
michael@0 84 registrar.registerFactory(id, "XULAppInfo", cid, factory);
michael@0 85 };
michael@0 86
michael@0 87 /**
michael@0 88 * Creates a fake crash in the Crash Reports directory.
michael@0 89 *
michael@0 90 * Currently, we just create a dummy file. A more robust implementation would
michael@0 91 * create something that actually resembles a crash report file.
michael@0 92 *
michael@0 93 * This is very similar to code in crashreporter/tests/browser/head.js.
michael@0 94 *
michael@0 95 * FUTURE consolidate code in a shared JSM.
michael@0 96 */
michael@0 97 this.createFakeCrash = function (submitted=false, date=new Date()) {
michael@0 98 let id = CommonUtils.generateUUID();
michael@0 99 let filename;
michael@0 100
michael@0 101 let paths = ["Crash Reports"];
michael@0 102 let mode;
michael@0 103
michael@0 104 if (submitted) {
michael@0 105 paths.push("submitted");
michael@0 106 filename = "bp-" + id + ".txt";
michael@0 107 mode = OS.Constants.libc.S_IRUSR | OS.Constants.libc.S_IWUSR |
michael@0 108 OS.Constants.libc.S_IRGRP | OS.Constants.libc.S_IROTH;
michael@0 109 } else {
michael@0 110 paths.push("pending");
michael@0 111 filename = id + ".dmp";
michael@0 112 mode = OS.Constants.libc.S_IRUSR | OS.Constants.libc.S_IWUSR;
michael@0 113 }
michael@0 114
michael@0 115 paths.push(filename);
michael@0 116
michael@0 117 let file = FileUtils.getFile("UAppData", paths, true);
michael@0 118 file.create(file.NORMAL_FILE_TYPE, mode);
michael@0 119 file.lastModifiedTime = date.getTime();
michael@0 120 dump("Created fake crash: " + id + "\n");
michael@0 121
michael@0 122 return id;
michael@0 123 };
michael@0 124
michael@0 125
michael@0 126 /**
michael@0 127 * A HealthReporter that is probed with various callbacks and counters.
michael@0 128 *
michael@0 129 * The purpose of this type is to aid testing of startup and shutdown.
michael@0 130 */
michael@0 131 this.InspectedHealthReporter = function (branch, policy, recorder, stateLeaf) {
michael@0 132 HealthReporter.call(this, branch, policy, recorder, stateLeaf);
michael@0 133
michael@0 134 this.onStorageCreated = null;
michael@0 135 this.onProviderManagerInitialized = null;
michael@0 136 this.providerManagerShutdownCount = 0;
michael@0 137 this.storageCloseCount = 0;
michael@0 138 }
michael@0 139
michael@0 140 InspectedHealthReporter.prototype = {
michael@0 141 __proto__: HealthReporter.prototype,
michael@0 142
michael@0 143 _onStorageCreated: function (storage) {
michael@0 144 if (this.onStorageCreated) {
michael@0 145 this.onStorageCreated(storage);
michael@0 146 }
michael@0 147
michael@0 148 return HealthReporter.prototype._onStorageCreated.call(this, storage);
michael@0 149 },
michael@0 150
michael@0 151 _initializeProviderManager: function () {
michael@0 152 for (let result of HealthReporter.prototype._initializeProviderManager.call(this)) {
michael@0 153 yield result;
michael@0 154 }
michael@0 155
michael@0 156 if (this.onInitializeProviderManagerFinished) {
michael@0 157 this.onInitializeProviderManagerFinished();
michael@0 158 }
michael@0 159 },
michael@0 160
michael@0 161 _onProviderManagerInitialized: function () {
michael@0 162 if (this.onProviderManagerInitialized) {
michael@0 163 this.onProviderManagerInitialized();
michael@0 164 }
michael@0 165
michael@0 166 return HealthReporter.prototype._onProviderManagerInitialized.call(this);
michael@0 167 },
michael@0 168
michael@0 169 _onProviderManagerShutdown: function () {
michael@0 170 this.providerManagerShutdownCount++;
michael@0 171
michael@0 172 return HealthReporter.prototype._onProviderManagerShutdown.call(this);
michael@0 173 },
michael@0 174
michael@0 175 _onStorageClose: function () {
michael@0 176 this.storageCloseCount++;
michael@0 177
michael@0 178 return HealthReporter.prototype._onStorageClose.call(this);
michael@0 179 },
michael@0 180 };
michael@0 181
michael@0 182 const DUMMY_URI="http://localhost:62013/";
michael@0 183
michael@0 184 this.getHealthReporter = function (name, uri=DUMMY_URI, inspected=false) {
michael@0 185 let branch = "healthreport.testing." + name + ".";
michael@0 186
michael@0 187 let prefs = new Preferences(branch + "healthreport.");
michael@0 188 prefs.set("documentServerURI", uri);
michael@0 189 prefs.set("dbName", name);
michael@0 190
michael@0 191 let reporter;
michael@0 192
michael@0 193 let policyPrefs = new Preferences(branch + "policy.");
michael@0 194 let policy = new DataReportingPolicy(policyPrefs, prefs, {
michael@0 195 onRequestDataUpload: function (request) {
michael@0 196 reporter.requestDataUpload(request);
michael@0 197 },
michael@0 198
michael@0 199 onNotifyDataPolicy: function (request) { },
michael@0 200
michael@0 201 onRequestRemoteDelete: function (request) {
michael@0 202 reporter.deleteRemoteData(request);
michael@0 203 },
michael@0 204 });
michael@0 205
michael@0 206 let type = inspected ? InspectedHealthReporter : HealthReporter;
michael@0 207 reporter = new type(branch + "healthreport.", policy, null,
michael@0 208 "state-" + name + ".json");
michael@0 209
michael@0 210 return reporter;
michael@0 211 };

mercurial