summaryrefslogtreecommitdiffstats
path: root/toolkit/components/telemetry/tests/unit/test_CoveragePing.js
blob: 7fd7da0baa3bc68f261f8f925bca2cee299a4352 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm");

const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");

const COVERAGE_VERSION = "2";

const COVERAGE_ENABLED_PREF = "toolkit.coverage.enabled";
const OPT_OUT_PREF = "toolkit.coverage.opt-out";
const ALREADY_RUN_PREF = `toolkit.coverage.already-run.v${COVERAGE_VERSION}`;
const COVERAGE_UUID_PREF = `toolkit.coverage.uuid.v${COVERAGE_VERSION}`;
const TELEMETRY_ENABLED_PREF = "datareporting.healthreport.uploadEnabled";
const REPORTING_ENDPOINT_BASE_PREF = "toolkit.coverage.endpoint.base";
const REPORTING_ENDPOINT = "submit/coverage/coverage";

Services.prefs.setIntPref("toolkit.coverage.log-level", 20);

add_task(async function setup() {
  let uuid = "test123";
  Services.prefs.setCharPref(COVERAGE_UUID_PREF, uuid);

  const server = new HttpServer();
  server.start(-1);
  const serverPort = server.identity.primaryPort;

  Services.prefs.setCharPref(
    REPORTING_ENDPOINT_BASE_PREF,
    `http://localhost:${serverPort}`
  );

  server.registerPathHandler(
    `/${REPORTING_ENDPOINT}/${COVERAGE_VERSION}/${uuid}`,
    (request, response) => {
      equal(request.method, "PUT");
      let telemetryEnabled = Services.prefs.getBoolPref(
        TELEMETRY_ENABLED_PREF,
        false
      );

      let requestBody = NetUtil.readInputStreamToString(
        request.bodyInputStream,
        request.bodyInputStream.available()
      );

      let resultObj = JSON.parse(requestBody);

      deepEqual(Object.keys(resultObj), [
        "appUpdateChannel",
        "osName",
        "osVersion",
        "telemetryEnabled",
      ]);

      if (telemetryEnabled) {
        ok(resultObj.telemetryEnabled);
      } else {
        ok(!resultObj.telemetryEnabled);
      }

      const response_body = "OK";
      response.bodyOutputStream.write(response_body, response_body.length);
      server.stop();
    }
  );

  // Trigger a proper telemetry init.
  do_get_profile(true);
  // Make sure we don't generate unexpected pings due to pref changes.
  await setEmptyPrefWatchlist();

  await TelemetryController.testSetup();
});

add_task(async function test_prefs() {
  // Telemetry reporting setting does not control this ping, but it
  // reported by this ping.
  Services.prefs.setBoolPref(TELEMETRY_ENABLED_PREF, false);

  // should not run if enabled pref is false
  Services.prefs.setBoolPref(COVERAGE_ENABLED_PREF, false);
  Services.prefs.setBoolPref(ALREADY_RUN_PREF, false);
  Services.prefs.setBoolPref(OPT_OUT_PREF, false);

  await TelemetryController.testReset();

  let alreadyRun = Services.prefs.getBoolPref(ALREADY_RUN_PREF, false);
  ok(!alreadyRun, "should not have run with enabled pref false");

  // should not run if opt-out pref is true
  Services.prefs.setBoolPref(COVERAGE_ENABLED_PREF, true);
  Services.prefs.setBoolPref(ALREADY_RUN_PREF, false);
  Services.prefs.setBoolPref(OPT_OUT_PREF, true);

  await TelemetryController.testReset();

  // should run if opt-out pref is false and coverage is enabled
  Services.prefs.setBoolPref(COVERAGE_ENABLED_PREF, true);
  Services.prefs.setBoolPref(ALREADY_RUN_PREF, false);
  Services.prefs.setBoolPref(OPT_OUT_PREF, false);

  await TelemetryController.testReset();

  // the telemetry setting should be set correctly
  Services.prefs.setBoolPref(TELEMETRY_ENABLED_PREF, true);

  await TelemetryController.testReset();

  alreadyRun = Services.prefs.getBoolPref(ALREADY_RUN_PREF, false);

  ok(alreadyRun, "should run if no opt-out and enabled");
});