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
|
// Copyright 2016 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/startup/startup_tab_provider.h"
#include "base/test/metrics/histogram_tester.h"
#include "build/build_config.h"
#include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#if !BUILDFLAG(IS_ANDROID)
#include "base/values.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension_builder.h"
#endif // !BUILDFLAG(IS_ANDROID)
#if BUILDFLAG(IS_WIN)
#define CMD_ARG(x) L##x
#else
#define CMD_ARG(x) x
#endif
TEST(StartupTabProviderTest, GetInitialPrefsTabsForState) {
std::vector<GURL> input = {GURL(u"https://new_tab_page"),
GURL(u"https://www.google.com")};
StartupTabs output =
StartupTabProviderImpl::GetInitialPrefsTabsForState(true, input);
ASSERT_EQ(2U, output.size());
EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), output[0].url);
EXPECT_EQ(output[0].type, StartupTab::Type::kNormal);
EXPECT_EQ(input[1], output[1].url);
EXPECT_EQ(output[1].type, StartupTab::Type::kNormal);
}
TEST(StartupTabProviderTest, GetInitialPrefsTabsForState_FirstRunOnly) {
std::vector<GURL> input = {GURL(u"https://www.google.com")};
StartupTabs output =
StartupTabProviderImpl::GetInitialPrefsTabsForState(false, input);
EXPECT_TRUE(output.empty());
}
TEST(StartupTabProviderTest, GetResetTriggerTabsForState) {
StartupTabs output =
StartupTabProviderImpl::GetResetTriggerTabsForState(true);
ASSERT_EQ(1U, output.size());
EXPECT_EQ(StartupTabProviderImpl::GetTriggeredResetSettingsUrl(),
output[0].url);
EXPECT_EQ(output[0].type, StartupTab::Type::kNormal);
}
TEST(StartupTabProviderTest, GetResetTriggerTabsForState_Negative) {
StartupTabs output =
StartupTabProviderImpl::GetResetTriggerTabsForState(false);
ASSERT_TRUE(output.empty());
}
TEST(StartupTabProviderTest, GetResetTriggerTabsForState_WelcomeSkipped) {
std::vector<GURL> input = {GURL(u"chrome://welcome"),
GURL(u"chrome://welcome-win10"),
GURL(u"http://welcome")};
base::HistogramTester tester;
StartupTabs output =
StartupTabProviderImpl::GetInitialPrefsTabsForState(true, input);
#if BUILDFLAG(IS_WIN)
// chrome://welcome-win10 existed only on Windows, so we check and skip it
// only there.
ASSERT_EQ(1U, output.size());
EXPECT_EQ(input[2], output[0].url);
tester.ExpectBucketCount("Startup.StartupTabs.IsWelcomePageSkipped", true, 2);
#else
ASSERT_EQ(2U, output.size());
EXPECT_EQ(input[1], output[0].url);
EXPECT_EQ(input[2], output[1].url);
tester.ExpectBucketCount("Startup.StartupTabs.IsWelcomePageSkipped", true, 1);
#endif
}
TEST(StartupTabProviderTest, GetPinnedTabsForState) {
StartupTabs pinned = {
StartupTab(GURL("https://www.google.com"), StartupTab::Type::kPinned)};
SessionStartupPref pref_default(SessionStartupPref::Type::DEFAULT);
SessionStartupPref pref_urls(SessionStartupPref::Type::URLS);
StartupTabs output = StartupTabProviderImpl::GetPinnedTabsForState(
pref_default, pinned, false);
ASSERT_EQ(1U, output.size());
EXPECT_EQ("www.google.com", output[0].url.host());
output =
StartupTabProviderImpl::GetPinnedTabsForState(pref_urls, pinned, false);
ASSERT_EQ(1U, output.size());
EXPECT_EQ("www.google.com", output[0].url.host());
}
TEST(StartupTabProviderTest, GetPinnedTabsForState_Negative) {
StartupTabs pinned = {
StartupTab(GURL("https://www.google.com"), StartupTab::Type::kPinned)};
SessionStartupPref pref_last(SessionStartupPref::Type::LAST);
SessionStartupPref pref_default(SessionStartupPref::Type::DEFAULT);
// Session restore preference should block reading pinned tabs.
StartupTabs output =
StartupTabProviderImpl::GetPinnedTabsForState(pref_last, pinned, false);
ASSERT_TRUE(output.empty());
// Pinned tabs are not added when this profile already has a nonempty tabbed
// browser open.
output =
StartupTabProviderImpl::GetPinnedTabsForState(pref_default, pinned, true);
ASSERT_TRUE(output.empty());
}
TEST(StartupTabProviderTest, GetPreferencesTabsForState) {
SessionStartupPref pref_urls(SessionStartupPref::Type::URLS);
SessionStartupPref pref_last_and_urls(
SessionStartupPref::Type::LAST_AND_URLS);
pref_urls.urls = {GURL(u"https://www.google.com")};
pref_last_and_urls.urls = {GURL(u"https://www.google.com")};
StartupTabs output =
StartupTabProviderImpl::GetPreferencesTabsForState(pref_urls, false);
ASSERT_EQ(1U, output.size());
EXPECT_EQ("www.google.com", output[0].url.host());
EXPECT_EQ(StartupTab::Type::kNormal, output[0].type);
output = StartupTabProviderImpl::GetPreferencesTabsForState(
pref_last_and_urls, false);
ASSERT_EQ(1U, output.size());
EXPECT_EQ("www.google.com", output[0].url.host());
EXPECT_EQ(StartupTab::Type::kFromLastAndUrlsStartupPref, output[0].type);
}
TEST(StartupTabProviderTest, GetPreferencesTabsForState_WrongType) {
SessionStartupPref pref_default(SessionStartupPref::Type::DEFAULT);
pref_default.urls = {GURL(u"https://www.google.com")};
StartupTabs output =
StartupTabProviderImpl::GetPreferencesTabsForState(pref_default, false);
EXPECT_TRUE(output.empty());
SessionStartupPref pref_last(SessionStartupPref::Type::LAST);
pref_last.urls = {GURL(u"https://www.google.com")};
output = StartupTabProviderImpl::GetPreferencesTabsForState(pref_last, false);
EXPECT_TRUE(output.empty());
}
TEST(StartupTabProviderTest, GetPreferencesTabsForState_NotFirstBrowser) {
SessionStartupPref pref(SessionStartupPref::Type::URLS);
pref.urls = {GURL(u"https://www.google.com")};
StartupTabs output =
StartupTabProviderImpl::GetPreferencesTabsForState(pref, true);
EXPECT_TRUE(output.empty());
}
TEST(StartupTabProviderTest, GetNewTabPageTabsForState) {
SessionStartupPref pref_default(SessionStartupPref::Type::DEFAULT);
SessionStartupPref pref_urls(SessionStartupPref::Type::URLS);
StartupTabs output =
StartupTabProviderImpl::GetNewTabPageTabsForState(pref_default);
ASSERT_EQ(1U, output.size());
EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), output[0].url);
output = StartupTabProviderImpl::GetNewTabPageTabsForState(pref_urls);
ASSERT_EQ(1U, output.size());
EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), output[0].url);
}
TEST(StartupTabProviderTest, GetNewTabPageTabsForState_Negative) {
SessionStartupPref pref_last(SessionStartupPref::Type::LAST);
StartupTabs output =
StartupTabProviderImpl::GetNewTabPageTabsForState(pref_last);
ASSERT_TRUE(output.empty());
}
TEST(StartupTabProviderTest, GetCommandLineTabs) {
content::BrowserTaskEnvironment task_environment;
TestingProfile profile;
// Set up and inject a real instance for the profile.
TemplateURLServiceFactory::GetInstance()->SetTestingSubclassFactoryAndUse(
&profile, base::BindOnce(&TemplateURLServiceFactory::BuildInstanceFor));
// Empty arguments case.
{
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
StartupTabProviderImpl instance;
StartupTabs output =
instance.GetCommandLineTabs(command_line, base::FilePath(), &profile);
EXPECT_TRUE(output.empty());
EXPECT_EQ(CommandLineTabsPresent::kNo,
instance.HasCommandLineTabs(command_line, base::FilePath()));
}
// Simple use. Pass google.com URL.
{
base::CommandLine command_line(
{CMD_ARG(""), CMD_ARG("https://google.com")});
StartupTabProviderImpl instance;
StartupTabs output =
instance.GetCommandLineTabs(command_line, base::FilePath(), &profile);
ASSERT_EQ(1u, output.size());
EXPECT_EQ(GURL("https://google.com"), output[0].url);
EXPECT_EQ(CommandLineTabsPresent::kYes,
instance.HasCommandLineTabs(command_line, base::FilePath()));
}
// Two URL case.
{
base::CommandLine command_line({CMD_ARG(""), CMD_ARG("https://google.com"),
CMD_ARG("https://gmail.com")});
StartupTabProviderImpl instance;
StartupTabs output =
instance.GetCommandLineTabs(command_line, base::FilePath(), &profile);
ASSERT_EQ(2u, output.size());
EXPECT_EQ(GURL("https://google.com"), output[0].url);
EXPECT_EQ(GURL("https://gmail.com"), output[1].url);
EXPECT_EQ(CommandLineTabsPresent::kYes,
instance.HasCommandLineTabs(command_line, base::FilePath()));
}
// Vista way search query.
{
base::CommandLine command_line({CMD_ARG(""), CMD_ARG("? Foo")});
StartupTabProviderImpl instance;
StartupTabs output =
instance.GetCommandLineTabs(command_line, base::FilePath(), &profile);
ASSERT_EQ(1u, output.size());
EXPECT_EQ(
GURL("https://www.google.com/search?q=Foo&sourceid=chrome&ie=UTF-8"),
output[0].url);
EXPECT_EQ(CommandLineTabsPresent::kUnknown,
instance.HasCommandLineTabs(command_line, base::FilePath()));
}
// Unsafe scheme should be filtered out.
{
base::CommandLine command_line({CMD_ARG(""), CMD_ARG("chrome://flags")});
StartupTabProviderImpl instance;
StartupTabs output =
instance.GetCommandLineTabs(command_line, base::FilePath(), &profile);
ASSERT_TRUE(output.empty());
EXPECT_EQ(CommandLineTabsPresent::kNo,
instance.HasCommandLineTabs(command_line, base::FilePath()));
}
// Exceptional settings page.
{
base::CommandLine command_line(
{CMD_ARG(""), CMD_ARG("chrome://settings/resetProfileSettings")});
StartupTabProviderImpl instance;
StartupTabs output =
instance.GetCommandLineTabs(command_line, base::FilePath(), &profile);
ASSERT_EQ(1u, output.size());
EXPECT_EQ(GURL("chrome://settings/resetProfileSettings"), output[0].url);
EXPECT_EQ(CommandLineTabsPresent::kYes,
instance.HasCommandLineTabs(command_line, base::FilePath()));
}
// chrome://settings/ page handling.
{
base::CommandLine command_line(
{CMD_ARG(""), CMD_ARG("chrome://settings/syncSetup")});
StartupTabProviderImpl instance;
StartupTabs output =
instance.GetCommandLineTabs(command_line, base::FilePath(), &profile);
auto has_tabs = instance.HasCommandLineTabs(command_line, base::FilePath());
#if BUILDFLAG(IS_CHROMEOS)
// On Chrome OS (ash-chrome), settings page is allowed to be specified.
ASSERT_EQ(1u, output.size());
EXPECT_EQ(GURL("chrome://settings/syncSetup"), output[0].url);
EXPECT_EQ(CommandLineTabsPresent::kYes, has_tabs);
#else
// On other platforms, it is blocked.
EXPECT_TRUE(output.empty());
EXPECT_EQ(CommandLineTabsPresent::kNo, has_tabs);
#endif
}
// about:blank URL.
{
base::CommandLine command_line({CMD_ARG(""), CMD_ARG("about:blank")});
StartupTabProviderImpl instance;
StartupTabs output =
instance.GetCommandLineTabs(command_line, base::FilePath(), &profile);
ASSERT_EQ(1u, output.size());
EXPECT_EQ(GURL("about:blank"), output[0].url);
EXPECT_EQ(CommandLineTabsPresent::kYes,
instance.HasCommandLineTabs(command_line, base::FilePath()));
}
}
// This test fails on Windows. TODO(crbug.com/40265634): Investigate and
// fix this test on Windows.
#if BUILDFLAG(IS_WIN)
#define MAYBE_GetCommandLineTabsFileUrl DISABLED_GetCommandLineTabsFileUrl
#else
#define MAYBE_GetCommandLineTabsFileUrl GetCommandLineTabsFileUrl
#endif
TEST(StartupTabProviderTest, MAYBE_GetCommandLineTabsFileUrl) {
content::BrowserTaskEnvironment task_environment;
TestingProfile profile;
// Set up and inject a real instance for the profile.
TemplateURLServiceFactory::GetInstance()->SetTestingSubclassFactoryAndUse(
&profile, base::BindOnce(&TemplateURLServiceFactory::BuildInstanceFor));
// "file:" path fix up.
{
base::CommandLine command_line({CMD_ARG(""), CMD_ARG("file:foo.txt")});
StartupTabProviderImpl instance;
StartupTabs output =
instance.GetCommandLineTabs(command_line, base::FilePath(), &profile);
ASSERT_EQ(1u, output.size());
EXPECT_EQ(GURL("file:///foo.txt"), output[0].url);
EXPECT_EQ(CommandLineTabsPresent::kYes,
instance.HasCommandLineTabs(command_line, base::FilePath()));
}
}
#if !BUILDFLAG(IS_ANDROID)
class StartupTabProviderPrivacySandboxTest : public testing::Test {
protected:
extensions::ExtensionRegistry* registry() {
return extensions::ExtensionRegistry::Get(&profile_);
}
TestingProfile* profile() { return &profile_; }
private:
content::BrowserTaskEnvironment task_environment_;
TestingProfile profile_;
};
TEST_F(StartupTabProviderPrivacySandboxTest, GetPrivacySandboxTabsForState) {
// If no suitable tabs are available, and the profile does not have a custom
// NTP, a generic new tab URL should be returned.
auto output = StartupTabProviderImpl::GetPrivacySandboxTabsForState(
registry(), GURL(chrome::kChromeUINewTabPageURL),
{{StartupTab(GURL("https://www.unrelated.com"))}});
ASSERT_EQ(1U, output.size());
EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), output[0].url);
}
TEST_F(StartupTabProviderPrivacySandboxTest,
GetPrivacySandboxTabsForState_SuitableTabAlready) {
// If there is already a suitable tab available for the Privacy Sandbox
// dialog, no additional tab should be added.
auto output = StartupTabProviderImpl::GetPrivacySandboxTabsForState(
registry(), GURL(chrome::kChromeUINewTabPageURL),
{{StartupTab(GURL("chrome://newtab"))}});
ASSERT_EQ(0U, output.size());
}
TEST_F(StartupTabProviderPrivacySandboxTest,
GetPrivacySandboxTabsForState_ExtensionControlledNtp) {
// Create an extension which overrides the NTP url. Even if a new tab is
// available as part of the startup tabs, an additional about:blank should be
// returned.
scoped_refptr<const extensions::Extension> extension =
extensions::ExtensionBuilder("1")
.SetManifestKey("chrome_url_overrides",
base::Value::Dict().Set("newtab", "custom_tab.html"))
.Build();
registry()->AddEnabled(extension);
auto output = StartupTabProviderImpl::GetPrivacySandboxTabsForState(
registry(), GURL(chrome::kChromeUINewTabPageURL),
{StartupTab(GURL(chrome::kChromeUINewTabURL))});
ASSERT_EQ(1U, output.size());
EXPECT_EQ(GURL(url::kAboutBlankURL), output[0].url);
}
TEST_F(StartupTabProviderPrivacySandboxTest,
GetPrivacySandboxTabsForState_DseControlledNtp) {
// If the user's DSE is changing the newtab such that it is no longer Chrome
// controlled, and the user has no other suitable startup tab, about:blank
// should be used.
auto output = StartupTabProviderImpl::GetPrivacySandboxTabsForState(
registry(), GURL("https://wwww.example.com/newtab"),
{StartupTab(GURL(chrome::kChromeUINewTabURL))});
ASSERT_EQ(1U, output.size());
EXPECT_EQ(GURL(url::kAboutBlankURL), output[0].url);
}
#endif // !BUILDFLAG(IS_ANDROID)
|