summaryrefslogtreecommitdiffstats
path: root/browser/components/downloads/test/browser/browser_image_mimetype_issues.js
blob: b893a26d89f7dd496ecd8e5a6c4986f9cfa48144 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

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

var MockFilePicker = SpecialPowers.MockFilePicker;
MockFilePicker.init(window);

/*
 * Popular websites implement image optimization as serving files with
 * extension ".jpg" but content type "image/webp". If we save such images,
 * we should actually save them with a .webp extension as that is what
 * they are.
 */

/**
 * Test the above with the "save image as" context menu.
 */
add_task(async function test_save_image_webp_with_jpeg_extension() {
  await BrowserTestUtils.withNewTab(
    `data:text/html,<img src="${TEST_ROOT}/not-really-a-jpeg.jpeg?convert=webp">`,
    async browser => {
      let menu = document.getElementById("contentAreaContextMenu");
      let popupShown = BrowserTestUtils.waitForEvent(menu, "popupshown");
      BrowserTestUtils.synthesizeMouse(
        "img",
        5,
        5,
        { type: "contextmenu", button: 2 },
        browser
      );
      await popupShown;

      await new Promise(resolve => {
        MockFilePicker.showCallback = function (fp) {
          ok(
            fp.defaultString.endsWith("webp"),
            `filepicker for image has "${fp.defaultString}", should end in webp`
          );
          setTimeout(resolve, 0);
          return Ci.nsIFilePicker.returnCancel;
        };
        let menuitem = menu.querySelector("#context-saveimage");
        menu.activateItem(menuitem);
      });
    }
  );
});

/**
 * Test with the "save link as" context menu.
 */
add_task(async function test_save_link_webp_with_jpeg_extension() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.download.always_ask_before_handling_new_types", false],
      ["browser.download.useDownloadDir", false],
    ],
  });
  await BrowserTestUtils.withNewTab(
    `data:text/html,<a href="${TEST_ROOT}/not-really-a-jpeg.jpeg?convert=webp">Nice image</a>`,
    async browser => {
      let menu = document.getElementById("contentAreaContextMenu");
      let popupShown = BrowserTestUtils.waitForEvent(menu, "popupshown");
      BrowserTestUtils.synthesizeMouse(
        "a[href]",
        5,
        5,
        { type: "contextmenu", button: 2 },
        browser
      );
      await popupShown;

      await new Promise(resolve => {
        MockFilePicker.showCallback = function (fp) {
          ok(
            fp.defaultString.endsWith("webp"),
            `filepicker for link has "${fp.defaultString}", should end in webp`
          );
          setTimeout(resolve, 0);
          return Ci.nsIFilePicker.returnCancel;
        };
        let menuitem = menu.querySelector("#context-savelink");
        menu.activateItem(menuitem);
      });
    }
  );
});

/**
 * Test with the main "save page" command.
 */
add_task(async function test_save_page_on_image_document() {
  await BrowserTestUtils.withNewTab(
    `${TEST_ROOT}/not-really-a-jpeg.jpeg?convert=webp`,
    async browser => {
      await new Promise(resolve => {
        MockFilePicker.showCallback = function (fp) {
          ok(
            fp.defaultString.endsWith("webp"),
            `filepicker for "save page" has "${fp.defaultString}", should end in webp`
          );
          setTimeout(resolve, 0);
          return Ci.nsIFilePicker.returnCancel;
        };
        document.getElementById("Browser:SavePage").doCommand();
      });
    }
  );
});

/**
 * Make sure that a valid JPEG image using the .JPG extension doesn't
 * get it replaced with .jpeg.
 */
add_task(async function test_save_page_on_JPEG_image_document() {
  await BrowserTestUtils.withNewTab(`${TEST_ROOT}/blank.JPG`, async browser => {
    await new Promise(resolve => {
      MockFilePicker.showCallback = function (fp) {
        ok(
          fp.defaultString.endsWith("JPG"),
          `filepicker for "save page" has "${fp.defaultString}", should end in JPG`
        );
        setTimeout(resolve, 0);
        return Ci.nsIFilePicker.returnCancel;
      };
      document.getElementById("Browser:SavePage").doCommand();
    });
  });
});