Commit a49a188c authored by Gauthier Ambard's avatar Gauthier Ambard Committed by Commit Bot

[iOS] Close NTP when switching tab

When the user switch from a NTP which doesn't have a back/forward
history to an open tab, the tab should be closed.

Bug: 908010, 908409
Change-Id: Ie4c783bef4c43ddafa175ba5796bd2a0b05cc54c
Reviewed-on: https://chromium-review.googlesource.com/c/1349264
Commit-Queue: Gauthier Ambard <gambard@chromium.org>
Reviewed-by: default avatarStepan Khapugin <stkhapugin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#611123}
parent e6842e2e
......@@ -4611,6 +4611,7 @@ applicationCommandEndpoint:(id<ApplicationCommands>)applicationCommandEndpoint
- (void)switchToTabWithURL:(const GURL&)URL {
NSInteger newWebStateIndex = 0;
WebStateList* webStateList = self.tabModel.webStateList;
NSInteger currentWebStateIndex = webStateList->active_index();
web::WebState* currentWebState = webStateList->GetActiveWebState();
// TODO(crbug.com/893121): This should probably live in the WebStateList.
......@@ -4667,6 +4668,16 @@ applicationCommandEndpoint:(id<ApplicationCommands>)applicationCommandEndpoint
inPosition:position];
webStateList->ActivateWebStateAt(newWebStateIndex);
// Close the tab if it is NTP with no back/forward history to avoid having
// empty tabs.
if (IsVisibleURLNewTabPage(currentWebState) &&
currentWebState->GetNavigationManager() &&
!currentWebState->GetNavigationManager()->CanGoBack() &&
!currentWebState->GetNavigationManager()->CanGoForward()) {
webStateList->CloseWebStateAt(currentWebStateIndex,
WebStateList::CLOSE_USER_ACTION);
}
}
#pragma mark - TabModelObserver methods
......
......@@ -96,6 +96,7 @@ source_set("eg_tests") {
"//base/test:test_support",
"//components/omnibox/browser",
"//ios/chrome/browser/ui:feature_flags",
"//ios/chrome/browser/ui/content_suggestions:content_suggestions_constant",
"//ios/chrome/browser/ui/tab_grid:egtest_support",
"//ios/chrome/test/earl_grey:test_support",
"//ios/testing/earl_grey:earl_grey_support",
......
......@@ -8,6 +8,7 @@
#include "base/strings/sys_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "components/omnibox/browser/omnibox_field_trial.h"
#import "ios/chrome/browser/ui/content_suggestions/ntp_home_constant.h"
#import "ios/chrome/browser/ui/omnibox/popup/omnibox_popup_row.h"
#import "ios/chrome/browser/ui/tab_grid/tab_grid_egtest_util.h"
#import "ios/chrome/test/earl_grey/chrome_earl_grey.h"
......@@ -242,4 +243,57 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
nil)] assertWithMatcher:grey_nil()];
}
- (void)testCloseNTPWhenSwitching {
// Open the first page.
GURL URL1 = self.testServer->GetURL(kPage1URL);
[ChromeEarlGrey loadURL:URL1];
[ChromeEarlGrey waitForWebViewContainingText:kPage1];
// Open a new tab and switch to the first tab.
[ChromeEarlGrey openNewTab];
[[EarlGrey
selectElementWithMatcher:grey_accessibilityID(
ntp_home::FakeOmniboxAccessibilityID())]
performAction:grey_typeText(base::SysUTF8ToNSString(URL1.host()))];
[[EarlGrey
selectElementWithMatcher:
grey_allOf(grey_ancestor(PopupRowWithUrl(URL1)),
grey_accessibilityID(
kOmniboxPopupRowSwitchTabAccessibilityIdentifier),
nil)] performAction:grey_tap()];
[ChromeEarlGrey waitForWebViewContainingText:kPage1];
// Check that the other tab is closed.
[ChromeEarlGrey waitForMainTabCount:1];
}
- (void)testDontCloseNTPWhenSwitchingWithForwardHistory {
// Open the first page.
GURL URL1 = self.testServer->GetURL(kPage1URL);
[ChromeEarlGrey loadURL:URL1];
[ChromeEarlGrey waitForWebViewContainingText:kPage1];
// Open a new tab, navigate to a page and go back to have forward history.
[ChromeEarlGrey openNewTab];
[ChromeEarlGrey loadURL:URL1];
[ChromeEarlGrey waitForWebViewContainingText:kPage1];
[ChromeEarlGrey goBack];
// Navigate to the other tab.
[[EarlGrey
selectElementWithMatcher:grey_accessibilityID(
ntp_home::FakeOmniboxAccessibilityID())]
performAction:grey_typeText(base::SysUTF8ToNSString(URL1.host()))];
[[EarlGrey
selectElementWithMatcher:
grey_allOf(grey_ancestor(PopupRowWithUrl(URL1)),
grey_accessibilityID(
kOmniboxPopupRowSwitchTabAccessibilityIdentifier),
nil)] performAction:grey_tap()];
[ChromeEarlGrey waitForWebViewContainingText:kPage1];
// Check that the other tab is not closed.
[ChromeEarlGrey waitForMainTabCount:2];
}
@end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment