summaryrefslogtreecommitdiffstats
path: root/browser/components/enterprisepolicies/tests/xpcshell/test_requestedlocales.js
blob: bfed2491be8078e1a30068a9c26239ca98ae700c (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

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

const REQ_LOC_CHANGE_EVENT = "intl:requested-locales-changed";

function promiseLocaleChanged(requestedLocale) {
  return new Promise(resolve => {
    let localeObserver = {
      observe(aSubject, aTopic) {
        switch (aTopic) {
          case REQ_LOC_CHANGE_EVENT:
            let reqLocs = Services.locale.requestedLocales;
            equal(reqLocs[0], requestedLocale);
            Services.obs.removeObserver(localeObserver, REQ_LOC_CHANGE_EVENT);
            resolve();
        }
      },
    };
    Services.obs.addObserver(localeObserver, REQ_LOC_CHANGE_EVENT);
  });
}

function promiseLocaleNotChanged() {
  return new Promise(resolve => {
    let localeObserver = {
      observe(aSubject, aTopic) {
        switch (aTopic) {
          case REQ_LOC_CHANGE_EVENT:
            ok(false, "Locale should not change.");
            Services.obs.removeObserver(localeObserver, REQ_LOC_CHANGE_EVENT);
            resolve();
        }
      },
    };
    Services.obs.addObserver(localeObserver, REQ_LOC_CHANGE_EVENT);
    /* eslint-disable mozilla/no-arbitrary-setTimeout */
    setTimeout(function () {
      Services.obs.removeObserver(localeObserver, REQ_LOC_CHANGE_EVENT);
      resolve();
    }, 100);
  });
}

add_task(async function test_requested_locale_array() {
  let originalLocales = Services.locale.requestedLocales;
  let localePromise = promiseLocaleChanged("de");
  await setupPolicyEngineWithJson({
    policies: {
      RequestedLocales: ["de"],
    },
  });
  await localePromise;
  Services.locale.requestedLocales = originalLocales;
});

add_task(async function test_requested_locale_string() {
  let originalLocales = Services.locale.requestedLocales;
  let localePromise = promiseLocaleChanged("fr");
  await setupPolicyEngineWithJson({
    policies: {
      RequestedLocales: "fr",
    },
  });
  await localePromise;
  Services.locale.requestedLocales = originalLocales;
});

add_task(async function test_system_locale_string() {
  let originalLocales = Services.locale.requestedLocales;

  let localePromise = promiseLocaleChanged("und");
  Services.locale.requestedLocales = ["und"];
  await localePromise;

  let systemLocale = Cc["@mozilla.org/intl/ospreferences;1"].getService(
    Ci.mozIOSPreferences
  ).systemLocale;
  localePromise = promiseLocaleChanged(systemLocale);

  await setupPolicyEngineWithJson({
    policies: {
      RequestedLocales: "",
    },
  });
  await localePromise;
  Services.locale.requestedLocales = originalLocales;
});

add_task(async function test_user_requested_locale_change() {
  let originalLocales = Services.locale.requestedLocales;
  let localePromise = promiseLocaleChanged("fr");
  await setupPolicyEngineWithJson({
    policies: {
      RequestedLocales: "fr",
    },
  });
  await localePromise;

  // Simulate user change of locale
  localePromise = promiseLocaleChanged("de");
  Services.locale.requestedLocales = ["de"];
  await localePromise;

  localePromise = promiseLocaleNotChanged("fr");
  await setupPolicyEngineWithJson({
    policies: {
      RequestedLocales: "fr",
    },
  });
  await localePromise;

  Services.locale.requestedLocales = originalLocales;
});