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
|
// Copyright 2013 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/browser.h"
#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/prefs/incognito_mode_prefs.h"
#include "chrome/browser/ui/browser_command_controller.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/policy/core/common/policy_pref_names.h"
#include "components/zoom/zoom_controller.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/web_contents_tester.h"
#include "printing/buildflags/buildflags.h"
#include "third_party/skia/include/core/SkColor.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "components/session_manager/core/session_manager.h"
#include "components/user_manager/fake_user_manager.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/user_manager/user_names.h"
#endif
using content::SiteInstance;
using content::WebContents;
using content::WebContentsTester;
#if BUILDFLAG(IS_CHROMEOS)
using session_manager::SessionState;
#endif
class BrowserUnitTest : public BrowserWithTestWindowTest {
public:
BrowserUnitTest() = default;
BrowserUnitTest(const BrowserUnitTest&) = delete;
BrowserUnitTest& operator=(const BrowserUnitTest&) = delete;
~BrowserUnitTest() override = default;
// Caller owns the memory.
std::unique_ptr<WebContents> CreateTestWebContents() {
return WebContentsTester::CreateTestWebContents(
profile(), SiteInstance::Create(profile()));
}
};
// Ensure crashed tabs are not reloaded when selected. crbug.com/232323
TEST_F(BrowserUnitTest, ReloadCrashedTab) {
TabStripModel* tab_strip_model = browser()->tab_strip_model();
// Start with a single foreground tab. |tab_strip_model| owns the memory.
std::unique_ptr<WebContents> contents1 = CreateTestWebContents();
content::WebContents* raw_contents1 = contents1.get();
tab_strip_model->AppendWebContents(std::move(contents1), true);
WebContentsTester::For(raw_contents1)->NavigateAndCommit(GURL("about:blank"));
WebContentsTester::For(raw_contents1)->TestSetIsLoading(false);
EXPECT_TRUE(tab_strip_model->IsTabSelected(0));
EXPECT_FALSE(raw_contents1->IsLoading());
// Add a second tab in the background.
std::unique_ptr<WebContents> contents2 = CreateTestWebContents();
content::WebContents* raw_contents2 = contents2.get();
tab_strip_model->AppendWebContents(std::move(contents2), false);
WebContentsTester::For(raw_contents2)->NavigateAndCommit(GURL("about:blank"));
WebContentsTester::For(raw_contents2)->TestSetIsLoading(false);
EXPECT_EQ(2, browser()->tab_strip_model()->count());
EXPECT_TRUE(tab_strip_model->IsTabSelected(0));
EXPECT_FALSE(raw_contents2->IsLoading());
// Simulate the second tab crashing.
WebContentsTester::For(raw_contents2)
->SetIsCrashed(base::TERMINATION_STATUS_PROCESS_CRASHED, -1);
EXPECT_TRUE(raw_contents2->IsCrashed());
// Selecting the second tab does not cause a load or clear the crash.
tab_strip_model->ActivateTabAt(
1, TabStripUserGestureDetails(
TabStripUserGestureDetails::GestureType::kOther));
EXPECT_TRUE(tab_strip_model->IsTabSelected(1));
EXPECT_FALSE(raw_contents2->IsLoading());
EXPECT_TRUE(raw_contents2->IsCrashed());
}
// This tests a workaround which is not necessary on Mac.
// https://crbug.com/719230
#if BUILDFLAG(IS_MAC)
#define MAYBE_SetBackgroundColorForNewTab DISABLED_SetBackgroundColorForNewTab
#else
#define MAYBE_SetBackgroundColorForNewTab SetBackgroundColorForNewTab
#endif
TEST_F(BrowserUnitTest, MAYBE_SetBackgroundColorForNewTab) {
TabStripModel* tab_strip_model = browser()->tab_strip_model();
std::unique_ptr<WebContents> contents1 = CreateTestWebContents();
content::WebContents* raw_contents1 = contents1.get();
tab_strip_model->AppendWebContents(std::move(contents1), true);
WebContentsTester::For(raw_contents1)->NavigateAndCommit(GURL("about:blank"));
WebContentsTester::For(raw_contents1)->TestSetIsLoading(false);
raw_contents1->GetPrimaryMainFrame()->GetView()->SetBackgroundColor(
SK_ColorRED);
// Add a second tab in the background.
std::unique_ptr<WebContents> contents2 = CreateTestWebContents();
content::WebContents* raw_contents2 = contents2.get();
tab_strip_model->AppendWebContents(std::move(contents2), false);
WebContentsTester::For(raw_contents2)->NavigateAndCommit(GURL("about:blank"));
WebContentsTester::For(raw_contents2)->TestSetIsLoading(false);
tab_strip_model->ActivateTabAt(
1, TabStripUserGestureDetails(
TabStripUserGestureDetails::GestureType::kOther));
ASSERT_TRUE(
raw_contents2->GetPrimaryMainFrame()->GetView()->GetBackgroundColor());
EXPECT_EQ(
SK_ColorRED,
*raw_contents2->GetPrimaryMainFrame()->GetView()->GetBackgroundColor());
}
#if BUILDFLAG(ENABLE_PRINTING)
// Ensure the print command gets disabled when a tab crashes.
TEST_F(BrowserUnitTest, DisablePrintOnCrashedTab) {
TabStripModel* tab_strip_model = browser()->tab_strip_model();
std::unique_ptr<WebContents> contents = CreateTestWebContents();
content::WebContents* raw_contents = contents.get();
tab_strip_model->AppendWebContents(std::move(contents), true);
WebContentsTester::For(raw_contents)->NavigateAndCommit(GURL("about:blank"));
CommandUpdater* command_updater = browser()->command_controller();
EXPECT_FALSE(raw_contents->IsCrashed());
EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_PRINT));
EXPECT_TRUE(chrome::CanPrint(browser()));
WebContentsTester::For(raw_contents)
->SetIsCrashed(base::TERMINATION_STATUS_PROCESS_CRASHED, -1);
EXPECT_TRUE(raw_contents->IsCrashed());
EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_PRINT));
EXPECT_FALSE(chrome::CanPrint(browser()));
}
#endif // BUILDFLAG(ENABLE_PRINTING)
// Ensure the zoom-in and zoom-out commands get disabled when a tab crashes.
TEST_F(BrowserUnitTest, DisableZoomOnCrashedTab) {
TabStripModel* tab_strip_model = browser()->tab_strip_model();
std::unique_ptr<WebContents> contents = CreateTestWebContents();
content::WebContents* raw_contents = contents.get();
tab_strip_model->AppendWebContents(std::move(contents), true);
WebContentsTester::For(raw_contents)->NavigateAndCommit(GURL("about:blank"));
zoom::ZoomController* zoom_controller =
zoom::ZoomController::FromWebContents(raw_contents);
EXPECT_TRUE(
zoom_controller->SetZoomLevel(zoom_controller->GetDefaultZoomLevel()));
CommandUpdater* command_updater = browser()->command_controller();
EXPECT_TRUE(zoom_controller->IsAtDefaultZoom());
EXPECT_FALSE(raw_contents->IsCrashed());
EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_ZOOM_PLUS));
EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_ZOOM_MINUS));
EXPECT_TRUE(chrome::CanZoomIn(raw_contents));
EXPECT_TRUE(chrome::CanZoomOut(raw_contents));
WebContentsTester::For(raw_contents)
->SetIsCrashed(base::TERMINATION_STATUS_PROCESS_CRASHED, -1);
EXPECT_TRUE(raw_contents->IsCrashed());
EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_ZOOM_PLUS));
EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_ZOOM_MINUS));
EXPECT_FALSE(chrome::CanZoomIn(raw_contents));
EXPECT_FALSE(chrome::CanZoomOut(raw_contents));
}
TEST_F(BrowserUnitTest, CreateBrowserFailsIfProfileDisallowsBrowserWindows) {
TestingProfile::Builder profile_builder;
profile_builder.DisallowBrowserWindows();
std::unique_ptr<TestingProfile> test_profile = profile_builder.Build();
TestingProfile::Builder otr_profile_builder;
otr_profile_builder.DisallowBrowserWindows();
otr_profile_builder.BuildIncognito(test_profile.get());
// Verify creating browser fails in both original and OTR version of the
// profile.
EXPECT_EQ(Browser::CreationStatus::kErrorProfileUnsuitable,
Browser::GetCreationStatusForProfile(test_profile.get()));
EXPECT_EQ(Browser::CreationStatus::kErrorProfileUnsuitable,
Browser::GetCreationStatusForProfile(
test_profile->GetPrimaryOTRProfile(/*create_if_needed=*/true)));
}
// Tests BrowserCreate() when Incognito mode is disabled.
TEST_F(BrowserUnitTest, CreateBrowserWithIncognitoModeDisabled) {
IncognitoModePrefs::SetAvailability(
profile()->GetPrefs(), policy::IncognitoModeAvailability::kDisabled);
// Creating a browser window in OTR profile should fail if incognito is
// disabled.
EXPECT_EQ(Browser::CreationStatus::kErrorProfileUnsuitable,
Browser::GetCreationStatusForProfile(
profile()->GetPrimaryOTRProfile(/*create_if_needed=*/true)));
// Verify creating a browser in the original profile succeeds.
Browser::CreateParams create_params(profile(), false);
std::unique_ptr<BrowserWindow> test_window(CreateBrowserWindow());
create_params.window = test_window.get();
std::unique_ptr<Browser> test_browser(Browser::Create(create_params));
EXPECT_TRUE(test_browser);
}
// Tests BrowserCreate() when Incognito mode is forced.
TEST_F(BrowserUnitTest, CreateBrowserWithIncognitoModeForced) {
IncognitoModePrefs::SetAvailability(
profile()->GetPrefs(), policy::IncognitoModeAvailability::kForced);
// Creating a browser window in the original profile should fail if incognito
// is forced.
EXPECT_EQ(Browser::CreationStatus::kErrorProfileUnsuitable,
Browser::GetCreationStatusForProfile(profile()));
// Creating a browser in OTR test profile should succeed.
Browser::CreateParams off_the_record_create_params(
profile()->GetPrimaryOTRProfile(/*create_if_needed=*/true), false);
std::unique_ptr<BrowserWindow> test_window(CreateBrowserWindow());
off_the_record_create_params.window = test_window.get();
std::unique_ptr<Browser> otr_browser(
Browser::Create(off_the_record_create_params));
EXPECT_TRUE(otr_browser);
}
// Tests BrowserCreate() with not restrictions on incognito mode.
TEST_F(BrowserUnitTest, CreateBrowserWithIncognitoModeEnabled) {
ASSERT_EQ(policy::IncognitoModeAvailability::kEnabled,
IncognitoModePrefs::GetAvailability(profile()->GetPrefs()));
// Creating a browser in the original test profile should succeed.
Browser::CreateParams create_params(profile(), false);
std::unique_ptr<BrowserWindow> test_window(CreateBrowserWindow());
create_params.window = test_window.get();
std::unique_ptr<Browser> test_browser(Browser::Create(create_params));
EXPECT_TRUE(test_browser);
// Creating a browser in OTR test profile should succeed.
Browser::CreateParams off_the_record_create_params(
profile()->GetPrimaryOTRProfile(/*create_if_needed=*/true), false);
std::unique_ptr<BrowserWindow> otr_test_window(CreateBrowserWindow());
off_the_record_create_params.window = otr_test_window.get();
std::unique_ptr<Browser> otr_browser(
Browser::Create(off_the_record_create_params));
EXPECT_TRUE(otr_browser);
}
#if BUILDFLAG(IS_CHROMEOS)
TEST_F(BrowserUnitTest, CreateBrowserDuringKioskSplashScreen) {
// Setting up user manager state to be in kiosk mode:
// Creating a new user manager.
auto* user_manager = new ash::FakeChromeUserManager();
user_manager::ScopedUserManager manager{
std::unique_ptr<user_manager::UserManager>(user_manager)};
const user_manager::User* user =
user_manager->AddKioskAppUser(AccountId::FromUserEmail("fake_user@test"));
user_manager->LoginUser(user->GetAccountId());
TestingProfile profile;
session_manager::SessionManager::Get()->SetSessionState(
SessionState::LOGIN_PRIMARY);
// Browser should not be created during login session state.
EXPECT_EQ(Browser::CreationStatus::kErrorLoadingKiosk,
Browser::GetCreationStatusForProfile(&profile));
Browser::CreateParams create_params = Browser::CreateParams(&profile, false);
std::unique_ptr<BrowserWindow> window = CreateBrowserWindow();
create_params.window = window.get();
session_manager::SessionManager::Get()->SetSessionState(SessionState::ACTIVE);
std::unique_ptr<Browser> test_browser(Browser::Create(create_params));
// Normal flow, creation succeeds.
EXPECT_TRUE(test_browser);
}
#endif
class BrowserBookmarkBarTest : public BrowserWithTestWindowTest {
public:
BrowserBookmarkBarTest() = default;
BrowserBookmarkBarTest(const BrowserBookmarkBarTest&) = delete;
BrowserBookmarkBarTest& operator=(const BrowserBookmarkBarTest&) = delete;
~BrowserBookmarkBarTest() override = default;
protected:
BookmarkBar::State window_bookmark_bar_state() const {
return static_cast<BookmarkBarStateTestBrowserWindow*>(browser()->window())
->bookmark_bar_state();
}
// BrowserWithTestWindowTest:
void SetUp() override {
BrowserWithTestWindowTest::SetUp();
static_cast<BookmarkBarStateTestBrowserWindow*>(browser()->window())
->set_browser(browser());
}
std::unique_ptr<BrowserWindow> CreateBrowserWindow() override {
return std::make_unique<BookmarkBarStateTestBrowserWindow>();
}
private:
class BookmarkBarStateTestBrowserWindow : public TestBrowserWindow {
public:
BookmarkBarStateTestBrowserWindow() : browser_(nullptr) {}
BookmarkBarStateTestBrowserWindow(
const BookmarkBarStateTestBrowserWindow&) = delete;
BookmarkBarStateTestBrowserWindow& operator=(
const BookmarkBarStateTestBrowserWindow&) = delete;
~BookmarkBarStateTestBrowserWindow() override = default;
void set_browser(Browser* browser) { browser_ = browser; }
BookmarkBar::State bookmark_bar_state() const {
return bookmark_bar_state_;
}
private:
// TestBrowserWindow:
void BookmarkBarStateChanged(
BookmarkBar::AnimateChangeType change_type) override {
bookmark_bar_state_ = browser_->bookmark_bar_state();
TestBrowserWindow::BookmarkBarStateChanged(change_type);
}
void OnActiveTabChanged(content::WebContents* old_contents,
content::WebContents* new_contents,
int index,
int reason) override {
bookmark_bar_state_ = browser_->bookmark_bar_state();
TestBrowserWindow::OnActiveTabChanged(old_contents, new_contents, index,
reason);
}
raw_ptr<Browser, DanglingUntriaged> browser_; // Weak ptr.
BookmarkBar::State bookmark_bar_state_ = BookmarkBar::HIDDEN;
};
};
// Ensure bookmark bar states in Browser and BrowserWindow are in sync after
// Browser::ActiveTabChanged() calls BrowserWindow::OnActiveTabChanged().
TEST_F(BrowserBookmarkBarTest, StateOnActiveTabChanged) {
ASSERT_EQ(BookmarkBar::HIDDEN, browser()->bookmark_bar_state());
ASSERT_EQ(BookmarkBar::HIDDEN, window_bookmark_bar_state());
GURL ntp_url("chrome://newtab");
GURL non_ntp_url("http://foo");
// Open a tab to NTP.
AddTab(browser(), ntp_url);
EXPECT_EQ(BookmarkBar::HIDDEN, browser()->bookmark_bar_state());
EXPECT_EQ(BookmarkBar::HIDDEN, window_bookmark_bar_state());
// Navigate 1st tab to a non-NTP URL.
NavigateAndCommitActiveTab(non_ntp_url);
EXPECT_EQ(BookmarkBar::HIDDEN, browser()->bookmark_bar_state());
EXPECT_EQ(BookmarkBar::HIDDEN, window_bookmark_bar_state());
// Open a tab to NTP at index 0.
AddTab(browser(), ntp_url);
EXPECT_EQ(BookmarkBar::HIDDEN, browser()->bookmark_bar_state());
EXPECT_EQ(BookmarkBar::HIDDEN, window_bookmark_bar_state());
// Activate the 2nd tab which is non-NTP.
browser()->tab_strip_model()->ActivateTabAt(
1, TabStripUserGestureDetails(
TabStripUserGestureDetails::GestureType::kOther));
EXPECT_EQ(BookmarkBar::HIDDEN, browser()->bookmark_bar_state());
EXPECT_EQ(BookmarkBar::HIDDEN, window_bookmark_bar_state());
// Toggle bookmark bar while 2nd tab (non-NTP) is active.
chrome::ToggleBookmarkBar(browser());
EXPECT_EQ(BookmarkBar::SHOW, browser()->bookmark_bar_state());
EXPECT_EQ(BookmarkBar::SHOW, window_bookmark_bar_state());
// Activate the 1st tab which is NTP.
browser()->tab_strip_model()->ActivateTabAt(
0, TabStripUserGestureDetails(
TabStripUserGestureDetails::GestureType::kOther));
EXPECT_EQ(BookmarkBar::SHOW, browser()->bookmark_bar_state());
EXPECT_EQ(BookmarkBar::SHOW, window_bookmark_bar_state());
// Activate the 2nd tab which is non-NTP.
browser()->tab_strip_model()->ActivateTabAt(
1, TabStripUserGestureDetails(
TabStripUserGestureDetails::GestureType::kOther));
EXPECT_EQ(BookmarkBar::SHOW, browser()->bookmark_bar_state());
EXPECT_EQ(BookmarkBar::SHOW, window_bookmark_bar_state());
}
// Tests that Browser::Create creates a guest session browser.
TEST_F(BrowserUnitTest, CreateGuestSessionBrowser) {
TestingProfile* test_profile = profile_manager()->CreateGuestProfile();
TestingProfile::Builder otr_profile_builder;
otr_profile_builder.SetGuestSession();
Profile* guest_profile = nullptr;
// Try creating a browser in original guest profile - it should fail.
EXPECT_EQ(Browser::CreationStatus::kErrorProfileUnsuitable,
Browser::GetCreationStatusForProfile(test_profile));
// Create OTR profile for the Guest profile.
EXPECT_TRUE(otr_profile_builder.BuildIncognito(test_profile));
guest_profile = test_profile->GetPrimaryOTRProfile(/*create_if_needed=*/true);
// Creating a browser should succeed.
Browser::CreateParams create_params =
Browser::CreateParams(guest_profile, false);
std::unique_ptr<BrowserWindow> test_window = CreateBrowserWindow();
create_params.window = test_window.get();
std::unique_ptr<Browser> browser =
std::unique_ptr<Browser>(Browser::Create(create_params));
EXPECT_TRUE(browser);
}
|