Skip to content

Commit 7235837

Browse files
committed
Closing last tab should open a new tab
1 parent 376f3c5 commit 7235837

7 files changed

+92
-0
lines changed

browser/ui/brave_browser.cc

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "chrome/browser/lifetime/browser_close_manager.h"
1111
#include "chrome/browser/profiles/profile.h"
1212
#include "chrome/browser/search/search.h"
13+
#include "chrome/browser/ui/browser_tabstrip.h"
1314
#include "chrome/common/webui_url_constants.h"
1415
#include "components/prefs/pref_service.h"
1516
#include "content/public/common/url_constants.h"
@@ -70,6 +71,14 @@ void BraveBrowser::ScheduleUIUpdate(content::WebContents* source,
7071
#endif
7172
}
7273

74+
void BraveBrowser::TabStripEmpty() {
75+
if (unload_controller_.is_attempting_to_close_browser()) {
76+
Browser::TabStripEmpty();
77+
return;
78+
}
79+
chrome::AddTabAt(this, GetNewTabURL(), -1, true);
80+
}
81+
7382
bool BraveBrowser::ShouldDisplayFavicon(
7483
content::WebContents* web_contents) const {
7584
// Override to not show favicon for NTP in tab.

browser/ui/brave_browser.h

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class BraveBrowser : public Browser {
4848
const base::RepeatingCallback<void(bool)>& on_close_confirmed) override;
4949
void ResetTryToCloseWindow() override;
5050

51+
void TabStripEmpty() override;
5152
// Returns true when we should ask browser closing to users before handling
5253
// any warning/onbeforeunload handlers.
5354
bool ShouldAskForBrowserClosingBeforeHandlers();

browser/ui/brave_browser_browsertest.cc

+47
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,64 @@
44
* You can obtain one at http://mozilla.org/MPL/2.0/. */
55

66
#include "brave/browser/ui/brave_browser.h"
7+
#include "chrome/browser/ui/browser_finder.h"
8+
#include "chrome/browser/ui/browser_tabstrip.h"
9+
#include "chrome/browser/ui/startup/launch_mode_recorder.h"
10+
#include "chrome/browser/ui/startup/startup_browser_creator.h"
11+
#include "chrome/browser/ui/startup/startup_browser_creator_impl.h"
712
#include "chrome/browser/ui/tabs/tab_strip_model.h"
13+
#include "chrome/browser/ui/views/frame/browser_view.h"
814
#include "chrome/test/base/in_process_browser_test.h"
915
#include "chrome/test/base/ui_test_utils.h"
1016
#include "content/public/test/browser_test.h"
17+
#include "content/public/test/test_navigation_observer.h"
18+
#include "content/public/test/test_utils.h"
1119

1220
using BraveBrowserBrowserTest = InProcessBrowserTest;
1321

22+
namespace {
23+
Browser* OpenNewBrowser(Profile* profile) {
24+
base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
25+
StartupBrowserCreatorImpl creator(base::FilePath(), dummy,
26+
chrome::startup::IsFirstRun::kYes);
27+
creator.Launch(profile, chrome::startup::IsProcessStartup::kNo, nullptr);
28+
return chrome::FindBrowserWithProfile(profile);
29+
}
30+
} // namespace
31+
1432
IN_PROC_BROWSER_TEST_F(BraveBrowserBrowserTest, NTPFaviconTest) {
1533
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GURL("brave://newtab/")));
1634

1735
auto* tab_model = browser()->tab_strip_model();
1836
EXPECT_FALSE(
1937
browser()->ShouldDisplayFavicon(tab_model->GetActiveWebContents()));
2038
}
39+
40+
IN_PROC_BROWSER_TEST_F(BraveBrowserBrowserTest, OpenNewTabWhenTabStripIsEmpty) {
41+
ASSERT_TRUE(embedded_test_server()->Start());
42+
Browser* new_browser = OpenNewBrowser(browser()->profile());
43+
ASSERT_TRUE(new_browser);
44+
TabStripModel* tab_strip = new_browser->tab_strip_model();
45+
auto page_url = embedded_test_server()->GetURL("/empty.html");
46+
ASSERT_TRUE(ui_test_utils::NavigateToURL(new_browser, page_url));
47+
48+
ASSERT_EQ(1, tab_strip->count());
49+
EXPECT_EQ(page_url,
50+
tab_strip->GetWebContentsAt(0)->GetURL().possibly_invalid_spec());
51+
// Close the last tab.
52+
tab_strip->GetActiveWebContents()->Close();
53+
base::RunLoop().RunUntilIdle();
54+
ASSERT_EQ(1, tab_strip->count());
55+
// Expecting a new tab is opened.
56+
EXPECT_EQ(new_browser->GetNewTabURL(),
57+
tab_strip->GetWebContentsAt(0)->GetURL().possibly_invalid_spec());
58+
// Add a couple of more tabs.
59+
chrome::AddTabAt(new_browser, new_browser->GetNewTabURL(), -1, true);
60+
chrome::AddTabAt(new_browser, new_browser->GetNewTabURL(), -1, true);
61+
ASSERT_EQ(3, tab_strip->count());
62+
EXPECT_EQ(chrome::GetTotalBrowserCount(), 2u);
63+
// Close the browser window.
64+
new_browser->window()->Close();
65+
base::RunLoop().RunUntilIdle();
66+
EXPECT_EQ(chrome::GetTotalBrowserCount(), 1u);
67+
}

browser/ui/webui/brave_welcome_ui_browsertest.cc

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "content/public/test/browser_test.h"
2121
#include "content/public/test/test_navigation_observer.h"
2222
#include "content/public/test/test_utils.h"
23+
#include "net/test/embedded_test_server/embedded_test_server.h"
2324

2425
namespace {
2526
Browser* OpenNewBrowser(Profile* profile) {

chromium_src/chrome/browser/ui/browser.h

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#define ScheduleUIUpdate virtual ScheduleUIUpdate
1313
#define ShouldDisplayFavicon virtual ShouldDisplayFavicon
1414
#define TryToCloseWindow virtual TryToCloseWindow
15+
#define TabStripEmpty virtual TabStripEmpty
1516
#define ResetTryToCloseWindow virtual ResetTryToCloseWindow
1617
#define FullscreenControllerInteractiveTest \
1718
FullscreenControllerInteractiveTest; \
@@ -23,6 +24,7 @@
2324
#undef FullscreenControllerInteractiveTest
2425
#undef ResetTryToCloseWindow
2526
#undef TryToCloseWindow
27+
#undef TabStripEmpty
2628
#undef ShouldDisplayFavicon
2729
#undef ScheduleUIUpdate
2830
#undef FinishWarnBeforeClosing
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
5+
6+
#include "chrome/browser/ui/browser.h"
7+
8+
#include "chrome/browser/ui/unload_controller.h"
9+
10+
#define TabStripEmpty \
11+
TabStripEmpty() {} \
12+
void UnloadController::TabStripEmpty_ChromiumImpl
13+
#include "src/chrome/browser/ui/unload_controller.cc"
14+
#undef TabStripEmpty
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
5+
6+
#ifndef BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_
7+
#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_
8+
9+
#include "chrome/browser/ui/browser.h"
10+
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
11+
12+
#define TabStripEmpty \
13+
TabStripEmpty_ChromiumImpl(); \
14+
void TabStripEmpty
15+
#include "src/chrome/browser/ui/unload_controller.h"
16+
#undef TabStripEmpty
17+
18+
#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_

0 commit comments

Comments
 (0)