summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/favicons/test_favicons_conversions.js
blob: 28a0fffb7f62a0cbdcaaf54777d6503b3d6640e5 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * This file tests the image conversions done by the favicon service.
 */

// Globals

// The pixel values we get on Windows are sometimes +/- 1 value compared to
// other platforms, so we need to skip some image content tests.
var isWindows = "@mozilla.org/windows-registry-key;1" in Cc;

/**
 * Checks the conversion of the given test image file.
 *
 * @param aFileName
 *        File that contains the favicon image, located in the test folder.
 * @param aFileMimeType
 *        MIME type of the image contained in the file.
 * @param aFileLength
 *        Expected length of the file.
 * @param aExpectConversion
 *        If false, the icon should be stored as is.  If true, the expected data
 *        is loaded from a file named "expected-" + aFileName + ".png".
 * @param aVaryOnWindows
 *        Indicates that the content of the converted image can be different on
 *        Windows and should not be checked on that platform.
 * @param aCallback
 *        This function is called after the check finished.
 */
async function checkFaviconDataConversion(
  aFileName,
  aFileMimeType,
  aFileLength,
  aExpectConversion,
  aVaryOnWindows
) {
  let pageURI = NetUtil.newURI("http://places.test/page/" + aFileName);
  await PlacesTestUtils.addVisits({
    uri: pageURI,
    transition: TRANSITION_TYPED,
  });
  let faviconURI = NetUtil.newURI("http://places.test/icon/" + aFileName);
  let fileData = readFileOfLength(aFileName, aFileLength);

  PlacesUtils.favicons.replaceFaviconData(faviconURI, fileData, aFileMimeType);
  await new Promise(resolve => {
    PlacesUtils.favicons.setAndFetchFaviconForPage(
      pageURI,
      faviconURI,
      true,
      PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE,
      (aURI, aDataLen, aData, aMimeType) => {
        if (!aExpectConversion) {
          Assert.ok(compareArrays(aData, fileData));
          Assert.equal(aMimeType, aFileMimeType);
        } else {
          if (!aVaryOnWindows || !isWindows) {
            let expectedFile = do_get_file("expected-" + aFileName + ".png");
            Assert.ok(compareArrays(aData, readFileData(expectedFile)));
          }
          Assert.equal(aMimeType, "image/png");
        }
        resolve();
      },
      Services.scriptSecurityManager.getSystemPrincipal()
    );
  });
}

add_task(async function test_storing_a_normal_16x16_icon() {
  // 16x16 png, 286 bytes.
  // optimized: no
  await checkFaviconDataConversion(
    "favicon-normal16.png",
    "image/png",
    286,
    false,
    false
  );
});

add_task(async function test_storing_a_normal_32x32_icon() {
  // 32x32 png, 344 bytes.
  // optimized: no
  await checkFaviconDataConversion(
    "favicon-normal32.png",
    "image/png",
    344,
    false,
    false
  );
});

add_task(async function test_storing_a_big_16x16_icon() {
  //  in: 16x16 ico, 1406 bytes.
  // optimized: yes
  await checkFaviconDataConversion(
    "favicon-big16.ico",
    "image/x-icon",
    1406,
    true,
    false
  );
});

add_task(async function test_storing_an_oversize_4x4_icon() {
  //  in: 4x4 jpg, 4751 bytes.
  // optimized: yes
  await checkFaviconDataConversion(
    "favicon-big4.jpg",
    "image/jpeg",
    4751,
    true,
    false
  );
});

add_task(async function test_storing_an_oversize_32x32_icon() {
  //  in: 32x32 jpg, 3494 bytes.
  // optimized: yes
  await checkFaviconDataConversion(
    "favicon-big32.jpg",
    "image/jpeg",
    3494,
    true,
    true
  );
});

add_task(async function test_storing_an_oversize_48x48_icon() {
  //  in: 48x48 ico, 56646 bytes.
  // (howstuffworks.com icon, contains 13 icons with sizes from 16x16 to
  // 48x48 in varying depths)
  // optimized: yes
  await checkFaviconDataConversion(
    "favicon-big48.ico",
    "image/x-icon",
    56646,
    true,
    false
  );
});

add_task(async function test_storing_an_oversize_64x64_icon() {
  //  in: 64x64 png, 10698 bytes.
  // optimized: yes
  await checkFaviconDataConversion(
    "favicon-big64.png",
    "image/png",
    10698,
    true,
    false
  );
});

add_task(async function test_scaling_an_oversize_160x3_icon() {
  //  in: 160x3 jpg, 5095 bytes.
  // optimized: yes
  await checkFaviconDataConversion(
    "favicon-scale160x3.jpg",
    "image/jpeg",
    5095,
    true,
    false
  );
});

add_task(async function test_scaling_an_oversize_3x160_icon() {
  //  in: 3x160 jpg, 5059 bytes.
  // optimized: yes
  await checkFaviconDataConversion(
    "favicon-scale3x160.jpg",
    "image/jpeg",
    5059,
    true,
    false
  );
});

add_task(async function test_animated_16x16_icon() {
  // in: 16x16 apng, 1791 bytes.
  // optimized: yes
  await checkFaviconDataConversion(
    "favicon-animated16.png",
    "image/png",
    1791,
    true,
    false
  );
});