summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/browser/browser_sidebarpanels_click.js
blob: 4b231c92b0a43c124ecb0b56cff0c1a620b3f952 (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
/* 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/. */

// This test makes sure that the items in the bookmarks and history sidebar
// panels are clickable in both LTR and RTL modes.

var sidebar;

function pushPref(name, val) {
  return SpecialPowers.pushPrefEnv({ set: [[name, val]] });
}

function popPref() {
  return SpecialPowers.popPrefEnv();
}

add_task(async function test_sidebarpanels_click() {
  ignoreAllUncaughtExceptions();

  const BOOKMARKS_SIDEBAR_ID = "viewBookmarksSidebar";
  const BOOKMARKS_SIDEBAR_TREE_ID = "bookmarks-view";
  const HISTORY_SIDEBAR_ID = "viewHistorySidebar";
  const HISTORY_SIDEBAR_TREE_ID = "historyTree";
  const TEST_URL =
    "http://mochi.test:8888/browser/browser/components/places/tests/browser/sidebarpanels_click_test_page.html";

  // If a sidebar is already open, close it.
  if (!document.getElementById("sidebar-box").hidden) {
    ok(
      false,
      "Unexpected sidebar found - a previous test failed to cleanup correctly"
    );
    SidebarUI.hide();
  }

  // Ensure history is clean before starting the test.
  await PlacesUtils.history.clear();

  sidebar = document.getElementById("sidebar");
  let tests = [];

  tests.push({
    _bookmark: null,
    async init() {
      // Add a bookmark to the Unfiled Bookmarks folder.
      this._bookmark = await PlacesUtils.bookmarks.insert({
        parentGuid: PlacesUtils.bookmarks.unfiledGuid,
        title: "test",
        url: TEST_URL,
      });
    },
    prepare() {},
    async selectNode(tree) {
      tree.selectItems([this._bookmark.guid]);
    },
    cleanup(aCallback) {
      return PlacesUtils.bookmarks.remove(this._bookmark);
    },
    sidebarName: BOOKMARKS_SIDEBAR_ID,
    treeName: BOOKMARKS_SIDEBAR_TREE_ID,
    desc: "Bookmarks sidebar test",
  });

  tests.push({
    async init() {
      // Add a history entry.
      let uri = Services.io.newURI(TEST_URL);
      await PlacesTestUtils.addVisits({
        uri,
        visitDate: Date.now() * 1000,
        transition: PlacesUtils.history.TRANSITION_TYPED,
      });
    },
    prepare() {
      sidebar.contentDocument.getElementById("byvisited").doCommand();
    },
    selectNode(tree) {
      tree.selectNode(tree.view.nodeForTreeIndex(0));
      is(
        tree.selectedNode.uri,
        TEST_URL,
        "The correct visit has been selected"
      );
      is(tree.selectedNode.itemId, -1, "The selected node is not bookmarked");
    },
    cleanup(aCallback) {
      return PlacesUtils.history.clear();
    },
    sidebarName: HISTORY_SIDEBAR_ID,
    treeName: HISTORY_SIDEBAR_TREE_ID,
    desc: "History sidebar test",
  });

  for (let test of tests) {
    gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser);
    info("Running " + test.desc + " in LTR mode");
    await testPlacesPanel(test);

    await pushPref("intl.l10n.pseudo", "bidi");
    info("Running " + test.desc + " in RTL mode");
    await testPlacesPanel(test);
    await popPref();

    // Remove tabs created by sub-tests.
    while (gBrowser.tabs.length > 1) {
      gBrowser.removeTab(gBrowser.tabs[gBrowser.tabs.length - 1]);
    }
  }
});

async function testPlacesPanel(testInfo) {
  await testInfo.init();

  let promise = new Promise(resolve => {
    sidebar.addEventListener(
      "load",
      function () {
        executeSoon(async function () {
          testInfo.prepare();

          let tree = sidebar.contentDocument.getElementById(testInfo.treeName);

          // Select the inserted places item.
          await testInfo.selectNode(tree);

          let promiseAlert = promiseAlertDialogObserved();

          synthesizeClickOnSelectedTreeCell(tree);
          // Now, wait for the observer to catch the alert dialog.
          // If something goes wrong, the test will time out at this stage.
          // Note that for the history sidebar, the URL itself is not opened,
          // and Places will show the load-js-data-url-error prompt as an alert
          // box, which means that the click actually worked, so it's good enough
          // for the purpose of this test.

          await promiseAlert;

          executeSoon(async function () {
            SidebarUI.hide();
            await testInfo.cleanup();
            resolve();
          });
        });
      },
      { capture: true, once: true }
    );
  });

  SidebarUI.show(testInfo.sidebarName);

  return promise;
}

function promiseAlertDialogObserved() {
  return new Promise(resolve => {
    async function observer(subject) {
      info("alert dialog observed as expected");
      Services.obs.removeObserver(observer, "common-dialog-loaded");
      Services.obs.removeObserver(observer, "tabmodal-dialog-loaded");

      if (subject.Dialog) {
        subject.Dialog.ui.button0.click();
      } else {
        subject.querySelector(".tabmodalprompt-button0").click();
      }
      resolve();
    }
    Services.obs.addObserver(observer, "common-dialog-loaded");
    Services.obs.addObserver(observer, "tabmodal-dialog-loaded");
  });
}