tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_tabgroups_restore.py (6202B)


      1 # This Source Code Form is subject to the terms of the Mozilla Public
      2 # License, v. 2.0. If a copy of the MPL was not distributed with this
      3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
      4 
      5 # add this directory to the path
      6 import os
      7 import sys
      8 import unittest
      9 from urllib.parse import quote
     10 
     11 sys.path.append(os.path.dirname(__file__))
     12 
     13 from session_store_test_case import SessionStoreTestCase
     14 
     15 
     16 def inline(doc):
     17    return f"data:text/html;charset=utf-8,{quote(doc)}"
     18 
     19 
     20 # Each list element represents a window of tabs loaded at
     21 # some testing URL
     22 DEFAULT_WINDOWS = set([
     23    # Window 1. Note the comma after the inline call -
     24    # this is Python's way of declaring a 1 item tuple.
     25    (inline("""<div">Lorem</div>"""), inline("""<div">Ipsum</div>""")),
     26 ])
     27 
     28 
     29 class TestAutoRestoreWithTabGroups(SessionStoreTestCase):
     30    def setUp(self):
     31        super().setUp(
     32            startup_page=3,
     33            include_private=False,
     34            restore_on_demand=True,
     35            test_windows=DEFAULT_WINDOWS,
     36        )
     37        self.marionette.set_context("chrome")
     38 
     39    def test_saved_groups_restored_after_quit(self):
     40        self.wait_for_windows(
     41            self.all_windows, "Not all requested windows have been opened"
     42        )
     43 
     44        self.marionette.execute_async_script(
     45            """
     46            let resolve = arguments[0];
     47            let group = gBrowser.addTabGroup([gBrowser.tabs[0]], { id: "test-group", label: "test-group" });
     48            let { TabStateFlusher } = ChromeUtils.importESModule("resource:///modules/sessionstore/TabStateFlusher.sys.mjs");
     49            TabStateFlusher.flushWindow(gBrowser.ownerGlobal).then(resolve);
     50            """
     51        )
     52 
     53        self.assertEqual(
     54            self.marionette.execute_script("return gBrowser.getAllTabGroups().length"),
     55            1,
     56            "There is one open group",
     57        )
     58 
     59        self.marionette.quit()
     60        self.marionette.start_session()
     61        self.marionette.set_context("chrome")
     62 
     63        self.assertEqual(
     64            self.marionette.execute_script("return gBrowser.getAllTabGroups().length"),
     65            1,
     66            "There is one open group",
     67        )
     68 
     69        self.assertEqual(
     70            self.marionette.execute_script("return SessionStore.savedGroups.length"),
     71            0,
     72            "The group was not saved because it was automatically restored",
     73        )
     74 
     75        self.marionette.execute_script(
     76            """
     77            let group = gBrowser.getTabGroupById("test-group");
     78            group.ownerGlobal.SessionStore.addSavedTabGroup(group);
     79            group.ownerGlobal.gBrowser.removeTabGroup(group, { animate: false });
     80            """
     81        )
     82 
     83        self.assertEqual(
     84            self.marionette.execute_script("return SessionStore.savedGroups.length"),
     85            1,
     86            "The group is now saved",
     87        )
     88 
     89        self.marionette.quit()
     90        self.marionette.start_session()
     91        self.marionette.set_context("chrome")
     92 
     93        self.assertEqual(
     94            self.marionette.execute_script("return gBrowser.getAllTabGroups().length"),
     95            0,
     96            "The group was not automatically restored because it was manually saved",
     97        )
     98 
     99        self.assertEqual(
    100            self.marionette.execute_script("return SessionStore.savedGroups.length"),
    101            1,
    102            "The saved group persists after a second restart",
    103        )
    104 
    105        self.marionette.execute_script(
    106            """
    107            SessionStore.forgetSavedTabGroup("test-group");
    108            """
    109        )
    110 
    111    @unittest.skipIf(
    112        sys.platform.startswith("darwin"),
    113        "macOS does not close Firefox when the last window closes",
    114    )
    115    def test_saved_groups_restored_after_closing_last_window(self):
    116        self.wait_for_windows(
    117            self.all_windows, "Not all requested windows have been opened"
    118        )
    119 
    120        self.marionette.execute_async_script(
    121            """
    122            let resolve = arguments[0];
    123            let group = gBrowser.addTabGroup([gBrowser.tabs[0]], { id: "test-group", label: "test-group" });
    124            let { TabStateFlusher } = ChromeUtils.importESModule("resource:///modules/sessionstore/TabStateFlusher.sys.mjs");
    125            TabStateFlusher.flushWindow(gBrowser.ownerGlobal).then(resolve);
    126            """
    127        )
    128 
    129        self.assertEqual(
    130            self.marionette.execute_script("return gBrowser.getAllTabGroups().length"),
    131            1,
    132            "There is one open group",
    133        )
    134 
    135        self.marionette.quit(callback=self._close_window)
    136        self.marionette.start_session()
    137        self.marionette.set_context("chrome")
    138 
    139        self.assertEqual(
    140            self.marionette.execute_script("return gBrowser.getAllTabGroups().length"),
    141            1,
    142            "There is one open group",
    143        )
    144 
    145        self.assertEqual(
    146            self.marionette.execute_script("return SessionStore.savedGroups.length"),
    147            0,
    148            "The group was not saved because it was automatically restored",
    149        )
    150 
    151        self.marionette.execute_script(
    152            """
    153            let group = gBrowser.getTabGroupById("test-group");
    154            group.ownerGlobal.SessionStore.addSavedTabGroup(group);
    155            group.ownerGlobal.gBrowser.removeTabGroup(group, { animate: false });
    156            """
    157        )
    158 
    159        self.assertEqual(
    160            self.marionette.execute_script("return SessionStore.savedGroups.length"),
    161            1,
    162            "The group is now saved",
    163        )
    164 
    165        self.marionette.quit(callback=self._close_window)
    166        self.marionette.start_session()
    167        self.marionette.set_context("chrome")
    168 
    169        self.assertEqual(
    170            self.marionette.execute_script("return gBrowser.getAllTabGroups().length"),
    171            0,
    172            "The group was not automatically restored because it was manually saved",
    173        )
    174 
    175        self.assertEqual(
    176            self.marionette.execute_script("return SessionStore.savedGroups.length"),
    177            1,
    178            "The saved group persists after a second restart",
    179        )
    180 
    181        self.marionette.execute_script(
    182            """
    183            SessionStore.forgetSavedTabGroup("test-group");
    184            """
    185        )