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 640
|
// 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 "chrome/browser/ui/views/privacy_sandbox/privacy_sandbox_dialog_view.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/run_until.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/privacy_sandbox/mock_privacy_sandbox_service.h"
#include "chrome/browser/privacy_sandbox/notice/mocks/mock_notice_service.h"
#include "chrome/browser/privacy_sandbox/notice/notice.mojom.h"
#include "chrome/browser/privacy_sandbox/notice/notice_service_factory.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_service.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/privacy_sandbox/privacy_sandbox_prompt.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/test/test_browser_dialog.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/privacy_sandbox/dialog_view_context.h"
#include "components/privacy_sandbox/privacy_sandbox_features.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "net/dns/mock_host_resolver.h"
#include "privacy_sandbox_dialog_view.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/views/test/widget_test.h"
#include "ui/views/widget/any_widget_observer.h"
#include "ui/views/widget/widget.h"
namespace {
using privacy_sandbox::MockPrivacySandboxNoticeService;
using privacy_sandbox::notice::mojom::PrivacySandboxNotice;
using enum privacy_sandbox::notice::mojom::PrivacySandboxNotice;
using testing::_;
constexpr int kAverageBrowserWidth = 800;
constexpr int kAverageBrowserHeight = 700;
constexpr base::TimeDelta kMaxWaitTime = base::Seconds(30);
// TODO(crbug.com/371041180): Refactor tests to remove these scripts and notify
// from the notice components instead.
std::string ScrollToBottomScript() {
return R"```(
(async () => {
return new Promise(async (resolve) => {
requestIdleCallback(async () => {
let dialogElement = document.querySelector("body > "+$1);
if ($2 !== "") dialogElement = dialogElement.shadowRoot.querySelector($2);
const scrollable = dialogElement.shadowRoot.querySelector('[scrollable]');
scrollFunction = () => new Promise(scrollResolve => {
let timeout = setTimeout(() => {
scrollable.removeEventListener('scrollend', scrollEndCallback);
scrollResolve();
}, 2000);
const scrollEndCallback = () => {
clearTimeout(timeout);
scrollable.removeEventListener('scrollend', scrollEndCallback);
scrollResolve();
};
scrollable.addEventListener('scrollend', scrollEndCallback);
scrollable.scrollTop = scrollable.scrollHeight;
});
waitUntilHidden = (el) => {
return new Promise(overlayResolve => {
const observer = new MutationObserver(mutations =>
mutations.some(mutation => mutation.type === 'attributes' &&
mutation.attributeName === 'hidden' && el.hasAttribute('hidden')) &&
requestAnimationFrame(() => (observer.disconnect(), overlayResolve()))
);
el.hasAttribute('hidden')
? requestAnimationFrame(overlayResolve)
: observer.observe(el, { attributes: true, attributeFilter: ['hidden'] });
});
};
await scrollFunction();
await waitUntilHidden(dialogElement.shadowRoot.querySelector('#showMoreOverlay'));
requestAnimationFrame(resolve);
});
});
})();
)```";
}
std::string WaitForPrivacyPolicyPageLoadScript() {
return R"(
(async () => {
return new Promise(async (resolve) => {
requestAnimationFrame(async () => {
dialogElement = document.querySelector("body > "+$1);
if($2 !== "") dialogElement = dialogElement.shadowRoot.querySelector($2);
waitForPrivacyPolicyResolve = (el) => new Promise(privacyPolicyResolve => {
let timeout = setTimeout(() => {
el.removeEventListener('privacy-policy-loaded', privacyPolicyLoadCallback);
privacyPolicyResolve();
}, 2000);
const privacyPolicyLoadCallback = () => {
clearTimeout(timeout);
el.removeEventListener('privacy-policy-loaded', privacyPolicyLoadCallback);
privacyPolicyResolve();
};
el.addEventListener('privacy-policy-loaded', privacyPolicyLoadCallback);
});
const privacyPolicyEl = dialogElement.shadowRoot.querySelector('privacy-sandbox-privacy-policy-dialog');
privacyPolicyEl.shadowRoot.querySelector('#privacyPolicy').src = "about:blank";
await waitForPrivacyPolicyResolve(privacyPolicyEl);
setTimeout(resolve,0);
});
});
})();
)";
}
std::string ClickLearnMoreButton3TimesScript() {
return R"(
(async () => {
return new Promise(async (resolve) => {
requestAnimationFrame(async () => {
dialogElement = document.querySelector("body > "+$1);
if($2 !== "") dialogElement = dialogElement.shadowRoot.querySelector($2);
const learnMoreElement = dialogElement.shadowRoot.querySelector($3);
const expandButtonElement = learnMoreElement.shadowRoot.querySelector('div > cr-expand-button');
const scrollable = dialogElement.shadowRoot.querySelector('[scrollable]');
waitForEndScroll = (el) => new Promise(scrollResolve => {
let timeout = setTimeout(() => {
el.removeEventListener('scrollend', scrollEndCallback);
scrollResolve();
}, 2000);
const scrollEndCallback = () => {
clearTimeout(timeout);
el.removeEventListener('scrollend', scrollEndCallback);
scrollResolve();
};
el.addEventListener('scrollend', scrollEndCallback);
expandButtonElement.click();
});
await waitForEndScroll(scrollable);
expandButtonElement.click();
await waitForEndScroll(scrollable);
expandButtonElement.blur();
setTimeout(resolve,0);
});
});
})();
)";
}
PrivacySandboxNotice GetNoticeName(const std::string& name) {
PrivacySandboxNotice notice;
if (name == "Consent") {
notice = kTopicsConsentNotice;
}
if (name == "PAMeasurement") {
notice = kProtectedAudienceMeasurementNotice;
}
if (name == "ThreeAdsApis") {
notice = kThreeAdsApisNotice;
}
if (name == "Measurement") {
notice = kMeasurementNotice;
}
return notice;
}
} // namespace
//-----------------------------------------------------------------------------
// Notice Framework Dialog View Browser Tests.
//-----------------------------------------------------------------------------
class PrivacySandboxPSNoticeDialogViewBrowserTest : public DialogBrowserTest {
public:
PrivacySandboxPSNoticeDialogViewBrowserTest() {
set_baseline("crrev.com/c/6575203");
scoped_feature_list_.InitWithFeatures(
// Enabled Features
{privacy_sandbox::kPrivacySandboxNoticeFramework},
/*disabled_features=*/{});
}
void SetUpInProcessBrowserTestFixture() override {
create_services_subscription_ =
BrowserContextDependencyManager::GetInstance()
->RegisterCreateServicesCallbackForTesting(
base::BindRepeating([](content::BrowserContext* context) {
PrivacySandboxNoticeServiceFactory::GetInstance()
->SetTestingFactory(
context,
base::BindRepeating(
&privacy_sandbox::
BuildMockPrivacySandboxNoticeService));
}));
}
// DialogBrowserTest:
void ShowUi(const std::string& name) override {
// Resize the browser window to guarantee enough space for the dialog.
BrowserView::GetBrowserViewForBrowser(browser())->GetWidget()->SetBounds(
{0, 0, kAverageBrowserWidth, kAverageBrowserHeight});
views::NamedWidgetShownWaiter waiter(
views::test::AnyWidgetTestPasskey{},
PrivacySandboxDialogView::kViewClassName);
PrivacySandboxDialog::Show(browser(), GetNoticeName(name));
views::Widget* dialog_widget = waiter.WaitIfNeededAndGet();
dialog_view_ = static_cast<PrivacySandboxDialogView*>(
dialog_widget->widget_delegate()->GetContentsView());
EXPECT_THAT(dialog_view_->GetPrivacySandboxNotice(), GetNoticeName(name));
// Verify that the DialogViewContext is present from the WebContents.
ASSERT_NE(privacy_sandbox::DialogViewContext::FromWebContents(
dialog_view_->GetWebContentsForTesting()),
nullptr);
}
void DismissUi() override {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(&PrivacySandboxDialogView::CloseNativeView,
base::Unretained(dialog_view_)));
dialog_view_ = nullptr;
}
private:
base::CallbackListSubscription create_services_subscription_;
raw_ptr<MockPrivacySandboxNoticeService> mock_notice_service_;
raw_ptr<PrivacySandboxDialogView> dialog_view_;
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_F(PrivacySandboxPSNoticeDialogViewBrowserTest,
InvokeUi_Consent) {
ShowAndVerifyUi();
}
IN_PROC_BROWSER_TEST_F(PrivacySandboxPSNoticeDialogViewBrowserTest,
InvokeUi_PAMeasurement) {
ShowAndVerifyUi();
}
IN_PROC_BROWSER_TEST_F(PrivacySandboxPSNoticeDialogViewBrowserTest,
InvokeUi_ThreeAdsApis) {
ShowAndVerifyUi();
}
IN_PROC_BROWSER_TEST_F(PrivacySandboxPSNoticeDialogViewBrowserTest,
InvokeUi_Measurement) {
ShowAndVerifyUi();
}
//-----------------------------------------------------------------------------
// Pre-migration Privacy Sandbox Dialog View Browser Tests.
//-----------------------------------------------------------------------------
class PrivacySandboxDialogViewBrowserTest : public DialogBrowserTest {
public:
PrivacySandboxDialogViewBrowserTest() {
set_baseline("crrev.com/c/6391455");
scoped_feature_list_.InitAndDisableFeature(
privacy_sandbox::kPrivacySandboxAdsApiUxEnhancements);
}
void SetUpOnMainThread() override {
mock_service_ = static_cast<MockPrivacySandboxService*>(
PrivacySandboxServiceFactory::GetInstance()->SetTestingFactoryAndUse(
browser()->profile(),
base::BindRepeating(&BuildMockPrivacySandboxService)));
}
// DialogBrowserTest:
void ShowUi(const std::string& name) override {
PrivacySandboxService::PromptType prompt_type =
PrivacySandboxService::PromptType::kNone;
if (name == "Consent") {
prompt_type = PrivacySandboxService::PromptType::kM1Consent;
}
if (name == "Notice") {
prompt_type = PrivacySandboxService::PromptType::kM1NoticeROW;
}
if (name == "RestrictedNotice") {
prompt_type = PrivacySandboxService::PromptType::kM1NoticeRestricted;
}
ASSERT_NE(prompt_type, PrivacySandboxService::PromptType::kNone);
// Resize the browser window to guarantee enough space for the dialog.
BrowserView::GetBrowserViewForBrowser(browser())->GetWidget()->SetBounds(
{0, 0, kAverageBrowserWidth, kAverageBrowserHeight});
views::NamedWidgetShownWaiter waiter(
views::test::AnyWidgetTestPasskey{},
PrivacySandboxDialogView::kViewClassName);
PrivacySandboxDialog::Show(browser(), prompt_type);
views::Widget* dialog_widget = waiter.WaitIfNeededAndGet();
auto* privacy_sandbox_dialog_view = static_cast<PrivacySandboxDialogView*>(
dialog_widget->widget_delegate()->GetContentsView());
// Verify that the DialogViewContext is present from the WebContents.
ASSERT_NE(privacy_sandbox::DialogViewContext::FromWebContents(
privacy_sandbox_dialog_view->GetWebContentsForTesting()),
nullptr);
}
MockPrivacySandboxService* mock_service() { return mock_service_; }
private:
raw_ptr<MockPrivacySandboxService, DanglingUntriaged> mock_service_;
base::test::ScopedFeatureList scoped_feature_list_;
};
#if !BUILDFLAG(IS_LINUX)
IN_PROC_BROWSER_TEST_F(PrivacySandboxDialogViewBrowserTest, InvokeUi_Consent) {
base::WaitableEvent shown_waiter;
base::WaitableEvent closed_waiter;
EXPECT_CALL(
*mock_service(),
PromptActionOccurred(PrivacySandboxService::PromptAction::kConsentShown,
PrivacySandboxService::SurfaceType::kDesktop))
.WillOnce([&shown_waiter] { shown_waiter.Signal(); });
EXPECT_CALL(*mock_service(),
PromptActionOccurred(
PrivacySandboxService::PromptAction::kConsentClosedNoDecision,
PrivacySandboxService::SurfaceType::kDesktop))
.WillOnce([&closed_waiter] { closed_waiter.Signal(); });
ShowAndVerifyUi();
shown_waiter.TimedWait(kMaxWaitTime);
closed_waiter.TimedWait(kMaxWaitTime);
}
IN_PROC_BROWSER_TEST_F(PrivacySandboxDialogViewBrowserTest, InvokeUi_Notice) {
base::WaitableEvent shown_waiter;
base::WaitableEvent closed_waiter;
EXPECT_CALL(
*mock_service(),
PromptActionOccurred(PrivacySandboxService::PromptAction::kNoticeShown,
PrivacySandboxService::SurfaceType::kDesktop))
.WillOnce([&shown_waiter] { shown_waiter.Signal(); });
EXPECT_CALL(
*mock_service(),
PromptActionOccurred(
PrivacySandboxService::PromptAction::kNoticeClosedNoInteraction,
PrivacySandboxService::SurfaceType::kDesktop))
.WillOnce([&closed_waiter] { closed_waiter.Signal(); });
ShowAndVerifyUi();
shown_waiter.TimedWait(kMaxWaitTime);
closed_waiter.TimedWait(kMaxWaitTime);
}
IN_PROC_BROWSER_TEST_F(PrivacySandboxDialogViewBrowserTest,
InvokeUi_RestrictedNotice) {
base::WaitableEvent shown_waiter;
base::WaitableEvent closed_waiter;
EXPECT_CALL(*mock_service(),
PromptActionOccurred(
PrivacySandboxService::PromptAction::kRestrictedNoticeShown,
PrivacySandboxService::SurfaceType::kDesktop))
.WillOnce([&shown_waiter] { shown_waiter.Signal(); });
EXPECT_CALL(
*mock_service(),
PromptActionOccurred(PrivacySandboxService::PromptAction::
kRestrictedNoticeClosedNoInteraction,
PrivacySandboxService::SurfaceType::kDesktop))
.WillOnce([&closed_waiter] { closed_waiter.Signal(); });
ShowAndVerifyUi();
shown_waiter.TimedWait(kMaxWaitTime);
closed_waiter.TimedWait(kMaxWaitTime);
}
#endif
class PrivacySandboxDialogViewAdsApiUxEnhancementBrowserTest
: public PrivacySandboxDialogViewBrowserTest {
public:
PrivacySandboxDialogViewAdsApiUxEnhancementBrowserTest() {
scoped_feature_list_.InitWithFeatures(
// Enabled Features
{privacy_sandbox::kPrivacySandboxAdsApiUxEnhancements},
// Disabled Features
{privacy_sandbox::kPrivacySandboxAdTopicsContentParity});
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_F(PrivacySandboxDialogViewAdsApiUxEnhancementBrowserTest,
InvokeUi_Consent) {
base::WaitableEvent shown_waiter;
base::WaitableEvent closed_waiter;
EXPECT_CALL(
*mock_service(),
PromptActionOccurred(PrivacySandboxService::PromptAction::kConsentShown,
PrivacySandboxService::SurfaceType::kDesktop))
.WillOnce([&shown_waiter] { shown_waiter.Signal(); });
EXPECT_CALL(*mock_service(),
PromptActionOccurred(
PrivacySandboxService::PromptAction::kConsentClosedNoDecision,
PrivacySandboxService::SurfaceType::kDesktop))
.WillOnce([&closed_waiter] { closed_waiter.Signal(); });
ShowAndVerifyUi();
shown_waiter.TimedWait(kMaxWaitTime);
closed_waiter.TimedWait(kMaxWaitTime);
}
// TODO(crbug.com/396446633): Add pixel tests for other dialogs with ads api ux
// enhancements and ad topics content parity.
class PrivacySandboxDialogViewPrivacyPolicyBrowserTest
: public PrivacySandboxDialogViewBrowserTest {
public:
PrivacySandboxDialogViewPrivacyPolicyBrowserTest() {
scoped_feature_list_.InitWithFeatures(
// Enabled
{},
// Disabled
{privacy_sandbox::kPrivacySandboxAdsApiUxEnhancements});
}
virtual std::string GetPrivacyPolicyLinkElementId() {
return "#privacyPolicyLink";
}
// DialogBrowserTest:
void ShowUi(const std::string& name) override {
PrivacySandboxService::PromptType prompt_type =
PrivacySandboxService::PromptType::kM1Consent;
// Resize the browser window to guarantee enough space for the dialog.
BrowserView::GetBrowserViewForBrowser(browser())->GetWidget()->SetBounds(
{0, 0, kAverageBrowserWidth, kAverageBrowserHeight});
views::NamedWidgetShownWaiter waiter(
views::test::AnyWidgetTestPasskey{},
PrivacySandboxDialogView::kViewClassName);
PrivacySandboxDialog::Show(browser(), prompt_type);
views::Widget* dialog_widget = waiter.WaitIfNeededAndGet();
views::test::WidgetVisibleWaiter(dialog_widget).Wait();
ASSERT_TRUE(dialog_widget->IsVisible());
auto* privacy_sandbox_dialog_view = static_cast<PrivacySandboxDialogView*>(
dialog_widget->widget_delegate()->GetContentsView());
// Verify that the DialogViewContext is present from the WebContents.
ASSERT_NE(privacy_sandbox::DialogViewContext::FromWebContents(
privacy_sandbox_dialog_view->GetWebContentsForTesting()),
nullptr);
// Privacy policy is initially not visible, click expand button and trigger
// visibility.
EXPECT_TRUE(
content::ExecJs(privacy_sandbox_dialog_view->GetWebContentsForTesting(),
"document.querySelector('body > "
"privacy-sandbox-combined-dialog-app').shadowRoot."
"querySelector('#consent').shadowRoot.querySelector('"
"privacy-sandbox-dialog-learn-more').shadowRoot."
"querySelector('div > cr-expand-button').click()"));
EXPECT_TRUE(content::ExecJs(
privacy_sandbox_dialog_view->GetWebContentsForTesting(),
content::JsReplace(WaitForPrivacyPolicyPageLoadScript(),
"privacy-sandbox-combined-dialog-app", "#consent")));
// Click Privacy Policy link.
EXPECT_TRUE(
content::ExecJs(privacy_sandbox_dialog_view->GetWebContentsForTesting(),
"document.querySelector('body > "
"privacy-sandbox-combined-dialog-app')"
".shadowRoot.querySelector('#consent')"
".shadowRoot.querySelector('" +
GetPrivacyPolicyLinkElementId() +
"')"
".click()"));
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
// TODO(https://crbug.com/415305952): High failure rate.
IN_PROC_BROWSER_TEST_F(PrivacySandboxDialogViewPrivacyPolicyBrowserTest,
InvokeUi_PrivacyPolicy) {
ShowAndVerifyUi();
}
class PrivacySandboxDialogViewAdsApiUxEnhancementPrivacyPolicyBrowserTest
: public PrivacySandboxDialogViewPrivacyPolicyBrowserTest {
public:
PrivacySandboxDialogViewAdsApiUxEnhancementPrivacyPolicyBrowserTest() {
scoped_feature_list_.InitWithFeatures(
// Enabled Features
{privacy_sandbox::kPrivacySandboxAdsApiUxEnhancements},
// Disabled Features
{privacy_sandbox::kPrivacySandboxAdTopicsContentParity});
}
std::string GetPrivacyPolicyLinkElementId() override {
return "#privacyPolicyLinkV2";
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
// TODO(https://crbug.com/415305952): High failure rate.
IN_PROC_BROWSER_TEST_F(
PrivacySandboxDialogViewAdsApiUxEnhancementPrivacyPolicyBrowserTest,
InvokeUi_PrivacyPolicy) {
ShowAndVerifyUi();
}
// TODO(crbug.com/378886088): Refactor file to reduce code duplication between
// different test classes.
class PrivacySandboxDialogViewAdsApiUxEnhancementsLearnMoreBrowserTest
: public PrivacySandboxDialogViewBrowserTest {
public:
PrivacySandboxDialogViewAdsApiUxEnhancementsLearnMoreBrowserTest() {
scoped_feature_list_.InitWithFeatures(
// Enabled Features
{privacy_sandbox::kPrivacySandboxAdsApiUxEnhancements},
// Disabled Features
{privacy_sandbox::kPrivacySandboxAdTopicsContentParity});
}
// DialogBrowserTest:
void ShowUi(const std::string& name) override {
// Resize the browser window to guarantee enough space for the dialog.
BrowserView::GetBrowserViewForBrowser(browser())->GetWidget()->SetBounds(
{0, 0, kAverageBrowserWidth, kAverageBrowserHeight});
views::NamedWidgetShownWaiter waiter(
views::test::AnyWidgetTestPasskey{},
PrivacySandboxDialogView::kViewClassName);
PrivacySandboxDialog::Show(browser(), GetPromptType(name));
views::Widget* dialog_widget = waiter.WaitIfNeededAndGet();
views::test::WidgetVisibleWaiter(dialog_widget).Wait();
ASSERT_TRUE(dialog_widget->IsVisible());
auto* privacy_sandbox_dialog_view = static_cast<PrivacySandboxDialogView*>(
dialog_widget->widget_delegate()->GetContentsView());
// Verify that the DialogViewContext is present from the WebContents.
ASSERT_NE(privacy_sandbox::DialogViewContext::FromWebContents(
privacy_sandbox_dialog_view->GetWebContentsForTesting()),
nullptr);
auto [primary_selector, secondary_selector] =
GetDialogElementSelector(name);
// Open, close, and reopen the learn more section. This ensures that there
// is no behind the scenes rendering that could cause a layout issue. This
// adds test coverage for a past regression where only the second expand
// caused a layout issue (crbug.com/388420268).
EXPECT_TRUE(
content::ExecJs(privacy_sandbox_dialog_view->GetWebContentsForTesting(),
content::JsReplace(ClickLearnMoreButton3TimesScript(),
primary_selector, secondary_selector,
GetLearnMoreElementSelector(name))));
// Scroll the view to the bottom before taking a screenshot.
EXPECT_TRUE(content::ExecJs(
privacy_sandbox_dialog_view->GetWebContentsForTesting(),
content::JsReplace(ScrollToBottomScript(), primary_selector,
secondary_selector)));
}
private:
PrivacySandboxService::PromptType GetPromptType(std::string_view name) {
if (name == "ConsentEEA") {
return PrivacySandboxService::PromptType::kM1Consent;
}
if (name == "NoticeEEAsiteSuggestedAds" ||
name == "NoticeEEAadsMeasurementLearnMore") {
return PrivacySandboxService::PromptType::kM1NoticeEEA;
}
if (name == "NoticeROW") {
return PrivacySandboxService::PromptType::kM1NoticeROW;
}
NOTREACHED();
}
std::pair<std::string, std::string> GetDialogElementSelector(
const std::string& name) {
if (name == "ConsentEEA") {
return std::make_pair("privacy-sandbox-combined-dialog-app", "#consent");
}
if (name == "NoticeEEAsiteSuggestedAds" ||
name == "NoticeEEAadsMeasurementLearnMore") {
return std::make_pair("privacy-sandbox-combined-dialog-app", "#notice");
}
if (name == "NoticeROW") {
return std::make_pair("privacy-sandbox-notice-dialog-app", "");
}
NOTREACHED();
}
std::string GetLearnMoreElementSelector(const std::string& name) {
if (name == "NoticeEEAsiteSuggestedAds") {
return "#siteSuggestedAdsLearnMore";
}
if (name == "NoticeEEAadsMeasurementLearnMore") {
return "#adsMeasurementLearnMore";
}
if (name == "ConsentEEA" || name == "NoticeROW") {
return "privacy-sandbox-dialog-learn-more";
}
NOTREACHED();
}
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_F(
PrivacySandboxDialogViewAdsApiUxEnhancementsLearnMoreBrowserTest,
InvokeUi_ConsentEEA) {
ShowAndVerifyUi();
}
IN_PROC_BROWSER_TEST_F(
PrivacySandboxDialogViewAdsApiUxEnhancementsLearnMoreBrowserTest,
InvokeUi_NoticeEEAsiteSuggestedAds) {
ShowAndVerifyUi();
}
IN_PROC_BROWSER_TEST_F(
PrivacySandboxDialogViewAdsApiUxEnhancementsLearnMoreBrowserTest,
InvokeUi_NoticeEEAadsMeasurementLearnMore) {
ShowAndVerifyUi();
}
IN_PROC_BROWSER_TEST_F(
PrivacySandboxDialogViewAdsApiUxEnhancementsLearnMoreBrowserTest,
InvokeUi_NoticeROW) {
ShowAndVerifyUi();
}
|