tor-browser

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

browser_customize_menu_content.js (9674B)


      1 "use strict";
      2 
      3 // test_newtab calls SpecialPowers.spawn, which injects ContentTaskUtils in the
      4 // scope of the callback. Eslint doesn't know about that.
      5 /* global ContentTaskUtils */
      6 
      7 const { WeatherFeed } = ChromeUtils.importESModule(
      8  "resource://newtab/lib/WeatherFeed.sys.mjs"
      9 );
     10 
     11 ChromeUtils.defineESModuleGetters(this, {
     12  GeolocationTestUtils:
     13    "resource://testing-common/GeolocationTestUtils.sys.mjs",
     14  MerinoTestUtils: "resource://testing-common/MerinoTestUtils.sys.mjs",
     15 });
     16 
     17 const { WEATHER_SUGGESTION } = MerinoTestUtils;
     18 
     19 add_setup(async function () {
     20  GeolocationTestUtils.init(this);
     21  GeolocationTestUtils.stubGeolocation(GeolocationTestUtils.SAN_FRANCISCO);
     22 });
     23 
     24 test_newtab({
     25  async before({ pushPrefs }) {
     26    await pushPrefs(
     27      ["browser.newtabpage.activity-stream.feeds.topsites", false],
     28      ["browser.newtabpage.activity-stream.feeds.section.topstories", false]
     29    );
     30  },
     31  test: async function test_render_customizeMenu() {
     32    function getSection(sectionIdentifier) {
     33      return content.document.querySelector(
     34        `section[data-section-id="${sectionIdentifier}"]`
     35      );
     36    }
     37    function promiseSectionShown(sectionIdentifier) {
     38      return ContentTaskUtils.waitForMutationCondition(
     39        content.document.querySelector("main"),
     40        { childList: true, subtree: true },
     41        () => getSection(sectionIdentifier)
     42      );
     43    }
     44    const TOPSITES_PREF = "browser.newtabpage.activity-stream.feeds.topsites";
     45    const TOPSTORIES_PREF =
     46      "browser.newtabpage.activity-stream.feeds.section.topstories";
     47 
     48    await ContentTaskUtils.waitForCondition(
     49      () => content.document.querySelector(".personalize-button"),
     50      "Wait for prefs button to load on the newtab page"
     51    );
     52 
     53    let customizeButton = content.document.querySelector(".personalize-button");
     54    customizeButton.click();
     55 
     56    let defaultPos = "matrix(1, 0, 0, 1, 0, 0)";
     57    await ContentTaskUtils.waitForCondition(
     58      () =>
     59        content.getComputedStyle(
     60          content.document.querySelector(".customize-menu-animate-wrapper")
     61        ).transform === defaultPos,
     62      "Customize Menu should be visible on screen"
     63    );
     64 
     65    // Test that clicking the shortcuts toggle will make the section
     66    // appear on the newtab page.
     67    //
     68    // We waive XRay wrappers because we want to call the click()
     69    // method defined on the toggle from this context.
     70    let shortcutsSwitch = Cu.waiveXrays(
     71      content.document.querySelector("#shortcuts-section moz-toggle")
     72    );
     73    Assert.ok(
     74      !Services.prefs.getBoolPref(TOPSITES_PREF),
     75      "Topsites are turned off"
     76    );
     77    Assert.ok(!getSection("topsites"), "Shortcuts section is not rendered");
     78 
     79    let sectionShownPromise = promiseSectionShown("topsites");
     80    shortcutsSwitch.click();
     81    await sectionShownPromise;
     82 
     83    Assert.ok(getSection("topsites"), "Shortcuts section is rendered");
     84 
     85    // Test that clicking the pocket toggle will make the pocket section
     86    // appear on the newtab page
     87    //
     88    // We waive XRay wrappers because we want to call the click()
     89    // method defined on the toggle from this context.
     90    let pocketSwitch = Cu.waiveXrays(
     91      content.document.querySelector("#pocket-section moz-toggle")
     92    );
     93    Assert.ok(
     94      !Services.prefs.getBoolPref(TOPSTORIES_PREF),
     95      "Pocket pref is turned off"
     96    );
     97    Assert.ok(!getSection("topstories"), "Pocket section is not rendered");
     98 
     99    sectionShownPromise = promiseSectionShown("topstories");
    100    pocketSwitch.click();
    101    await sectionShownPromise;
    102 
    103    Assert.ok(getSection("topstories"), "Pocket section is rendered");
    104  },
    105  async after() {
    106    Services.prefs.clearUserPref(
    107      "browser.newtabpage.activity-stream.feeds.topsites"
    108    );
    109    Services.prefs.clearUserPref(
    110      "browser.newtabpage.activity-stream.feeds.section.topstories"
    111    );
    112  },
    113 });
    114 
    115 test_newtab({
    116  async before({ pushPrefs }) {
    117    sinon.stub(WeatherFeed.prototype, "MerinoClient").returns({
    118      fetch: () => [WEATHER_SUGGESTION],
    119    });
    120    await pushPrefs(
    121      ["browser.newtabpage.activity-stream.system.showWeather", true],
    122      ["browser.newtabpage.activity-stream.showWeather", false]
    123    );
    124  },
    125  test: async function test_render_customizeMenuWeather() {
    126    // Weather Widget Fecthing
    127    function getWeatherWidget() {
    128      return content.document.querySelector(`.weather`);
    129    }
    130 
    131    function promiseWeatherShown() {
    132      return ContentTaskUtils.waitForMutationCondition(
    133        content.document.querySelector(".weatherWrapper"),
    134        { childList: true, subtree: true },
    135        () => getWeatherWidget()
    136      );
    137    }
    138 
    139    const WEATHER_PREF = "browser.newtabpage.activity-stream.showWeather";
    140 
    141    await ContentTaskUtils.waitForCondition(
    142      () => content.document.querySelector(".personalize-button"),
    143      "Wait for prefs button to load on the newtab page"
    144    );
    145 
    146    let customizeButton = content.document.querySelector(".personalize-button");
    147    customizeButton.click();
    148 
    149    let defaultPos = "matrix(1, 0, 0, 1, 0, 0)";
    150    await ContentTaskUtils.waitForCondition(
    151      () =>
    152        content.getComputedStyle(
    153          content.document.querySelector(".customize-menu-animate-wrapper")
    154        ).transform === defaultPos,
    155      "Customize Menu should be visible on screen"
    156    );
    157 
    158    // Test that clicking the weather toggle will make the
    159    // weather widget appear on the newtab page.
    160    //
    161    // We waive XRay wrappers because we want to call the click()
    162    // method defined on the toggle from this context.
    163    let weatherSwitch = Cu.waiveXrays(
    164      content.document.querySelector("#weather-section moz-toggle")
    165    );
    166    Assert.ok(
    167      !Services.prefs.getBoolPref(WEATHER_PREF),
    168      "Weather pref is turned off"
    169    );
    170    Assert.ok(!getWeatherWidget(), "Weather widget is not rendered");
    171 
    172    let sectionShownPromise = promiseWeatherShown();
    173    weatherSwitch.click();
    174    await sectionShownPromise;
    175 
    176    Assert.ok(getWeatherWidget(), "Weather widget is rendered");
    177  },
    178  async after() {
    179    sinon.restore();
    180    Services.prefs.clearUserPref(
    181      "browser.newtabpage.activity-stream.showWeather"
    182    );
    183    Services.prefs.clearUserPref(
    184      "browser.newtabpage.activity-stream.system.showWeather"
    185    );
    186  },
    187 });
    188 
    189 test_newtab({
    190  test: async function test_open_close_customizeMenu() {
    191    const EventUtils = ContentTaskUtils.getEventUtils(content);
    192    await ContentTaskUtils.waitForCondition(
    193      () => content.document.querySelector(".personalize-button"),
    194      "Wait for prefs button to load on the newtab page"
    195    );
    196 
    197    let customizeButton = content.document.querySelector(".personalize-button");
    198    customizeButton.click();
    199 
    200    let defaultPos = "matrix(1, 0, 0, 1, 0, 0)";
    201    await ContentTaskUtils.waitForCondition(
    202      () =>
    203        content.getComputedStyle(
    204          content.document.querySelector(".customize-menu-animate-wrapper")
    205        ).transform === defaultPos,
    206      "Customize Menu should be visible on screen"
    207    );
    208 
    209    await ContentTaskUtils.waitForCondition(
    210      () => content.document.activeElement.id === "close-button",
    211      "Close button should be focused when menu becomes visible"
    212    );
    213 
    214    await ContentTaskUtils.waitForCondition(
    215      () =>
    216        content.getComputedStyle(
    217          content.document.querySelector(".personalize-button")
    218        ).visibility === "hidden",
    219      "Personalize button should become hidden"
    220    );
    221 
    222    // Test close button.
    223    let closeButton = content.document.querySelector("#close-button");
    224    closeButton.click();
    225    await ContentTaskUtils.waitForCondition(
    226      () =>
    227        content.getComputedStyle(
    228          content.document.querySelector(".customize-menu-animate-wrapper")
    229        ).transform !== defaultPos,
    230      "Customize Menu should not be visible anymore"
    231    );
    232 
    233    await ContentTaskUtils.waitForCondition(
    234      () =>
    235        content.document.activeElement.classList.contains("personalize-button"),
    236      "Personalize button should be focused when menu closes"
    237    );
    238 
    239    await ContentTaskUtils.waitForCondition(
    240      () =>
    241        content.getComputedStyle(
    242          content.document.querySelector(".personalize-button")
    243        ).visibility === "visible",
    244      "Personalize button should become visible"
    245    );
    246 
    247    // Reopen the customize menu
    248    customizeButton.click();
    249    await ContentTaskUtils.waitForCondition(
    250      () =>
    251        content.getComputedStyle(
    252          content.document.querySelector(".customize-menu-animate-wrapper")
    253        ).transform === defaultPos,
    254      "Customize Menu should be visible on screen now"
    255    );
    256 
    257    // Test closing with esc key.
    258    EventUtils.synthesizeKey("VK_ESCAPE", {}, content);
    259    await ContentTaskUtils.waitForCondition(
    260      () =>
    261        content.getComputedStyle(
    262          content.document.querySelector(".customize-menu-animate-wrapper")
    263        ).transform !== defaultPos,
    264      "Customize Menu should not be visible anymore"
    265    );
    266 
    267    // Reopen the customize menu
    268    customizeButton.click();
    269    await ContentTaskUtils.waitForCondition(
    270      () =>
    271        content.getComputedStyle(
    272          content.document.querySelector(".customize-menu-animate-wrapper")
    273        ).transform === defaultPos,
    274      "Customize Menu should be visible on screen now"
    275    );
    276 
    277    // Test closing with external click.
    278    let outerWrapper = content.document.querySelector(".outer-wrapper");
    279    outerWrapper.click();
    280    await ContentTaskUtils.waitForCondition(
    281      () =>
    282        content.getComputedStyle(
    283          content.document.querySelector(".customize-menu-animate-wrapper")
    284        ).transform !== defaultPos,
    285      "Customize Menu should not be visible anymore"
    286    );
    287  },
    288 });