1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/services/metrics/modules-testing/mocks.jsm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,154 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +this.EXPORTED_SYMBOLS = [ 1.11 + "DummyMeasurement", 1.12 + "DummyProvider", 1.13 + "DummyConstantProvider", 1.14 + "DummyPullOnlyThrowsOnInitProvider", 1.15 + "DummyThrowOnInitProvider", 1.16 + "DummyThrowOnShutdownProvider", 1.17 +]; 1.18 + 1.19 +const {utils: Cu} = Components; 1.20 + 1.21 +Cu.import("resource://gre/modules/Promise.jsm"); 1.22 +Cu.import("resource://gre/modules/Metrics.jsm"); 1.23 +Cu.import("resource://gre/modules/Task.jsm"); 1.24 + 1.25 +this.DummyMeasurement = function DummyMeasurement(name="DummyMeasurement") { 1.26 + this.name = name; 1.27 + 1.28 + Metrics.Measurement.call(this); 1.29 +} 1.30 + 1.31 +DummyMeasurement.prototype = { 1.32 + __proto__: Metrics.Measurement.prototype, 1.33 + 1.34 + version: 1, 1.35 + 1.36 + fields: { 1.37 + "daily-counter": {type: Metrics.Storage.FIELD_DAILY_COUNTER}, 1.38 + "daily-discrete-numeric": {type: Metrics.Storage.FIELD_DAILY_DISCRETE_NUMERIC}, 1.39 + "daily-discrete-text": {type: Metrics.Storage.FIELD_DAILY_DISCRETE_TEXT}, 1.40 + "daily-last-numeric": {type: Metrics.Storage.FIELD_DAILY_LAST_NUMERIC}, 1.41 + "daily-last-text": {type: Metrics.Storage.FIELD_DAILY_LAST_TEXT}, 1.42 + "last-numeric": {type: Metrics.Storage.FIELD_LAST_NUMERIC}, 1.43 + "last-text": {type: Metrics.Storage.FIELD_LAST_TEXT}, 1.44 + }, 1.45 +}; 1.46 + 1.47 + 1.48 +this.DummyProvider = function DummyProvider(name="DummyProvider") { 1.49 + Object.defineProperty(this, "name", { 1.50 + value: name, 1.51 + }); 1.52 + 1.53 + this.measurementTypes = [DummyMeasurement]; 1.54 + 1.55 + Metrics.Provider.call(this); 1.56 + 1.57 + this.constantMeasurementName = "DummyMeasurement"; 1.58 + this.collectConstantCount = 0; 1.59 + this.throwDuringCollectConstantData = null; 1.60 + this.throwDuringConstantPopulate = null; 1.61 + 1.62 + this.collectDailyCount = 0; 1.63 + 1.64 + this.havePushedMeasurements = true; 1.65 +} 1.66 + 1.67 +DummyProvider.prototype = { 1.68 + __proto__: Metrics.Provider.prototype, 1.69 + 1.70 + name: "DummyProvider", 1.71 + 1.72 + collectConstantData: function () { 1.73 + this.collectConstantCount++; 1.74 + 1.75 + if (this.throwDuringCollectConstantData) { 1.76 + throw new Error(this.throwDuringCollectConstantData); 1.77 + } 1.78 + 1.79 + return this.enqueueStorageOperation(function doStorage() { 1.80 + if (this.throwDuringConstantPopulate) { 1.81 + throw new Error(this.throwDuringConstantPopulate); 1.82 + } 1.83 + 1.84 + let m = this.getMeasurement("DummyMeasurement", 1); 1.85 + let now = new Date(); 1.86 + m.incrementDailyCounter("daily-counter", now); 1.87 + m.addDailyDiscreteNumeric("daily-discrete-numeric", 1, now); 1.88 + m.addDailyDiscreteNumeric("daily-discrete-numeric", 2, now); 1.89 + m.addDailyDiscreteText("daily-discrete-text", "foo", now); 1.90 + m.addDailyDiscreteText("daily-discrete-text", "bar", now); 1.91 + m.setDailyLastNumeric("daily-last-numeric", 3, now); 1.92 + m.setDailyLastText("daily-last-text", "biz", now); 1.93 + m.setLastNumeric("last-numeric", 4, now); 1.94 + return m.setLastText("last-text", "bazfoo", now); 1.95 + }.bind(this)); 1.96 + }, 1.97 + 1.98 + collectDailyData: function () { 1.99 + this.collectDailyCount++; 1.100 + 1.101 + return Promise.resolve(); 1.102 + }, 1.103 +}; 1.104 + 1.105 + 1.106 +this.DummyConstantProvider = function () { 1.107 + DummyProvider.call(this, this.name); 1.108 +} 1.109 + 1.110 +DummyConstantProvider.prototype = { 1.111 + __proto__: DummyProvider.prototype, 1.112 + 1.113 + name: "DummyConstantProvider", 1.114 + 1.115 + pullOnly: true, 1.116 +}; 1.117 + 1.118 +this.DummyThrowOnInitProvider = function () { 1.119 + DummyProvider.call(this, "DummyThrowOnInitProvider"); 1.120 + 1.121 + throw new Error("Dummy Error"); 1.122 +}; 1.123 + 1.124 +this.DummyThrowOnInitProvider.prototype = { 1.125 + __proto__: DummyProvider.prototype, 1.126 + 1.127 + name: "DummyThrowOnInitProvider", 1.128 +}; 1.129 + 1.130 +this.DummyPullOnlyThrowsOnInitProvider = function () { 1.131 + DummyConstantProvider.call(this); 1.132 + 1.133 + throw new Error("Dummy Error"); 1.134 +}; 1.135 + 1.136 +this.DummyPullOnlyThrowsOnInitProvider.prototype = { 1.137 + __proto__: DummyConstantProvider.prototype, 1.138 + 1.139 + name: "DummyPullOnlyThrowsOnInitProvider", 1.140 +}; 1.141 + 1.142 +this.DummyThrowOnShutdownProvider = function () { 1.143 + DummyProvider.call(this, "DummyThrowOnShutdownProvider"); 1.144 +}; 1.145 + 1.146 +this.DummyThrowOnShutdownProvider.prototype = { 1.147 + __proto__: DummyProvider.prototype, 1.148 + 1.149 + name: "DummyThrowOnShutdownProvider", 1.150 + 1.151 + pullOnly: true, 1.152 + 1.153 + onShutdown: function () { 1.154 + throw new Error("Dummy shutdown error"); 1.155 + }, 1.156 +}; 1.157 +