| // Copyright 2022 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "build/build_config.h" |
| #include "chrome/browser/privacy_sandbox/mock_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/privacy_sandbox_dialog_view.h" |
| #include "content/public/browser/web_contents.h" |
| #include "content/public/test/browser_test.h" |
| #include "testing/gmock/include/gmock/gmock.h" |
| #include "ui/views/widget/any_widget_observer.h" |
| #include "ui/views/widget/widget.h" |
| |
| namespace { |
| |
| constexpr int kAverageBrowserWidth = 800; |
| constexpr int kAverageBrowserHeight = 700; |
| |
| } // namespace |
| |
| class PrivacySandboxDialogViewBrowserTest : public DialogBrowserTest { |
| public: |
| 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::kConsent; |
| } |
| if (name == "Notice") { |
| prompt_type = PrivacySandboxService::PromptType::kNotice; |
| } |
| 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); |
| ShowPrivacySandboxPrompt(browser(), prompt_type); |
| waiter.WaitIfNeededAndGet(); |
| |
| base::RunLoop().RunUntilIdle(); |
| } |
| |
| MockPrivacySandboxService* mock_service() { return mock_service_; } |
| |
| private: |
| raw_ptr<MockPrivacySandboxService> mock_service_; |
| }; |
| |
| IN_PROC_BROWSER_TEST_F(PrivacySandboxDialogViewBrowserTest, InvokeUi_Consent) { |
| EXPECT_CALL( |
| *mock_service(), |
| PromptActionOccurred(PrivacySandboxService::PromptAction::kConsentShown)); |
| EXPECT_CALL( |
| *mock_service(), |
| PromptActionOccurred( |
| PrivacySandboxService::PromptAction::kConsentClosedNoDecision)); |
| ShowAndVerifyUi(); |
| } |
| |
| IN_PROC_BROWSER_TEST_F(PrivacySandboxDialogViewBrowserTest, InvokeUi_Notice) { |
| EXPECT_CALL( |
| *mock_service(), |
| PromptActionOccurred(PrivacySandboxService::PromptAction::kNoticeShown)); |
| EXPECT_CALL( |
| *mock_service(), |
| PromptActionOccurred( |
| PrivacySandboxService::PromptAction::kNoticeClosedNoInteraction)); |
| ShowAndVerifyUi(); |
| } |