tor-browser

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

test_quicksuggest_mdn.js (7394B)


      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 MDN quick suggest results.
      6 
      7 "use strict";
      8 
      9 const REMOTE_SETTINGS_DATA = [
     10  {
     11    type: "mdn-suggestions",
     12    attachment: [
     13      {
     14        url: "https://example.com/array-filter",
     15        title: "Array.prototype.filter()",
     16        description:
     17          "The filter() method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.",
     18        keywords: ["array filter"],
     19        score: 0.24,
     20      },
     21      {
     22        url: "https://example.com/input",
     23        title: "<input>: The Input (Form Input) element",
     24        description:
     25          "The <input> HTML element is used to create interactive controls for web-based forms in order to accept data from the user; a wide variety of types of input data and control widgets are available, depending on the device and user agent. The <input> element is one of the most powerful and complex in all of HTML due to the sheer number of combinations of input types and attributes.",
     26        keywords: ["input"],
     27        score: 0.24,
     28      },
     29      {
     30        url: "https://example.com/grid",
     31        title: "CSS Grid Layout",
     32        description:
     33          "CSS Grid Layout excels at dividing a page into major regions or defining the relationship in terms of size, position, and layer, between parts of a control built from HTML primitives.",
     34        keywords: ["grid"],
     35        score: 0.24,
     36      },
     37    ],
     38  },
     39 ];
     40 
     41 add_setup(async function init() {
     42  // Disable search suggestions so we don't hit the network.
     43  Services.prefs.setBoolPref("browser.search.suggest.enabled", false);
     44 
     45  await QuickSuggestTestUtils.ensureQuickSuggestInit({
     46    remoteSettingsRecords: REMOTE_SETTINGS_DATA,
     47    prefs: [
     48      ["suggest.quicksuggest.all", true],
     49      ["suggest.quicksuggest.sponsored", false],
     50    ],
     51  });
     52 });
     53 
     54 add_task(async function basic() {
     55  for (const suggestion of REMOTE_SETTINGS_DATA[0].attachment) {
     56    const fullKeyword = suggestion.keywords[0];
     57    const firstWord = fullKeyword.split(" ")[0];
     58    for (let i = 1; i < fullKeyword.length; i++) {
     59      const keyword = fullKeyword.substring(0, i);
     60      const shouldMatch = i >= firstWord.length;
     61      const matches = shouldMatch
     62        ? [QuickSuggestTestUtils.mdnResult(suggestion)]
     63        : [];
     64      await check_results({
     65        context: createContext(keyword, {
     66          providers: [UrlbarProviderQuickSuggest.name],
     67          isPrivate: false,
     68        }),
     69        matches,
     70      });
     71    }
     72 
     73    await check_results({
     74      context: createContext(fullKeyword + " ", {
     75        providers: [UrlbarProviderQuickSuggest.name],
     76        isPrivate: false,
     77      }),
     78      matches: !fullKeyword.includes(" ")
     79        ? [QuickSuggestTestUtils.mdnResult(suggestion)]
     80        : [],
     81    });
     82  }
     83 });
     84 
     85 // Check wheather the MDN suggestions will be hidden by the pref.
     86 add_task(async function disableByLocalPref() {
     87  const suggestion = REMOTE_SETTINGS_DATA[0].attachment[0];
     88  const keyword = suggestion.keywords[0];
     89 
     90  const prefs = [
     91    "suggest.mdn",
     92    "mdn.featureGate",
     93    "quicksuggest.enabled",
     94    "suggest.quicksuggest.all",
     95  ];
     96 
     97  for (const pref of prefs) {
     98    // First make sure the suggestion is added.
     99    await check_results({
    100      context: createContext(keyword, {
    101        providers: [UrlbarProviderQuickSuggest.name],
    102        isPrivate: false,
    103      }),
    104      matches: [QuickSuggestTestUtils.mdnResult(suggestion)],
    105    });
    106 
    107    // Now disable them.
    108    UrlbarPrefs.set(pref, false);
    109    await check_results({
    110      context: createContext(keyword, {
    111        providers: [UrlbarProviderQuickSuggest.name],
    112        isPrivate: false,
    113      }),
    114      matches: [],
    115    });
    116 
    117    // Revert.
    118    UrlbarPrefs.set(pref, true);
    119    await QuickSuggestTestUtils.forceSync();
    120  }
    121 });
    122 
    123 // Check wheather the MDN suggestions will be shown by the setup of Nimbus
    124 // variable.
    125 add_task(async function nimbus() {
    126  const defaultPrefs = Services.prefs.getDefaultBranch("browser.urlbar.");
    127 
    128  const suggestion = REMOTE_SETTINGS_DATA[0].attachment[0];
    129  const keyword = suggestion.keywords[0];
    130 
    131  // Disable the fature gate.
    132  defaultPrefs.setBoolPref("mdn.featureGate", false);
    133  await check_results({
    134    context: createContext(keyword, {
    135      providers: [UrlbarProviderQuickSuggest.name],
    136      isPrivate: false,
    137    }),
    138    matches: [],
    139  });
    140 
    141  // Enable by Nimbus.
    142  const cleanUpNimbusEnable = await UrlbarTestUtils.initNimbusFeature(
    143    { mdnFeatureGate: true },
    144    "urlbar",
    145    "config"
    146  );
    147  await QuickSuggestTestUtils.forceSync();
    148  await check_results({
    149    context: createContext(keyword, {
    150      providers: [UrlbarProviderQuickSuggest.name],
    151      isPrivate: false,
    152    }),
    153    matches: [QuickSuggestTestUtils.mdnResult(suggestion)],
    154  });
    155  await cleanUpNimbusEnable();
    156 
    157  // Enable locally.
    158  defaultPrefs.setBoolPref("mdn.featureGate", true);
    159  await QuickSuggestTestUtils.forceSync();
    160 
    161  // Disable by Nimbus.
    162  const cleanUpNimbusDisable = await UrlbarTestUtils.initNimbusFeature(
    163    { mdnFeatureGate: false },
    164    "urlbar",
    165    "config"
    166  );
    167  await check_results({
    168    context: createContext(keyword, {
    169      providers: [UrlbarProviderQuickSuggest.name],
    170      isPrivate: false,
    171    }),
    172    matches: [],
    173  });
    174  await cleanUpNimbusDisable();
    175 
    176  // Revert.
    177  defaultPrefs.setBoolPref("mdn.featureGate", true);
    178  await QuickSuggestTestUtils.forceSync();
    179 });
    180 
    181 add_task(async function mixedCaseQuery() {
    182  const suggestion = REMOTE_SETTINGS_DATA[0].attachment[1];
    183  const keyword = "InPuT";
    184 
    185  await check_results({
    186    context: createContext(keyword, {
    187      providers: [UrlbarProviderQuickSuggest.name],
    188      isPrivate: false,
    189    }),
    190    matches: [QuickSuggestTestUtils.mdnResult(suggestion)],
    191  });
    192 });
    193 
    194 // Tests the "Not relevant" command: a dismissed suggestion shouldn't be added.
    195 add_task(async function notRelevant() {
    196  await doDismissOneTest({
    197    result: QuickSuggestTestUtils.mdnResult(
    198      REMOTE_SETTINGS_DATA[0].attachment[0]
    199    ),
    200    command: "not_relevant",
    201    feature: QuickSuggest.getFeature("MDNSuggestions"),
    202    queriesForDismissals: [
    203      {
    204        query: REMOTE_SETTINGS_DATA[0].attachment[0].keywords[0],
    205      },
    206    ],
    207    queriesForOthers: [
    208      {
    209        query: REMOTE_SETTINGS_DATA[0].attachment[1].keywords[0],
    210        expectedResults: [
    211          QuickSuggestTestUtils.mdnResult(
    212            REMOTE_SETTINGS_DATA[0].attachment[1]
    213          ),
    214        ],
    215      },
    216    ],
    217  });
    218 });
    219 
    220 // Tests the "Not interested" command: all MDN suggestions should be disabled
    221 // and not added anymore.
    222 add_task(async function notInterested() {
    223  await doDismissAllTest({
    224    result: QuickSuggestTestUtils.mdnResult(
    225      REMOTE_SETTINGS_DATA[0].attachment[0]
    226    ),
    227    command: "not_interested",
    228    feature: QuickSuggest.getFeature("MDNSuggestions"),
    229    pref: "suggest.mdn",
    230    queries: [
    231      {
    232        query: REMOTE_SETTINGS_DATA[0].attachment[0].keywords[0],
    233      },
    234      {
    235        query: REMOTE_SETTINGS_DATA[0].attachment[1].keywords[0],
    236        expectedResults: [
    237          QuickSuggestTestUtils.mdnResult(
    238            REMOTE_SETTINGS_DATA[0].attachment[1]
    239          ),
    240        ],
    241      },
    242    ],
    243  });
    244 });