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
|
// Copyright 2025 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/privacy_sandbox/notice/desktop_entrypoint_handlers.h"
#include "base/test/mock_callback.h"
#include "chrome/browser/privacy_sandbox/notice/mocks/mock_desktop_view_manager.h"
#include "chrome/browser/privacy_sandbox/notice/mocks/mock_notice_service.h"
#include "chrome/browser/privacy_sandbox/notice/notice_service_factory.h"
#include "chrome/browser/privacy_sandbox/notice/notice_service_interface.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/browser_window.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/signin/signin_view_controller.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/platform_browser_test.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/ui_test_utils.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/sync/test/test_sync_service.h"
#include "content/public/test/browser_test.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
namespace privacy_sandbox {
namespace {
using ::testing::Mock;
class PrivacySandboxNoticeEntryPointHandlersTest : public InProcessBrowserTest {
public:
PrivacySandboxNoticeEntryPointHandlersTest()
: https_test_server_(net::EmbeddedTestServer::TYPE_HTTPS) {
feature_list_.InitWithFeaturesAndParameters(
/*enabled_features=*/{{privacy_sandbox::kPrivacySandboxNoticeFramework,
{}}},
{});
}
void RegisterTestingSyncServiceFactory(content::BrowserContext* context) {
SyncServiceFactory::GetInstance()->SetTestingFactory(
context,
base::BindRepeating(
[](content::BrowserContext*) -> std::unique_ptr<KeyedService> {
return std::make_unique<syncer::TestSyncService>();
}));
}
void SetUpInProcessBrowserTestFixture() override {
services_subscription_ =
BrowserContextDependencyManager::GetInstance()
->RegisterCreateServicesCallbackForTesting(base::BindRepeating(
&PrivacySandboxNoticeEntryPointHandlersTest::
RegisterTestingSyncServiceFactory,
base::Unretained(this)));
}
void SetUpOnMainThread() override {
https_test_server()->AddDefaultHandlers(
base::FilePath(FILE_PATH_LITERAL("content/test/data")));
ASSERT_TRUE(https_test_server()->Start());
mock_notice_service_ = static_cast<MockPrivacySandboxNoticeService*>(
PrivacySandboxNoticeServiceFactory::GetInstance()
->SetTestingFactoryAndUse(
browser()->profile(),
base::BindRepeating(&BuildMockPrivacySandboxNoticeService)));
}
void TearDownOnMainThread() override {
mock_notice_service_ = nullptr;
}
net::EmbeddedTestServer* https_test_server() { return &https_test_server_; }
MockDesktopViewManager* mock_view_manager() {
return static_cast<MockDesktopViewManager*>(
mock_notice_service_->GetDesktopViewManager());
}
syncer::TestSyncService* test_sync_service() {
return static_cast<syncer::TestSyncService*>(
SyncServiceFactory::GetForProfile(browser()->profile()));
}
protected:
raw_ptr<MockPrivacySandboxNoticeService> mock_notice_service_;
net::EmbeddedTestServer https_test_server_;
base::CallbackListSubscription services_subscription_;
base::test::ScopedFeatureList feature_list_;
};
// Test that navigation to unsuitable URLS do not alert view manager.
IN_PROC_BROWSER_TEST_F(PrivacySandboxNoticeEntryPointHandlersTest,
UnsuitableUrl) {
EXPECT_CALL(*mock_view_manager(), HandleChromeOwnedPageNavigation).Times(0);
std::vector<GURL> urls_to_open = {
https_test_server()->GetURL("a.test", "/title1.html"),
GURL(chrome::kChromeUISettingsURL).Resolve(chrome::kAutofillSubPage)};
for (size_t i = 0; i < urls_to_open.size(); ++i) {
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), urls_to_open[i]));
}
Mock::VerifyAndClearExpectations(mock_view_manager());
}
IN_PROC_BROWSER_TEST_F(PrivacySandboxNoticeEntryPointHandlersTest,
NoPromptInNonNormalBrowser) {
EXPECT_CALL(*mock_view_manager(), HandleChromeOwnedPageNavigation).Times(0);
NavigateParams params(browser(), GURL(chrome::kChromeUINewTabPageURL),
ui::PAGE_TRANSITION_FIRST);
params.window_action = NavigateParams::SHOW_WINDOW;
params.disposition = WindowOpenDisposition::NEW_POPUP;
ui_test_utils::NavigateToURL(¶ms);
Mock::VerifyAndClearExpectations(mock_view_manager());
}
// The test checks that a prompt is shown on kChromeUINewTabURL navigation.
// For non-ChromeOS platforms this works because kChromeUINewTabURL redirects to
// kChromeUINewTabPageURL according to
// https://g3doc.corp.google.com/chrome/newtab/g3doc/ntp-types.md?cl=head.
// For ChromeOS platforms this works because about:Blank is opened on
// kChromeUINewTabURL navigation, allowing the prompt to show.
IN_PROC_BROWSER_TEST_F(PrivacySandboxNoticeEntryPointHandlersTest,
PromptShowsNewTabChromeOS) {
EXPECT_CALL(*mock_view_manager(), HandleChromeOwnedPageNavigation).Times(1);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(),
GURL(chrome::kChromeUINewTabURL)));
Mock::VerifyAndClearExpectations(mock_view_manager());
}
IN_PROC_BROWSER_TEST_F(PrivacySandboxNoticeEntryPointHandlersTest,
NoPromptInSmallBrowser) {
EXPECT_CALL(*mock_view_manager(), HandleChromeOwnedPageNavigation).Times(0);
ui_test_utils::SetAndWaitForBounds(*browser(), gfx::Rect(0, 0, 50, 50));
// Resizing does not work on Mac because of minimum window height. Ensure the
// minimum height is still > 100, then skip test.
#if BUILDFLAG(IS_MAC)
ASSERT_TRUE(browser()->window()->GetBounds().height() > 100);
GTEST_SKIP();
#endif
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(),
GURL(chrome::kChromeUISettingsURL)));
Mock::VerifyAndClearExpectations(mock_view_manager());
}
IN_PROC_BROWSER_TEST_F(PrivacySandboxNoticeEntryPointHandlersTest,
NoPromptSync) {
// Check when sync setup is in progress, that no prompt is shown.
EXPECT_CALL(*mock_view_manager(), HandleChromeOwnedPageNavigation).Times(0);
test_sync_service()->SetSetupInProgress();
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(),
GURL(chrome::kChromeUISettingsURL)));
Mock::VerifyAndClearExpectations(mock_view_manager());
}
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
// Check when profile setup is in progress, that no prompt is shown.
IN_PROC_BROWSER_TEST_F(PrivacySandboxNoticeEntryPointHandlersTest,
NoPromptProfileSetup) {
EXPECT_CALL(*mock_view_manager(), HandleChromeOwnedPageNavigation).Times(0);
// Show the profile customization dialog.
browser()
->GetFeatures()
.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));
Mock::VerifyAndClearExpectations(mock_view_manager());
}
#endif // BUILDFLAG(ENABLE_DICE_SUPPORT)
// SearchEngineChoiceCheck
class PrivacySandboxNoticeEntryPointHandlersTest_SearchEngineChoiceDialog
: public PrivacySandboxNoticeEntryPointHandlersTest {
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitchASCII(switches::kSearchEngineChoiceCountry, "BE");
command_line->AppendSwitch(
switches::kIgnoreNoFirstRunForSearchEngineChoiceScreen);
}
void SetUpOnMainThread() override {
PrivacySandboxNoticeEntryPointHandlersTest::SetUpOnMainThread();
SearchEngineChoiceDialogService::SetDialogDisabledForTests(
/*dialog_disabled=*/false);
}
private:
base::AutoReset<bool> scoped_chrome_build_override_ =
SearchEngineChoiceDialogServiceFactory::
ScopedChromeBuildOverrideForTesting(
/*force_chrome_build=*/true);
};
IN_PROC_BROWSER_TEST_F(
PrivacySandboxNoticeEntryPointHandlersTest_SearchEngineChoiceDialog,
NoPromptSearchEngineChoiceDialog) {
// Check when sync setup is in progress, that no prompt is shown.
EXPECT_CALL(*mock_view_manager(), HandleChromeOwnedPageNavigation).Times(0);
// Navigate to a page where the DMA notice should show.
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL)));
// 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 the Privacy Sandbox prompt doesn't get displayed on the next
// navigation.
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(),
GURL(chrome::kChromeUISettingsURL)));
Mock::VerifyAndClearExpectations(mock_view_manager());
}
// URLS check
class PrivacySandboxNoticeEntryPointHandlersTest_SuitableUrls
: public PrivacySandboxNoticeEntryPointHandlersTest,
public testing::WithParamInterface<GURL> {};
// Test that navigation to suitable URLS alert view manager.
IN_PROC_BROWSER_TEST_P(PrivacySandboxNoticeEntryPointHandlersTest_SuitableUrls,
SuitableUrl) {
GURL url_to_open = GetParam();
EXPECT_CALL(*mock_view_manager(), HandleChromeOwnedPageNavigation).Times(1);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url_to_open));
Mock::VerifyAndClearExpectations(mock_view_manager());
}
// Define the test parameters.
INSTANTIATE_TEST_SUITE_P(
AllSuitableUrls,
PrivacySandboxNoticeEntryPointHandlersTest_SuitableUrls,
testing::Values(GURL(chrome::kChromeUINewTabURL),
GURL(chrome::kChromeUINewTabPageURL),
GURL(url::kAboutBlankURL),
GURL(chrome::kChromeUISettingsURL),
GURL(chrome::kChromeUIHistoryURL)));
} // namespace
} // namespace privacy_sandbox
|