Skip to content

Commit

Permalink
Fix JS dialog titles for about: URLs.
Browse files Browse the repository at this point in the history
[email protected]

(cherry picked from commit d926dbc)

Bug: 923695
Change-Id: I7266b9d08e66b267241c4a2e59b1104b26fa92ef
Reviewed-on: https://chromium-review.googlesource.com/c/1435626
Auto-Submit: Eugene But <[email protected]>
Reviewed-by: Kurt Horimoto <[email protected]>
Commit-Queue: Eugene But <[email protected]>
Cr-Original-Commit-Position: refs/heads/master@{#626779}
Reviewed-on: https://chromium-review.googlesource.com/c/1444701
Reviewed-by: Eugene But <[email protected]>
Cr-Commit-Position: refs/branch-heads/3683@{#57}
Cr-Branched-From: e510299-refs/heads/master@{#625896}
  • Loading branch information
Eugene But authored and Eugene But committed Jan 30, 2019
1 parent 3300475 commit b777c82
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
1 change: 1 addition & 0 deletions ios/chrome/browser/ui/dialogs/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 5 additions & 10 deletions ios/chrome/browser/ui/dialogs/dialog_presenter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions ios/chrome/browser/ui/dialogs/dialog_presenter_unittest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
22 changes: 10 additions & 12 deletions ios/chrome/browser/ui/dialogs/javascript_dialog_egtest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
}

Expand All @@ -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<GREYMatcher> titleLabel =
chrome_test_util::StaticTextWithAccessibilityLabel(expectedTitle);
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit b777c82

Please sign in to comment.