summaryrefslogtreecommitdiffstats
path: root/browser/components/search/test/browser/telemetry/browser_search_telemetry_categorization_timing.js
blob: a43fae7ca248224bbb2f7b4e8f154d664f4ed01d (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/*
 * Checks that telemetry on the runtime performance of categorizing the SERP
 * works as normal.
 */

"use strict";

const TEST_PROVIDER_INFO = [
  {
    telemetryId: "example",
    searchPageRegexp:
      /^https:\/\/example.org\/browser\/browser\/components\/search\/test\/browser\/telemetry\/searchTelemetry(?:Ad)/,
    queryParamNames: ["s"],
    codeParamName: "abc",
    taggedCodes: ["ff"],
    followOnParamNames: ["a"],
    extraAdServersRegexps: [/^https:\/\/example\.com\/ad2?/],
    components: [
      {
        type: SearchSERPTelemetryUtils.COMPONENTS.AD_LINK,
        default: true,
      },
    ],
  },
];

add_setup(async function () {
  SearchSERPTelemetry.overrideSearchTelemetryForTests(TEST_PROVIDER_INFO);
  await waitForIdle();

  registerCleanupFunction(async () => {
    SearchSERPTelemetry.overrideSearchTelemetryForTests();
    resetTelemetry();
  });
});

add_task(async function test_tab_contains_measurement() {
  resetTelemetry();

  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    getSERPUrl("searchTelemetryAd_components_text.html")
  );
  await waitForPageWithAdImpressions();

  await Services.fog.testFlushAllChildren();
  Assert.ok(
    Glean.serp.adImpression.testGetValue().length,
    "Should have received ad impressions."
  );

  let durations = Glean.serp.categorizationDuration.testGetValue();
  Assert.ok(durations.sum > 0, "Sum should be more than 0.");

  BrowserTestUtils.removeTab(tab);
});

// If the user opened a SERP and closed it quickly or navigated away from it
// and no ad impressions were recorded, we shouldn't record a measurement.
add_task(async function test_before_ad_impressions_recorded() {
  resetTelemetry();

  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    getSERPUrl("searchTelemetryAd_components_text.html")
  );
  BrowserTestUtils.removeTab(tab);

  Assert.ok(
    !Glean.serp.adImpression.testGetValue(),
    "Should not have an ad impression."
  );

  await Services.fog.testFlushAllChildren();
  let durations = Glean.serp.categorizationDuration.testGetValue();
  Assert.equal(durations, undefined, "Should not have received any values.");
});