Skip to content

Commit

Permalink
Bug 1863692 - Only transfer window attributes for pinned tabs with de…
Browse files Browse the repository at this point in the history
…ferred sessions r=sessionstore-reviewers,dao

* add new marionette test

Differential Revision: https://phabricator.services.mozilla.com/D195006
  • Loading branch information
sarah-clements committed Dec 4, 2023
1 parent 3b643c8 commit a7e4b96
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 9 deletions.
20 changes: 11 additions & 9 deletions browser/components/sessionstore/SessionStore.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6560,20 +6560,16 @@ var SessionStoreInternal = {

hasPinnedTabs ||= !!newWindowState.tabs.length;

// At this point the window in the state object has been modified (or not)
// We want to build the rest of this new window object if we have pinnedTabs.
if (
newWindowState.tabs.length ||
(PERSIST_SESSIONS && newWindowState._closedTabs.length)
) {
// First get the other attributes off the window
// Only transfer over window attributes for pinned tabs, which has
// already been extracted into newWindowState.tabs.
if (newWindowState.tabs.length) {
WINDOW_ATTRIBUTES.forEach(function (attr) {
if (attr in window) {
newWindowState[attr] = window[attr];
delete window[attr];
}
});
// We're just copying position data into the pinned window.
// We're just copying position data into the window for pinned tabs.
// Not copying over:
// - extData
// - isPopup
Expand All @@ -6583,8 +6579,14 @@ var SessionStoreInternal = {
// remaining data
window.__lastSessionWindowID = newWindowState.__lastSessionWindowID =
"" + Date.now() + Math.random();
}

// Actually add this window to our defaultState
// If this newWindowState contains pinned tabs (stored in tabs) or
// closed tabs, add it to the defaultState so they're available immediately.
if (
newWindowState.tabs.length ||
(PERSIST_SESSIONS && newWindowState._closedTabs.length)
) {
defaultState.windows.push(newWindowState);
// Remove the window from the state if it doesn't have any tabs
if (!window.tabs.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ tags = local

[test_persist_closed_tabs_restore_manually.py]
[test_restore_loading_tab.py]
[test_restore_manually.py]
[test_restore_manually_with_pinned_tabs.py]
[test_restore_windows_after_restart_and_quit.py]
[test_restore_windows_after_windows_shutdown.py]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# 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/.

import os
import sys

# add this directory to the path
sys.path.append(os.path.dirname(__file__))

from session_store_test_case import SessionStoreTestCase


def inline(title):
return "data:text/html;charset=utf-8,<html><head><title>{}</title></head><body></body></html>".format(
title
)


class TestSessionRestoreManually(SessionStoreTestCase):
"""
Test that window attributes for each window are restored
correctly (with manual session restore) in a new session.
"""

def setUp(self):
super(TestSessionRestoreManually, self).setUp(
startup_page=1,
include_private=False,
restore_on_demand=True,
test_windows=set(
[
# Window 1
(
inline("lorem ipsom"),
inline("dolor"),
),
# Window 2
(
inline("sit"),
)
]
),
)

def test_restore(self):
self.marionette.execute_script(
"""
Services.prefs.setBoolPref("browser.sessionstore.persist_closed_tabs_between_sessions", true);
"""
)

self.wait_for_windows(
self.all_windows, "Not all requested windows have been opened"
)

self.assertEqual(
len(self.marionette.chrome_window_handles),
2,
msg="Should have 3 windows open.",
)
self.assertEqual(
self.marionette.execute_script(
"""
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
SessionStore: "resource:///modules/sessionstore/SessionStore.sys.mjs",
});
function getAllBrowserWindows() {
return Array.from(Services.wm.getEnumerator("navigator:browser"));
}
let windows = getAllBrowserWindows();
windows[1].resizeTo(500, 500)
return windows[1].document.documentElement.getAttribute("height")
"""
),
"500",
"Window has been set to correct height"
)

self.marionette.quit()
self.marionette.start_session()
self.marionette.set_context("chrome")


# restore the previous session
self.marionette.execute_script(
"""
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
SessionStore: "resource:///modules/sessionstore/SessionStore.sys.mjs",
});
function observeClosedObjectsChange() {
return new Promise(resolve => {
function observe(subject, topic, data) {
if (topic == "sessionstore-closed-objects-changed") {
Services.obs.removeObserver(this, "sessionstore-closed-objects-changed");
resolve('observed closed objects changed');
};
}
Services.obs.addObserver(observe, "sessionstore-closed-objects-changed");
});
};
async function checkForWindowHeight() {
let closedWindowsObserver = observeClosedObjectsChange();
lazy.SessionStore.restoreLastSession();
await closedWindowsObserver;
}
checkForWindowHeight();
"""
)

self.assertEqual(
len(self.marionette.chrome_window_handles),
2,
msg="Windows from last session have been restored.",
)

self.assertEqual(
self.marionette.execute_script(
"""
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
SessionStore: "resource:///modules/sessionstore/SessionStore.sys.mjs",
});
let state = SessionStore.getCurrentState()
return state.windows[1]["height"]
"""
),
500,
"Second window has correct height"
)

0 comments on commit a7e4b96

Please sign in to comment.