summaryrefslogtreecommitdiffstats
path: root/browser/extensions/screenshots/test/browser/browser_screenshots_download.js
blob: 3e32f06da6ecafb7b23dbc7dc75919c0c5fe5921 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
add_task(async function test_fullPageScreenshot() {
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: TEST_GREEN_PAGE,
    },

    async browser => {
      const tests = [
        { title: "Green Page", expected: "Green Page.png" },
        { title: "\tA*B\\/+?<>\u200f\x1fC ", expected: "A B__ C.png" },
        {
          title: "簡単".repeat(35),
          expected: " " + "簡単".repeat(35) + ".png",
        },
        {
          title: "簡単".repeat(36),
          expected: " " + "簡単".repeat(26) + "[...].png",
        },
        {
          title: "簡単".repeat(56) + "?",
          expected: " " + "簡単".repeat(26) + "[...].png",
        },
      ];

      for (let test of tests) {
        info("Testing with title " + test.title);

        await SpecialPowers.spawn(browser, [test.title], titleToUse => {
          content.document.title = titleToUse;
        });

        let helper = new ScreenshotsHelper(browser);
        await helper.triggerUIFromToolbar();

        await helper.clickUIElement(
          helper.selector.preselectIframe,
          helper.selector.fullPageButton
        );

        info("Waiting for the preview UI and the download button");
        await helper.waitForUIContent(
          helper.selector.previewIframe,
          helper.selector.downloadButton
        );

        let publicList = await Downloads.getList(Downloads.PUBLIC);
        let downloadPromise = new Promise(resolve => {
          let downloadView = {
            onDownloadAdded(download) {
              publicList.removeView(downloadView);
              resolve(download);
            },
          };

          publicList.addView(downloadView);
        });

        await helper.clickUIElement(
          helper.selector.previewIframe,
          helper.selector.downloadButton
        );

        let download = await downloadPromise;
        let filename = PathUtils.filename(download.target.path);
        ok(
          filename.endsWith(test.expected),
          "Used correct filename '" +
            filename +
            "', expected: '" +
            test.expected +
            "'"
        );

        await task_resetState();
      }
    }
  );
});

// This is from browser/components/downloads/test/browser/head.js
async function task_resetState() {
  let publicList = await Downloads.getList(Downloads.PUBLIC);
  let downloads = await publicList.getAll();
  for (let download of downloads) {
    await publicList.remove(download);
    await download.finalize(true);
    if (await IOUtils.exists(download.target.path)) {
      if (Services.appinfo.OS === "WINNT") {
        // We need to make the file writable to delete it on Windows.
        await IOUtils.setPermissions(download.target.path, 0o600);
      }
      await IOUtils.remove(download.target.path);
    }
  }

  DownloadsPanel.hidePanel();
}