3333
3434namespace containers {
3535
36+ constexpr char kTestContainerId [] = " test-container-id" ;
37+
3638class ContainersBrowserTest : public InProcessBrowserTest {
3739 public:
3840 ContainersBrowserTest () : https_server_(net::EmbeddedTestServer::TYPE_HTTPS ) {
@@ -48,6 +50,11 @@ class ContainersBrowserTest : public InProcessBrowserTest {
4850
4951 ~ContainersBrowserTest () override = default ;
5052
53+ void SetUp () override {
54+ set_open_about_blank_on_browser_launch (false );
55+ InProcessBrowserTest::SetUp ();
56+ }
57+
5158 void SetUpCommandLine (base::CommandLine* command_line) override {
5259 InProcessBrowserTest::SetUpCommandLine (command_line);
5360 command_line->AppendSwitchASCII (
@@ -332,6 +339,73 @@ IN_PROC_BROWSER_TEST_F(ContainersBrowserTest, IsolateCookiesAndStorage) {
332339 GetIndexedDBJS (" test_key" )));
333340}
334341
342+ IN_PROC_BROWSER_TEST_F (ContainersBrowserTest,
343+ PRE_StoragePersistenceAcrossSessions) {
344+ const GURL url (" https://a.test/simple.html" );
345+
346+ // Navigate to the page
347+ NavigateParams params (browser (), url, ui::PAGE_TRANSITION_LINK );
348+ params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB ;
349+ params.storage_partition_config = content::StoragePartitionConfig::Create (
350+ browser ()->profile (), kContainersStoragePartitionDomain , kTestContainerId ,
351+ browser ()->profile ()->IsOffTheRecord ());
352+ ui_test_utils::NavigateToURL (¶ms);
353+
354+ content::WebContents* web_contents =
355+ browser ()->tab_strip_model ()->GetActiveWebContents ();
356+ ASSERT_TRUE (web_contents);
357+
358+ // Set persistent storage data
359+ EXPECT_TRUE (content::ExecJs (
360+ web_contents, SetCookieJS (" persistent_cookie" , " persistent_value" )));
361+ EXPECT_TRUE (content::ExecJs (
362+ web_contents, SetLocalStorageJS (" persistent_key" , " persistent_value" )));
363+
364+ EXPECT_TRUE (content::ExecJs (
365+ web_contents, SetIndexedDBJS (" persistent_key" , " persistent_value" )));
366+
367+ // Verify data is set
368+ content::EvalJsResult cookie_result =
369+ content::EvalJs (web_contents, GetCookiesJS ());
370+ EXPECT_TRUE (cookie_result.ExtractString ().find (
371+ " persistent_cookie=persistent_value" ) != std::string::npos);
372+
373+ EXPECT_EQ (" persistent_value" ,
374+ content::EvalJs (web_contents, GetLocalStorageJS (" persistent_key" )));
375+ EXPECT_EQ (" persistent_value" ,
376+ content::EvalJs (web_contents, GetIndexedDBJS (" persistent_key" )));
377+ }
378+
379+ IN_PROC_BROWSER_TEST_F (ContainersBrowserTest,
380+ StoragePersistenceAcrossSessions) {
381+ const GURL url (" https://a.test/simple.html" );
382+
383+ // Navigate to the page
384+ NavigateParams params (browser (), url, ui::PAGE_TRANSITION_LINK );
385+ params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB ;
386+ params.storage_partition_config = content::StoragePartitionConfig::Create (
387+ browser ()->profile (), kContainersStoragePartitionDomain , kTestContainerId ,
388+ browser ()->profile ()->IsOffTheRecord ());
389+ ui_test_utils::NavigateToURL (¶ms);
390+
391+ content::WebContents* web_contents_reloaded =
392+ browser ()->tab_strip_model ()->GetActiveWebContents ();
393+ ASSERT_TRUE (web_contents_reloaded);
394+
395+ // Verify persistent data is still available after reload
396+ content::EvalJsResult cookie_result_reloaded =
397+ content::EvalJs (web_contents_reloaded, GetCookiesJS ());
398+ EXPECT_TRUE (cookie_result_reloaded.ExtractString ().find (
399+ " persistent_cookie=persistent_value" ) != std::string::npos);
400+
401+ EXPECT_EQ (" persistent_value" ,
402+ content::EvalJs (web_contents_reloaded,
403+ GetLocalStorageJS (" persistent_key" )));
404+ EXPECT_EQ (
405+ " persistent_value" ,
406+ content::EvalJs (web_contents_reloaded, GetIndexedDBJS (" persistent_key" )));
407+ }
408+
335409IN_PROC_BROWSER_TEST_F (ContainersBrowserTest,
336410 LinkNavigationInheritsContainerStoragePartition) {
337411 const GURL url (" https://a.test/simple.html" );
@@ -340,8 +414,8 @@ IN_PROC_BROWSER_TEST_F(ContainersBrowserTest,
340414 NavigateParams params (browser (), url, ui::PAGE_TRANSITION_LINK );
341415 params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB ;
342416 params.storage_partition_config = content::StoragePartitionConfig::Create (
343- browser ()->profile (), kContainersStoragePartitionDomain ,
344- " container-for-links " , browser ()->profile ()->IsOffTheRecord ());
417+ browser ()->profile (), kContainersStoragePartitionDomain , kTestContainerId ,
418+ browser ()->profile ()->IsOffTheRecord ());
345419 ui_test_utils::NavigateToURL (¶ms);
346420
347421 content::WebContents* container_web_contents =
@@ -385,6 +459,13 @@ IN_PROC_BROWSER_TEST_F(ContainersBrowserTest,
385459 // Verify the new tab is on the correct URL
386460 EXPECT_EQ (url, new_tab_contents->GetLastCommittedURL ());
387461
462+ content::StoragePartition* storage_partition =
463+ new_tab_contents->GetPrimaryMainFrame ()->GetStoragePartition ();
464+ ASSERT_TRUE (storage_partition);
465+ EXPECT_EQ (kContainersStoragePartitionDomain ,
466+ storage_partition->GetConfig ().partition_domain ());
467+ EXPECT_EQ (kTestContainerId , storage_partition->GetConfig ().partition_name ());
468+
388469 // Verify the new tab has access to the same container storage partition
389470 content::EvalJsResult cookie_result =
390471 content::EvalJs (new_tab_contents, GetCookiesJS ());
@@ -899,4 +980,70 @@ IN_PROC_BROWSER_TEST_F(ContainersBrowserTest, ShouldShowTabAccent) {
899980 EXPECT_FALSE (tab_in_container->ShouldShowLargeAccentIcon ());
900981}
901982
983+ IN_PROC_BROWSER_TEST_F (ContainersBrowserTest,
984+ PRE_ServiceWorkerPersistenceAcrossSessions) {
985+ const GURL url (" https://a.test/containers/container_test.html" );
986+ const GURL worker_url (" https://a.test/containers/container_worker.js" );
987+ const std::string scope = " https://a.test/containers/" ;
988+
989+ // Navigate to the page with a container
990+ NavigateParams params (browser (), url, ui::PAGE_TRANSITION_LINK );
991+ params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB ;
992+ params.storage_partition_config = content::StoragePartitionConfig::Create (
993+ browser ()->profile (), kContainersStoragePartitionDomain , kTestContainerId ,
994+ browser ()->profile ()->IsOffTheRecord ());
995+ ui_test_utils::NavigateToURL (¶ms);
996+
997+ content::WebContents* web_contents =
998+ browser ()->tab_strip_model ()->GetActiveWebContents ();
999+ ASSERT_TRUE (web_contents);
1000+
1001+ // Register service worker
1002+ EXPECT_TRUE (content::ExecJs (
1003+ web_contents, RegisterServiceWorkerJS (worker_url.spec (), scope)));
1004+
1005+ // Verify service worker is registered
1006+ EXPECT_EQ (
1007+ " registered" ,
1008+ content::EvalJs (web_contents, CheckServiceWorkerRegisteredJS (scope)));
1009+
1010+ // Set some persistent storage data that the service worker might use
1011+ EXPECT_TRUE (content::ExecJs (
1012+ web_contents, SetLocalStorageJS (" sw_data" , " persistent_value" )));
1013+ EXPECT_TRUE (content::ExecJs (web_contents,
1014+ SetCookieJS (" sw_cookie" , " persistent_cookie" )));
1015+ }
1016+
1017+ IN_PROC_BROWSER_TEST_F (ContainersBrowserTest,
1018+ ServiceWorkerPersistenceAcrossSessions) {
1019+ const GURL url (" https://a.test/containers/container_test.html" );
1020+ const std::string scope = " https://a.test/containers/" ;
1021+
1022+ // Navigate to the page with the same container
1023+ NavigateParams params (browser (), url, ui::PAGE_TRANSITION_LINK );
1024+ params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB ;
1025+ params.storage_partition_config = content::StoragePartitionConfig::Create (
1026+ browser ()->profile (), kContainersStoragePartitionDomain , kTestContainerId ,
1027+ browser ()->profile ()->IsOffTheRecord ());
1028+ ui_test_utils::NavigateToURL (¶ms);
1029+
1030+ content::WebContents* web_contents_reloaded =
1031+ browser ()->tab_strip_model ()->GetActiveWebContents ();
1032+ ASSERT_TRUE (web_contents_reloaded);
1033+
1034+ // Verify service worker is still registered after browser restart
1035+ EXPECT_EQ (" registered" ,
1036+ content::EvalJs (web_contents_reloaded,
1037+ CheckServiceWorkerRegisteredJS (scope)));
1038+
1039+ // Verify persistent storage data is still available
1040+ EXPECT_EQ (" persistent_value" , content::EvalJs (web_contents_reloaded,
1041+ GetLocalStorageJS (" sw_data" )));
1042+
1043+ content::EvalJsResult cookie_result =
1044+ content::EvalJs (web_contents_reloaded, GetCookiesJS ());
1045+ EXPECT_TRUE (cookie_result.ExtractString ().find (
1046+ " sw_cookie=persistent_cookie" ) != std::string::npos);
1047+ }
1048+
9021049} // namespace containers
0 commit comments