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

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: Add string comparison unit test 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/time/time.h" 7 #include "base/time/time.h"
8 #include "components/strings/grit/components_strings.h"
8 #import "ios/chrome/browser/ui/alert_coordinator/alert_coordinator.h" 9 #import "ios/chrome/browser/ui/alert_coordinator/alert_coordinator.h"
9 #import "ios/web/public/test/fakes/test_web_state.h" 10 #import "ios/web/public/test/fakes/test_web_state.h"
10 #include "ios/web/public/web_state/web_state_observer.h" 11 #include "ios/web/public/web_state/web_state_observer.h"
11 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
12 #include "testing/gtest_mac.h" 13 #include "testing/gtest_mac.h"
13 #include "testing/platform_test.h" 14 #include "testing/platform_test.h"
15 #include "ui/base/l10n/l10n_util.h"
14 #include "url/gurl.h" 16 #include "url/gurl.h"
15 17
16 #if !defined(__has_feature) || !__has_feature(objc_arc) 18 #if !defined(__has_feature) || !__has_feature(objc_arc)
17 #error "This file requires ARC support." 19 #error "This file requires ARC support."
18 #endif 20 #endif
19 21
20 namespace { 22 namespace {
21 // TestWebState subclass that supports the WebStateDestroyed() callback for a 23 // TestWebState subclass that supports the WebStateDestroyed() callback for a
22 // single observer. 24 // single observer.
23 class DialogPresenterTestWebState : public web::TestWebState { 25 class DialogPresenterTestWebState : public web::TestWebState {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 TEST_F(DialogPresenterTest, SimpleTest) { 92 TEST_F(DialogPresenterTest, SimpleTest) {
91 DialogPresenterTestWebState webState; 93 DialogPresenterTestWebState webState;
92 [presenter() runJavaScriptAlertPanelWithMessage:@"" 94 [presenter() runJavaScriptAlertPanelWithMessage:@""
93 requestURL:GURL() 95 requestURL:GURL()
94 webState:&webState 96 webState:&webState
95 completionHandler:nil]; 97 completionHandler:nil];
96 EXPECT_EQ(1U, delegate().presentedWebStates.size()); 98 EXPECT_EQ(1U, delegate().presentedWebStates.size());
97 EXPECT_EQ(&webState, delegate().presentedWebStates.front()); 99 EXPECT_EQ(&webState, delegate().presentedWebStates.front());
98 } 100 }
99 101
102 // Test that javascript dialogs are presented with a different title when they
103 // are presented from a URL with a different origin to the webstate origin.
104 TEST_F(DialogPresenterTest, IFrameTest) {
105 DialogPresenterTestWebState web_state;
106 GURL foo_url = GURL("http://foo.com");
107 GURL bar_url = GURL("http://bar.com");
108
109 web_state.SetCurrentURL(foo_url);
110 [presenter() runJavaScriptAlertPanelWithMessage:@""
111 requestURL:foo_url
112 webState:&web_state
113 completionHandler:nil];
114
115 // Ensure alerts from the same domain have a title.
116 NSString* same_origin_title =
117 [presenter() presentedDialogCoordinator].alertController.title;
118 EXPECT_FALSE([same_origin_title length] == 0);
Eugene But (OOO till 7-30) 2017/06/29 20:35:10 Is it possible to check the title in this test? Te
PL 2017/06/29 21:09:04 Done!
119
120 [presenter() cancelAllDialogs];
121
122 // Ensure that alerts from an embedded iframe with a different domain have
123 // a title and it's different to the same-origin title.
124 web_state.SetCurrentURL(bar_url);
125 [presenter() runJavaScriptAlertPanelWithMessage:@""
126 requestURL:foo_url
127 webState:&web_state
128 completionHandler:nil];
129 NSString* different_origin_title =
130 [presenter() presentedDialogCoordinator].alertController.title;
131 EXPECT_FALSE([different_origin_title length] == 0);
Eugene But (OOO till 7-30) 2017/06/29 20:35:10 Instead of this and the next "EXPECT" call you can
PL 2017/06/29 21:09:04 Done!
132 EXPECT_TRUE([different_origin_title
133 isEqualToString:
134 l10n_util::GetNSString(
135 IDS_JAVASCRIPT_MESSAGEBOX_TITLE_NONSTANDARD_URL_IFRAME)]);
136 EXPECT_FALSE([same_origin_title isEqualToString:different_origin_title]);
Eugene But (OOO till 7-30) 2017/06/29 20:35:10 Is it possible to drop this "EXPECT" in favor of c
PL 2017/06/29 21:09:04 Done! You're right, this one won't be needed any m
137 }
138
100 // Tests that multiple JavaScript dialogs are queued 139 // Tests that multiple JavaScript dialogs are queued
101 TEST_F(DialogPresenterTest, QueueTest) { 140 TEST_F(DialogPresenterTest, QueueTest) {
102 // Tests that the dialog for |webState1| has been shown. 141 // Tests that the dialog for |webState1| has been shown.
103 DialogPresenterTestWebState webState1; 142 DialogPresenterTestWebState webState1;
104 [presenter() runJavaScriptAlertPanelWithMessage:@"" 143 [presenter() runJavaScriptAlertPanelWithMessage:@""
105 requestURL:GURL() 144 requestURL:GURL()
106 webState:&webState1 145 webState:&webState1
107 completionHandler:nil]; 146 completionHandler:nil];
108 EXPECT_EQ(1U, delegate().presentedWebStates.size()); 147 EXPECT_EQ(1U, delegate().presentedWebStates.size());
109 EXPECT_EQ(&webState1, delegate().presentedWebStates.front()); 148 EXPECT_EQ(&webState1, delegate().presentedWebStates.front());
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 completion3_called = YES; 244 completion3_called = YES;
206 }]; 245 }];
207 EXPECT_EQ(1U, delegate().presentedWebStates.size()); 246 EXPECT_EQ(1U, delegate().presentedWebStates.size());
208 EXPECT_EQ(&webState1, delegate().presentedWebStates.front()); 247 EXPECT_EQ(&webState1, delegate().presentedWebStates.front());
209 // Cancel all dialogs and verify that all |completion_called| were called. 248 // Cancel all dialogs and verify that all |completion_called| were called.
210 [presenter() cancelAllDialogs]; 249 [presenter() cancelAllDialogs];
211 EXPECT_TRUE(completion1_called); 250 EXPECT_TRUE(completion1_called);
212 EXPECT_TRUE(completion2_called); 251 EXPECT_TRUE(completion2_called);
213 EXPECT_TRUE(completion3_called); 252 EXPECT_TRUE(completion3_called);
214 } 253 }
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