1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/base/content/test/newtab/browser_newtab_focus.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,57 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +/* 1.8 + * These tests make sure that focusing the 'New Tage Page' works as expected. 1.9 + */ 1.10 +function runTests() { 1.11 + // Handle the OSX full keyboard access setting 1.12 + Services.prefs.setIntPref("accessibility.tabfocus", 7); 1.13 + 1.14 + // Focus count in new tab page. 1.15 + // 30 = 9 * 3 + 3 = 9 sites, each with link, pin and remove buttons; search 1.16 + // bar; search button; and toggle button. 1.17 + let FOCUS_COUNT = 30; 1.18 + 1.19 + // Create a new tab page. 1.20 + yield setLinks("0,1,2,3,4,5,6,7,8"); 1.21 + setPinnedLinks(""); 1.22 + 1.23 + yield addNewTabPageTab(); 1.24 + gURLBar.focus(); 1.25 + 1.26 + // Count the focus with the enabled page. 1.27 + yield countFocus(FOCUS_COUNT); 1.28 + 1.29 + // Disable page and count the focus with the disabled page. 1.30 + NewTabUtils.allPages.enabled = false; 1.31 + yield countFocus(1); 1.32 + 1.33 + Services.prefs.clearUserPref("accessibility.tabfocus"); 1.34 + NewTabUtils.allPages.enabled = true; 1.35 +} 1.36 + 1.37 +/** 1.38 + * Focus the urlbar and count how many focus stops to return again to the urlbar. 1.39 + */ 1.40 +function countFocus(aExpectedCount) { 1.41 + let focusCount = 0; 1.42 + let contentDoc = getContentDocument(); 1.43 + 1.44 + window.addEventListener("focus", function onFocus() { 1.45 + let focusedElement = document.commandDispatcher.focusedElement; 1.46 + if (focusedElement && focusedElement.classList.contains("urlbar-input")) { 1.47 + window.removeEventListener("focus", onFocus, true); 1.48 + is(focusCount, aExpectedCount, "Validate focus count in the new tab page."); 1.49 + executeSoon(TestRunner.next); 1.50 + } else { 1.51 + if (focusedElement && focusedElement.ownerDocument == contentDoc && 1.52 + focusedElement instanceof HTMLElement) { 1.53 + focusCount++; 1.54 + } 1.55 + document.commandDispatcher.advanceFocus(); 1.56 + } 1.57 + }, true); 1.58 + 1.59 + document.commandDispatcher.advanceFocus(); 1.60 +}