michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0:
michael@0: // This file is tests for the default titles that new bookmarks get.
michael@0:
michael@0: let tests = [
michael@0: // Common page.
michael@0: ['http://example.com/browser/browser/base/content/test/general/dummy_page.html',
michael@0: 'Dummy test page'],
michael@0: // Data URI.
michael@0: ['data:text/html;charset=utf-8,
test%20data:%20url',
michael@0: 'test data: url'],
michael@0: // about:neterror
michael@0: ['data:application/vnd.mozilla.xul+xml,',
michael@0: 'data:application/vnd.mozilla.xul+xml,'],
michael@0: // about:certerror
michael@0: ['https://untrusted.example.com/somepage.html',
michael@0: 'https://untrusted.example.com/somepage.html']
michael@0: ];
michael@0:
michael@0: function generatorTest() {
michael@0: gBrowser.selectedTab = gBrowser.addTab();
michael@0: let browser = gBrowser.selectedBrowser;
michael@0: browser.stop(); // stop the about:blank load.
michael@0:
michael@0: browser.addEventListener("DOMContentLoaded", event => {
michael@0: if (event.originalTarget != browser.contentDocument ||
michael@0: event.target.location.href == "about:blank") {
michael@0: info("skipping spurious load event");
michael@0: return;
michael@0: }
michael@0: nextStep();
michael@0: }, true);
michael@0: registerCleanupFunction(function () {
michael@0: browser.removeEventListener("DOMContentLoaded", nextStep, true);
michael@0: gBrowser.removeCurrentTab();
michael@0: });
michael@0:
michael@0: // Test that a bookmark of each URI gets the corresponding default title.
michael@0: for (let i = 0; i < tests.length; ++i) {
michael@0: let [uri, title] = tests[i];
michael@0: content.location = uri;
michael@0: yield undefined;
michael@0: checkBookmark(uri, title);
michael@0: }
michael@0:
michael@0: // Network failure test: now that dummy_page.html is in history, bookmarking
michael@0: // it should give the last known page title as the default bookmark title.
michael@0:
michael@0: // Simulate a network outage with offline mode. (Localhost is still
michael@0: // accessible in offline mode, so disable the test proxy as well.)
michael@0: BrowserOffline.toggleOfflineStatus();
michael@0: let proxy = Services.prefs.getIntPref('network.proxy.type');
michael@0: Services.prefs.setIntPref('network.proxy.type', 0);
michael@0: registerCleanupFunction(function () {
michael@0: BrowserOffline.toggleOfflineStatus();
michael@0: Services.prefs.setIntPref('network.proxy.type', proxy);
michael@0: });
michael@0:
michael@0: // LOAD_FLAGS_BYPASS_CACHE isn't good enough. So clear the cache.
michael@0: Services.cache2.clear();
michael@0:
michael@0: let [uri, title] = tests[0];
michael@0: content.location = uri;
michael@0: yield undefined;
michael@0: // The offline mode test is only good if the page failed to load.
michael@0: is(content.document.documentURI.substring(0, 14), 'about:neterror',
michael@0: "Offline mode successfully simulated network outage.");
michael@0: checkBookmark(uri, title);
michael@0: }
michael@0:
michael@0: // Bookmark the current page and confirm that the new bookmark has the expected
michael@0: // title. (Then delete the bookmark.)
michael@0: function checkBookmark(uri, expected_title) {
michael@0: is(gBrowser.selectedBrowser.currentURI.spec, uri,
michael@0: "Trying to bookmark the expected uri");
michael@0: PlacesCommandHook.bookmarkCurrentPage(false);
michael@0:
michael@0: let id = PlacesUtils.getMostRecentBookmarkForURI(PlacesUtils._uri(uri));
michael@0: ok(id > 0, "Found the expected bookmark");
michael@0: let title = PlacesUtils.bookmarks.getItemTitle(id);
michael@0: is(title, expected_title, "Bookmark got a good default title.");
michael@0:
michael@0: PlacesUtils.bookmarks.removeItem(id);
michael@0: }