summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/pref-window/browser_fontChooser.js
blob: bf75433ec395d86f049e963070355ad815e5b878 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* -*- Mode: JavaScript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * Test various things about the font chooser window, including
 * - whether if the font defined in font.name.<style>.<language> is not present
 * on the computer, we fall back to displaying what's in
 * font.name-list.<style>.<language>.
 */

"use strict";

var utils = ChromeUtils.import("resource://testing-common/mozmill/utils.jsm");
var { content_tab_e } = ChromeUtils.import(
  "resource://testing-common/mozmill/ContentTabHelpers.jsm"
);
var { mc } = ChromeUtils.import(
  "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
);
var { close_pref_tab, open_pref_tab } = ChromeUtils.import(
  "resource://testing-common/mozmill/PrefTabHelpers.jsm"
);
var { wait_for_frame_load } = ChromeUtils.import(
  "resource://testing-common/mozmill/WindowHelpers.jsm"
);

var { Preferences } = ChromeUtils.importESModule(
  "resource://gre/modules/Preferences.sys.mjs"
);

var gFontEnumerator;
var gTodayPane;

// We'll test with Western. Unicode has issues on Windows (bug 550443).
const kLanguage = "x-western";

// A list of fonts present on the computer for each font type.
var gRealFontLists = {};

// A list of font types to consider
const kFontTypes = ["serif", "sans-serif", "monospace"];

add_setup(function () {
  if (AppConstants.platform == "win") {
    Services.prefs.setStringPref(
      "font.name-list.serif.x-western",
      "bc7e8c62-0634-467f-a029-fe6abcdf1582, Times New Roman"
    );
    Services.prefs.setStringPref(
      "font.name-list.sans-serif.x-western",
      "419129aa-43b7-40c4-b554-83d99b504b89, Arial"
    );
    Services.prefs.setStringPref(
      "font.name-list.monospace.x-western",
      "348df6e5-e874-4d21-ad4b-359b530a33b7, Courier New"
    );
  } else if (AppConstants.platform == "macosx") {
    Services.prefs.setStringPref(
      "font.name-list.serif.x-western",
      "bc7e8c62-0634-467f-a029-fe6abcdf1582, Times"
    );
    Services.prefs.setStringPref(
      "font.name-list.sans-serif.x-western",
      "419129aa-43b7-40c4-b554-83d99b504b89, Helvetica"
    );
    Services.prefs.setStringPref(
      "font.name-list.monospace.x-western",
      "348df6e5-e874-4d21-ad4b-359b530a33b7, Courier"
    );
  } else {
    Services.prefs.setStringPref(
      "font.name-list.serif.x-western",
      "bc7e8c62-0634-467f-a029-fe6abcdf1582, serif"
    );
    Services.prefs.setStringPref(
      "font.name-list.sans-serif.x-western",
      "419129aa-43b7-40c4-b554-83d99b504b89, sans-serif"
    );
    Services.prefs.setStringPref(
      "font.name-list.monospace.x-western",
      "348df6e5-e874-4d21-ad4b-359b530a33b7, monospace"
    );
  }

  let finished = false;
  buildFontList().then(() => (finished = true), console.error);
  utils.waitFor(
    () => finished,
    "Timeout waiting for font enumeration to complete."
  );

  // Hide Lightning's Today pane as it obscures buttons in preferences in the
  // small TB window our tests run in.
  gTodayPane = mc.window.document.getElementById("today-pane-panel");
  if (gTodayPane) {
    if (!gTodayPane.collapsed) {
      EventUtils.synthesizeKey("VK_F11", {});
    } else {
      gTodayPane = null;
    }
  }
});

async function buildFontList() {
  gFontEnumerator = Cc["@mozilla.org/gfx/fontenumerator;1"].createInstance(
    Ci.nsIFontEnumerator
  );
  for (let fontType of kFontTypes) {
    gRealFontLists[fontType] = await gFontEnumerator.EnumerateFontsAsync(
      kLanguage,
      fontType
    );
    if (gRealFontLists[fontType].length == 0) {
      throw new Error(
        "No fonts found for language " +
          kLanguage +
          " and font type " +
          fontType +
          "."
      );
    }
  }
}

