tor-browser

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

test_weather_geolocation.js (2484B)


      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 the weather suggestion with geolocation.
      6 
      7 "use strict";
      8 
      9 add_setup(async () => {
     10  await MerinoTestUtils.server.start();
     11  await QuickSuggestTestUtils.ensureQuickSuggestInit({
     12    remoteSettingsRecords: [QuickSuggestTestUtils.weatherRecord()],
     13    prefs: [
     14      ["suggest.quicksuggest.sponsored", true],
     15      ["weather.featureGate", true],
     16    ],
     17  });
     18 });
     19 
     20 const TEST_DATA = [
     21  {
     22    geolocation: {
     23      country_code: "US",
     24      region_code: "CA",
     25      city: "San Francisco",
     26    },
     27    expected: {
     28      country: "US",
     29      region: "CA",
     30      city: "San Francisco",
     31    },
     32  },
     33  {
     34    geolocation: {
     35      region_code: "CA",
     36      city: "San Francisco",
     37    },
     38    expected: {
     39      region: "CA",
     40      city: "San Francisco",
     41    },
     42  },
     43  {
     44    geolocation: {
     45      country_code: "US",
     46      region_code: "CA",
     47    },
     48    expected: {
     49      country: "US",
     50      region: "CA",
     51    },
     52  },
     53  {
     54    geolocation: {
     55      country_code: "US",
     56      city: "San Francisco",
     57    },
     58    expected: {
     59      country: "US",
     60      city: "San Francisco",
     61    },
     62  },
     63  {
     64    // Use region as city if no city.
     65    geolocation: {
     66      country_code: "JP",
     67      region: "Kanagawa",
     68      region_code: "14",
     69    },
     70    expected: {
     71      country: "JP",
     72      region: "14",
     73      city: "Kanagawa",
     74    },
     75  },
     76 ];
     77 
     78 add_task(async function () {
     79  // No need actual suggestions as we check only the http requests in Merino
     80  // server.
     81  MerinoTestUtils.server.response.body.suggestions = [];
     82 
     83  let controller = UrlbarTestUtils.newMockController();
     84 
     85  for (let [index, { geolocation, expected }] of TEST_DATA.entries()) {
     86    info(`Test for ${JSON.stringify(geolocation)}`);
     87    let cleanup = GeolocationTestUtils.stubGeolocation(geolocation);
     88 
     89    await controller.startQuery(
     90      createContext("weather", {
     91        providers: [UrlbarProviderQuickSuggest.name],
     92        isPrivate: false,
     93      })
     94    );
     95 
     96    MerinoTestUtils.server.checkAndClearRequests([
     97      {
     98        params: {
     99          [MerinoTestUtils.SEARCH_PARAMS.QUERY]: "",
    100          [MerinoTestUtils.SEARCH_PARAMS.PROVIDERS]: "accuweather",
    101          [MerinoTestUtils.SEARCH_PARAMS.SEQUENCE_NUMBER]: index,
    102          source: "urlbar",
    103          ...expected,
    104        },
    105      },
    106    ]);
    107 
    108    await cleanup();
    109  }
    110 });