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

"use strict";

add_setup(async () => {
  // Ensure all bookmarks cleared before the test starts.
  await PlacesUtils.bookmarks.eraseEverything();
});

add_task(async function test() {
  let bookmark = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    title: "Plain Bob",
    url: "http://example.com",
  });

  registerCleanupFunction(async () => {
    await PlacesUtils.bookmarks.remove(bookmark);
  });

  let sidebarBox = document.getElementById("sidebar-box");
  is(sidebarBox.hidden, true, "The sidebar should be hidden");

  // Uncollapse the personal toolbar if needed.
  let toolbar = document.getElementById("PersonalToolbar");
  let wasCollapsed = toolbar.collapsed;
  if (wasCollapsed) {
    await promiseSetToolbarVisibility(toolbar, true);
  }

  let sidebar = await promiseLoadedSidebar("viewBookmarksSidebar");
  registerCleanupFunction(() => {
    SidebarUI.hide();
  });

  // Focus the tree and check if its controller is returned.
  let tree = sidebar.contentDocument.getElementById("bookmarks-view");
  tree.focus();

  let controller = PlacesUIUtils.getControllerForCommand(
    window,
    "placesCmd_copy"
  );
  let treeController =
    tree.controllers.getControllerForCommand("placesCmd_copy");
  ok(controller == treeController, "tree controller was returned");

  // Open the context menu for a toolbar item, and check if the toolbar's
  // controller is returned.
  let toolbarItems = document.getElementById("PlacesToolbarItems");
  // Ensure the toolbar has displayed the bookmark. This might be async, so
  // wait a little if necessary.
  await TestUtils.waitForCondition(
    () => toolbarItems.children.length == 1,
    "Should have only one item on the toolbar"
  );

  let placesContext = document.getElementById("placesContext");
  let popupShownPromise = BrowserTestUtils.waitForEvent(
    placesContext,
    "popupshown"
  );
  EventUtils.synthesizeMouse(
    toolbarItems.children[0],
    4,
    4,
    { type: "contextmenu", button: 2 },
    window
  );
  await popupShownPromise;

  controller = PlacesUIUtils.getControllerForCommand(window, "placesCmd_copy");
  let toolbarController = document
    .getElementById("PlacesToolbar")
    .controllers.getControllerForCommand("placesCmd_copy");
  ok(controller == toolbarController, "the toolbar controller was returned");

  let popupHiddenPromise = BrowserTestUtils.waitForEvent(
    placesContext,
    "popuphidden"
  );
  placesContext.hidePopup();
  await popupHiddenPromise;

  // Now that the context menu is closed, try to get the tree controller again.
  tree.focus();
  controller = PlacesUIUtils.getControllerForCommand(window, "placesCmd_copy");
  ok(controller == treeController, "tree controller was returned");

  if (wasCollapsed) {
    await promiseSetToolbarVisibility(toolbar, false);
  }
});

function promiseLoadedSidebar(cmd) {
  return new Promise(resolve => {
    let sidebar = document.getElementById("sidebar");
    sidebar.addEventListener(
      "load",
      function () {
        executeSoon(() => resolve(sidebar));
      },
      { capture: true, once: true }
    );

    SidebarUI.show(cmd);
  });
}