summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/pageActions/browser_PageActions_bookmark.js
blob: a77095f2cddd105c474f5ca92fe9707a94222d55 (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
"use strict";

add_task(async function starButtonCtrlClick() {
  // Open a unique page.
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  let url = "http://example.com/browser_page_action_star_button";
  await BrowserTestUtils.withNewTab(url, async () => {
    StarUI._createPanelIfNeeded();
    // The button ignores activation while the bookmarked status is being
    // updated. So, wait for it to finish updating.
    await TestUtils.waitForCondition(
      () => BookmarkingUI.status != BookmarkingUI.STATUS_UPDATING
    );

    const popup = document.getElementById("editBookmarkPanel");
    const starButtonBox = document.getElementById("star-button-box");

    let shownPromise = promisePanelShown(popup);
    EventUtils.synthesizeMouseAtCenter(starButtonBox, { ctrlKey: true });
    await shownPromise;
    ok(true, "Panel shown after button pressed");

    let hiddenPromise = promisePanelHidden(popup);
    document.getElementById("editBookmarkPanelRemoveButton").click();
    await hiddenPromise;
  });
});

add_task(async function bookmark() {
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  const url = "http://example.com/browser_page_action_menu";

  const win = await BrowserTestUtils.openNewBrowserWindow();
  registerCleanupFunction(async () => {
    await BrowserTestUtils.closeWindow(win);
  });

  await BrowserTestUtils.withNewTab(
    { gBrowser: win.gBrowser, url },
    async () => {
      // The bookmark button should not be starred.
      const bookmarkButton =
        win.BrowserPageActions.urlbarButtonNodeForActionID("bookmark");
      Assert.ok(!bookmarkButton.hasAttribute("starred"));

      info("Click the button.");
      // The button ignores activation while the bookmarked status is being
      // updated. So, wait for it to finish updating.
      await TestUtils.waitForCondition(
        () => BookmarkingUI.status != BookmarkingUI.STATUS_UPDATING
      );
      const starUIPanel = win.StarUI.panel;
      let panelShown = BrowserTestUtils.waitForPopupEvent(starUIPanel, "shown");
      EventUtils.synthesizeMouseAtCenter(bookmarkButton, {}, win);
      await panelShown;
      is(
        await PlacesUtils.bookmarks.fetch({ url }),
        null,
        "Bookmark has not been created before save."
      );

      // The bookmark button should now be starred.
      Assert.equal(bookmarkButton.firstChild.getAttribute("starred"), "true");

      info("Save the bookmark.");
      const onItemAddedPromise = PlacesTestUtils.waitForNotification(
        "bookmark-added",
        events => events.some(event => event.url == url)
      );
      starUIPanel.hidePopup();
      await onItemAddedPromise;

      info("Click it again.");
      // The button ignores activation while the bookmarked status is being
      // updated. So, wait for it to finish updating.
      await TestUtils.waitForCondition(
        () => BookmarkingUI.status != BookmarkingUI.STATUS_UPDATING
      );
      panelShown = BrowserTestUtils.waitForPopupEvent(starUIPanel, "shown");
      EventUtils.synthesizeMouseAtCenter(bookmarkButton, {}, win);
      await panelShown;

      info("Remove the bookmark.");
      const onItemRemovedPromise = PlacesTestUtils.waitForNotification(
        "bookmark-removed",
        events => events.some(event => event.url == url)
      );
      win.StarUI._element("editBookmarkPanelRemoveButton").click();
      await onItemRemovedPromise;
    }
  );
});

add_task(async function bookmarkNoEditDialog() {
  const url =
    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
    "http://example.com/browser_page_action_menu_no_edit_dialog";

  await SpecialPowers.pushPrefEnv({
    set: [["browser.bookmarks.editDialog.showForNewBookmarks", false]],
  });
  const win = await BrowserTestUtils.openNewBrowserWindow();
  registerCleanupFunction(async () => {
    await BrowserTestUtils.closeWindow(win);
    await PlacesUtils.bookmarks.eraseEverything();
  });

  await BrowserTestUtils.withNewTab(
    { gBrowser: win.gBrowser, url },
    async () => {
      info("Click the button.");
      // The button ignores activation while the bookmarked status is being
      // updated. So, wait for it to finish updating.
      await TestUtils.waitForCondition(
        () => BookmarkingUI.status != BookmarkingUI.STATUS_UPDATING
      );
      const bookmarkButton = win.document.getElementById(
        BrowserPageActions.urlbarButtonNodeIDForActionID("bookmark")
      );

      // The bookmark should be saved immediately after clicking the star.
      const onItemAddedPromise = PlacesTestUtils.waitForNotification(
        "bookmark-added",
        events => events.some(event => event.url == url)
      );
      EventUtils.synthesizeMouseAtCenter(bookmarkButton, {}, win);
      await onItemAddedPromise;
    }
  );
});