tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

SearchUITestUtils.sys.mjs (4065B)


      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 const lazy = {};
      6 
      7 ChromeUtils.defineESModuleGetters(lazy, {
      8  BrowserSearchTelemetry:
      9    "moz-src:///browser/components/search/BrowserSearchTelemetry.sys.mjs",
     10  TelemetryTestUtils: "resource://testing-common/TelemetryTestUtils.sys.mjs",
     11  TestUtils: "resource://testing-common/TestUtils.sys.mjs",
     12 });
     13 
     14 /**
     15 * A class containing useful testing functions for Search UI based tests.
     16 */
     17 export const SearchUITestUtils = new (class {
     18  /**
     19   * The test scope that the test is running in.
     20   *
     21   * @type {object}
     22   */
     23  #testScope = null;
     24 
     25  /**
     26   * Sets the scope for the test.
     27   *
     28   * @param {object} testScope
     29   *   The global scope for the test.
     30   */
     31  init(testScope) {
     32    this.#testScope = testScope;
     33  }
     34 
     35  /**
     36   * Asserts that the Search Access Point (SAP) telemetry is reported correctly.
     37   * It assumes that the reported telemetry is from a single source and no other
     38   * reports for the probes are expected.
     39   *
     40   * You may need to clear telemetry before running the test.
     41   *
     42   * @param {object} expected
     43   * @param {?string} expected.engineId
     44   *   The identifier of the simulated application provided search engine. If it
     45   *   is not an application provided engine, do not specify this value.
     46   * @param {?string} expected.engineName
     47   *   The name of the search engine.
     48   * @param {?boolean} expected.overriddenByThirdParty
     49   *   Set to true if the simulated application provided search engine is being
     50   *   overridden by another engine.
     51   * @param {?string} expected.partnerCode
     52   *   The expected partner code. Only applicable to simulated application
     53   *   provided engines.
     54   * @param {?boolean} expected.expectLegacyTelemetry
     55   *   Whether the `SEARCH_COUNTS` legacy histogram is expected to be updated.
     56   *   Pass false if the SAP telemetry is expected to be recorded only by Glean.
     57   * @param {string} expected.source
     58   *   The source of the search (e.g. urlbar, contextmenu etc.).
     59   * @param {number} expected.count
     60   *   The expected count for the source.
     61   */
     62  async assertSAPTelemetry({
     63    engineId = null,
     64    engineName = "",
     65    overriddenByThirdParty = false,
     66    partnerCode = null,
     67    expectLegacyTelemetry = true,
     68    source,
     69    count,
     70  }) {
     71    await lazy.TestUtils.waitForCondition(() => {
     72      return Glean.sap.counts.testGetValue().length == count;
     73    }, "The correct number of events should have been reported for sap.counts");
     74 
     75    let expectedEvents = [];
     76    for (let i = 0; i < count; i++) {
     77      let expected = {
     78        provider_id: engineId ?? "other",
     79        provider_name: engineName,
     80        source: lazy.BrowserSearchTelemetry.KNOWN_SEARCH_SOURCES.get(source),
     81        overridden_by_third_party: overriddenByThirdParty.toString(),
     82      };
     83 
     84      if (partnerCode) {
     85        expected.partner_code = partnerCode;
     86      }
     87 
     88      expectedEvents.push(expected);
     89    }
     90 
     91    let sapEvent = Glean.sap.counts.testGetValue();
     92    this.#testScope.Assert.deepEqual(
     93      sapEvent.map(e => e.extra),
     94      expectedEvents,
     95      "Should have the expected event telemetry data for sap.counts"
     96    );
     97 
     98    let histogram = Services.telemetry.getKeyedHistogramById("SEARCH_COUNTS");
     99 
    100    let histogramKey = overriddenByThirdParty
    101      ? `${engineId}-addon.${source}`
    102      : `${engineId ? "" : "other-"}${engineName}.${source}`;
    103 
    104    let expectedSum;
    105    let expectedSnapshotKeys = [];
    106    if (expectLegacyTelemetry) {
    107      expectedSum = count;
    108      expectedSnapshotKeys = [histogramKey];
    109    }
    110 
    111    lazy.TelemetryTestUtils.assertKeyedHistogramSum(
    112      histogram,
    113      histogramKey,
    114      expectedSum
    115    );
    116    // Also ensure no other keys were set.
    117    let snapshot = histogram.snapshot();
    118    this.#testScope.Assert.deepEqual(
    119      Object.keys(snapshot),
    120      expectedSnapshotKeys,
    121      "Should have only the expected keys in the SEARCH_COUNTS histogram"
    122    );
    123  }
    124 })();