summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/browser/browser_bookmark_change_location.js
blob: 3a82b67a93c33775748130a731df41c8d5926102 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

/**
 * Test that the bookmark location (url) can be changed from the toolbar and the sidebar.
 */
"use strict";

const TEST_URL = "about:buildconfig";
const TEST_URL2 = "about:credits";
const TEST_URL3 = "about:config";

// Setup.
add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.toolbars.bookmarks.visibility", "always"]],
  });

  // The following initialization code is necessary to avoid a frequent
  // intermittent failure in verify-fission where, due to timings, we may or
  // may not import default bookmarks. We also want to avoid the empty toolbar
  // placeholder shifting stuff around.
  info("Ensure Places init is complete");
  let placesInitCompleteObserved = TestUtils.topicObserved(
    "places-browser-init-complete"
  );
  Cc["@mozilla.org/browser/browserglue;1"]
    .getService(Ci.nsIObserver)
    .observe(null, "browser-glue-test", "places-browser-init-complete");
  await placesInitCompleteObserved;
  info("Add a bookmark to avoid showing the empty toolbar placeholder.");
  await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    title: "initial",
    url: TEST_URL,
  });

  let toolbar = document.getElementById("PersonalToolbar");
  let wasCollapsed = toolbar.collapsed;
  if (wasCollapsed) {
    info("Show the bookmarks toolbar");
    await promiseSetToolbarVisibility(toolbar, true);
    info("Ensure toolbar visibility was updated");
    await BrowserTestUtils.waitForEvent(
      toolbar,
      "BookmarksToolbarVisibilityUpdated"
    );
  }

  // Cleanup.
  registerCleanupFunction(async () => {
    // Collapse the personal toolbar if needed.
    if (wasCollapsed) {
      await promiseSetToolbarVisibility(toolbar, false);
    }
    await PlacesUtils.bookmarks.eraseEverything();
  });
});

add_task(async function test_change_location_from_Toolbar() {
  let toolbarBookmark = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    title: "",
    url: TEST_URL,
  });

  let toolbarNode = getToolbarNodeForItemGuid(toolbarBookmark.guid);

  await withBookmarksDialog(
    false,
    async function openPropertiesDialog() {
      let placesContext = document.getElementById("placesContext");
      let promisePopup = BrowserTestUtils.waitForEvent(
        placesContext,
        "popupshown"
      );
      EventUtils.synthesizeMouseAtCenter(toolbarNode, {
        button: 2,
        type: "contextmenu",
      });
      await promisePopup;

      let properties = document.getElementById(
        "placesContext_show_bookmark:info"
      );
      placesContext.activateItem(properties);
    },
    async function test(dialogWin) {
      // Check the initial location.
      let locationPicker = dialogWin.document.getElementById(
        "editBMPanel_locationField"
      );
      Assert.equal(
        locationPicker.value,
        TEST_URL,
        "EditBookmark: The current location is the expected one."
      );

      // To check whether the lastModified field will be updated correctly.
      let lastModified = _getLastModified(toolbarBookmark.guid);

      // Update the "location" field.
      fillBookmarkTextField(
        "editBMPanel_locationField",
        TEST_URL2,
        dialogWin,
        false
      );

      locationPicker.blur();

      Assert.equal(
        locationPicker.value,
        TEST_URL2,
        "EditBookmark: The changed location is the expected one."
      );

      locationPicker.focus();
      // Confirm and close the dialog.
      EventUtils.synthesizeKey("VK_RETURN", {}, dialogWin);

      await TestUtils.waitForCondition(
        () => _getLastModified(toolbarBookmark.guid) > lastModified,
        "EditBookmark: The lastModified will be greater than before updating."
      );
    }
  );

  let updatedBm = await PlacesUtils.bookmarks.fetch(toolbarBookmark.guid);
  Assert.equal(
    updatedBm.url,
    TEST_URL2,
    "EditBookmark: Should have updated the bookmark location in the database."
  );
});

add_task(async function test_change_location_from_Sidebar() {
  let bm = await PlacesUtils.bookmarks.fetch({ url: TEST_URL2 });

  await withSidebarTree("bookmarks", async function (tree) {
    tree.selectItems([bm.guid]);

    await withBookmarksDialog(
      false,
      function openPropertiesDialog() {
        tree.controller.doCommand("placesCmd_show:info");
      },
      async function test(dialogWin) {
        // Check the initial location.
        let locationPicker = dialogWin.document.getElementById(
          "editBMPanel_locationField"
        );
        Assert.equal(
          locationPicker.value,
          TEST_URL2,
          "Sidebar - EditBookmark: The current location is the expected one."
        );

        // To check whether the lastModified field will be updated correctly.
        let lastModified = _getLastModified(bm.guid);

        // Update the "location" field.
        fillBookmarkTextField(
          "editBMPanel_locationField",
          TEST_URL3,
          dialogWin,
          false
        );

        Assert.equal(
          locationPicker.value,
          TEST_URL3,
          "Sidebar - EditBookmark: The location is changed in dialog for prefered one."
        );

        // Confirm and close the dialog.
        EventUtils.synthesizeKey("VK_RETURN", {}, dialogWin);

        await TestUtils.waitForCondition(
          () => _getLastModified(bm.guid) > lastModified,
          "Sidebar - EditBookmark: The lastModified will be greater than before updating."
        );
      }
    );

    let updatedBm = await PlacesUtils.bookmarks.fetch(bm.guid);
    Assert.equal(
      updatedBm.url,
      TEST_URL3,
      "Sidebar - EditBookmark: Should have updated the bookmark location in the database."
    );
  });
});

function _getLastModified(guid) {
  const toolbarNode = PlacesUtils.getFolderContents(
    PlacesUtils.bookmarks.toolbarGuid
  ).root;

  try {
    for (let i = 0; i < toolbarNode.childCount; i++) {
      const node = toolbarNode.getChild(i);
      if (node.bookmarkGuid === guid) {
        return node.lastModified;
      }
    }

    throw new Error(`Node for ${guid} was not found`);
  } finally {
    toolbarNode.containerOpen = false;
  }
}