Skip to content

Commit

Permalink
Closing last tab should open a new tab
Browse files Browse the repository at this point in the history
  • Loading branch information
spylogsster committed Jul 11, 2022
1 parent 376f3c5 commit 7235837
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 0 deletions.
9 changes: 9 additions & 0 deletions browser/ui/brave_browser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "chrome/browser/lifetime/browser_close_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/common/webui_url_constants.h"
#include "components/prefs/pref_service.h"
#include "content/public/common/url_constants.h"
Expand Down Expand Up @@ -70,6 +71,14 @@ void BraveBrowser::ScheduleUIUpdate(content::WebContents* source,
#endif
}

void BraveBrowser::TabStripEmpty() {
if (unload_controller_.is_attempting_to_close_browser()) {
Browser::TabStripEmpty();
return;
}
chrome::AddTabAt(this, GetNewTabURL(), -1, true);
}

bool BraveBrowser::ShouldDisplayFavicon(
content::WebContents* web_contents) const {
// Override to not show favicon for NTP in tab.
Expand Down
1 change: 1 addition & 0 deletions browser/ui/brave_browser.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class BraveBrowser : public Browser {
const base::RepeatingCallback<void(bool)>& on_close_confirmed) override;
void ResetTryToCloseWindow() override;

void TabStripEmpty() override;
// Returns true when we should ask browser closing to users before handling
// any warning/onbeforeunload handlers.
bool ShouldAskForBrowserClosingBeforeHandlers();
Expand Down
47 changes: 47 additions & 0 deletions browser/ui/brave_browser_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,64 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/ui/brave_browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/startup/launch_mode_recorder.h"
#include "chrome/browser/ui/startup/startup_browser_creator.h"
#include "chrome/browser/ui/startup/startup_browser_creator_impl.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_utils.h"

using BraveBrowserBrowserTest = InProcessBrowserTest;

namespace {
Browser* OpenNewBrowser(Profile* profile) {
base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
StartupBrowserCreatorImpl creator(base::FilePath(), dummy,
chrome::startup::IsFirstRun::kYes);
creator.Launch(profile, chrome::startup::IsProcessStartup::kNo, nullptr);
return chrome::FindBrowserWithProfile(profile);
}
} // namespace

IN_PROC_BROWSER_TEST_F(BraveBrowserBrowserTest, NTPFaviconTest) {
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GURL("brave://newtab/")));

auto* tab_model = browser()->tab_strip_model();
EXPECT_FALSE(
browser()->ShouldDisplayFavicon(tab_model->GetActiveWebContents()));
}

IN_PROC_BROWSER_TEST_F(BraveBrowserBrowserTest, OpenNewTabWhenTabStripIsEmpty) {
ASSERT_TRUE(embedded_test_server()->Start());
Browser* new_browser = OpenNewBrowser(browser()->profile());
ASSERT_TRUE(new_browser);
TabStripModel* tab_strip = new_browser->tab_strip_model();
auto page_url = embedded_test_server()->GetURL("/empty.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(new_browser, page_url));

ASSERT_EQ(1, tab_strip->count());
EXPECT_EQ(page_url,
tab_strip->GetWebContentsAt(0)->GetURL().possibly_invalid_spec());
// Close the last tab.
tab_strip->GetActiveWebContents()->Close();
base::RunLoop().RunUntilIdle();
ASSERT_EQ(1, tab_strip->count());
// Expecting a new tab is opened.
EXPECT_EQ(new_browser->GetNewTabURL(),
tab_strip->GetWebContentsAt(0)->GetURL().possibly_invalid_spec());
// Add a couple of more tabs.
chrome::AddTabAt(new_browser, new_browser->GetNewTabURL(), -1, true);
chrome::AddTabAt(new_browser, new_browser->GetNewTabURL(), -1, true);
ASSERT_EQ(3, tab_strip->count());
EXPECT_EQ(chrome::GetTotalBrowserCount(), 2u);
// Close the browser window.
new_browser->window()->Close();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(chrome::GetTotalBrowserCount(), 1u);
}
1 change: 1 addition & 0 deletions browser/ui/webui/brave_welcome_ui_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "content/public/test/browser_test.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_utils.h"
#include "net/test/embedded_test_server/embedded_test_server.h"

namespace {
Browser* OpenNewBrowser(Profile* profile) {
Expand Down
2 changes: 2 additions & 0 deletions chromium_src/chrome/browser/ui/browser.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define ScheduleUIUpdate virtual ScheduleUIUpdate
#define ShouldDisplayFavicon virtual ShouldDisplayFavicon
#define TryToCloseWindow virtual TryToCloseWindow
#define TabStripEmpty virtual TabStripEmpty
#define ResetTryToCloseWindow virtual ResetTryToCloseWindow
#define FullscreenControllerInteractiveTest \
FullscreenControllerInteractiveTest; \
Expand All @@ -23,6 +24,7 @@
#undef FullscreenControllerInteractiveTest
#undef ResetTryToCloseWindow
#undef TryToCloseWindow
#undef TabStripEmpty
#undef ShouldDisplayFavicon
#undef ScheduleUIUpdate
#undef FinishWarnBeforeClosing
Expand Down
14 changes: 14 additions & 0 deletions chromium_src/chrome/browser/ui/unload_controller.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "chrome/browser/ui/browser.h"

#include "chrome/browser/ui/unload_controller.h"

#define TabStripEmpty \
TabStripEmpty() {} \
void UnloadController::TabStripEmpty_ChromiumImpl
#include "src/chrome/browser/ui/unload_controller.cc"
#undef TabStripEmpty
18 changes: 18 additions & 0 deletions chromium_src/chrome/browser/ui/unload_controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_
#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_

#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"

#define TabStripEmpty \
TabStripEmpty_ChromiumImpl(); \
void TabStripEmpty
#include "src/chrome/browser/ui/unload_controller.h"
#undef TabStripEmpty

#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_

0 comments on commit 7235837

Please sign in to comment.