1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/base/content/test/general/browser_bookmark_titles.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,86 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +// This file is tests for the default titles that new bookmarks get. 1.9 + 1.10 +let tests = [ 1.11 + // Common page. 1.12 + ['http://example.com/browser/browser/base/content/test/general/dummy_page.html', 1.13 + 'Dummy test page'], 1.14 + // Data URI. 1.15 + ['data:text/html;charset=utf-8,<title>test%20data:%20url</title>', 1.16 + 'test data: url'], 1.17 + // about:neterror 1.18 + ['data:application/vnd.mozilla.xul+xml,', 1.19 + 'data:application/vnd.mozilla.xul+xml,'], 1.20 + // about:certerror 1.21 + ['https://untrusted.example.com/somepage.html', 1.22 + 'https://untrusted.example.com/somepage.html'] 1.23 +]; 1.24 + 1.25 +function generatorTest() { 1.26 + gBrowser.selectedTab = gBrowser.addTab(); 1.27 + let browser = gBrowser.selectedBrowser; 1.28 + browser.stop(); // stop the about:blank load. 1.29 + 1.30 + browser.addEventListener("DOMContentLoaded", event => { 1.31 + if (event.originalTarget != browser.contentDocument || 1.32 + event.target.location.href == "about:blank") { 1.33 + info("skipping spurious load event"); 1.34 + return; 1.35 + } 1.36 + nextStep(); 1.37 + }, true); 1.38 + registerCleanupFunction(function () { 1.39 + browser.removeEventListener("DOMContentLoaded", nextStep, true); 1.40 + gBrowser.removeCurrentTab(); 1.41 + }); 1.42 + 1.43 + // Test that a bookmark of each URI gets the corresponding default title. 1.44 + for (let i = 0; i < tests.length; ++i) { 1.45 + let [uri, title] = tests[i]; 1.46 + content.location = uri; 1.47 + yield undefined; 1.48 + checkBookmark(uri, title); 1.49 + } 1.50 + 1.51 + // Network failure test: now that dummy_page.html is in history, bookmarking 1.52 + // it should give the last known page title as the default bookmark title. 1.53 + 1.54 + // Simulate a network outage with offline mode. (Localhost is still 1.55 + // accessible in offline mode, so disable the test proxy as well.) 1.56 + BrowserOffline.toggleOfflineStatus(); 1.57 + let proxy = Services.prefs.getIntPref('network.proxy.type'); 1.58 + Services.prefs.setIntPref('network.proxy.type', 0); 1.59 + registerCleanupFunction(function () { 1.60 + BrowserOffline.toggleOfflineStatus(); 1.61 + Services.prefs.setIntPref('network.proxy.type', proxy); 1.62 + }); 1.63 + 1.64 + // LOAD_FLAGS_BYPASS_CACHE isn't good enough. So clear the cache. 1.65 + Services.cache2.clear(); 1.66 + 1.67 + let [uri, title] = tests[0]; 1.68 + content.location = uri; 1.69 + yield undefined; 1.70 + // The offline mode test is only good if the page failed to load. 1.71 + is(content.document.documentURI.substring(0, 14), 'about:neterror', 1.72 + "Offline mode successfully simulated network outage."); 1.73 + checkBookmark(uri, title); 1.74 +} 1.75 + 1.76 +// Bookmark the current page and confirm that the new bookmark has the expected 1.77 +// title. (Then delete the bookmark.) 1.78 +function checkBookmark(uri, expected_title) { 1.79 + is(gBrowser.selectedBrowser.currentURI.spec, uri, 1.80 + "Trying to bookmark the expected uri"); 1.81 + PlacesCommandHook.bookmarkCurrentPage(false); 1.82 + 1.83 + let id = PlacesUtils.getMostRecentBookmarkForURI(PlacesUtils._uri(uri)); 1.84 + ok(id > 0, "Found the expected bookmark"); 1.85 + let title = PlacesUtils.bookmarks.getItemTitle(id); 1.86 + is(title, expected_title, "Bookmark got a good default title."); 1.87 + 1.88 + PlacesUtils.bookmarks.removeItem(id); 1.89 +}