summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/marionette/test_persist_closed_tabs_restore_manually.py
blob: 9aa2a3871f7b18b68a692bb23470b27eb3272d50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# 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 marionette_driver import Wait, errors
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 TestSessionRestoreClosedTabs(SessionStoreTestCase):
    """
    Test that closed tabs persist between sessions and
    that any previously open tabs are added to the recently
    closed tab list. When a previous session is restored,
    an open tab is restored and removed from the closed tabs list.

    If additional tabs are opened (and closed) before a previous
    session is restored, those should be merged with the restored open
    and closed tabs, preserving their state.
    """

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

    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"
        )
        # Close the second tab leaving the first tab open
        self.marionette.execute_async_script(
            """
            let resolve = arguments[0];
            let tab = gBrowser.tabs[1];
            gBrowser.removeTab(tab);
            let { TabStateFlusher } = ChromeUtils.importESModule("resource:///modules/sessionstore/TabStateFlusher.sys.mjs");
            TabStateFlusher.flush(tab).then(resolve);
            """
        )

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

        self.assertEqual(
            self.marionette.execute_script(
                """
            let { SessionStore } = ChromeUtils.importESModule("resource:///modules/sessionstore/SessionStore.sys.mjs");
            let state = JSON.parse(SessionStore.getBrowserState());
            return state.windows[0]._closedTabs.length;
            """
            ),
            2,
            msg="Should have 2 closed tabs after restart.",
        )

        self.assertEqual(
            self.marionette.execute_script(
                """
            let { SessionStore } = ChromeUtils.importESModule("resource:///modules/sessionstore/SessionStore.sys.mjs");
            let state = JSON.parse(SessionStore.getBrowserState());
            return state.windows[0]._closedTabs[0].removeAfterRestore;
            """
            ),
            True,
            msg="Previously open tab that was added to closedTabs should have removeAfterRestore property.",
        )

        # open two new tabs, the second one will be closed
        win = self.marionette.current_chrome_window_handle
        self.open_tabs(win, (inline("sit"), inline("amet")))

        self.assertEqual(
            self.marionette.execute_script(
                """
                return gBrowser.tabs[0].label
                """
            ),
            "sit",
            msg="First open tab should now be sit",
        )

        self.assertEqual(
            self.marionette.execute_script(
                """
                return gBrowser.tabs[1].label
                """
            ),
            "amet",
            msg="Second open tab should be amet",
        )

        self.assertEqual(
            self.marionette.execute_script(
                """
                return gBrowser.tabs.length
                """
            ),
            2,
            msg="should have 2 tabs open",
        )

        self.marionette.execute_async_script(
            """
            let resolve = arguments[0];
            let tab = gBrowser.tabs[1];
            gBrowser.removeTab(tab);
            let { TabStateFlusher } = ChromeUtils.importESModule("resource:///modules/sessionstore/TabStateFlusher.sys.mjs");
            TabStateFlusher.flush(tab).then(resolve);
            """
        )

        self.wait_for_tabcount(1, "Waiting for 1 tabs")

        # restore the previous session
        self.assertEqual(
            self.marionette.execute_script(
                """
                const lazy = {};
                ChromeUtils.defineESModuleGetters(lazy, {
                    SessionStore: "resource:///modules/sessionstore/SessionStore.sys.mjs",
                });
                function observeClosedTabsChange() {
                    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 checkForClosedTabs() {
                    let closedTabsObserver = observeClosedTabsChange();
                    lazy.SessionStore.restoreLastSession();
                    await closedTabsObserver;
                    let state = JSON.parse(lazy.SessionStore.getBrowserState());
                    return state.windows[0]._closedTabs.length;
                }
                return checkForClosedTabs();
                """
            ),
            2,
            msg="Should have 2 closed tab after restoring session.",
        )

        self.wait_for_tabcount(2, "Waiting for 2 tabs")

        self.assertEqual(
            self.marionette.execute_script(
                """
                return gBrowser.tabs[0].label
                """
            ),
            "sit",
            msg="Newly opened tab should still exist",
        )

        self.assertEqual(
            self.marionette.execute_script(
                """
                return gBrowser.tabs[1].label
                """
            ),
            "lorem ipsom",
            msg="The open tab from the previous session should be restored",
        )

        # temporary until we remove this pref
        self.marionette.execute_script(
            """
            Services.prefs.clearUserPref("browser.sessionstore.persist_closed_tabs_between_sessions");
            """
        )

    def wait_for_tabcount(self, expected_tabcount, message, timeout=5):
        current_tabcount = None

        def check(_):
            nonlocal current_tabcount
            current_tabcount = self.marionette.execute_script(
                "return gBrowser.tabs.length;"
            )
            return current_tabcount == expected_tabcount

        try:
            wait = Wait(self.marionette, timeout=timeout, interval=0.1)
            wait.until(check, message=message)
        except errors.TimeoutException as e:
            # Update the message to include the most recent list of windows
            message = (
                f"{e.message}. Expected {expected_tabcount}, got {current_tabcount}."
            )
            raise errors.TimeoutException(message)