tor-browser

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

test_quicksuggest_yelp_realtime.js (12362B)


      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 // Tests Market suggestions.
      6 
      7 "use strict";
      8 
      9 const OPT_IN_REMOTE_SETTINGS = [
     10  {
     11    type: "dynamic-suggestions",
     12    suggestion_type: "yelpRealtime_opt_in",
     13    attachment: [
     14      {
     15        keywords: ["coffee"],
     16      },
     17    ],
     18  },
     19 ];
     20 
     21 const YELP_MERINO_SINGLE = [
     22  {
     23    provider: "yelp",
     24    is_sponsored: true,
     25    custom_details: {
     26      yelp: {
     27        values: [
     28          {
     29            some_value: "foo",
     30          },
     31        ],
     32      },
     33    },
     34  },
     35 ];
     36 
     37 add_setup(async function init() {
     38  // Disable search suggestions so we don't hit the network.
     39  await Services.search.init();
     40  Services.prefs.setBoolPref("browser.search.suggest.enabled", false);
     41 
     42  await QuickSuggestTestUtils.ensureQuickSuggestInit({
     43    remoteSettingsRecords: OPT_IN_REMOTE_SETTINGS,
     44    merinoSuggestions: YELP_MERINO_SINGLE,
     45    prefs: [
     46      ["yelpRealtime.featureGate", true],
     47      ["suggest.yelpRealtime", true],
     48      ["suggest.quicksuggest.sponsored", true],
     49    ],
     50  });
     51 });
     52 
     53 add_task(async function telemetry_type_on_realtime() {
     54  Assert.equal(
     55    QuickSuggest.getFeature(
     56      "YelpRealtimeSuggestions"
     57    ).getSuggestionTelemetryType({}),
     58    "yelpRealtime",
     59    "Telemetry type should be 'yelpRealtime'"
     60  );
     61 });
     62 
     63 add_task(async function disabledPrefs() {
     64  let prefs = [
     65    "quicksuggest.enabled",
     66    "suggest.yelpRealtime",
     67    "suggest.quicksuggest.all",
     68  ];
     69 
     70  for (let pref of prefs) {
     71    info("Testing pref: " + pref);
     72 
     73    // First make sure the suggestion is added.
     74    await check_results({
     75      context: createContext("coffee", {
     76        providers: [UrlbarProviderQuickSuggest.name],
     77        isPrivate: false,
     78      }),
     79      matches: [yelpMerinoResult()],
     80    });
     81 
     82    // Now disable them.
     83    UrlbarPrefs.set(pref, false);
     84    await check_results({
     85      context: createContext("coffee", {
     86        providers: [UrlbarProviderQuickSuggest.name],
     87        isPrivate: false,
     88      }),
     89      matches: [],
     90    });
     91 
     92    // Revert.
     93    UrlbarPrefs.set(pref, true);
     94    await QuickSuggestTestUtils.forceSync();
     95  }
     96 });
     97 
     98 add_task(async function not_interested_on_realtime() {
     99  await doDismissAllTest({
    100    result: yelpMerinoResult(),
    101    command: "not_interested",
    102    feature: QuickSuggest.getFeature("YelpRealtimeSuggestions"),
    103    pref: "suggest.yelpRealtime",
    104    queries: [{ query: "coffee" }],
    105  });
    106 });
    107 
    108 add_task(async function show_less_frequently_on_realtime() {
    109  UrlbarPrefs.clear("yelpRealtime.showLessFrequentlyCount");
    110  UrlbarPrefs.clear("yelpRealtime.minKeywordLength");
    111 
    112  let cleanUpNimbus = await UrlbarTestUtils.initNimbusFeature({
    113    realtimeMinKeywordLength: 0,
    114    realtimeShowLessFrequentlyCap: 3,
    115  });
    116 
    117  let result = yelpMerinoResult();
    118 
    119  const testData = [
    120    {
    121      input: "cof",
    122      before: {
    123        canShowLessFrequently: true,
    124        showLessFrequentlyCount: 0,
    125        minKeywordLength: 0,
    126      },
    127      after: {
    128        canShowLessFrequently: true,
    129        showLessFrequentlyCount: 1,
    130        minKeywordLength: 4,
    131      },
    132    },
    133    {
    134      input: "coffe",
    135      before: {
    136        canShowLessFrequently: true,
    137        showLessFrequentlyCount: 1,
    138        minKeywordLength: 4,
    139      },
    140      after: {
    141        canShowLessFrequently: true,
    142        showLessFrequentlyCount: 2,
    143        minKeywordLength: 6,
    144      },
    145    },
    146    {
    147      input: "coffee",
    148      before: {
    149        canShowLessFrequently: true,
    150        showLessFrequentlyCount: 2,
    151        minKeywordLength: 6,
    152      },
    153      after: {
    154        canShowLessFrequently: false,
    155        showLessFrequentlyCount: 3,
    156        minKeywordLength: 7,
    157      },
    158    },
    159  ];
    160 
    161  for (let { input, before, after } of testData) {
    162    let feature = QuickSuggest.getFeature("YelpRealtimeSuggestions");
    163 
    164    await check_results({
    165      context: createContext(input, {
    166        providers: [UrlbarProviderQuickSuggest.name],
    167        isPrivate: false,
    168      }),
    169      matches: [result],
    170    });
    171 
    172    Assert.equal(
    173      UrlbarPrefs.get("yelpRealtime.minKeywordLength"),
    174      before.minKeywordLength
    175    );
    176    Assert.equal(feature.canShowLessFrequently, before.canShowLessFrequently);
    177    Assert.equal(
    178      feature.showLessFrequentlyCount,
    179      before.showLessFrequentlyCount
    180    );
    181 
    182    triggerCommand({
    183      result,
    184      feature,
    185      command: "show_less_frequently",
    186      searchString: input,
    187    });
    188 
    189    Assert.equal(
    190      UrlbarPrefs.get("yelpRealtime.minKeywordLength"),
    191      after.minKeywordLength
    192    );
    193    Assert.equal(feature.canShowLessFrequently, after.canShowLessFrequently);
    194    Assert.equal(
    195      feature.showLessFrequentlyCount,
    196      after.showLessFrequentlyCount
    197    );
    198 
    199    await check_results({
    200      context: createContext(input, {
    201        providers: [UrlbarProviderQuickSuggest.name],
    202        isPrivate: false,
    203      }),
    204      matches: [],
    205    });
    206  }
    207 
    208  await cleanUpNimbus();
    209  UrlbarPrefs.clear("yelpRealtime.showLessFrequentlyCount");
    210  UrlbarPrefs.clear("yelpRealtime.minKeywordLength");
    211 });
    212 
    213 add_task(async function opt_in_on_opt_in_prompt() {
    214  setupOptInPromptTest();
    215 
    216  let input = "coffee";
    217  let optInResult = yelpOptInResult();
    218  let merinoResult = yelpMerinoResult();
    219 
    220  info("Confirm whether the opt-in result is shown");
    221  await check_results({
    222    context: createContext(input, {
    223      providers: [UrlbarProviderQuickSuggest.name],
    224      isPrivate: false,
    225    }),
    226    matches: [optInResult],
    227  });
    228 
    229  info("Trigger opt_in command");
    230  triggerCommand({
    231    result: optInResult,
    232    feature: QuickSuggest.getFeature("YelpRealtimeSuggestions"),
    233    command: "opt_in",
    234    searchString: input,
    235    expectedCountsByCall: {
    236      startQuery: 1,
    237    },
    238  });
    239 
    240  info("Check the prefs after triggering");
    241  Assert.ok(UrlbarPrefs.get("quicksuggest.online.enabled"));
    242 
    243  info("Check the result after triggering");
    244  await check_results({
    245    context: createContext(input, {
    246      providers: [UrlbarProviderQuickSuggest.name],
    247      isPrivate: false,
    248    }),
    249    matches: [merinoResult],
    250  });
    251 });
    252 
    253 add_task(async function not_now_on_opt_in_prompt() {
    254  setupOptInPromptTest();
    255 
    256  let input = "coffee";
    257  let beforeNotNowResult = yelpOptInResult();
    258  let afterNotNowResult = yelpOptInResult({ dismissButton: true });
    259 
    260  info("Confirm whether the opt-in result is shown");
    261  await check_results({
    262    context: createContext(input, {
    263      providers: [UrlbarProviderQuickSuggest.name],
    264      isPrivate: false,
    265    }),
    266    matches: [beforeNotNowResult],
    267  });
    268 
    269  info("Trigger not_now command");
    270  triggerCommand({
    271    result: beforeNotNowResult,
    272    feature: QuickSuggest.getFeature("YelpRealtimeSuggestions"),
    273    command: "not_now",
    274    searchString: input,
    275    expectedCountsByCall: {
    276      removeResult: 1,
    277    },
    278  });
    279 
    280  info("Check the prefs after triggering");
    281  Assert.greaterOrEqual(
    282    Date.now() / 1000,
    283    UrlbarPrefs.get("quicksuggest.realtimeOptIn.notNowTimeSeconds")
    284  );
    285  Assert.ok(
    286    UrlbarPrefs.get("quicksuggest.realtimeOptIn.notNowTypes").has(
    287      "yelpRealtime"
    288    )
    289  );
    290 
    291  info("Check the result after triggering");
    292  await check_results({
    293    context: createContext(input, {
    294      providers: [UrlbarProviderQuickSuggest.name],
    295      isPrivate: false,
    296    }),
    297    matches: [],
    298  });
    299 
    300  info("Simulate the passage of some days to show the result again");
    301  let notNowTimeSeconds = UrlbarPrefs.get(
    302    "quicksuggest.realtimeOptIn.notNowTimeSeconds"
    303  );
    304  let notNowReshowAfterPeriodDays = UrlbarPrefs.get(
    305    "quicksuggest.realtimeOptIn.notNowReshowAfterPeriodDays"
    306  );
    307  let newTime =
    308    notNowTimeSeconds - notNowReshowAfterPeriodDays * 24 * 60 * 60 - 60;
    309  UrlbarPrefs.set("quicksuggest.realtimeOptIn.notNowTimeSeconds", newTime);
    310 
    311  info("Check the result again");
    312  await check_results({
    313    context: createContext(input, {
    314      providers: [UrlbarProviderQuickSuggest.name],
    315      isPrivate: false,
    316    }),
    317    matches: [afterNotNowResult],
    318  });
    319 });
    320 
    321 add_task(async function dismiss_on_opt_in_prompt() {
    322  setupOptInPromptTest();
    323 
    324  let input = "coffee";
    325  let result = yelpOptInResult();
    326 
    327  info("Confirm whether the opt-in result is shown");
    328  await check_results({
    329    context: createContext(input, {
    330      providers: [UrlbarProviderQuickSuggest.name],
    331      isPrivate: false,
    332    }),
    333    matches: [result],
    334  });
    335 
    336  info("Trigger dismiss command");
    337  triggerCommand({
    338    result,
    339    feature: QuickSuggest.getFeature("YelpRealtimeSuggestions"),
    340    command: "dismiss",
    341    searchString: input,
    342    expectedCountsByCall: {
    343      removeResult: 1,
    344    },
    345  });
    346 
    347  info("Check the prefs after triggering");
    348  Assert.ok(
    349    UrlbarPrefs.get("quicksuggest.realtimeOptIn.dismissTypes").has(
    350      "yelpRealtime"
    351    )
    352  );
    353 
    354  info("Check the result after triggering");
    355  await check_results({
    356    context: createContext(input, {
    357      providers: [UrlbarProviderQuickSuggest.name],
    358      isPrivate: false,
    359    }),
    360    matches: [],
    361  });
    362 });
    363 
    364 add_task(async function not_interested_on_opt_in_prompt() {
    365  setupOptInPromptTest();
    366 
    367  let input = "coffee";
    368  let result = yelpOptInResult();
    369 
    370  info("Confirm whether the opt-in result is shown");
    371  await check_results({
    372    context: createContext(input, {
    373      providers: [UrlbarProviderQuickSuggest.name],
    374      isPrivate: false,
    375    }),
    376    matches: [result],
    377  });
    378 
    379  info("Trigger not_interested command");
    380  triggerCommand({
    381    result,
    382    feature: QuickSuggest.getFeature("YelpRealtimeSuggestions"),
    383    command: "not_interested",
    384    searchString: input,
    385    expectedCountsByCall: {
    386      removeResult: 1,
    387    },
    388  });
    389 
    390  info("Check the prefs after triggering");
    391  Assert.ok(!UrlbarPrefs.get("suggest.realtimeOptIn"));
    392 
    393  info("Check the result after triggering");
    394  await check_results({
    395    context: createContext(input, {
    396      providers: [UrlbarProviderQuickSuggest.name],
    397      isPrivate: false,
    398    }),
    399    matches: [],
    400  });
    401 });
    402 
    403 function setupOptInPromptTest() {
    404  UrlbarPrefs.set("suggest.realtimeOptIn", true);
    405  UrlbarPrefs.set("quicksuggest.online.available", true);
    406  UrlbarPrefs.set("quicksuggest.online.enabled", false);
    407  UrlbarPrefs.set("quicksuggest.realtimeOptIn.dismissTypes", "");
    408  UrlbarPrefs.set("quicksuggest.realtimeOptIn.notNowTimeSeconds", 0);
    409  UrlbarPrefs.set("quicksuggest.realtimeOptIn.notNowTypes", "");
    410 }
    411 
    412 function yelpOptInResult({ dismissButton = false } = {}) {
    413  let dismissOrNotNowButton = dismissButton
    414    ? {
    415        command: "dismiss",
    416        l10n: {
    417          id: "urlbar-result-realtime-opt-in-dismiss",
    418        },
    419      }
    420    : {
    421        command: "not_now",
    422        l10n: {
    423          id: "urlbar-result-realtime-opt-in-not-now",
    424        },
    425      };
    426  return {
    427    type: UrlbarUtils.RESULT_TYPE.TIP,
    428    source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
    429    heuristic: false,
    430    isBestMatch: true,
    431    payload: {
    432      source: "rust",
    433      provider: "Dynamic",
    434      type: "realtime_opt_in",
    435      suggestionType: "yelpRealtime_opt_in",
    436      telemetryType: "yelpRealtime_opt_in",
    437      isSponsored: true,
    438      icon: "chrome://browser/skin/illustrations/yelpRealtime-opt-in.svg",
    439      titleL10n: {
    440        id: "urlbar-result-yelp-realtime-opt-in-title",
    441      },
    442      descriptionL10n: {
    443        id: "urlbar-result-yelp-realtime-opt-in-description",
    444        parseMarkup: true,
    445      },
    446      descriptionLearnMoreTopic: "firefox-suggest",
    447      buttons: [
    448        {
    449          command: "opt_in",
    450          l10n: {
    451            id: "urlbar-result-realtime-opt-in-allow",
    452          },
    453          input: "coffee",
    454          attributes: {
    455            primary: "",
    456          },
    457        },
    458        {
    459          ...dismissOrNotNowButton,
    460          menu: [
    461            {
    462              name: "not_interested",
    463              l10n: {
    464                id: "urlbar-result-realtime-opt-in-dismiss-all",
    465              },
    466            },
    467          ],
    468        },
    469      ],
    470    },
    471  };
    472 }
    473 
    474 function yelpMerinoResult() {
    475  return {
    476    type: UrlbarUtils.RESULT_TYPE.DYNAMIC,
    477    source: UrlbarUtils.RESULT_SOURCE.SEARCH,
    478    heuristic: false,
    479    isBestMatch: true,
    480    payload: {
    481      source: "merino",
    482      provider: "yelp",
    483      dynamicType: "realtime-yelpRealtime",
    484      telemetryType: "yelpRealtime",
    485      isSponsored: true,
    486      items: [
    487        {
    488          some_value: "foo",
    489        },
    490      ],
    491    },
    492  };
    493 }