tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

RemoteL10n.test.js (7124B)


      1 import { RemoteL10n, _RemoteL10n } from "modules/RemoteL10n.sys.mjs";
      2 import { GlobalOverrider } from "tests/unit/utils";
      3 
      4 describe("RemoteL10n", () => {
      5  let sandbox;
      6  let globals;
      7  let domL10nStub;
      8  let l10nRegStub;
      9  let l10nRegInstance;
     10  let fileSourceStub;
     11  beforeEach(() => {
     12    sandbox = sinon.createSandbox();
     13    globals = new GlobalOverrider();
     14    domL10nStub = sandbox.stub();
     15    l10nRegInstance = {
     16      hasSource: sandbox.stub(),
     17      registerSources: sandbox.stub(),
     18      removeSources: sandbox.stub(),
     19    };
     20 
     21    fileSourceStub = sandbox.stub();
     22    l10nRegStub = {
     23      getInstance: () => {
     24        return l10nRegInstance;
     25      },
     26    };
     27    globals.set("DOMLocalization", domL10nStub);
     28    globals.set("L10nRegistry", l10nRegStub);
     29    globals.set("L10nFileSource", fileSourceStub);
     30  });
     31  afterEach(() => {
     32    sandbox.restore();
     33    globals.restore();
     34  });
     35  describe("#RemoteL10n", () => {
     36    it("should create a new instance", () => {
     37      assert.ok(new _RemoteL10n());
     38    });
     39    it("should create a DOMLocalization instance", () => {
     40      domL10nStub.returns({ instance: true });
     41      const instance = new _RemoteL10n();
     42 
     43      assert.propertyVal(instance._createDOML10n(), "instance", true);
     44      assert.calledOnce(domL10nStub);
     45    });
     46    it("should create a new instance", () => {
     47      domL10nStub.returns({ instance: true });
     48      const instance = new _RemoteL10n();
     49 
     50      assert.ok(instance.l10n);
     51 
     52      instance.reloadL10n();
     53 
     54      assert.ok(instance.l10n);
     55 
     56      assert.calledTwice(domL10nStub);
     57    });
     58    it("should reuse the instance", () => {
     59      domL10nStub.returns({ instance: true });
     60      const instance = new _RemoteL10n();
     61 
     62      assert.ok(instance.l10n);
     63      assert.ok(instance.l10n);
     64 
     65      assert.calledOnce(domL10nStub);
     66    });
     67  });
     68  describe("#_createDOML10n", () => {
     69    it("should load the remote Fluent file if USE_REMOTE_L10N_PREF is true", async () => {
     70      sandbox.stub(global.Services.prefs, "getBoolPref").returns(true);
     71      l10nRegInstance.hasSource.returns(false);
     72      RemoteL10n._createDOML10n();
     73 
     74      assert.calledOnce(domL10nStub);
     75      const { args } = domL10nStub.firstCall;
     76      // The first arg is the resource array,
     77      // the second one is false (use async),
     78      // and the third one is the bundle generator.
     79      assert.equal(args.length, 2);
     80      assert.deepEqual(args[0], [
     81        "branding/brand.ftl",
     82        "browser/defaultBrowserNotification.ftl",
     83        "browser/newtab/asrouter.ftl",
     84        "browser/profiles.ftl",
     85        "browser/termsofuse.ftl",
     86        "toolkit/branding/brandings.ftl",
     87      ]);
     88      assert.isFalse(args[1]);
     89      assert.calledOnce(l10nRegInstance.hasSource);
     90      assert.calledOnce(l10nRegInstance.registerSources);
     91      assert.notCalled(l10nRegInstance.removeSources);
     92    });
     93    it("should load the local Fluent file if USE_REMOTE_L10N_PREF is false", () => {
     94      sandbox.stub(global.Services.prefs, "getBoolPref").returns(false);
     95      l10nRegInstance.hasSource.returns(true);
     96      RemoteL10n._createDOML10n();
     97 
     98      const { args } = domL10nStub.firstCall;
     99      // The first arg is the resource array,
    100      // the second one is false (use async),
    101      // and the third one is null.
    102      assert.equal(args.length, 2);
    103      assert.deepEqual(args[0], [
    104        "branding/brand.ftl",
    105        "browser/defaultBrowserNotification.ftl",
    106        "browser/newtab/asrouter.ftl",
    107        "browser/profiles.ftl",
    108        "browser/termsofuse.ftl",
    109        "toolkit/branding/brandings.ftl",
    110      ]);
    111      assert.isFalse(args[1]);
    112      assert.calledOnce(l10nRegInstance.hasSource);
    113      assert.notCalled(l10nRegInstance.registerSources);
    114      assert.calledOnce(l10nRegInstance.removeSources);
    115    });
    116  });
    117  describe("#createElement", () => {
    118    let doc;
    119    let instance;
    120    let setStringStub;
    121    let elem;
    122    beforeEach(() => {
    123      elem = document.createElement("div");
    124      doc = {
    125        createElement: sandbox.stub().returns(elem),
    126        createElementNS: sandbox.stub().returns(elem),
    127      };
    128      instance = new _RemoteL10n();
    129      setStringStub = sandbox.stub(instance, "setString");
    130    });
    131    it("should call createElement if string_id is defined", () => {
    132      instance.createElement(doc, "span", { content: { string_id: "foo" } });
    133 
    134      assert.calledOnce(doc.createElement);
    135    });
    136    it("should call createElementNS if string_id is not present", () => {
    137      instance.createElement(doc, "span", { content: "foo" });
    138 
    139      assert.calledOnce(doc.createElementNS);
    140    });
    141    it("should set classList", () => {
    142      instance.createElement(doc, "span", { classList: "foo" });
    143 
    144      assert.isTrue(elem.classList.contains("foo"));
    145    });
    146    it("should call setString", () => {
    147      const options = { classList: "foo" };
    148      instance.createElement(doc, "span", options);
    149 
    150      assert.calledOnce(setStringStub);
    151      assert.calledWithExactly(setStringStub, elem, options);
    152    });
    153  });
    154  describe("#setString", () => {
    155    let instance;
    156    beforeEach(() => {
    157      instance = new _RemoteL10n();
    158    });
    159    it("should set fluent variables and id", () => {
    160      let el = { setAttribute: sandbox.stub() };
    161      instance.setString(el, {
    162        content: { string_id: "foo" },
    163        attributes: { bar: "bar", baz: "baz" },
    164      });
    165 
    166      assert.calledThrice(el.setAttribute);
    167      assert.calledWithExactly(el.setAttribute, "fluent-variable-bar", "bar");
    168      assert.calledWithExactly(el.setAttribute, "fluent-variable-baz", "baz");
    169      assert.calledWithExactly(el.setAttribute, "fluent-remote-id", "foo");
    170    });
    171    it("should set content if no string_id", () => {
    172      let el = { setAttribute: sandbox.stub() };
    173      instance.setString(el, { content: "foo" });
    174 
    175      assert.notCalled(el.setAttribute);
    176      assert.equal(el.textContent, "foo");
    177    });
    178  });
    179  describe("#isLocaleSupported", () => {
    180    it("should return true if the locale is en-US", () => {
    181      assert.ok(RemoteL10n.isLocaleSupported("en-US"));
    182    });
    183    it("should return true if the locale is in all-locales", () => {
    184      assert.ok(RemoteL10n.isLocaleSupported("en-CA"));
    185    });
    186    it("should return false if the locale is not in all-locales", () => {
    187      assert.ok(!RemoteL10n.isLocaleSupported("und"));
    188    });
    189  });
    190  describe("#formatLocalizableText", () => {
    191    let instance;
    192    let formatValueStub;
    193    beforeEach(() => {
    194      instance = new _RemoteL10n();
    195      formatValueStub = sandbox.stub();
    196      sandbox
    197        .stub(instance, "l10n")
    198        .get(() => ({ formatValue: formatValueStub }));
    199    });
    200    it("should localize a string_id", async () => {
    201      formatValueStub.resolves("VALUE");
    202 
    203      assert.equal(
    204        await instance.formatLocalizableText({ string_id: "ID" }),
    205        "VALUE"
    206      );
    207      assert.calledOnce(formatValueStub);
    208    });
    209    it("should pass through a string", async () => {
    210      formatValueStub.reset();
    211 
    212      assert.equal(
    213        await instance.formatLocalizableText("unchanged"),
    214        "unchanged"
    215      );
    216      assert.isFalse(formatValueStub.called);
    217    });
    218  });
    219 });