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

Side by Side Diff: ios/chrome/browser/autofill/form_suggestion_controller_unittest.mm

Issue 2915603002: Do not use CRWWebController in FormSuggestionControllerTest. (Closed)
Patch Set: Fixed compilation Created 3 years, 6 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/autofill/form_suggestion_controller.h" 5 #import "ios/chrome/browser/autofill/form_suggestion_controller.h"
6 6
7 #include <utility> 7 #include <utility>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/ios/ios_util.h" 10 #include "base/ios/ios_util.h"
11 #include "base/mac/foundation_util.h" 11 #include "base/mac/foundation_util.h"
12 #include "base/path_service.h"
13 #include "base/strings/stringprintf.h"
12 #import "components/autofill/ios/browser/form_suggestion.h" 14 #import "components/autofill/ios/browser/form_suggestion.h"
13 #import "ios/chrome/browser/autofill/form_input_accessory_view_controller.h" 15 #import "ios/chrome/browser/autofill/form_input_accessory_view_controller.h"
14 #import "ios/chrome/browser/autofill/form_suggestion_provider.h" 16 #import "ios/chrome/browser/autofill/form_suggestion_provider.h"
15 #import "ios/chrome/browser/autofill/form_suggestion_view.h" 17 #import "ios/chrome/browser/autofill/form_suggestion_view.h"
16 #include "ios/chrome/browser/ui/ui_util.h" 18 #include "ios/chrome/browser/ui/ui_util.h"
17 #import "ios/chrome/browser/web/chrome_web_test.h" 19 #import "ios/chrome/browser/web/chrome_web_test.h"
18 #include "ios/chrome/test/base/scoped_block_swizzler.h" 20 #import "ios/web/public/navigation_manager.h"
19 #import "ios/web/public/web_state/ui/crw_web_view_proxy.h" 21 #import "ios/web/public/web_state/ui/crw_web_view_proxy.h"
20 #import "ios/web/web_state/ui/crw_web_controller.h" 22 #import "ios/web/public/web_state/web_state.h"
21 #import "testing/gtest_mac.h" 23 #import "testing/gtest_mac.h"
22 #import "third_party/ocmock/OCMock/OCMock.h" 24 #import "third_party/ocmock/OCMock/OCMock.h"
23 #include "third_party/ocmock/gtest_support.h" 25 #include "third_party/ocmock/gtest_support.h"
24 26
25 #if !defined(__has_feature) || !__has_feature(objc_arc) 27 #if !defined(__has_feature) || !__has_feature(objc_arc)
26 #error "This file requires ARC support." 28 #error "This file requires ARC support."
27 #endif 29 #endif
28 30
29 @interface FormInputAccessoryViewController (Testing) 31 @interface FormInputAccessoryViewController (Testing)
30 - (instancetype)initWithWebState:(web::WebState*)webState 32 - (instancetype)initWithWebState:(web::WebState*)webState
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 void TearDown() override { 156 void TearDown() override {
155 [suggestion_controller_ detachFromWebState]; 157 [suggestion_controller_ detachFromWebState];
156 ChromeWebTest::TearDown(); 158 ChromeWebTest::TearDown();
157 } 159 }
158 160
159 // Sets |url| to be current for WebState. 161 // Sets |url| to be current for WebState.
160 void SetCurrentUrl(const std::string& url) { 162 void SetCurrentUrl(const std::string& url) {
161 LoadHtml(@"<html></html>", GURL(url)); 163 LoadHtml(@"<html></html>", GURL(url));
162 } 164 }
163 165
164 // Swizzles the current web controller to set whether the content is HTML.
165 void SetContentIsHtml(BOOL content_is_html) {
166 id content_is_html_block = ^BOOL(CRWWebController* webController) {
167 return content_is_html;
168 };
169 content_is_html_swizzler_.reset(new ScopedBlockSwizzler(
170 [CRWWebController class], @selector(contentIsHTML),
171 content_is_html_block));
172 }
173
174 protected: 166 protected:
175 // Sets up |suggestion_controller_| with the specified array of 167 // Sets up |suggestion_controller_| with the specified array of
176 // FormSuggestionProviders. 168 // FormSuggestionProviders.
177 void SetUpController(NSArray* providers) { 169 void SetUpController(NSArray* providers) {
178 suggestion_controller_ = [[FormSuggestionController alloc] 170 suggestion_controller_ = [[FormSuggestionController alloc]
179 initWithWebState:web_state() 171 initWithWebState:web_state()
180 providers:providers 172 providers:providers
181 JsSuggestionManager:mock_js_suggestion_manager_]; 173 JsSuggestionManager:mock_js_suggestion_manager_];
182 [suggestion_controller_ setWebViewProxy:mock_web_view_proxy_]; 174 [suggestion_controller_ setWebViewProxy:mock_web_view_proxy_];
183 @autoreleasepool { 175 @autoreleasepool {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 void (^mockRestore)(NSInvocation*) = ^(NSInvocation* invocation) { 212 void (^mockRestore)(NSInvocation*) = ^(NSInvocation* invocation) {
221 for (UIView* view in [input_accessory_view_ subviews]) { 213 for (UIView* view in [input_accessory_view_ subviews]) {
222 [view removeFromSuperview]; 214 [view removeFromSuperview];
223 } 215 }
224 }; 216 };
225 [[[mock_accessory_controller stub] andDo:mockRestore] 217 [[[mock_accessory_controller stub] andDo:mockRestore]
226 restoreDefaultInputAccessoryView]; 218 restoreDefaultInputAccessoryView];
227 } 219 }
228 } 220 }
229 221
230 // Swizzler for [CRWWebController contentIsHTML].
231 std::unique_ptr<ScopedBlockSwizzler> content_is_html_swizzler_;
232
233 // The FormSuggestionController under test. 222 // The FormSuggestionController under test.
234 FormSuggestionController* suggestion_controller_; 223 FormSuggestionController* suggestion_controller_;
235 224
236 // A fake keyboard accessory view. 225 // A fake keyboard accessory view.
237 UIView* input_accessory_view_; 226 UIView* input_accessory_view_;
238 227
239 // Mock JsSuggestionManager for verifying interactions. 228 // Mock JsSuggestionManager for verifying interactions.
240 id mock_js_suggestion_manager_; 229 id mock_js_suggestion_manager_;
241 230
242 // Mock CRWWebViewProxy for verifying interactions. 231 // Mock CRWWebViewProxy for verifying interactions.
(...skipping 11 matching lines...) Expand all
254 SetCurrentUrl("data:text/html;charset=utf8;base64,"); 243 SetCurrentUrl("data:text/html;charset=utf8;base64,");
255 [suggestion_controller_ webState:web_state() didLoadPageWithSuccess:YES]; 244 [suggestion_controller_ webState:web_state() didLoadPageWithSuccess:YES];
256 245
257 EXPECT_FALSE(GetSuggestionView(input_accessory_view_)); 246 EXPECT_FALSE(GetSuggestionView(input_accessory_view_));
258 EXPECT_OCMOCK_VERIFY(mock_js_suggestion_manager_); 247 EXPECT_OCMOCK_VERIFY(mock_js_suggestion_manager_);
259 } 248 }
260 249
261 // Tests that pages whose content isn't HTML aren't processed. 250 // Tests that pages whose content isn't HTML aren't processed.
262 TEST_F(FormSuggestionControllerTest, PageLoadShouldBeIgnoredWhenNotHtml) { 251 TEST_F(FormSuggestionControllerTest, PageLoadShouldBeIgnoredWhenNotHtml) {
263 SetUpController(@[]); 252 SetUpController(@[]);
264 SetCurrentUrl("http://foo.com");
265 SetContentIsHtml(NO);
266 [suggestion_controller_ webState:web_state() didLoadPageWithSuccess:YES];
267 253
254 // Construct file:// URL for a PDF file.
255 base::FilePath path;
256 base::PathService::Get(base::DIR_MODULE, &path);
257 const char kPdfFilePath[] = "ios/testing/data/http_server_files/testpage.pdf";
258 path = path.Append(FILE_PATH_LITERAL(kPdfFilePath));
259 GURL url(base::StringPrintf("file://%s", path.value().c_str()));
260
261 // Load PDF file URL.
262 web::NavigationManager::WebLoadParams params(url);
263 web_state()->GetNavigationManager()->LoadURLWithParams(params);
264 WaitForCondition(^{
265 return !web_state()->IsLoading();
266 });
267
268 ASSERT_EQ("application/pdf", web_state()->GetContentsMimeType());
268 EXPECT_FALSE(GetSuggestionView(input_accessory_view_)); 269 EXPECT_FALSE(GetSuggestionView(input_accessory_view_));
269 EXPECT_OCMOCK_VERIFY(mock_js_suggestion_manager_); 270 EXPECT_OCMOCK_VERIFY(mock_js_suggestion_manager_);
270 } 271 }
271 272
272 // Tests that the keyboard accessory view is reset and JavaScript is injected 273 // Tests that the keyboard accessory view is reset and JavaScript is injected
273 // when a page is loaded. 274 // when a page is loaded.
274 TEST_F(FormSuggestionControllerTest, 275 TEST_F(FormSuggestionControllerTest,
275 PageLoadShouldRestoreKeyboardAccessoryViewAndInjectJavaScript) { 276 PageLoadShouldRestoreKeyboardAccessoryViewAndInjectJavaScript) {
276 SetUpController(@[]); 277 SetUpController(@[]);
277 SetCurrentUrl("http://foo.com"); 278 SetCurrentUrl("http://foo.com");
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 433
433 // Selecting a suggestion should notify the delegate. 434 // Selecting a suggestion should notify the delegate.
434 [suggestion_controller_ didSelectSuggestion:suggestions[0]]; 435 [suggestion_controller_ didSelectSuggestion:suggestions[0]];
435 EXPECT_TRUE([provider selected]); 436 EXPECT_TRUE([provider selected]);
436 EXPECT_NSEQ(@"form", [provider formName]); 437 EXPECT_NSEQ(@"form", [provider formName]);
437 EXPECT_NSEQ(@"field", [provider fieldName]); 438 EXPECT_NSEQ(@"field", [provider fieldName]);
438 EXPECT_NSEQ(suggestions[0], [provider suggestion]); 439 EXPECT_NSEQ(suggestions[0], [provider suggestion]);
439 } 440 }
440 441
441 } // namespace 442 } // namespace
OLDNEW
« no previous file with comments | « ios/chrome/browser/autofill/form_input_accessory_view_controller.mm ('k') | ios/chrome/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698