summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/browser/browser_forgetthissite.js
blob: 380497aed7c6d2d26c8f25f63dadec83e0d947ac (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* 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/. */

"use strict";

// Tests the "Forget About This Site" button from the libary view
const { PromptTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/PromptTestUtils.sys.mjs"
);
const { sinon } = ChromeUtils.importESModule(
  "resource://testing-common/Sinon.sys.mjs"
);
const { ForgetAboutSite } = ChromeUtils.importESModule(
  "resource://gre/modules/ForgetAboutSite.sys.mjs"
);

const TEST_URIs = [
  { title: "0", uri: "http://example.com" },
  { title: "1", uri: "http://www.mozilla.org/test1" },
  { title: "2", uri: "http://www.mozilla.org/test2" },
  { title: "3", uri: "https://192.168.200.1/login.html" },
];

async function setup() {
  registerCleanupFunction(async function () {
    // Clean up any leftover stubs.
    sinon.restore();
  });

  let places = [];
  let transition = PlacesUtils.history.TRANSITION_TYPED;
  TEST_URIs.forEach(({ title, uri }) =>
    places.push({ uri: Services.io.newURI(uri), transition, title })
  );
  await PlacesTestUtils.addVisits(places);
}

async function teardown(organizer) {
  // Close the library window.
  await promiseLibraryClosed(organizer);
  await PlacesUtils.history.clear();
}

// Selects the sites specified by sitesToSelect
// If multiple sites are selected they can't be forgotten
// Should forget selects the answer in the confirmation dialogue
// removedEntries specifies which entries should be forgotten
async function testForgetAboutThisSite(
  sitesToSelect,
  shouldForget,
  removedEntries,
  cancelConfirmWithEsc = false
) {
  if (cancelConfirmWithEsc) {
    ok(
      !shouldForget,
      "If cancelConfirmWithEsc is set we don't expect to clear entries."
    );
  }

  ok(PlacesUtils, "checking PlacesUtils, running in chrome context?");
  await setup();
  let organizer = await promiseHistoryView();
  let doc = organizer.document;
  let tree = doc.getElementById("placeContent");

  //Sort by name in descreasing order
  tree.view._result.sortingMode =
    Ci.nsINavHistoryQueryOptions.SORT_BY_TITLE_ASCENDING;

  let selection = tree.view.selection;
  selection.clearSelection();
  sitesToSelect.forEach(index => selection.rangedSelect(index, index, true));

  let selectionCount = sitesToSelect.length;
  is(
    selection.count,
    selectionCount,
    "The selected range is as big as expected"
  );
  // Open the context menu.
  let contextmenu = doc.getElementById("placesContext");
  let popupShown = promisePopupShown(contextmenu);

  // Get cell coordinates.
  let rect = tree.getCoordsForCellItem(
    sitesToSelect[0],
    tree.columns[0],
    "text"
  );
  // Initiate a context menu for the selected cell.
  EventUtils.synthesizeMouse(
    tree.body,
    rect.x + rect.width / 2,
    rect.y + rect.height / 2,
    { type: "contextmenu", button: 2 },
    organizer
  );
  await popupShown;

  let forgetThisSite = doc.getElementById("placesContext_deleteHost");
  let hideForgetThisSite = selectionCount > 1;
  is(
    forgetThisSite.hidden,
    hideForgetThisSite,
    `The Forget this site menu item should ${
      hideForgetThisSite ? "" : "not "
    }be hidden with ${selectionCount} items selected`
  );
  if (hideForgetThisSite) {
    // Close the context menu.
    contextmenu.hidePopup();
    await teardown(organizer);
    return;
  }

  // Resolves once the confirmation prompt has been closed.
  let promptPromise;

  // Cancel prompt via esc key. We have to get the prompt closed promise
  // ourselves.
  if (cancelConfirmWithEsc) {
    promptPromise = PromptTestUtils.waitForPrompt(organizer, {
      modalType: Services.prompt.MODAL_TYPE_WINDOW,
      promptType: "confirmEx",
    }).then(dialog => {
      let dialogWindow = dialog.ui.prompt;
      let dialogClosedPromise = BrowserTestUtils.waitForEvent(
        dialogWindow.opener,
        "DOMModalDialogClosed"
      );
      EventUtils.synthesizeKey("KEY_Escape", undefined, dialogWindow);

      return dialogClosedPromise;
    });
  } else {
    // Close prompt via buttons. PromptTestUtils supplies the closed promise.
    promptPromise = PromptTestUtils.handleNextPrompt(
      organizer,
      { modalType: Services.prompt.MODAL_TYPE_WINDOW, promptType: "confirmEx" },
      { buttonNumClick: shouldForget ? 0 : 1 }
    );
  }

  // If we cancel the prompt, create stubs to check that none of the clear
  // methods are called.
  if (!shouldForget) {
    sinon.stub(ForgetAboutSite, "removeDataFromBaseDomain").resolves();
    sinon.stub(ForgetAboutSite, "removeDataFromDomain").resolves();
  }

  let pageRemovedEventPromise;
  if (shouldForget) {
    pageRemovedEventPromise =
      PlacesTestUtils.waitForNotification("page-removed");
  }

  // Execute the delete command.
  contextmenu.activateItem(forgetThisSite);

  // Wait for prompt to be handled.
  await promptPromise;

  // If we expect to remove items, wait the page-removed event to fire. If we
  // don't wait, we may test the list before any items have been removed.
  await pageRemovedEventPromise;

  if (!shouldForget) {
    ok(
      ForgetAboutSite.removeDataFromBaseDomain.notCalled &&
        ForgetAboutSite.removeDataFromDomain.notCalled,
      "Should not call ForgetAboutSite when the confirmation prompt is cancelled."
    );
    // Remove the stubs.
    sinon.restore();
  }

  // Check that the entries have been removed.
  await Promise.all(
    removedEntries.map(async ({ uri }) => {
      Assert.ok(
        !(await PlacesUtils.history.fetch(uri)),
        `History entry for ${uri} has been correctly removed`
      );
    })
  );
  await Promise.all(
    TEST_URIs.filter(x => !removedEntries.includes(x)).map(async ({ uri }) => {
      Assert.ok(
        await PlacesUtils.history.fetch(uri),
        `History entry for ${uri} has been kept`
      );
    })
  );

  // Cleanup.
  await teardown(organizer);
}

/*
 * Opens the history view in the PlacesOrganziner window
 * @returns {Promise}
 * @resolves The PlacesOrganizer
 */
async function promiseHistoryView() {
  let organizer = await promiseLibrary();

  // Select History in the left pane.
  let po = organizer.PlacesOrganizer;
  po.selectLeftPaneBuiltIn("History");

  let histContainer = po._places.selectedNode.QueryInterface(
    Ci.nsINavHistoryContainerResultNode
  );
  histContainer.containerOpen = true;
  po._places.selectNode(histContainer.getChild(0));

  return organizer;
}
/*
 * @returns {Promise}
 * @resolves once the popup is shown
 */
function promisePopupShown(popup) {
  return new Promise(resolve => {
    popup.addEventListener(
      "popupshown",
      function () {
        resolve();
      },
      { capture: true, once: true }
    );
  });
}

// This test makes sure that the Forget This Site command is hidden for multiple
// selections.
add_task(async function selectMultiple() {
  await testForgetAboutThisSite([0, 1]);
});

// This test makes sure that forgetting "http://www.mozilla.org/test2" also removes "http://www.mozilla.org/test1"
add_task(async function forgettingBasedomain() {
  await testForgetAboutThisSite([1], true, TEST_URIs.slice(1, 3));
});

// This test makes sure that forgetting by IP address works
add_task(async function forgettingIPAddress() {
  await testForgetAboutThisSite([3], true, TEST_URIs.slice(3, 4));
});

// This test makes sure that forgetting file URLs works
add_task(async function dontAlwaysForget() {
  await testForgetAboutThisSite([0], false, []);
});

// When cancelling the confirmation prompt via ESC key, no entries should be
// cleared.
add_task(async function cancelConfirmWithEsc() {
  await testForgetAboutThisSite([0], false, [], true);
});