summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/browser/browser_bookmark_titles.js
blob: 8b989b19583b8083084f336c354c0fd9e7e403af (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
/* 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/. */

// This file is tests for the default titles that new bookmarks get.

var tests = [
  // Common page.
  {
    url: "http://example.com/browser/browser/components/places/tests/browser/dummy_page.html",
    title: "Dummy test page",
    isError: false,
  },
  // Data URI.
  {
    url: "data:text/html;charset=utf-8,<title>test%20data:%20url</title>",
    title: "test data: url",
    isError: false,
  },
  // about:neterror
  {
    url: "data:application/xhtml+xml,",
    title: "data:application/xhtml+xml,",
    isError: true,
  },
  // about:certerror
  {
    url: "https://untrusted.example.com/somepage.html",
    title: "https://untrusted.example.com/somepage.html",
    isError: true,
  },
];

SpecialPowers.pushPrefEnv({
  set: [["browser.bookmarks.editDialog.showForNewBookmarks", false]],
});

add_task(async function check_default_bookmark_title() {
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "http://www.example.com/"
  );
  let browser = tab.linkedBrowser;

  // Test that a bookmark of each URI gets the corresponding default title.
  for (let { url, title, isError } of tests) {
    let promiseLoaded = BrowserTestUtils.browserLoaded(
      browser,
      false,
      url,
      isError
    );
    BrowserTestUtils.loadURIString(browser, url);
    await promiseLoaded;

    await checkBookmark(url, title);
  }

  // Network failure test: now that dummy_page.html is in history, bookmarking
  // it should give the last known page title as the default bookmark title.

  // Simulate a network outage with offline mode. (Localhost is still
  // accessible in offline mode, so disable the test proxy as well.)
  BrowserOffline.toggleOfflineStatus();
  let proxy = Services.prefs.getIntPref("network.proxy.type");
  Services.prefs.setIntPref("network.proxy.type", 0);
  registerCleanupFunction(function () {
    BrowserOffline.toggleOfflineStatus();
    Services.prefs.setIntPref("network.proxy.type", proxy);
  });

  // LOAD_FLAGS_BYPASS_CACHE isn't good enough. So clear the cache.
  Services.cache2.clear();

  let { url, title } = tests[0];

  let promiseLoaded = BrowserTestUtils.browserLoaded(
    browser,
    false,
    null,
    true
  );
  BrowserTestUtils.loadURIString(browser, url);
  await promiseLoaded;

  // The offline mode test is only good if the page failed to load.
  await SpecialPowers.spawn(browser, [], function () {
    Assert.equal(
      content.document.documentURI.substring(0, 14),
      "about:neterror",
      "Offline mode successfully simulated network outage."
    );
  });
  await checkBookmark(url, title);

  BrowserTestUtils.removeTab(tab);
});

// Bookmark the current page and confirm that the new bookmark has the expected
// title. (Then delete the bookmark.)
async function checkBookmark(url, expected_title) {
  Assert.equal(
    gBrowser.selectedBrowser.currentURI.spec,
    url,
    "Trying to bookmark the expected uri"
  );

  let promiseBookmark = PlacesTestUtils.waitForNotification(
    "bookmark-added",
    events =>
      events.some(
        ({ url: eventUrl }) =>
          eventUrl == gBrowser.selectedBrowser.currentURI.spec
      )
  );
  PlacesCommandHook.bookmarkPage();
  await promiseBookmark;

  let bookmark = await PlacesUtils.bookmarks.fetch({ url });

  Assert.ok(bookmark, "Found the expected bookmark");
  Assert.equal(
    bookmark.title,
    expected_title,
    "Bookmark got a good default title."
  );

  await PlacesUtils.bookmarks.remove(bookmark);
}