tor-browser

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

browser_glean_telemetry_bounce.js (6652B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 /**
      7 * Tests bounce event .
      8 */
      9 
     10 const { Interactions } = ChromeUtils.importESModule(
     11  "moz-src:///browser/components/places/Interactions.sys.mjs"
     12 );
     13 
     14 const BOUNCE_THRESHOLD_SECONDS = 10;
     15 const ENGINE_ID =
     16  "other-browser_searchSuggestionEngine searchSuggestionEngine.xml";
     17 
     18 add_setup(async function () {
     19  await SpecialPowers.pushPrefEnv({
     20    set: [
     21      [
     22        "browser.urlbar.events.bounce.maxSecondsFromLastSearch",
     23        BOUNCE_THRESHOLD_SECONDS,
     24      ],
     25      ["browser.search.totalSearches", 0],
     26    ],
     27  });
     28 
     29  let root = gTestPath;
     30  let engineURL = new URL("../../browser/searchSuggestionEngine.xml", root)
     31    .href;
     32 
     33  await SearchTestUtils.installOpenSearchEngine({
     34    url: engineURL,
     35    setAsDefault: true,
     36  });
     37 
     38  registerCleanupFunction(async function () {
     39    Services.prefs.clearUserPref(
     40      "browser.urlbar.quickactions.timesShownOnboardingLabel"
     41    );
     42  });
     43 });
     44 
     45 const expected = {
     46  selected_result: "search_engine",
     47  results: "search_engine",
     48  n_results: "1",
     49  interaction: "typed",
     50  search_mode: "",
     51  search_engine_default_id:
     52    "other-browser_searchSuggestionEngine searchSuggestionEngine.xml",
     53  n_chars: "4",
     54  n_words: "1",
     55  engagement_type: "enter",
     56  provider: "UrlbarProviderHeuristicFallback",
     57  threshold: "10",
     58 };
     59 
     60 add_task(async function test_bounce_tab_close() {
     61  await doTest(async () => {
     62    let tab = await BrowserTestUtils.openNewForegroundTab(
     63      window.gBrowser,
     64      "example.com"
     65    );
     66 
     67    let browser = window.gBrowser.selectedBrowser;
     68    await openPopup("test");
     69    await doEnter();
     70 
     71    let state = gURLBar.controller.input.getBrowserState(browser);
     72    state.bounceEventTracking.startTime = 1000;
     73 
     74    const stub = sinon
     75      .stub(Interactions, "getRecentInteractionsForBrowser")
     76      .returns([
     77        { created_at: 500, totalViewTime: 300 },
     78        { created_at: 1500, totalViewTime: 200 },
     79        { created_at: 1700, totalViewTime: 1000 },
     80      ]);
     81 
     82    await BrowserTestUtils.removeTab(tab);
     83    await Interactions.interactionUpdatePromise;
     84 
     85    assertBounceTelemetry([
     86      {
     87        view_time: "1.2",
     88        selected_result: expected.selected_result,
     89        results: expected.results,
     90        n_results: expected.n_results,
     91        interaction: expected.interaction,
     92        search_mode: expected.search_mode,
     93        search_engine_default_id: expected.search_engine_default_id,
     94        n_chars: expected.n_chars,
     95        n_words: expected.n_words,
     96        engagement_type: expected.engagement_type,
     97        provider: expected.provider,
     98        threshold: expected.threshold,
     99      },
    100    ]);
    101 
    102    await PlacesUtils.history.clear();
    103    stub.restore();
    104  });
    105 });
    106 
    107 add_task(async function test_no_bounce() {
    108  await doTest(async () => {
    109    let tab = await BrowserTestUtils.openNewForegroundTab(
    110      window.gBrowser,
    111      "example.com"
    112    );
    113 
    114    let browser = window.gBrowser.selectedBrowser;
    115    await openPopup("test");
    116    await doEnter();
    117 
    118    let state = gURLBar.controller.input.getBrowserState(browser);
    119    state.bounceEventTracking.startTime = 1000;
    120 
    121    const stub = sinon
    122      .stub(Interactions, "getRecentInteractionsForBrowser")
    123      .returns([
    124        { created_at: 500, totalViewTime: 3000 },
    125        { created_at: 3500, totalViewTime: 8000 },
    126        { created_at: 11500, totalViewTime: 5000 },
    127      ]);
    128 
    129    await BrowserTestUtils.removeTab(tab);
    130    await Interactions.interactionUpdatePromise;
    131 
    132    assertBounceTelemetry([]);
    133 
    134    await PlacesUtils.history.clear();
    135    stub.restore();
    136  });
    137 });
    138 
    139 add_task(async function test_bounce_back_button() {
    140  await doTest(async () => {
    141    let tab = await BrowserTestUtils.openNewForegroundTab(
    142      window.gBrowser,
    143      "example.com"
    144    );
    145 
    146    let browser = window.gBrowser.selectedBrowser;
    147    await openPopup("test");
    148    await doEnter();
    149 
    150    let state = gURLBar.controller.input.getBrowserState(browser);
    151    state.bounceEventTracking.startTime = 1000;
    152 
    153    const stub = sinon
    154      .stub(Interactions, "getRecentInteractionsForBrowser")
    155      .returns([
    156        { created_at: 500, totalViewTime: 300 },
    157        { created_at: 1500, totalViewTime: 200 },
    158        { created_at: 1700, totalViewTime: 1000 },
    159      ]);
    160 
    161    gBrowser.goBack();
    162    await TestUtils.waitForCondition(
    163      () => browser.currentURI?.spec == "https://example.com/",
    164      "Waiting for previous page to load"
    165    );
    166 
    167    await Interactions.interactionUpdatePromise;
    168 
    169    assertBounceTelemetry([
    170      {
    171        view_time: "1.2",
    172        selected_result: expected.selected_result,
    173        results: expected.results,
    174        n_results: expected.n_results,
    175        interaction: expected.interaction,
    176        search_mode: expected.search_mode,
    177        search_engine_default_id: expected.search_engine_default_id,
    178        n_chars: expected.n_chars,
    179        n_words: expected.n_words,
    180        engagement_type: expected.engagement_type,
    181        provider: expected.provider,
    182        threshold: expected.threshold,
    183      },
    184    ]);
    185 
    186    stub.restore();
    187    await PlacesUtils.history.clear();
    188    await BrowserTestUtils.removeTab(tab);
    189  });
    190 });
    191 
    192 add_task(async function test_other_engagement() {
    193  await doTest(async () => {
    194    let tab = await BrowserTestUtils.openNewForegroundTab(
    195      window.gBrowser,
    196      "example.com"
    197    );
    198 
    199    let browser = window.gBrowser.selectedBrowser;
    200    await openPopup("test");
    201    await doEnter();
    202 
    203    let state = gURLBar.controller.input.getBrowserState(browser);
    204    state.bounceEventTracking.startTime = 1000;
    205 
    206    const stub = sinon
    207      .stub(Interactions, "getRecentInteractionsForBrowser")
    208      .returns([
    209        { created_at: 500, totalViewTime: 300 },
    210        { created_at: 1500, totalViewTime: 200 },
    211        { created_at: 1700, totalViewTime: 1000 },
    212      ]);
    213 
    214    await PlacesUtils.history.clear();
    215 
    216    await openPopup("test");
    217    await doEnter();
    218 
    219    await Interactions.interactionUpdatePromise;
    220 
    221    assertBounceTelemetry([
    222      {
    223        view_time: "1.2",
    224        selected_result: expected.selected_result,
    225        results: expected.results,
    226        n_results: expected.n_results,
    227        interaction: expected.interaction,
    228        search_mode: expected.search_mode,
    229        search_engine_default_id: expected.search_engine_default_id,
    230        n_chars: expected.n_chars,
    231        n_words: expected.n_words,
    232        engagement_type: expected.engagement_type,
    233        provider: expected.provider,
    234        threshold: expected.threshold,
    235      },
    236    ]);
    237 
    238    stub.restore();
    239    await BrowserTestUtils.removeTab(tab);
    240  });
    241 });