summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/browser/browser_addBookmarkForFrame.js
blob: 44a7f5e17adc1de41932ac9a1b8420f3208f82da (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
/**
 * Tests that the add bookmark for frame dialog functions correctly.
 */

const BASE_URL =
  "http://mochi.test:8888/browser/browser/components/places/tests/browser";
const PAGE_URL = BASE_URL + "/framedPage.html";
const LEFT_URL = BASE_URL + "/frameLeft.html";
const RIGHT_URL = BASE_URL + "/frameRight.html";

function activateBookmarkFrame(contentAreaContextMenu) {
  let popupHiddenPromise = BrowserTestUtils.waitForEvent(
    contentAreaContextMenu,
    "popuphidden"
  );
  return async function () {
    let frameMenuItem = document.getElementById("frame");
    let frameMenu = frameMenuItem.querySelector(":scope > menupopup");
    let frameMenuShown = BrowserTestUtils.waitForEvent(frameMenu, "popupshown");
    frameMenuItem.openMenu(true);
    await frameMenuShown;
    let bookmarkFrame = document.getElementById("context-bookmarkframe");
    frameMenu.activateItem(bookmarkFrame);
    await popupHiddenPromise;
  };
}

async function withAddBookmarkForFrame(taskFn) {
  // Open a tab and wait for all the subframes to load.
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE_URL);

  let contentAreaContextMenu = document.getElementById(
    "contentAreaContextMenu"
  );

  let popupShownPromise = BrowserTestUtils.waitForEvent(
    contentAreaContextMenu,
    "popupshown"
  );
  await BrowserTestUtils.synthesizeMouseAtCenter(
    "#left",
    { type: "contextmenu", button: 2 },
    gBrowser.selectedBrowser
  );
  await popupShownPromise;

  await withBookmarksDialog(
    true,
    activateBookmarkFrame(contentAreaContextMenu),
    taskFn
  );

  BrowserTestUtils.removeTab(tab);
}

add_task(async function test_open_add_bookmark_for_frame() {
  info("Test basic opening of the add bookmark for frame dialog.");
  await withAddBookmarkForFrame(async dialogWin => {
    let namepicker = dialogWin.document.getElementById(
      "editBMPanel_namePicker"
    );
    Assert.ok(!namepicker.readOnly, "Name field is writable");
    Assert.equal(namepicker.value, "Left frame", "Name field is correct.");

    let expectedFolder = "BookmarksToolbarFolderTitle";
    let expectedFolderName = PlacesUtils.getString(expectedFolder);

    let folderPicker = dialogWin.document.getElementById(
      "editBMPanel_folderMenuList"
    );

    await TestUtils.waitForCondition(
      () => folderPicker.selectedItem.label == expectedFolderName,
      "Dialog: The folder is the expected one."
    );

    let tagsField = dialogWin.document.getElementById("editBMPanel_tagsField");
    Assert.equal(tagsField.value, "", "Dialog: The tags field should be empty");
  });
});

add_task(async function test_move_bookmark_whilst_add_bookmark_open() {
  info(
    "EditBookmark: Test moving a bookmark whilst the add bookmark for frame dialog is open."
  );
  await PlacesUtils.bookmarks.eraseEverything();
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE_URL);

  let contentAreaContextMenu = document.getElementById(
    "contentAreaContextMenu"
  );

  let popupShownPromise = BrowserTestUtils.waitForEvent(
    contentAreaContextMenu,
    "popupshown"
  );
  await BrowserTestUtils.synthesizeMouseAtCenter(
    "#left",
    { type: "contextmenu", button: 2 },
    gBrowser.selectedBrowser
  );
  await popupShownPromise;

  await withBookmarksDialog(
    false,
    activateBookmarkFrame(contentAreaContextMenu),
    async function (dialogWin) {
      let expectedGuid = await PlacesUIUtils.defaultParentGuid;
      let expectedFolder = "BookmarksToolbarFolderTitle";
      let expectedFolderName = PlacesUtils.getString(expectedFolder);

      let folderPicker = dialogWin.document.getElementById(
        "editBMPanel_folderMenuList"
      );

      Assert.equal(
        folderPicker.selectedItem.label,
        expectedFolderName,
        "EditBookmark: The folder is the expected one."
      );

      Assert.equal(
        folderPicker.getAttribute("selectedGuid"),
        expectedGuid,
        "EditBookmark: Should have the correct default guid selected"
      );

      dialogWin.document.getElementById("editBMPanel_foldersExpander").click();
      let folderTree = dialogWin.document.getElementById(
        "editBMPanel_folderTree"
      );
      folderTree.selectItems([PlacesUtils.bookmarks.menuGuid]);
      folderTree.blur();

      EventUtils.synthesizeKey("VK_RETURN", {}, dialogWin);
    }
  );
  let url = makeURI(LEFT_URL);
  // Check the bookmark has been moved as expected.
  let bookmark = await PlacesUtils.bookmarks.fetch({ url });

  Assert.equal(
    bookmark.parentGuid,
    PlacesUtils.bookmarks.menuGuid,
    "EditBookmark: The bookmark should be moved to the expected folder."
  );

  BrowserTestUtils.removeTab(tab);
  await PlacesUtils.bookmarks.eraseEverything();
});