From df2d627e1c92fc446f0089932fbaedc829ebfb6a Mon Sep 17 00:00:00 2001 From: Tim Taubert Date: Wed, 18 May 2011 00:00:05 +0200 Subject: [PATCH] Bug 656778 - Enable session restore when Panorama usage is detected; r=zpao+dolske, ui-r=limi --- browser/base/content/browser-tabview.js | 68 ++++++--- browser/base/content/tabview/ui.js | 3 + browser/base/content/test/tabview/Makefile.in | 1 + .../test/tabview/browser_tabview_bug656778.js | 141 ++++++++++++++++++ .../preferences/tests/browser_bug567487.js | 11 +- 5 files changed, 201 insertions(+), 23 deletions(-) create mode 100644 browser/base/content/test/tabview/browser_tabview_bug656778.js diff --git a/browser/base/content/browser-tabview.js b/browser/base/content/browser-tabview.js index 3020c7a58385..0a492d3cdae1 100644 --- a/browser/base/content/browser-tabview.js +++ b/browser/base/content/browser-tabview.js @@ -40,10 +40,13 @@ let TabView = { _deck: null, _iframe: null, _window: null, - _firstUseExperienced: false, _browserKeyHandlerInitialized: false, _isFrameLoading: false, _initFrameCallbacks: [], + PREF_BRANCH: "browser.panorama.", + PREF_FIRST_RUN: "browser.panorama.experienced_first_run", + PREF_STARTUP_PAGE: "browser.startup.page", + PREF_RESTORE_ENABLED_ONCE: "browser.panorama.session_restore_enabled_once", VISIBILITY_IDENTIFIER: "tabview-visibility", // ---------- @@ -57,24 +60,35 @@ let TabView = { // ---------- get firstUseExperienced() { - return this._firstUseExperienced; + let pref = this.PREF_FIRST_RUN; + if (Services.prefs.prefHasUserValue(pref)) + return Services.prefs.getBoolPref(pref); + + return false; }, // ---------- set firstUseExperienced(val) { - if (val != this._firstUseExperienced) - Services.prefs.setBoolPref("browser.panorama.experienced_first_run", val); + Services.prefs.setBoolPref(this.PREF_FIRST_RUN, val); + }, + + // ---------- + get sessionRestoreEnabledOnce() { + let pref = this.PREF_RESTORE_ENABLED_ONCE; + if (Services.prefs.prefHasUserValue(pref)) + return Services.prefs.getBoolPref(pref); + + return false; + }, + + // ---------- + set sessionRestoreEnabledOnce(val) { + Services.prefs.setBoolPref(this.PREF_RESTORE_ENABLED_ONCE, val); }, // ---------- init: function TabView_init() { - if (!Services.prefs.prefHasUserValue("browser.panorama.experienced_first_run") || - !Services.prefs.getBoolPref("browser.panorama.experienced_first_run")) { - Services.prefs.addObserver( - "browser.panorama.experienced_first_run", this, false); - } else { - this._firstUseExperienced = true; - + if (this.firstUseExperienced) { if ((gBrowser.tabs.length - gBrowser.visibleTabs.length) > 0) this._setBrowserKeyHandlers(); @@ -100,26 +114,24 @@ let TabView = { "TabShow", this._tabShowEventListener, true); } } + + Services.prefs.addObserver(this.PREF_BRANCH, this, false); }, // ---------- // Observes topic changes. observe: function TabView_observe(subject, topic, data) { - if (topic == "nsPref:changed") { - Services.prefs.removeObserver( - "browser.panorama.experienced_first_run", this); - this._firstUseExperienced = true; + if (data == this.PREF_FIRST_RUN && this.firstUseExperienced) { this._addToolbarButton(); + this.enableSessionRestore(); } }, // ---------- // Uninitializes TabView. uninit: function TabView_uninit() { - if (!this._firstUseExperienced) { - Services.prefs.removeObserver( - "browser.panorama.experienced_first_run", this); - } + Services.prefs.removeObserver(this.PREF_BRANCH, this); + if (this._tabShowEventListener) { gBrowser.tabContainer.removeEventListener( "TabShow", this._tabShowEventListener, true); @@ -366,5 +378,23 @@ let TabView = { toolbar.currentSet = currentSet; toolbar.setAttribute("currentset", currentSet); document.persist(toolbar.id, "currentset"); + }, + + // ---------- + // Function: enableSessionRestore + // Enables automatic session restore when the browser is started. Does + // nothing if we already did that once in the past. + enableSessionRestore: function UI_enableSessionRestore() { + if (!this._window || !this.firstUseExperienced) + return; + + // do nothing if we already enabled session restore once + if (this.sessionRestoreEnabledOnce) + return; + + this.sessionRestoreEnabledOnce = true; + + // enable session restore + Services.prefs.setIntPref(this.PREF_STARTUP_PAGE, 3); } }; diff --git a/browser/base/content/tabview/ui.js b/browser/base/content/tabview/ui.js index 4f4c448d91f5..644135e8363f 100644 --- a/browser/base/content/tabview/ui.js +++ b/browser/base/content/tabview/ui.js @@ -552,6 +552,9 @@ let UI = { TabItems.resumePainting(); } + + if (gTabView.firstUseExperienced) + gTabView.enableSessionRestore(); }, // ---------- diff --git a/browser/base/content/test/tabview/Makefile.in b/browser/base/content/test/tabview/Makefile.in index 8fa21faf7506..8626987df1f5 100644 --- a/browser/base/content/test/tabview/Makefile.in +++ b/browser/base/content/test/tabview/Makefile.in @@ -138,6 +138,7 @@ _BROWSER_FILES = \ browser_tabview_bug649307.js \ browser_tabview_bug651311.js \ browser_tabview_bug654941.js \ + browser_tabview_bug656778.js \ browser_tabview_dragdrop.js \ browser_tabview_exit_button.js \ browser_tabview_expander.js \ diff --git a/browser/base/content/test/tabview/browser_tabview_bug656778.js b/browser/base/content/test/tabview/browser_tabview_bug656778.js new file mode 100644 index 000000000000..a17bdf3afcb7 --- /dev/null +++ b/browser/base/content/test/tabview/browser_tabview_bug656778.js @@ -0,0 +1,141 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +function test() { + waitForExplicitFinish(); + + registerCleanupFunction(function () { + Services.prefs.clearUserPref(TabView.PREF_FIRST_RUN); + Services.prefs.clearUserPref(TabView.PREF_STARTUP_PAGE); + Services.prefs.clearUserPref(TabView.PREF_RESTORE_ENABLED_ONCE); + }); + + let assertBoolPref = function (pref, value) { + is(Services.prefs.getBoolPref(pref), value, pref + " is " + value); + }; + + let assertIntPref = function (pref, value) { + is(Services.prefs.getIntPref(pref), value, pref + " is " + value); + }; + + let setPreferences = function (startupPage, firstRun, enabledOnce) { + Services.prefs.setIntPref(TabView.PREF_STARTUP_PAGE, startupPage); + Services.prefs.setBoolPref(TabView.PREF_FIRST_RUN, firstRun); + Services.prefs.setBoolPref(TabView.PREF_RESTORE_ENABLED_ONCE, enabledOnce); + }; + + let assertPreferences = function (startupPage, firstRun, enabledOnce) { + assertIntPref(TabView.PREF_STARTUP_PAGE, startupPage); + assertBoolPref(TabView.PREF_FIRST_RUN, firstRun); + assertBoolPref(TabView.PREF_RESTORE_ENABLED_ONCE, enabledOnce); + }; + + let next = function () { + if (tests.length == 0) { + waitForFocus(finish); + return; + } + + let test = tests.shift(); + info("running " + test.name + "..."); + test(); + }; + + // State: + // Panorama was already used before (firstUseExperienced = true) but session + // restore is deactivated. We did not automatically enable SR, yet. + // + // Expected result: + // When entering Panorma session restore will be enabled and a notification + // banner is shown. + let test1 = function test1() { + setPreferences(1, true, false); + + newWindowWithTabView(function (win) { + assertPreferences(3, true, true); + + win.close(); + next(); + }); + }; + + // State: + // Panorama has not been used before (firstUseExperienced = false) and session + // restore is deactivated. We did not automatically enable SR, yet. That state + // is equal to starting the browser the first time. + // + // Expected result: + // When entering Panorma nothing happens. When we detect that Panorama is + // really used (firstUseExperienced = true) we notify that session restore + // is now enabled. + let test2 = function test2() { + setPreferences(1, false, false); + + newWindowWithTabView(function (win) { + assertPreferences(1, false, false); + + win.TabView.firstUseExperienced = true; + + assertPreferences(3, true, true); + + win.close(); + next(); + }); + }; + + // State: + // Panorama was already used before (firstUseExperienced = true) and session + // restore is activated. We did not automatically enable SR, yet. + // + // Expected result: + // When entering Panorama nothing happens because session store is already + // enabled so there's no reason to notify. + let test3 = function test3() { + setPreferences(3, true, false); + + newWindowWithTabView(function (win) { + assertPreferences(3, true, true); + + win.close(); + next(); + }); + }; + + // State: + // Panorama was already used before (firstUseExperienced = true) and session + // restore has been automatically activated. + // + // Expected result: + // When entering Panorama nothing happens. + let test4 = function test4() { + setPreferences(3, true, true); + + newWindowWithTabView(function (win) { + assertPreferences(3, true, true); + + win.close(); + next(); + }); + }; + + // State: + // Panorama was already used before (firstUseExperienced = true) and session + // restore has been automatically activated. Session store was afterwards + // disabled by the user so we won't touch that again. + // + // Expected result: + // When entering Panorama nothing happens and we didn't enable session restore. + let test5 = function test5() { + setPreferences(1, true, true); + + newWindowWithTabView(function (win) { + assertPreferences(1, true, true); + + win.close(); + next(); + }); + }; + + let tests = [test1, test2, test3, test4, test5]; + next(); +} diff --git a/browser/components/preferences/tests/browser_bug567487.js b/browser/components/preferences/tests/browser_bug567487.js index 7184f372a8a4..0d04351e48cb 100644 --- a/browser/components/preferences/tests/browser_bug567487.js +++ b/browser/components/preferences/tests/browser_bug567487.js @@ -1,5 +1,6 @@ function test() { waitForExplicitFinish(); + resetPreferences(); function observer(win, topic, data) { if (topic != "main-pane-loaded") @@ -57,11 +58,13 @@ function runTest(win) { is(menu.selectedItem, option, "The correct value should be restored"); // cleanup - [pbAutoStartPref, startupPref].forEach(function (pref) { - if (pref.hasUserValue) - pref.reset(); - }); + resetPreferences(); win.close(); finish(); } + +function resetPreferences() { + Services.prefs.clearUserPref("browser.startup.page"); + Services.prefs.clearUserPref("browser.privatebrowsing.autostart"); +}