services/metrics/modules-testing/mocks.jsm
author Kartikaya Gupta <kgupta@mozilla.com>
Fri, 04 Jan 2013 16:32:58 -0500
changeset 117678 5a7b468178a28e1bd2b761f8781414f9b7c0c940
parent 113826 5901554be0faae6973827bd41752badb03bc1950
child 117842 f3f08413e8635fcb94f9365cb947a63ab92af796
permissions -rw-r--r--
Bug 826381 - Update more tests to go with cset 11f420dd6b47. r=fix-test

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

this.EXPORTED_SYMBOLS = [
  "DummyMeasurement",
  "DummyProvider",
];

const {utils: Cu} = Components;

Cu.import("resource://gre/modules/services/metrics/dataprovider.jsm");

this.DummyMeasurement = function DummyMeasurement(name="DummyMeasurement") {
  MetricsMeasurement.call(this, name, 2);
}
DummyMeasurement.prototype = {
  __proto__: MetricsMeasurement.prototype,

  fields: {
    "string": {
      type: "TYPE_STRING",
    },

    "uint32": {
      type: "TYPE_UINT32",
      optional: true,
    },
  },
};


this.DummyProvider = function DummyProvider(name="DummyProvider") {
  MetricsProvider.call(this, name);

  this.constantMeasurementName = "DummyMeasurement";
  this.collectConstantCount = 0;
  this.throwDuringCollectConstantMeasurements = null;
  this.throwDuringConstantPopulate = null;
}
DummyProvider.prototype = {
  __proto__: MetricsProvider.prototype,

  collectConstantMeasurements: function collectConstantMeasurements() {
    this.collectConstantCount++;

    let result = this.createResult();
    result.expectMeasurement(this.constantMeasurementName);

    result.populate = this._populateConstantResult.bind(this);

    if (this.throwDuringCollectConstantMeasurements) {
      throw new Error(this.throwDuringCollectConstantMeasurements);
    }

    return result;
  },

  _populateConstantResult: function _populateConstantResult(result) {
    if (this.throwDuringConstantPopulate) {
      throw new Error(this.throwDuringConstantPopulate);
    }

    this._log.debug("Populating constant measurement in DummyProvider.");
    result.addMeasurement(new DummyMeasurement(this.constantMeasurementName));

    result.setValue(this.constantMeasurementName, "string", "foo");
    result.setValue(this.constantMeasurementName, "uint32", 24);

    result.finish();
  },
};