services/metrics/modules-testing/mocks.jsm

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:89aba8b1195c
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 "use strict";
6
7 this.EXPORTED_SYMBOLS = [
8 "DummyMeasurement",
9 "DummyProvider",
10 "DummyConstantProvider",
11 "DummyPullOnlyThrowsOnInitProvider",
12 "DummyThrowOnInitProvider",
13 "DummyThrowOnShutdownProvider",
14 ];
15
16 const {utils: Cu} = Components;
17
18 Cu.import("resource://gre/modules/Promise.jsm");
19 Cu.import("resource://gre/modules/Metrics.jsm");
20 Cu.import("resource://gre/modules/Task.jsm");
21
22 this.DummyMeasurement = function DummyMeasurement(name="DummyMeasurement") {
23 this.name = name;
24
25 Metrics.Measurement.call(this);
26 }
27
28 DummyMeasurement.prototype = {
29 __proto__: Metrics.Measurement.prototype,
30
31 version: 1,
32
33 fields: {
34 "daily-counter": {type: Metrics.Storage.FIELD_DAILY_COUNTER},
35 "daily-discrete-numeric": {type: Metrics.Storage.FIELD_DAILY_DISCRETE_NUMERIC},
36 "daily-discrete-text": {type: Metrics.Storage.FIELD_DAILY_DISCRETE_TEXT},
37 "daily-last-numeric": {type: Metrics.Storage.FIELD_DAILY_LAST_NUMERIC},
38 "daily-last-text": {type: Metrics.Storage.FIELD_DAILY_LAST_TEXT},
39 "last-numeric": {type: Metrics.Storage.FIELD_LAST_NUMERIC},
40 "last-text": {type: Metrics.Storage.FIELD_LAST_TEXT},
41 },
42 };
43
44
45 this.DummyProvider = function DummyProvider(name="DummyProvider") {
46 Object.defineProperty(this, "name", {
47 value: name,
48 });
49
50 this.measurementTypes = [DummyMeasurement];
51
52 Metrics.Provider.call(this);
53
54 this.constantMeasurementName = "DummyMeasurement";
55 this.collectConstantCount = 0;
56 this.throwDuringCollectConstantData = null;
57 this.throwDuringConstantPopulate = null;
58
59 this.collectDailyCount = 0;
60
61 this.havePushedMeasurements = true;
62 }
63
64 DummyProvider.prototype = {
65 __proto__: Metrics.Provider.prototype,
66
67 name: "DummyProvider",
68
69 collectConstantData: function () {
70 this.collectConstantCount++;
71
72 if (this.throwDuringCollectConstantData) {
73 throw new Error(this.throwDuringCollectConstantData);
74 }
75
76 return this.enqueueStorageOperation(function doStorage() {
77 if (this.throwDuringConstantPopulate) {
78 throw new Error(this.throwDuringConstantPopulate);
79 }
80
81 let m = this.getMeasurement("DummyMeasurement", 1);
82 let now = new Date();
83 m.incrementDailyCounter("daily-counter", now);
84 m.addDailyDiscreteNumeric("daily-discrete-numeric", 1, now);
85 m.addDailyDiscreteNumeric("daily-discrete-numeric", 2, now);
86 m.addDailyDiscreteText("daily-discrete-text", "foo", now);
87 m.addDailyDiscreteText("daily-discrete-text", "bar", now);
88 m.setDailyLastNumeric("daily-last-numeric", 3, now);
89 m.setDailyLastText("daily-last-text", "biz", now);
90 m.setLastNumeric("last-numeric", 4, now);
91 return m.setLastText("last-text", "bazfoo", now);
92 }.bind(this));
93 },
94
95 collectDailyData: function () {
96 this.collectDailyCount++;
97
98 return Promise.resolve();
99 },
100 };
101
102
103 this.DummyConstantProvider = function () {
104 DummyProvider.call(this, this.name);
105 }
106
107 DummyConstantProvider.prototype = {
108 __proto__: DummyProvider.prototype,
109
110 name: "DummyConstantProvider",
111
112 pullOnly: true,
113 };
114
115 this.DummyThrowOnInitProvider = function () {
116 DummyProvider.call(this, "DummyThrowOnInitProvider");
117
118 throw new Error("Dummy Error");
119 };
120
121 this.DummyThrowOnInitProvider.prototype = {
122 __proto__: DummyProvider.prototype,
123
124 name: "DummyThrowOnInitProvider",
125 };
126
127 this.DummyPullOnlyThrowsOnInitProvider = function () {
128 DummyConstantProvider.call(this);
129
130 throw new Error("Dummy Error");
131 };
132
133 this.DummyPullOnlyThrowsOnInitProvider.prototype = {
134 __proto__: DummyConstantProvider.prototype,
135
136 name: "DummyPullOnlyThrowsOnInitProvider",
137 };
138
139 this.DummyThrowOnShutdownProvider = function () {
140 DummyProvider.call(this, "DummyThrowOnShutdownProvider");
141 };
142
143 this.DummyThrowOnShutdownProvider.prototype = {
144 __proto__: DummyProvider.prototype,
145
146 name: "DummyThrowOnShutdownProvider",
147
148 pullOnly: true,
149
150 onShutdown: function () {
151 throw new Error("Dummy shutdown error");
152 },
153 };
154

mercurial