diff --git a/ios/chrome/browser/ui/dialogs/BUILD.gn b/ios/chrome/browser/ui/dialogs/BUILD.gn index f99224478565..4f054c6cc013 100644 --- a/ios/chrome/browser/ui/dialogs/BUILD.gn +++ b/ios/chrome/browser/ui/dialogs/BUILD.gn @@ -110,6 +110,7 @@ source_set("eg_tests") { "//base", "//base/test:test_support", "//components/strings", + "//components/url_formatter", "//ios/chrome/app/strings", "//ios/chrome/browser/ui", "//ios/chrome/test/app:test_support", diff --git a/ios/chrome/browser/ui/dialogs/dialog_presenter.mm b/ios/chrome/browser/ui/dialogs/dialog_presenter.mm index 4b3d5bccfaaa..a60d95b561eb 100644 --- a/ios/chrome/browser/ui/dialogs/dialog_presenter.mm +++ b/ios/chrome/browser/ui/dialogs/dialog_presenter.mm @@ -9,8 +9,8 @@ #include "base/containers/circular_deque.h" #import "base/ios/block_types.h" #include "base/logging.h" -#include "base/strings/sys_string_conversions.h" #include "components/strings/grit/components_strings.h" +#include "components/url_formatter/elide_url.h" #import "ios/chrome/browser/ui/alert_coordinator/action_sheet_coordinator.h" #import "ios/chrome/browser/ui/alert_coordinator/alert_coordinator.h" #import "ios/chrome/browser/ui/alert_coordinator/input_alert_coordinator.h" @@ -376,19 +376,14 @@ - (void)tryToPresent { + (NSString*)localizedTitleForJavaScriptAlertFromPage:(const GURL&)pageURL mainFrameURL: (const GURL&)mainFrameURL { - NSString* localizedTitle = nil; - NSString* hostname = base::SysUTF8ToNSString(pageURL.host()); - bool sameOriginAsMainFrame = pageURL.GetOrigin() == mainFrameURL.GetOrigin(); - if (!sameOriginAsMainFrame) { - localizedTitle = l10n_util::GetNSString( + return l10n_util::GetNSString( IDS_JAVASCRIPT_MESSAGEBOX_TITLE_NONSTANDARD_URL_IFRAME); - } else { - localizedTitle = l10n_util::GetNSStringF( - IDS_JAVASCRIPT_MESSAGEBOX_TITLE, base::SysNSStringToUTF16(hostname)); } - return localizedTitle; + base::string16 title = url_formatter::FormatUrlForSecurityDisplay( + pageURL, url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS); + return l10n_util::GetNSStringF(IDS_JAVASCRIPT_MESSAGEBOX_TITLE, title); } #pragma mark - Private methods. diff --git a/ios/chrome/browser/ui/dialogs/dialog_presenter_unittest.mm b/ios/chrome/browser/ui/dialogs/dialog_presenter_unittest.mm index 5fda24714a0a..51906f8a77ee 100644 --- a/ios/chrome/browser/ui/dialogs/dialog_presenter_unittest.mm +++ b/ios/chrome/browser/ui/dialogs/dialog_presenter_unittest.mm @@ -5,6 +5,7 @@ #import "ios/chrome/browser/ui/dialogs/dialog_presenter.h" #include "base/strings/sys_string_conversions.h" +#include "base/strings/utf_string_conversions.h" #include "base/time/time.h" #include "components/strings/grit/components_strings.h" #import "ios/chrome/browser/ui/alert_coordinator/alert_coordinator.h" @@ -138,6 +139,23 @@ - (void)dialogPresenter:(DialogPresenter*)presenter EXPECT_NSEQ(expected_title, different_origin_title); } +// Tests that JavaScript dialogs have correct title when they are presented from +// about:blank page. +TEST_F(DialogPresenterTest, AboutBlankTest) { + DialogPresenterTestWebState web_state; + web_state.SetCurrentURL(GURL(url::kAboutBlankURL)); + [presenter() runJavaScriptAlertPanelWithMessage:@"" + requestURL:GURL(url::kAboutBlankURL) + webState:&web_state + completionHandler:nil]; + + NSString* expected_title = l10n_util::GetNSStringF( + IDS_JAVASCRIPT_MESSAGEBOX_TITLE, base::UTF8ToUTF16(url::kAboutBlankURL)); + NSString* actual_title = + [presenter() presentedDialogCoordinator].alertController.title; + EXPECT_NSEQ(expected_title, actual_title); +} + // Tests that multiple JavaScript dialogs are queued TEST_F(DialogPresenterTest, QueueTest) { // Tests that the dialog for |webState1| has been shown. diff --git a/ios/chrome/browser/ui/dialogs/javascript_dialog_egtest.mm b/ios/chrome/browser/ui/dialogs/javascript_dialog_egtest.mm index 345409b289b1..4f0b046fe350 100644 --- a/ios/chrome/browser/ui/dialogs/javascript_dialog_egtest.mm +++ b/ios/chrome/browser/ui/dialogs/javascript_dialog_egtest.mm @@ -11,6 +11,7 @@ #include "base/strings/utf_string_conversions.h" #import "base/test/ios/wait_util.h" #include "components/strings/grit/components_strings.h" +#include "components/url_formatter/elide_url.h" #import "ios/chrome/browser/ui/dialogs/dialog_presenter.h" #include "ios/chrome/browser/ui/util/ui_util.h" #include "ios/chrome/grit/ios_strings.h" @@ -232,10 +233,10 @@ void WaitForAlertToBeShown(NSString* alert_label) { // Waits for a JavaScript dialog to be shown from the page at |url|. void WaitForJavaScriptDialogToBeShown(const GURL& url) { - NSString* hostname = base::SysUTF8ToNSString(url.host()); - NSString* expectedTitle = l10n_util::GetNSStringF( - IDS_JAVASCRIPT_MESSAGEBOX_TITLE, base::SysNSStringToUTF16(hostname)); - + base::string16 URLString = url_formatter::FormatUrlForSecurityDisplay( + url, url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS); + NSString* expectedTitle = + l10n_util::GetNSStringF(IDS_JAVASCRIPT_MESSAGEBOX_TITLE, URLString); WaitForAlertToBeShown(expectedTitle); } @@ -258,9 +259,10 @@ void ShowJavaScriptDialog(JavaScriptAlertType type, const GURL& url) { void AssertJavaScriptAlertNotPresent(const GURL& url) { ConditionBlock condition = ^{ NSError* error = nil; - NSString* hostname = base::SysUTF8ToNSString(url.host()); - NSString* expectedTitle = l10n_util::GetNSStringF( - IDS_JAVASCRIPT_MESSAGEBOX_TITLE, base::SysNSStringToUTF16(hostname)); + base::string16 URLString = url_formatter::FormatUrlForSecurityDisplay( + url, url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS); + NSString* expectedTitle = + l10n_util::GetNSStringF(IDS_JAVASCRIPT_MESSAGEBOX_TITLE, URLString); id titleLabel = chrome_test_util::StaticTextWithAccessibilityLabel(expectedTitle); @@ -607,11 +609,7 @@ - (void)testShowJavaScriptAfterNewTabAnimation { } // Wait for the alert to be shown. - NSString* hostname = base::SysUTF8ToNSString(self.onLoadPageURL.host()); - NSString* expectedTitle = l10n_util::GetNSStringF( - IDS_JAVASCRIPT_MESSAGEBOX_TITLE, base::SysNSStringToUTF16(hostname)); - - WaitForAlertToBeShown(expectedTitle); + WaitForJavaScriptDialogToBeShown(self.onLoadPageURL); // Verify that the omnibox shows the correct URL when the dialog is visible. std::string title =