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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browsing_topics/browsing_topics_service_factory.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/first_party_sets/first_party_sets_policy_service_factory.h"
#include "chrome/browser/privacy_sandbox/mock_privacy_sandbox_service.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_queue_manager.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_service.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_service_factory.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_service_impl.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_settings_factory.h"
#include "chrome/browser/privacy_sandbox/tracking_protection_settings_factory.h"
#include "chrome/browser/search_engine_choice/search_engine_choice_dialog_service.h"
#include "chrome/browser/search_engine_choice/search_engine_choice_dialog_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/privacy_sandbox/privacy_sandbox_prompt_helper.h"
#include "chrome/browser/ui/views/privacy_sandbox/privacy_sandbox_dialog_view.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
#include "components/privacy_sandbox/privacy_sandbox_features.h"
#include "components/profile_metrics/browser_profile_type.h"
#include "components/regional_capabilities/regional_capabilities_switches.h"
#include "components/search_engines/search_engines_switches.h"
#include "components/user_education/test/test_product_messaging_controller.h"
#include "components/variations/service/variations_service.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "ui/views/widget/any_widget_observer.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chromeos/ash/components/browser_context_helper/browser_context_types.h"
#endif
DEFINE_LOCAL_REQUIRED_NOTICE_IDENTIFIER(kNoticeId);
namespace {
// TODO(crbug.com/420707919): Move this to it's own file to be reused across
// tests.
class TestPrivacySandboxServiceImpl : public PrivacySandboxServiceImpl {
public:
TestPrivacySandboxServiceImpl(
Profile* profile,
privacy_sandbox::PrivacySandboxSettings* privacy_sandbox_settings,
privacy_sandbox::TrackingProtectionSettings* tracking_protection_settings,
scoped_refptr<content_settings::CookieSettings> cookie_settings,
PrefService* pref_service,
content::InterestGroupManager* interest_group_manager,
profile_metrics::BrowserProfileType profile_type,
content::BrowsingDataRemover* browsing_data_remover,
HostContentSettingsMap* host_content_settings_map,
browsing_topics::BrowsingTopicsService* browsing_topics_service,
first_party_sets::FirstPartySetsPolicyService* first_party_sets_service,
PrivacySandboxCountries* privacy_sandbox_countries)
: PrivacySandboxServiceImpl(profile,
privacy_sandbox_settings,
tracking_protection_settings,
cookie_settings,
pref_service,
interest_group_manager,
profile_type,
browsing_data_remover,
host_content_settings_map,
browsing_topics_service,
first_party_sets_service,
privacy_sandbox_countries) {}
MOCK_METHOD(PrivacySandboxService::PromptType,
GetRequiredPromptType,
(PrivacySandboxService::SurfaceType),
(override));
};
profile_metrics::BrowserProfileType GetProfileType(Profile* profile) {
#if BUILDFLAG(IS_CHROMEOS)
if (!ash::IsUserBrowserContext(profile)) {
return profile_metrics::BrowserProfileType::kSystem;
}
#endif
return profile_metrics::GetBrowserProfileType(profile);
}
std::unique_ptr<KeyedService> BuildPrivacySandboxServiceTest(
content::BrowserContext* context) {
Profile* profile = Profile::FromBrowserContext(context);
return std::make_unique<TestPrivacySandboxServiceImpl>(
profile, PrivacySandboxSettingsFactory::GetForProfile(profile),
TrackingProtectionSettingsFactory::GetForProfile(profile),
CookieSettingsFactory::GetForProfile(profile), profile->GetPrefs(),
profile->GetDefaultStoragePartition()->GetInterestGroupManager(),
GetProfileType(profile),
(!profile->IsGuestSession() || profile->IsOffTheRecord())
? profile->GetBrowsingDataRemover()
: nullptr,
HostContentSettingsMapFactory::GetForProfile(profile),
browsing_topics::BrowsingTopicsServiceFactory::GetForProfile(profile),
first_party_sets::FirstPartySetsPolicyServiceFactory::
GetForBrowserContext(context),
GetSingletonPrivacySandboxCountries());
}
// This file is meant to test the general code path that causes notices to show.
// Specifically triggering of the notice queue.
class PrivacySandboxQueueNoticeBrowserTest : public InProcessBrowserTest {
public:
void SetUpInProcessBrowserTestFixture() override {
feature_list_.InitWithFeatures(
{privacy_sandbox::kPrivacySandboxNoticeQueue}, {});
create_services_subscription_ =
BrowserContextDependencyManager::GetInstance()
->RegisterCreateServicesCallbackForTesting(
base::BindRepeating([](content::BrowserContext* context) {
PrivacySandboxServiceFactory::GetInstance()
->SetTestingFactory(
context,
base::BindRepeating(&BuildPrivacySandboxServiceTest));
}));
}
void SetUpOnMainThread() override {
ON_CALL(*privacy_sandbox_service(), GetRequiredPromptType(testing::_))
.WillByDefault(
testing::Return(PrivacySandboxService::PromptType::kM1NoticeEEA));
g_browser_process->variations_service()->OverrideStoredPermanentCountry(
"be");
}
content::WebContents* web_contents(PrivacySandboxDialogView* dialog_widget) {
return dialog_widget->GetWebContentsForTesting();
}
privacy_sandbox::PrivacySandboxQueueManager& queue_manager() {
return privacy_sandbox_service()->GetPrivacySandboxNoticeQueueManager();
}
TestPrivacySandboxServiceImpl* privacy_sandbox_service() {
return static_cast<TestPrivacySandboxServiceImpl*>(
PrivacySandboxServiceFactory::GetForProfile(browser()->profile()));
}
std::string element_path_ =
"document.querySelector('privacy-sandbox-combined-dialog-app')."
"shadowRoot.querySelector('#notice').shadowRoot.querySelector('#"
"ackButton')";
protected:
base::CallbackListSubscription create_services_subscription_;
base::test::ScopedFeatureList feature_list_;
};
// Navigate to a invalid then valid webpage. Ensure handle is held throughout.
IN_PROC_BROWSER_TEST_F(PrivacySandboxQueueNoticeBrowserTest, NoPrompt) {
// Navigate to invalid page.
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUINewTabPageURL),
WindowOpenDisposition::NEW_WINDOW,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
// When we navigate to a page that doesn't require a prompt, we should still
// hog handle.
ASSERT_TRUE(queue_manager().IsHoldingHandle());
// Navigate to valid page.
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUISettingsURL),
WindowOpenDisposition::NEW_WINDOW,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
ASSERT_TRUE(queue_manager().IsHoldingHandle());
}
// Navigate to a valid webpage (settings page) and click a notice. One window.
IN_PROC_BROWSER_TEST_F(PrivacySandboxQueueNoticeBrowserTest, PromptShows) {
// Navigate.
views::NamedWidgetShownWaiter waiter(
views::test::AnyWidgetTestPasskey{},
PrivacySandboxDialogView::kViewClassName);
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUISettingsURL),
WindowOpenDisposition::NEW_WINDOW,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
auto* dialog_widget = static_cast<PrivacySandboxDialogView*>(
waiter.WaitIfNeededAndGet()->widget_delegate()->GetContentsView());
// Before we click, should still be holding handle.
ASSERT_TRUE(queue_manager().IsHoldingHandle());
// Click ack button.
const std::string& code = element_path_ + ".click();";
EXPECT_TRUE(content::ExecJs(web_contents(dialog_widget), code,
content::EXECUTE_SCRIPT_DEFAULT_OPTIONS,
1 /* world_id */));
// After click, should release handle.
ASSERT_FALSE(queue_manager().IsHoldingHandle());
}
// Navigate to a valid webpage (settings page) and click a notice. Two windows.
IN_PROC_BROWSER_TEST_F(PrivacySandboxQueueNoticeBrowserTest,
PromptShowsMultipleWindows) {
// Navigate.
views::NamedWidgetShownWaiter waiter(
views::test::AnyWidgetTestPasskey{},
PrivacySandboxDialogView::kViewClassName);
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUISettingsURL),
WindowOpenDisposition::NEW_WINDOW,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
auto* dialog_widget = static_cast<PrivacySandboxDialogView*>(
waiter.WaitIfNeededAndGet()->widget_delegate()->GetContentsView());
// After first nav, we should be holding handle.
ASSERT_TRUE(queue_manager().IsHoldingHandle());
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUISettingsURL),
WindowOpenDisposition::NEW_WINDOW,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
// After second nav, should still be holding handle.
ASSERT_TRUE(queue_manager().IsHoldingHandle());
// Click ack button on one window.
const std::string& code = element_path_ + ".click();";
EXPECT_TRUE(content::ExecJs(web_contents(dialog_widget), code,
content::EXECUTE_SCRIPT_DEFAULT_OPTIONS,
1 /* world_id */));
// After click, should release handle.
ASSERT_FALSE(queue_manager().IsHoldingHandle());
}
// Browser startup assumes we don't need a notice. Then we need a notice.
IN_PROC_BROWSER_TEST_F(PrivacySandboxQueueNoticeBrowserTest,
PromptNeededAtStartupThenNotAtNavigation) {
// Set flags incorrectly so we don't need a prompt.
ON_CALL(*privacy_sandbox_service(), GetRequiredPromptType(testing::_))
.WillByDefault(testing::Return(PrivacySandboxService::PromptType::kNone));
// Navigate.
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUINewTabPageURL),
WindowOpenDisposition::NEW_WINDOW,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
// After first nav, we should not be holding handle.
ASSERT_FALSE(queue_manager().IsHoldingHandle());
ASSERT_FALSE(queue_manager().IsNoticeQueued());
// Set the correct flag.
ON_CALL(*privacy_sandbox_service(), GetRequiredPromptType(testing::_))
.WillByDefault(
testing::Return(PrivacySandboxService::PromptType::kM1Consent));
// Navigate again and now we want to queue and hold the handle.
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUISettingsURL),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
// After second nav, should have been queued and holding handle.
ASSERT_TRUE(queue_manager().IsHoldingHandle());
}
// Browser startup assumes we need a notice. Then we realize we don't need it.
IN_PROC_BROWSER_TEST_F(PrivacySandboxQueueNoticeBrowserTest,
PromptNotNeededAtStartupThenNeededAtNavigation) {
// Navigate.
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUISettingsURL),
WindowOpenDisposition::NEW_WINDOW,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
// After first trigger, should have been queued and holding handle.
ASSERT_TRUE(queue_manager().IsHoldingHandle());
// Change our mind about wanting a prompt.
ON_CALL(*privacy_sandbox_service(), GetRequiredPromptType(testing::_))
.WillByDefault(testing::Return(PrivacySandboxService::PromptType::kNone));
// Navigate.
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUISettingsURL),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
// After second nav do not hold handle.
ASSERT_FALSE(queue_manager().IsNoticeQueued());
ASSERT_FALSE(queue_manager().IsHoldingHandle());
}
// Don't allow the notice to be queued, such that the handle check fails, and we
// can log a QueueState.
IN_PROC_BROWSER_TEST_F(PrivacySandboxQueueNoticeBrowserTest,
TestNoticeQueueStateNotInQueue) {
base::HistogramTester histogram_tester;
// Suppress attempts to queue.
queue_manager().SetSuppressQueue(true);
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUISettingsURL),
WindowOpenDisposition::NEW_WINDOW,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
EXPECT_FALSE(histogram_tester
.GetTotalCountsForPrefix(
"PrivacySandbox.Notice.NotHoldingHandle.NotInQueue")
.empty());
}
// Allow the notice to be queued, but add a blocking notice that doesn't allow
// our spot in the queue to trigger. The handle check repeatedly fails, and we
// are still in the queue.
IN_PROC_BROWSER_TEST_F(PrivacySandboxQueueNoticeBrowserTest,
TestNoticeQueueStateStuckInQueue) {
base::HistogramTester histogram_tester;
// Add a test notice before our notice that hogs the handle.
user_education::test::TestNotice notice(
*queue_manager().GetProductMessagingController(), kNoticeId);
// Navigate to valid page, failing the holdingHandle check.
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUISettingsURL),
WindowOpenDisposition::NEW_WINDOW,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
EXPECT_FALSE(histogram_tester
.GetTotalCountsForPrefix(
"PrivacySandbox.Notice.NotHoldingHandle.InQueue")
.empty());
}
class PrivacySandboxQueueNoticeWithSearchEngineBrowserTest
: public PrivacySandboxQueueNoticeBrowserTest {
// Override the country to simulate showing the search engine choice dialog.
public:
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitchASCII(switches::kSearchEngineChoiceCountry, "BE");
command_line->AppendSwitch(
switches::kIgnoreNoFirstRunForSearchEngineChoiceScreen);
}
void SetUpOnMainThread() override {
PrivacySandboxQueueNoticeBrowserTest::SetUpOnMainThread();
SearchEngineChoiceDialogService::SetDialogDisabledForTests(
/*dialog_disabled=*/false);
}
private:
base::AutoReset<bool> scoped_chrome_build_override_ =
SearchEngineChoiceDialogServiceFactory::
ScopedChromeBuildOverrideForTesting(
/*force_chrome_build=*/true);
};
// Navigate to a page where the DMA notice should show and ensure suppression.
IN_PROC_BROWSER_TEST_F(PrivacySandboxQueueNoticeWithSearchEngineBrowserTest,
PromptSuppressed) {
// When we navigate to valid page for SE dialog, we should unqueue.
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(url::kAboutBlankURL), WindowOpenDisposition::NEW_WINDOW,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
ASSERT_FALSE(queue_manager().IsHoldingHandle());
// Navigate again to a valid notice page.
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUISettingsURL),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
// After second nav do not queue or hold the handle.
ASSERT_FALSE(queue_manager().IsNoticeQueued());
ASSERT_FALSE(queue_manager().IsHoldingHandle());
}
// TODO(crbug.com/409386887): This test can be removed once end-to-end tests are
// written.
class PrivacySandboxQueueNoticeFeatureDisabledBrowserTest
: public PrivacySandboxQueueNoticeBrowserTest {
public:
void SetUpInProcessBrowserTestFixture() override {
feature_list_.InitWithFeatures(
/*enabled=*/{}, {privacy_sandbox::kPrivacySandboxNoticeQueue});
create_services_subscription_ =
BrowserContextDependencyManager::GetInstance()
->RegisterCreateServicesCallbackForTesting(
base::BindRepeating([](content::BrowserContext* context) {
PrivacySandboxServiceFactory::GetInstance()
->SetTestingFactory(
context,
base::BindRepeating(&BuildPrivacySandboxServiceTest));
}));
}
};
// Navigate to a page and click a button.
IN_PROC_BROWSER_TEST_F(PrivacySandboxQueueNoticeFeatureDisabledBrowserTest,
ShowAndClickPrompt) {
// Should not be queued after browser startup
ASSERT_FALSE(queue_manager().IsNoticeQueued());
ASSERT_FALSE(queue_manager().IsHoldingHandle());
// Navigate
views::NamedWidgetShownWaiter waiter(
views::test::AnyWidgetTestPasskey{},
PrivacySandboxDialogView::kViewClassName);
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUISettingsURL),
WindowOpenDisposition::NEW_WINDOW,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
auto* dialog_widget = static_cast<PrivacySandboxDialogView*>(
waiter.WaitIfNeededAndGet()->widget_delegate()->GetContentsView());
// Before we click, should still be holding handle.
ASSERT_FALSE(queue_manager().IsNoticeQueued());
ASSERT_FALSE(queue_manager().IsHoldingHandle());
// Click ack button.
const std::string& code = element_path_ + ".click();";
EXPECT_TRUE(content::ExecJs(web_contents(dialog_widget), code,
content::EXECUTE_SCRIPT_DEFAULT_OPTIONS,
1 /* world_id */));
// After click, should release handle.
ASSERT_FALSE(queue_manager().IsNoticeQueued());
ASSERT_FALSE(queue_manager().IsHoldingHandle());
}
} // namespace
|