function assert_fonts_equal(aDescription, aExpected, aActual, aPrefix = false) {
  if (
    !(
      (!aPrefix && aExpected == aActual) ||
      (aPrefix && aActual.startsWith(aExpected))
    )
  ) {
    throw new Error(
      "The " +
        aDescription +
        " font should be '" +
        aExpected +
        "', but " +
        (aActual.length == 0
          ? "nothing is actually selected."
          : "is actually: " + aActual + ".")
    );
  }
}

/**
 * Verify that the given fonts are displayed in the font chooser. This opens the
 * pref window to the display pane and checks that, then opens the font chooser
 * and checks that too.
 */
async function _verify_fonts_displayed(
  aDefaults,
  aSerif,
  aSansSerif,
  aMonospace
) {
  // Bring up the preferences window.
  let prefTab = open_pref_tab("paneGeneral");
  let contentDoc = prefTab.browser.contentDocument;
  let prefsWindow = contentDoc.ownerGlobal;
  prefsWindow.resizeTo(screen.availWidth, screen.availHeight);

  let isSansDefault =
    Services.prefs.getCharPref("font.default." + kLanguage) == "sans-serif";
  let displayPaneExpected = isSansDefault ? aSansSerif : aSerif;
  let displayPaneActual = content_tab_e(prefTab, "defaultFont");
  utils.waitFor(
    () => displayPaneActual.itemCount > 0,
    "No font names were populated in the font picker."
  );
  assert_fonts_equal(
    "display pane",
    displayPaneExpected,
    displayPaneActual.value
  );

  let advancedFonts = contentDoc.getElementById("advancedFonts");
  advancedFonts.scrollIntoView(false);
  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
  await new Promise(resolve => setTimeout(resolve, 500));
  // Now open the advanced dialog.
  EventUtils.synthesizeMouseAtCenter(advancedFonts, {}, prefsWindow);
  let fontc = wait_for_frame_load(
    prefsWindow.gSubDialog._topDialog._frame,
    "chrome://messenger/content/preferences/fonts.xhtml"
  );

  // The font pickers are populated async so we need to wait for it.
  for (let fontElemId of ["serif", "sans-serif", "monospace"]) {
    utils.waitFor(
      () => fontc.window.document.getElementById(fontElemId).label != "",
      "Timeout waiting for font picker '" + fontElemId + "' to populate."
    );
  }

  if (!aDefaults) {
    assert_fonts_equal(
      "serif",
      aSerif,
      fontc.window.document.getElementById("serif").value
    );
    assert_fonts_equal(
      "sans-serif",
      aSansSerif,
      fontc.window.document.getElementById("sans-serif").value
    );
    assert_fonts_equal(
      "monospace",
      aMonospace,
      fontc.window.document.getElementById("monospace").value
    );
  } else if (AppConstants.platform == "linux") {
    // When default fonts are displayed in the menulist, there is no value set,
    // only the label, in the form "Default (font name)".

    // On Linux the prefs we set contained only the generic font names,
    // like 'serif', but here a specific font name will be shown, but it is
    // system-dependent what it will be. So we just check for the 'Default'
    // prefix.
    assert_fonts_equal(
      "serif",
      `Default (`,
      fontc.window.document.getElementById("serif").label,
      true
    );
    assert_fonts_equal(
      "sans-serif",
      `Default (`,
      fontc.window.document.getElementById("sans-serif").label,
      true
    );
    assert_fonts_equal(
      "monospace",
      `Default (`,
      fontc.window.document.getElementById("monospace").label,
      true
    );
  } else {
    assert_fonts_equal(
      "serif",
      `Default (${aSerif})`,
      fontc.window.document.getElementById("serif").label
    );
    assert_fonts_equal(
      "sans-serif",
      `Default (${aSansSerif})`,
      fontc.window.document.getElementById("sans-serif").label
    );
    assert_fonts_equal(
      "monospace",
      `Default (${aMonospace})`,
      fontc.window.document.getElementById("monospace").label
    );
  }

  close_pref_tab(prefTab);
}

