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 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/privacy_sandbox/privacy_sandbox_prompt_helper.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/privacy_sandbox/mock_privacy_sandbox_service.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_service.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_service_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/sync/sync_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/search/ntp_test_utils.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/keyed_service/content/browser_context_dependency_manager.h"
#include "components/privacy_sandbox/privacy_sandbox_features.h"
#include "components/regional_capabilities/regional_capabilities_switches.h"
#include "components/search_engines/search_engines_switches.h"
#include "components/signin/public/base/signin_switches.h"
#include "components/sync/test/test_sync_service.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/browser_test.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "url/url_constants.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "ash/webui/help_app_ui/url_constants.h"
#endif
namespace {
const char kPrivacySandboxDialogDisplayHostHistogram[] =
"Settings.PrivacySandbox.DialogDisplayHost";
constexpr char kPrivacySandboxPromptHelperEventHistogram[] =
"Settings.PrivacySandbox.PromptHelperEvent2";
std::unique_ptr<KeyedService> CreateTestSyncService(content::BrowserContext*) {
return std::make_unique<syncer::TestSyncService>();
}
} // namespace
class PrivacySandboxPromptHelperTest : public InProcessBrowserTest {
public:
PrivacySandboxPromptHelperTest()
: https_test_server_(net::EmbeddedTestServer::TYPE_HTTPS) {}
// This setup happens before the tests run as a second browser startup.
// Mock service creation happens here so that we have a valid browser() object
// to initialize the queue_manager_ with after the first browser startup in
// SetUpInProcessBrowserTestFixture().
void SetUpOnMainThread() override {
mock_privacy_sandbox_service_ = static_cast<MockPrivacySandboxService*>(
PrivacySandboxServiceFactory::GetInstance()->SetTestingFactoryAndUse(
browser()->profile(),
base::BindRepeating(&BuildMockPrivacySandboxService)));
ON_CALL(*mock_privacy_sandbox_service_,
GetRequiredPromptType(PrivacySandboxService::SurfaceType::kDesktop))
.WillByDefault(testing::Return(TestPromptType()));
ON_CALL(*mock_privacy_sandbox_service_, IsPromptOpenForBrowser(testing::_))
.WillByDefault(testing::Return(false));
queue_manager_ =
std::make_unique<privacy_sandbox::PrivacySandboxQueueManager>(
browser()->profile());
ON_CALL(*mock_privacy_sandbox_service_,
GetPrivacySandboxNoticeQueueManager())
.WillByDefault(testing::ReturnRef(*queue_manager_.get()));
}
// Reset queue manager after all browser-related processes have completed,
// including tear down.
void PostRunTestOnMainThread() override {
InProcessBrowserTest::PostRunTestOnMainThread();
queue_manager_.reset();
}
// This setup happens before SetUpOnMainThread() as an initial startup.
void SetUpInProcessBrowserTestFixture() override {
PrivacySandboxService::SetPromptDisabledForTests(false);
host_resolver()->AddRule("*", "127.0.0.1");
https_test_server()->AddDefaultHandlers(
base::FilePath(FILE_PATH_LITERAL("content/test/data")));
ASSERT_TRUE(https_test_server()->Start());
create_services_subscription_ =
BrowserContextDependencyManager::GetInstance()
->RegisterCreateServicesCallbackForTesting(base::BindRepeating(
&PrivacySandboxPromptHelperTest::SetupTestFactories,
base::Unretained(this)));
}
void SetupTestFactories(content::BrowserContext* context) {
SyncServiceFactory::GetInstance()->SetTestingFactory(
context, base::BindRepeating(&CreateTestSyncService));
}
virtual PrivacySandboxService::PromptType TestPromptType() {
return PrivacySandboxService::PromptType::kNone;
}
void ValidatePromptEventEntries(
base::HistogramTester* histogram_tester,
std::map<
PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent,
int> expected_event_count) {
int total_expected_count = 0;
for (const auto& event_to_count : expected_event_count) {
histogram_tester->ExpectBucketCount(
kPrivacySandboxPromptHelperEventHistogram, event_to_count.first,
event_to_count.second);
total_expected_count += event_to_count.second;
}
// Always ignore any entries for non-top frame and pending navigations,
// these are recorded for completeness, but are not directly tested as they
// are fragile.
total_expected_count += histogram_tester->GetBucketCount(
kPrivacySandboxPromptHelperEventHistogram,
PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kNonTopFrameNavigation);
histogram_tester->ExpectTotalCount(
kPrivacySandboxPromptHelperEventHistogram, total_expected_count);
}
syncer::TestSyncService* test_sync_service() {
return static_cast<syncer::TestSyncService*>(
SyncServiceFactory::GetForProfile(browser()->profile()));
}
MockPrivacySandboxService* mock_privacy_sandbox_service() {
return mock_privacy_sandbox_service_;
}
net::EmbeddedTestServer* https_test_server() { return &https_test_server_; }
private:
base::CallbackListSubscription create_services_subscription_;
net::EmbeddedTestServer https_test_server_;
std::unique_ptr<privacy_sandbox::PrivacySandboxQueueManager> queue_manager_;
raw_ptr<MockPrivacySandboxService, DanglingUntriaged>
mock_privacy_sandbox_service_;
};
IN_PROC_BROWSER_TEST_F(PrivacySandboxPromptHelperTest, NoPromptRequired) {
// Check when no prompt is required, it is not shown.
base::HistogramTester histogram_tester;
EXPECT_CALL(*mock_privacy_sandbox_service(),
PromptOpenedForBrowser(browser(), testing::_))
.Times(0);
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUINewTabPageURL),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
base::RunLoop().RunUntilIdle();
ValidatePromptEventEntries(&histogram_tester, {});
}
class PrivacySandboxPromptHelperTestWithParam
: public PrivacySandboxPromptHelperTest,
public testing::WithParamInterface<PrivacySandboxService::PromptType> {
private:
PrivacySandboxService::PromptType TestPromptType() override {
// Setup appropriate prompt type based on testing parameter. Helper
// behavior should be "identical" regardless of which type of prompt is
// required.
return GetParam();
}
base::test::ScopedFeatureList feature_list_{
privacy_sandbox::kPrivacySandboxSettings4};
};
IN_PROC_BROWSER_TEST_P(PrivacySandboxPromptHelperTestWithParam,
PromptOpensOnNtp) {
// Check when a navigation to the Chrome controlled NTP occurs, which is a
// suitable location, a prompt is shown.
base::HistogramTester histogram_tester;
EXPECT_CALL(*mock_privacy_sandbox_service(),
PromptOpenedForBrowser(browser(), testing::_))
.Times(1);
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUINewTabPageURL),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
base::RunLoop().RunUntilIdle();
histogram_tester.ExpectUniqueSample(
kPrivacySandboxDialogDisplayHostHistogram,
static_cast<base::HistogramBase::Sample32>(base::Hash("new-tab-page")), 1);
ValidatePromptEventEntries(
&histogram_tester,
{{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kCreated,
1},
{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kPromptShown,
1}});
}
IN_PROC_BROWSER_TEST_P(PrivacySandboxPromptHelperTestWithParam,
PromptOpensAboutBlank) {
// Check when a navigation to about:blank occurs, which is a suitable
// location, a prompt is shown.
base::HistogramTester histogram_tester;
EXPECT_CALL(*mock_privacy_sandbox_service(),
PromptOpenedForBrowser(browser(), testing::_))
.Times(1);
EXPECT_CALL(*mock_privacy_sandbox_service(),
IsPromptOpenForBrowser(browser()))
.Times(1)
.WillOnce(testing::Return(false));
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(url::kAboutBlankURL),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
base::RunLoop().RunUntilIdle();
histogram_tester.ExpectUniqueSample(
kPrivacySandboxDialogDisplayHostHistogram,
static_cast<base::HistogramBase::Sample32>(base::Hash("about:blank")), 1);
ValidatePromptEventEntries(
&histogram_tester,
{{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kCreated,
1},
{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kPromptShown,
1}});
}
IN_PROC_BROWSER_TEST_P(PrivacySandboxPromptHelperTestWithParam,
PromptOpensOnSettings) {
// Check when a navigation to the Chrome settings occurs, which is a
// suitable location, a prompt is shown.
base::HistogramTester histogram_tester;
EXPECT_CALL(*mock_privacy_sandbox_service(),
PromptOpenedForBrowser(browser(), testing::_))
.Times(1);
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUISettingsURL),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
base::RunLoop().RunUntilIdle();
histogram_tester.ExpectUniqueSample(
kPrivacySandboxDialogDisplayHostHistogram,
static_cast<base::HistogramBase::Sample32>(base::Hash("settings")), 1);
ValidatePromptEventEntries(
&histogram_tester,
{{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kCreated,
1},
{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kPromptShown,
1}});
}
// TODO(crbug.com/40270789): Debug and re-enable the test.
#if BUILDFLAG(IS_CHROMEOS)
#define MAYBE_PromptOpensOnHistory DISABLED_PromptOpensOnHistory
#else
#define MAYBE_PromptOpensOnHistory PromptOpensOnHistory
#endif
IN_PROC_BROWSER_TEST_P(PrivacySandboxPromptHelperTestWithParam,
MAYBE_PromptOpensOnHistory) {
// Check when a navigation to the Chrome history occurs, which is a
// suitable location, a prompt is shown.
base::HistogramTester histogram_tester;
EXPECT_CALL(*mock_privacy_sandbox_service(),
PromptOpenedForBrowser(browser(), testing::_))
.Times(1);
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUIHistoryURL),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
base::RunLoop().RunUntilIdle();
histogram_tester.ExpectUniqueSample(
kPrivacySandboxDialogDisplayHostHistogram,
static_cast<base::HistogramBase::Sample32>(base::Hash("history")), 1);
ValidatePromptEventEntries(
&histogram_tester,
{{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kCreated,
1},
{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kPromptShown,
1}});
}
#if !BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_P(PrivacySandboxPromptHelperTestWithParam,
NoPromptNonDefaultNtp) {
// Check that navigations to the generic chrome://newtab, when a non default
// NTP is used, do not show a prompt. On ChromeOS, it opens an about blank
// tab to display the prompt because it cannot be handled during startup
// there.
base::HistogramTester histogram_tester;
EXPECT_CALL(*mock_privacy_sandbox_service(),
PromptOpenedForBrowser(browser(), testing::_))
.Times(0);
// Sets up explicit redirect to invalid URL. Otherwise, redirects to
// chrome://new-tab-page/, which is considered a valid URL.
GURL ntp_url = https_test_server()->GetURL("/title1.html");
ntp_test_utils::SetUserSelectedDefaultSearchProvider(
browser()->profile(), https_test_server()->base_url().spec(),
ntp_url.spec());
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUINewTabURL),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
base::RunLoop().RunUntilIdle();
histogram_tester.ExpectTotalCount(kPrivacySandboxDialogDisplayHostHistogram,
0);
ValidatePromptEventEntries(
&histogram_tester,
{{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kCreated,
1},
{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kUrlNotSuitable,
1}});
}
#endif
IN_PROC_BROWSER_TEST_P(PrivacySandboxPromptHelperTestWithParam, NoPromptSync) {
// Check when sync setup is in progress, that no prompt is shown.
base::HistogramTester histogram_tester;
EXPECT_CALL(*mock_privacy_sandbox_service(),
PromptOpenedForBrowser(browser(), testing::_))
.Times(0);
test_sync_service()->SetSetupInProgress();
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUINewTabPageURL),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
base::RunLoop().RunUntilIdle();
histogram_tester.ExpectTotalCount(kPrivacySandboxDialogDisplayHostHistogram,
0);
ValidatePromptEventEntries(
&histogram_tester,
{{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kCreated,
1},
{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kSyncSetupInProgress,
1}});
}
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
IN_PROC_BROWSER_TEST_P(PrivacySandboxPromptHelperTestWithParam,
NoPromptProfileSetup) {
// Check when profile setup is in progress, that no prompt is shown.
base::HistogramTester histogram_tester;
EXPECT_CALL(*mock_privacy_sandbox_service(),
PromptOpenedForBrowser(browser(), testing::_))
.Times(0);
// Show the profile customization dialog.
browser()->signin_view_controller()->ShowModalProfileCustomizationDialog(
/*is_local_profile_creation=*/true);
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUINewTabPageURL),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
base::RunLoop().RunUntilIdle();
histogram_tester.ExpectTotalCount(kPrivacySandboxDialogDisplayHostHistogram,
0);
ValidatePromptEventEntries(
&histogram_tester,
{{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kCreated,
1},
{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kSigninDialogShown,
1}});
}
#endif // BUILDFLAG(ENABLE_DICE_SUPPORT)
IN_PROC_BROWSER_TEST_P(PrivacySandboxPromptHelperTestWithParam, UnsuitableUrl) {
// Check that no prompt is shown for navigations to unsuitable URLs.
base::HistogramTester histogram_tester;
EXPECT_CALL(*mock_privacy_sandbox_service(),
PromptOpenedForBrowser(browser(), testing::_))
.Times(0);
std::vector<GURL> urls_to_open = {
https_test_server()->GetURL("a.test", "/title1.html"),
GURL(chrome::kChromeUISettingsURL).Resolve(chrome::kAutofillSubPage),
#if BUILDFLAG(IS_CHROMEOS)
GURL(ash::kChromeUIHelpAppURL),
GURL(chrome::kChromeUIOSSettingsURL),
#endif
};
for (size_t i = 0; i < urls_to_open.size(); ++i) {
if (i == 0) {
// Open the first URL in a new tab to create a fresh new tab helper.
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), urls_to_open[i], WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
} else {
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), urls_to_open[i]));
}
}
base::RunLoop().RunUntilIdle();
histogram_tester.ExpectTotalCount(kPrivacySandboxDialogDisplayHostHistogram,
0);
ValidatePromptEventEntries(
&histogram_tester,
{{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kCreated,
1},
{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kUrlNotSuitable,
urls_to_open.size()}});
}
IN_PROC_BROWSER_TEST_P(PrivacySandboxPromptHelperTestWithParam,
SinglePromptPerBrowser) {
// Check that only a single prompt is opened per browser window at a time.
base::HistogramTester histogram_tester;
EXPECT_CALL(*mock_privacy_sandbox_service(),
PromptOpenedForBrowser(browser(), testing::_))
.Times(1);
EXPECT_CALL(*mock_privacy_sandbox_service(),
IsPromptOpenForBrowser(browser()))
.WillOnce(testing::Return(false))
.WillRepeatedly(testing::Return(true));
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUINewTabPageURL),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), GURL(chrome::kChromeUINewTabPageURL)));
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), GURL(chrome::kChromeUINewTabPageURL)));
base::RunLoop().RunUntilIdle();
histogram_tester.ExpectUniqueSample(
kPrivacySandboxDialogDisplayHostHistogram,
static_cast<base::HistogramBase::Sample32>(base::Hash("new-tab-page")), 1);
ValidatePromptEventEntries(
&histogram_tester,
{{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kCreated,
1},
{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kPromptAlreadyExistsForBrowser,
2},
{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kPromptShown,
1}});
}
IN_PROC_BROWSER_TEST_P(PrivacySandboxPromptHelperTestWithParam,
MultipleBrowserWindows) {
// Check that if multiple browser windows are opened, and navigated to
// appropriate tabs, two prompts are opened.
base::HistogramTester histogram_tester;
EXPECT_CALL(*mock_privacy_sandbox_service(),
PromptOpenedForBrowser(testing::_, testing::_))
.Times(2);
ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(url::kAboutBlankURL), WindowOpenDisposition::NEW_WINDOW,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(chrome::kChromeUINewTabPageURL),
WindowOpenDisposition::NEW_WINDOW,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
histogram_tester.ExpectBucketCount(
kPrivacySandboxDialogDisplayHostHistogram,
static_cast<base::HistogramBase::Sample32>(base::Hash("new-tab-page")), 1);
histogram_tester.ExpectBucketCount(
kPrivacySandboxDialogDisplayHostHistogram,
static_cast<base::HistogramBase::Sample32>(base::Hash("about:blank")), 1);
ValidatePromptEventEntries(
&histogram_tester,
{{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kCreated,
2},
{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kPromptShown,
2}});
}
INSTANTIATE_TEST_SUITE_P(
PrivacySandboxPromptHelperTestWithParamInstance,
PrivacySandboxPromptHelperTestWithParam,
testing::Values(PrivacySandboxService::PromptType::kM1Consent,
PrivacySandboxService::PromptType::kM1NoticeEEA,
PrivacySandboxService::PromptType::kM1NoticeROW));
class PrivacySandboxPromptNonNormalBrowserTest
: public PrivacySandboxPromptHelperTest,
public testing::WithParamInterface<PrivacySandboxService::PromptType> {
public:
PrivacySandboxService::PromptType TestPromptType() override {
return GetParam();
}
};
IN_PROC_BROWSER_TEST_P(PrivacySandboxPromptNonNormalBrowserTest,
NoPromptInLargeBrowser) {
base::HistogramTester histogram_tester;
EXPECT_CALL(*mock_privacy_sandbox_service(),
PromptOpenedForBrowser(testing::_, testing::_))
.Times(0);
NavigateParams params(browser(), GURL(chrome::kChromeUINewTabPageURL),
ui::PAGE_TRANSITION_FIRST);
params.window_action = NavigateParams::SHOW_WINDOW;
params.disposition = WindowOpenDisposition::NEW_POPUP;
params.window_features.bounds = gfx::Rect(0, 0, 500, 500);
ui_test_utils::NavigateToURL(¶ms);
ValidatePromptEventEntries(
&histogram_tester,
{{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kCreated,
1},
{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kNonNormalBrowser,
1},
{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kPromptShown,
0}});
}
IN_PROC_BROWSER_TEST_P(PrivacySandboxPromptNonNormalBrowserTest,
NoPromptInSmallBrowser) {
base::HistogramTester histogram_tester;
EXPECT_CALL(*mock_privacy_sandbox_service(),
PromptOpenedForBrowser(testing::_, testing::_))
.Times(0);
NavigateParams params(browser(), GURL(chrome::kChromeUINewTabPageURL),
ui::PAGE_TRANSITION_FIRST);
params.window_action = NavigateParams::SHOW_WINDOW;
params.disposition = WindowOpenDisposition::NEW_POPUP;
params.window_features.bounds = gfx::Rect(0, 0, 200, 200);
ui_test_utils::NavigateToURL(¶ms);
ValidatePromptEventEntries(
&histogram_tester,
{{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kCreated,
1},
{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kNonNormalBrowser,
1},
{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kPromptShown,
0}});
}
INSTANTIATE_TEST_SUITE_P(
PrivacySandboxPromptNonNormalBrowserTestInstance,
PrivacySandboxPromptNonNormalBrowserTest,
testing::Values(PrivacySandboxService::PromptType::kM1Consent,
PrivacySandboxService::PromptType::kM1NoticeEEA,
PrivacySandboxService::PromptType::kM1NoticeROW,
PrivacySandboxService::PromptType::kM1NoticeRestricted));
class PrivacySandboxPromptHelperTestWithSearchEngineChoiceEnabled
: public PrivacySandboxPromptHelperTestWithParam {
public:
void SetUpOnMainThread() override {
PrivacySandboxPromptHelperTestWithParam::SetUpOnMainThread();
SearchEngineChoiceDialogService::SetDialogDisabledForTests(
/*dialog_disabled=*/false);
}
// Override the country to simulate showing the search engine choice dialog.
void SetUpCommandLine(base::CommandLine* command_line) override {
PrivacySandboxPromptHelperTestWithParam::SetUpCommandLine(command_line);
command_line->AppendSwitchASCII(switches::kSearchEngineChoiceCountry, "BE");
command_line->AppendSwitch(
switches::kIgnoreNoFirstRunForSearchEngineChoiceScreen);
}
private:
base::AutoReset<bool> scoped_chrome_build_override_ =
SearchEngineChoiceDialogServiceFactory::
ScopedChromeBuildOverrideForTesting(
/*force_chrome_build=*/true);
};
IN_PROC_BROWSER_TEST_P(
PrivacySandboxPromptHelperTestWithSearchEngineChoiceEnabled,
NoPromptWhenSearchEngineChoiceDialogIsDisplayed) {
// Check that the Privacy Sandbox dialog is not shown.
base::HistogramTester histogram_tester;
EXPECT_CALL(*mock_privacy_sandbox_service(),
PromptOpenedForBrowser(browser(), testing::_))
.Times(0);
// Navigate to a url to show the search engine choice dialog.
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(url::kAboutBlankURL),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
histogram_tester.ExpectTotalCount(kPrivacySandboxDialogDisplayHostHistogram,
0);
ValidatePromptEventEntries(
&histogram_tester,
{{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kSearchEngineChoiceDialogShown,
1}});
// Make a search engine choice to close the dialog.
SearchEngineChoiceDialogService* search_engine_choice_dialog_service =
SearchEngineChoiceDialogServiceFactory::GetForProfile(
browser()->profile());
search_engine_choice_dialog_service->NotifyChoiceMade(
/*prepopulate_id=*/1, /*save_guest_mode_selection=*/false,
SearchEngineChoiceDialogService::EntryPoint::kDialog);
// Make sure that the Privacy Sandbox prompt doesn't get displayed on the next
// navigation.
EXPECT_CALL(*mock_privacy_sandbox_service(),
PromptOpenedForBrowser(browser(), testing::_))
.Times(0);
ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(url::kAboutBlankURL), WindowOpenDisposition::CURRENT_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP));
}
INSTANTIATE_TEST_SUITE_P(
PrivacySandboxPromptHelperTestWithParamInstance,
PrivacySandboxPromptHelperTestWithSearchEngineChoiceEnabled,
testing::Values(PrivacySandboxService::PromptType::kM1Consent,
PrivacySandboxService::PromptType::kM1NoticeEEA,
PrivacySandboxService::PromptType::kM1NoticeROW));
|