summaryrefslogtreecommitdiff
path: root/browser/base/content/test/browser_urlbar_search_healthreport.js
blob: 4315a98649ed630bfbd9a3e46f8d5cbccbafe51b (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

function test() {
  waitForExplicitFinish();
  try {
    let cm = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
    cm.getCategoryEntry("healthreport-js-provider-default", "SearchesProvider");
  } catch (ex) {
    // Health Report disabled, or no SearchesProvider.
    ok(true, "Firefox Health Report is not enabled.");
    finish();
    return;
  }

  let reporter = Cc["@mozilla.org/datareporting/service;1"]
                   .getService()
                   .wrappedJSObject
                   .healthReporter;
  ok(reporter, "Health Reporter available.");
  reporter.onInit().then(function onInit() {
    let provider = reporter.getProvider("org.mozilla.searches");
    ok(provider, "Searches provider is available.");
    let m = provider.getMeasurement("counts", 2);

    m.getValues().then(function onData(data) {
      let now = new Date();
      let oldCount = 0;

      // This will to be need changed if default search engine is not Google.
      let field = "google.urlbar";

      if (data.days.hasDay(now)) {
        let day = data.days.getDay(now);
        if (day.has(field)) {
          oldCount = day.get(field);
        }
      }

      let tab = gBrowser.addTab();
      gBrowser.selectedTab = tab;

      gURLBar.value = "firefox health report";
      gURLBar.handleCommand();

      executeSoon(() => executeSoon(() => {
        gBrowser.removeTab(tab);

        m.getValues().then(function onData(data) {
          ok(data.days.hasDay(now), "FHR has data for today.");
          let day = data.days.getDay(now);
          ok(day.has(field), "FHR has url bar count for today.");

          let newCount = day.get(field);

          is(newCount, oldCount + 1, "Exactly one search has been recorded.");
          finish();
        });
      }));
    });
  });
}