browser_protectionsUI_tracker_cookies_subview.js (4769B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 /** 7 * Bug 1918341 - Testing the cookie category of the protection panel shows the 8 * tracker domains who access cookies when the tracker cookie 9 * blocking is disabled. 10 */ 11 12 const TEST_PAGE = 13 "https://example.net/browser/browser/base/content/test/protectionsUI/trackerCookiePage.html"; 14 15 add_setup(async function () { 16 await UrlClassifierTestUtils.addTestTrackers(); 17 18 await SpecialPowers.pushPrefEnv({ 19 set: [ 20 [ 21 "network.cookie.cookieBehavior", 22 Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN, 23 ], 24 ["network.cookie.cookieBehavior.trackerCookieBlocking", false], 25 ], 26 }); 27 28 registerCleanupFunction(() => { 29 UrlClassifierTestUtils.cleanupTestTrackers(); 30 }); 31 }); 32 33 add_task(async function testTrackerCookiesSubView() { 34 // Open a tab which embeds a third-party tracker iframe which sets a cookie. 35 let tab = await BrowserTestUtils.openNewForegroundTab({ 36 url: TEST_PAGE, 37 gBrowser, 38 }); 39 40 await openProtectionsPanel(); 41 42 let categoryItem = document.getElementById( 43 "protections-popup-category-cookies" 44 ); 45 46 // Explicitly waiting for the category item becoming visible. 47 await TestUtils.waitForCondition(() => { 48 return BrowserTestUtils.isVisible(categoryItem); 49 }); 50 51 ok( 52 BrowserTestUtils.isVisible(categoryItem), 53 "Cookie category item is visible" 54 ); 55 let cookiesView = document.getElementById("protections-popup-cookiesView"); 56 let viewShown = BrowserTestUtils.waitForEvent(cookiesView, "ViewShown"); 57 categoryItem.click(); 58 await viewShown; 59 60 ok(true, "Cookies view was shown"); 61 62 let listItems = cookiesView.querySelectorAll(".protections-popup-list-item"); 63 is(listItems.length, 1, `We have 1 item in the list`); 64 65 let label = listItems[0].querySelector(".protections-popup-list-host-label"); 66 is( 67 label.value, 68 "https://itisatracker.org", 69 "Has an item for itisatracker.org" 70 ); 71 72 await closeProtectionsPanel(); 73 74 // Fetch a non-tracker third party cookie. Ensure it's not shown in the 75 // cookie subview. 76 await SpecialPowers.spawn(tab.linkedBrowser, [], async _ => { 77 await content.fetch( 78 "https://example.com/browser/browser/base/content/test/protectionsUI/cookieServer.sjs?type=partitioned", 79 { credentials: "include" } 80 ); 81 }); 82 83 await openProtectionsPanel(); 84 85 viewShown = BrowserTestUtils.waitForEvent(cookiesView, "ViewShown"); 86 categoryItem.click(); 87 await viewShown; 88 89 listItems = cookiesView.querySelectorAll(".protections-popup-list-item"); 90 is(listItems.length, 1, `We still have 1 item in the list`); 91 92 await closeProtectionsPanel(); 93 94 // Fetch a third party cookie from another tracker. Ensure it's shown in the 95 // cookie subview. 96 await SpecialPowers.spawn(tab.linkedBrowser, [], async _ => { 97 await content.fetch( 98 "https://tracking.example.org/browser/browser/base/content/test/protectionsUI/cookieServer.sjs?type=partitioned", 99 { credentials: "include" } 100 ); 101 }); 102 103 await openProtectionsPanel(); 104 105 viewShown = BrowserTestUtils.waitForEvent(cookiesView, "ViewShown"); 106 categoryItem.click(); 107 await viewShown; 108 109 listItems = cookiesView.querySelectorAll(".protections-popup-list-item"); 110 is(listItems.length, 2, `We have 2 items in the list`); 111 112 label = listItems[0].querySelector(".protections-popup-list-host-label"); 113 is( 114 label.value, 115 "https://itisatracker.org", 116 "Has an item for itisatracker.org" 117 ); 118 119 label = listItems[1].querySelector(".protections-popup-list-host-label"); 120 is( 121 label.value, 122 "https://tracking.example.org", 123 "Has an item for tracking.example.org" 124 ); 125 126 await closeProtectionsPanel(); 127 128 BrowserTestUtils.removeTab(tab); 129 }); 130 131 // Verify that the partitioned tracker cookies are recorded in the tracking DB. 132 add_task(async function testPartitionedTrackerCookiesInTrackingDB() { 133 await TrackingDBService.clearAll(); 134 135 // Open a tab which embeds a third-party tracker iframe which sets a cookie. 136 let tab = await BrowserTestUtils.openNewForegroundTab({ 137 url: TEST_PAGE, 138 gBrowser, 139 }); 140 141 let promise = BrowserTestUtils.waitForCondition(async () => { 142 let foundTrackerCookieEvent = false; 143 let events = await TrackingDBService.getEventsByDateRange(0, Date.now()); 144 145 for (let event of events) { 146 let type = event.getResultByName("type"); 147 if (type == Ci.nsITrackingDBService.TRACKING_COOKIES_ID) { 148 foundTrackerCookieEvent = true; 149 break; 150 } 151 } 152 153 return foundTrackerCookieEvent; 154 }); 155 156 BrowserTestUtils.removeTab(tab); 157 158 await promise; 159 160 ok(true, "Partitioned tracker cookies are recorded in the tracking DB"); 161 });