summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/sidebar/browser_sidebar_app_locale_changed.js
blob: 5b07da9839ceb5f3ab1aec9f7954a2c750df5a03 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * This file tests that the sidebar recreates the contents of the <tree> element
 * for live app locale switching.
 */

add_task(function cleanup() {
  registerCleanupFunction(() => {
    SidebarUI.hide();
  });
});

/**
 * @param {string} sidebarName
 */
async function testLiveReloading(sidebarName) {
  info("Showing the sidebar " + sidebarName);
  await SidebarUI.show(sidebarName);

  function getTreeChildren() {
    const sidebarDoc =
      document.querySelector("#sidebar").contentWindow.document;
    return sidebarDoc.querySelector(".sidebar-placesTreechildren");
  }

  const childrenBefore = getTreeChildren();
  ok(childrenBefore, "Found the sidebar children");
  is(childrenBefore, getTreeChildren(), "The children start out as equal");

  info("Simulating an app locale change.");
  Services.obs.notifyObservers(null, "intl:app-locales-changed");

  await TestUtils.waitForCondition(
    getTreeChildren,
    "Waiting for a new child tree element."
  );

  isnot(
    childrenBefore,
    getTreeChildren(),
    "The tree's contents are re-computed."
  );

  info("Hiding the sidebar");
  SidebarUI.hide();
}

add_task(async function test_bookmarks_sidebar() {
  await testLiveReloading("viewBookmarksSidebar");
});

add_task(async function test_history_sidebar() {
  await testLiveReloading("viewHistorySidebar");
});

add_task(async function test_ext_sidebar_panel_reloaded_on_locale_changes() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      sidebar_action: {
        default_panel: "sidebar.html",
      },
    },
    useAddonManager: "temporary",

    files: {
      "sidebar.html": `<html>
          <head>
            <meta charset="utf-8"/>
            <script src="sidebar.js"></script>
          </head>
          <body>
            A Test Sidebar
          </body>
        </html>`,
      "sidebar.js": function () {
        const { browser } = this;
        window.onload = () => {
          browser.test.sendMessage("sidebar");
        };
      },
    },
  });
  await extension.startup();
  // Test sidebar is opened on install
  await extension.awaitMessage("sidebar");

  // Test sidebar is opened on simulated locale changes.
  info("Switch browser to bidi and expect the sidebar panel to be reloaded");

  await SpecialPowers.pushPrefEnv({
    set: [["intl.l10n.pseudo", "bidi"]],
  });
  await extension.awaitMessage("sidebar");
  is(
    window.document.documentElement.getAttribute("dir"),
    "rtl",
    "browser window changed direction to rtl as expected"
  );

  await SpecialPowers.popPrefEnv();
  await extension.awaitMessage("sidebar");
  is(
    window.document.documentElement.getAttribute("dir"),
    "ltr",
    "browser window changed direction to ltr as expected"
  );

  await extension.unload();
});