Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(845)

Side by Side Diff: ios/chrome/browser/ui/dialogs/dialog_presenter_unittest.mm

Issue 2957423002: Check page URL origin to determine JavaScript alert titles. (Closed)
Patch Set: Review feedback: Make localizedTitle...: private, tweaks. Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "ios/chrome/browser/ui/dialogs/dialog_presenter.h" 5 #import "ios/chrome/browser/ui/dialogs/dialog_presenter.h"
6 6
7 #include "base/strings/sys_string_conversions.h"
7 #include "base/time/time.h" 8 #include "base/time/time.h"
9 #include "components/strings/grit/components_strings.h"
8 #import "ios/chrome/browser/ui/alert_coordinator/alert_coordinator.h" 10 #import "ios/chrome/browser/ui/alert_coordinator/alert_coordinator.h"
9 #import "ios/web/public/test/fakes/test_web_state.h" 11 #import "ios/web/public/test/fakes/test_web_state.h"
10 #include "ios/web/public/web_state/web_state_observer.h" 12 #include "ios/web/public/web_state/web_state_observer.h"
11 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
12 #include "testing/gtest_mac.h" 14 #include "testing/gtest_mac.h"
13 #include "testing/platform_test.h" 15 #include "testing/platform_test.h"
16 #include "ui/base/l10n/l10n_util.h"
14 #include "url/gurl.h" 17 #include "url/gurl.h"
15 18
16 #if !defined(__has_feature) || !__has_feature(objc_arc) 19 #if !defined(__has_feature) || !__has_feature(objc_arc)
17 #error "This file requires ARC support." 20 #error "This file requires ARC support."
18 #endif 21 #endif
19 22
20 namespace { 23 namespace {
21 // TestWebState subclass that supports the WebStateDestroyed() callback for a 24 // TestWebState subclass that supports the WebStateDestroyed() callback for a
22 // single observer. 25 // single observer.
23 class DialogPresenterTestWebState : public web::TestWebState { 26 class DialogPresenterTestWebState : public web::TestWebState {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 TEST_F(DialogPresenterTest, SimpleTest) { 93 TEST_F(DialogPresenterTest, SimpleTest) {
91 DialogPresenterTestWebState webState; 94 DialogPresenterTestWebState webState;
92 [presenter() runJavaScriptAlertPanelWithMessage:@"" 95 [presenter() runJavaScriptAlertPanelWithMessage:@""
93 requestURL:GURL() 96 requestURL:GURL()
94 webState:&webState 97 webState:&webState
95 completionHandler:nil]; 98 completionHandler:nil];
96 EXPECT_EQ(1U, delegate().presentedWebStates.size()); 99 EXPECT_EQ(1U, delegate().presentedWebStates.size());
97 EXPECT_EQ(&webState, delegate().presentedWebStates.front()); 100 EXPECT_EQ(&webState, delegate().presentedWebStates.front());
98 } 101 }
99 102
103 // Test that javascript dialogs are presented with a different title when they
104 // are presented from a URL with a different origin to the webstate origin.
105 TEST_F(DialogPresenterTest, IFrameTest) {
106 DialogPresenterTestWebState web_state;
107 GURL foo_url = GURL("http://foo.com");
108 GURL bar_url = GURL("http://bar.com");
109
110 web_state.SetCurrentURL(foo_url);
111 [presenter() runJavaScriptAlertPanelWithMessage:@""
112 requestURL:foo_url
113 webState:&web_state
114 completionHandler:nil];
115
116 // Ensure alerts from the same domain have a title.
117 NSString* same_origin_title =
118 [presenter() presentedDialogCoordinator].alertController.title;
119 NSString* hostname = base::SysUTF8ToNSString(foo_url.host());
120 NSString* expected_title = l10n_util::GetNSStringF(
121 IDS_JAVASCRIPT_MESSAGEBOX_TITLE, base::SysNSStringToUTF16(hostname));
122 EXPECT_NSEQ(same_origin_title, expected_title);
Eugene But (OOO till 7-30) 2017/06/29 22:03:49 EXPECT_NSEQ takes first argument as expected resul
123
124 [presenter() cancelAllDialogs];
125
126 // Ensure that alerts from an embedded iframe with a different domain have
127 // a title and it's different to the same-origin title.
128 web_state.SetCurrentURL(bar_url);
129 [presenter() runJavaScriptAlertPanelWithMessage:@""
130 requestURL:foo_url
131 webState:&web_state
132 completionHandler:nil];
133 NSString* different_origin_title =
134 [presenter() presentedDialogCoordinator].alertController.title;
135 expected_title = l10n_util::GetNSString(
136 IDS_JAVASCRIPT_MESSAGEBOX_TITLE_NONSTANDARD_URL_IFRAME);
137 EXPECT_NSEQ(different_origin_title, expected_title);
Eugene But (OOO till 7-30) 2017/06/29 22:03:49 ditto
138 }
139
100 // Tests that multiple JavaScript dialogs are queued 140 // Tests that multiple JavaScript dialogs are queued
101 TEST_F(DialogPresenterTest, QueueTest) { 141 TEST_F(DialogPresenterTest, QueueTest) {
102 // Tests that the dialog for |webState1| has been shown. 142 // Tests that the dialog for |webState1| has been shown.
103 DialogPresenterTestWebState webState1; 143 DialogPresenterTestWebState webState1;
104 [presenter() runJavaScriptAlertPanelWithMessage:@"" 144 [presenter() runJavaScriptAlertPanelWithMessage:@""
105 requestURL:GURL() 145 requestURL:GURL()
106 webState:&webState1 146 webState:&webState1
107 completionHandler:nil]; 147 completionHandler:nil];
108 EXPECT_EQ(1U, delegate().presentedWebStates.size()); 148 EXPECT_EQ(1U, delegate().presentedWebStates.size());
109 EXPECT_EQ(&webState1, delegate().presentedWebStates.front()); 149 EXPECT_EQ(&webState1, delegate().presentedWebStates.front());
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 completion3_called = YES; 245 completion3_called = YES;
206 }]; 246 }];
207 EXPECT_EQ(1U, delegate().presentedWebStates.size()); 247 EXPECT_EQ(1U, delegate().presentedWebStates.size());
208 EXPECT_EQ(&webState1, delegate().presentedWebStates.front()); 248 EXPECT_EQ(&webState1, delegate().presentedWebStates.front());
209 // Cancel all dialogs and verify that all |completion_called| were called. 249 // Cancel all dialogs and verify that all |completion_called| were called.
210 [presenter() cancelAllDialogs]; 250 [presenter() cancelAllDialogs];
211 EXPECT_TRUE(completion1_called); 251 EXPECT_TRUE(completion1_called);
212 EXPECT_TRUE(completion2_called); 252 EXPECT_TRUE(completion2_called);
213 EXPECT_TRUE(completion3_called); 253 EXPECT_TRUE(completion3_called);
214 } 254 }
OLDNEW
« no previous file with comments | « ios/chrome/browser/ui/dialogs/dialog_presenter.mm ('k') | ios/chrome/browser/ui/dialogs/javascript_dialog_egtest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698