Bug 1869043 allow a device to be specified with MediaTrackGraph::NotifyWhenDeviceStar...
[gecko.git] / netwerk / test / marionette / test_purge_http_cache_at_shutdown.py
blobee27e29e8d6c279eca2f4674ff44a72f261a9ba5
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/.
5 import os
6 from pathlib import Path
8 from marionette_driver import Wait
9 from marionette_harness import MarionetteTestCase
12 class PurgeHTTPCacheAtShutdownTestCase(MarionetteTestCase):
13 def setUp(self):
14 super().setUp()
15 self.marionette.enforce_gecko_prefs(
17 "privacy.sanitize.sanitizeOnShutdown": True,
18 "privacy.clearOnShutdown.cache": True,
19 "network.cache.shutdown_purge_in_background_task": True,
23 self.profile_path = Path(self.marionette.profile_path)
24 self.cache_path = self.profile_path.joinpath("cache2")
26 def tearDown(self):
27 self.marionette.cleanup()
28 super().tearDown()
30 def cacheDirExists(self):
31 return self.cache_path.exists()
33 def renamedDirExists(self):
34 return any(
35 child.name.endswith(".purge.bg_rm") for child in self.profile_path.iterdir()
38 def initLockDir(self):
39 self.lock_dir = None
40 with self.marionette.using_context("chrome"):
41 path = self.marionette.execute_script(
42 """
43 return Services.dirsvc.get("UpdRootD", Ci.nsIFile).parent.parent.path;
44 """
46 self.lock_dir = Path(path)
48 def assertNoLocks(self):
49 locks = [
51 for x in self.lock_dir.iterdir()
52 if x.is_file() and "-cachePurge" in x.name
54 self.assertEqual(locks, [], "All locks should have been removed")
56 # Tests are run in lexicographical order, so test_a* is run first to
57 # cleanup locks that may be there from previous runs.
58 def test_asetup(self):
59 self.initLockDir()
61 self.marionette.quit()
63 Wait(self.marionette, timeout=60).until(
64 lambda _: not self.cacheDirExists() and not self.renamedDirExists(),
65 message="Cache directory must be removed after orderly shutdown",
68 # delete locks from previous runs
69 locks = [
71 for x in self.lock_dir.iterdir()
72 if x.is_file() and "-cachePurge" in x.name
74 for lock in locks:
75 os.remove(lock)
76 # all locks should have been removed successfully.
77 self.assertNoLocks()
79 def test_ensure_cache_purge_after_in_app_quit(self):
80 self.assertTrue(self.cacheDirExists(), "Cache directory must exist")
81 self.initLockDir()
83 self.marionette.quit()
85 Wait(self.marionette, timeout=60).until(
86 lambda _: not self.cacheDirExists() and not self.renamedDirExists(),
87 message="Cache directory must be removed after orderly shutdown",
90 self.assertNoLocks()
92 def test_longstanding_cache_purge_after_in_app_quit(self):
93 self.assertTrue(self.cacheDirExists(), "Cache directory must exist")
94 self.initLockDir()
96 self.marionette.set_pref(
97 "toolkit.background_tasks.remove_directory.testing.sleep_ms", 5000
100 self.marionette.quit()
102 Wait(self.marionette, timeout=60).until(
103 lambda _: not self.cacheDirExists() and not self.renamedDirExists(),
104 message="Cache directory must be removed after orderly shutdown",
107 self.assertNoLocks()
109 def test_ensure_cache_purge_after_forced_restart(self):
111 Doing forced restart here to prevent the shutdown phase purging and only allow startup
112 phase one, via `CacheFileIOManager::OnDelayedStartupFinished`.
114 self.profile_path.joinpath("foo.purge.bg_rm").mkdir()
115 self.initLockDir()
117 self.marionette.restart(in_app=False)
119 Wait(self.marionette, timeout=60).until(
120 lambda _: not self.renamedDirExists(),
121 message="Directories with .purge.bg_rm postfix must be removed at startup after"
122 "disorderly shutdown",
125 self.assertNoLocks()