summaryrefslogtreecommitdiffstats
path: root/toolkit/components/reader/test/browser_bug1780350_readerModeSaveScroll.js
blob: 76add5511edac17aed920f2f75af30f678d90539 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* eslint-disable @microsoft/sdl/no-insecure-url */
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

const TEST_PATH = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  "http://example.com"
);

// This test verifies that when a link is clicked from
// within reader view, when navigating back to reader view
// the previous scroll position of the page is restored.
add_task(async function test_save_scroll_position() {
  await BrowserTestUtils.withNewTab(
    TEST_PATH + "readerModeArticleContainsLink.html",
    async function (browser) {
      let pageShownPromise = BrowserTestUtils.waitForContentEvent(
        browser,
        "AboutReaderContentReady"
      );
      let browserLoadedPromise = BrowserTestUtils.browserLoaded(browser);
      let readerButton = document.getElementById("reader-mode-button");
      readerButton.click();
      await Promise.all([pageShownPromise, browserLoadedPromise]);
      let scrollEventPromise = BrowserTestUtils.waitForContentEvent(
        browser,
        "scroll",
        true
      );
      // Set scroll position in reader to 200px down.
      await SpecialPowers.spawn(browser, [], async function () {
        content.document.documentElement.scrollTop = 200;
      });
      await scrollEventPromise;

      // Click linked page and check that scroll pos resets.
      await SpecialPowers.spawn(browser, [], async function () {
        let linkElement = content.document.getElementById("link");
        linkElement.click();
      });
      await BrowserTestUtils.browserLoaded(browser);
      await SpecialPowers.spawn(browser, [], async function () {
        is(
          content.document.documentElement.scrollTop,
          0,
          "vertical scroll position should reset to zero when navigating to linked page."
        );
        content.window.history.back();
      });

      // Navigate back to reader and check that scroll position is restored.
      await BrowserTestUtils.browserLoaded(browser);
      scrollEventPromise = BrowserTestUtils.waitForContentEvent(
        browser,
        "scroll",
        true
      );
      await scrollEventPromise;
      await SpecialPowers.spawn(browser, [], async function () {
        let doc = content.document;
        is(
          doc.documentElement.scrollTop,
          200,
          "should restore saved scroll position when navigating back from link."
        );
      });
    }
  );
});