/**
 * Test that for a particular language, whatever's in
 * font.name.<type>.<language> is displayed in the font chooser (if it is
 * present on the computer).
 */
add_task(async function test_font_name_displayed() {
  Services.prefs.setCharPref("font.language.group", kLanguage);

  // Pick the first font for each font type and set it.
  let expected = {};
  for (let [fontType, fontList] of Object.entries(gRealFontLists)) {
    // Work around bug 698238 (on Windows, Courier is returned by the enumerator but
    // substituted with Courier New) by getting the standard (substituted) family
    // name for each font.
    let standardFamily = gFontEnumerator.getStandardFamilyName(fontList[0]);
    Services.prefs.setCharPref(
      "font.name." + fontType + "." + kLanguage,
      standardFamily
    );
    expected[fontType] = standardFamily;
  }

  let fontTypes = kFontTypes.map(fontType => expected[fontType]);
  await _verify_fonts_displayed(false, ...fontTypes);
  teardownTest();
});

// Fonts definitely not present on a computer -- we simply use UUIDs. These
// should be kept in sync with the ones in *-prefs.js.
const kFakeFonts = {
  serif: "bc7e8c62-0634-467f-a029-fe6abcdf1582",
  "sans-serif": "419129aa-43b7-40c4-b554-83d99b504b89",
  monospace: "348df6e5-e874-4d21-ad4b-359b530a33b7",
};

/**
 * Test that for a particular language, if font.name.<type>.<language> is not
 * present on the computer, we fall back to displaying what's in
 * font.name-list.<type>.<language>.
 */
add_task(async function test_font_name_not_present() {
  Services.prefs.setCharPref("font.language.group", kLanguage);

  // The fonts we're expecting to see selected in the font chooser for
  // test_font_name_not_present.
  let expected = {};
  for (let [fontType, fakeFont] of Object.entries(kFakeFonts)) {
    // Look at the font.name-list. We need to verify that the first font is the
    // fake one, and that the second one is present on the user's computer.
    let listPref = "font.name-list." + fontType + "." + kLanguage;
    let fontList = Services.prefs.getCharPref(listPref);
    let fonts = fontList.split(",").map(font => font.trim());
    if (fonts.length != 2) {
      throw new Error(
        listPref +
          " should have exactly two fonts, but it is '" +
          fontList +
          "'."
      );
    }

    if (fonts[0] != fakeFont) {
      throw new Error(
        "The first font in " +
          listPref +
          " should be '" +
          fakeFont +
          "', but is actually: " +
          fonts[0] +
          "."
      );
    }

    if (!gRealFontLists[fontType].includes(fonts[1])) {
      throw new Error(
        "The second font in " +
          listPref +
          " (" +
          fonts[1] +
          ") should be present on this computer, but isn't."
      );
    }
    expected[fontType] = fonts[1];

    // Set font.name to be a nonsense name that shouldn't exist.
    // font.name-list is handled by wrapper.py.
    Services.prefs.setCharPref(
      "font.name." + fontType + "." + kLanguage,
      fakeFont
    );
  }

  let fontTypes = kFontTypes.map(fontType => expected[fontType]);
  await _verify_fonts_displayed(true, ...fontTypes);
  teardownTest();
});

function teardownTest() {
  // nsIPrefBranch.resetBranch() is not implemented in M-C, so we can't use
  // Services.prefs.resetBranch().
  Preferences.resetBranch("font.name.");
}

registerCleanupFunction(function () {
  Services.prefs.clearUserPref("font.language.group");
  if (gTodayPane && gTodayPane.collapsed) {
    EventUtils.synthesizeKey("VK_F11", {});
  }

  Assert.report(
    false,
    undefined,
    undefined,
    "Test ran to completion successfully"
  );
});