| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/web/error_page_generator.h" | 5 #import "ios/chrome/browser/web/error_page_generator.h" |
| 6 #include "base/mac/scoped_nsobject.h" | |
| 7 #include "base/strings/sys_string_conversions.h" | 6 #include "base/strings/sys_string_conversions.h" |
| 8 #include "ios/web/public/test/web_test.h" | 7 #include "ios/web/public/test/web_test.h" |
| 9 #include "ios/web/web_state/error_translation_util.h" | 8 #include "ios/web/web_state/error_translation_util.h" |
| 10 #import "net/base/mac/url_conversions.h" | 9 #import "net/base/mac/url_conversions.h" |
| 11 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "testing/gtest_mac.h" | 12 #include "testing/gtest_mac.h" |
| 14 #import "third_party/ocmock/OCMock/OCMock.h" | 13 #import "third_party/ocmock/OCMock/OCMock.h" |
| 15 #import "third_party/ocmock/gtest_support.h" | 14 #import "third_party/ocmock/gtest_support.h" |
| 16 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 17 | 16 |
| 17 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 18 #error "This file requires ARC support." |
| 19 #endif |
| 20 |
| 18 // From static_html_view_controller.mm: callback for the HtmlGenerator protocol. | 21 // From static_html_view_controller.mm: callback for the HtmlGenerator protocol. |
| 19 typedef void (^HtmlCallback)(NSString*); | 22 typedef void (^HtmlCallback)(NSString*); |
| 20 | 23 |
| 21 // To test the error page generator code, generate error data, and examine the | 24 // To test the error page generator code, generate error data, and examine the |
| 22 // generated HTML to make sure the expected text has been included. In detail: | 25 // generated HTML to make sure the expected text has been included. In detail: |
| 23 // 1. Use GenerateError to create an NSError object to pass in. | 26 // 1. Use GenerateError to create an NSError object to pass in. |
| 24 // 2. Initialize errorPageGenerator_ with NSError objects. | 27 // 2. Initialize errorPageGenerator_ with NSError objects. |
| 25 // 3. Pass the |TestHTMLForError| function to errorPageContent_'s generateHTML | 28 // 3. Pass the |TestHTMLForError| function to errorPageContent_'s generateHTML |
| 26 // function, along with the string that's expected to appear in the | 29 // function, along with the string that's expected to appear in the |
| 27 // generated HTML for this error. | 30 // generated HTML for this error. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 42 NSInteger errorCode) { | 45 NSInteger errorCode) { |
| 43 NSDictionary* info = @{ | 46 NSDictionary* info = @{ |
| 44 NSURLErrorFailingURLStringErrorKey : | 47 NSURLErrorFailingURLStringErrorKey : |
| 45 base::SysUTF8ToNSString(errorURL.spec()) | 48 base::SysUTF8ToNSString(errorURL.spec()) |
| 46 }; | 49 }; |
| 47 return web::NetErrorFromError( | 50 return web::NetErrorFromError( |
| 48 [NSError errorWithDomain:errorDomain code:errorCode userInfo:info]); | 51 [NSError errorWithDomain:errorDomain code:errorCode userInfo:info]); |
| 49 } | 52 } |
| 50 | 53 |
| 51 // ErrorPageContent object to be tested. | 54 // ErrorPageContent object to be tested. |
| 52 base::scoped_nsobject<ErrorPageGenerator> errorPageGenerator_; | 55 ErrorPageGenerator* errorPageGenerator_; |
| 53 | 56 |
| 54 // Mock for the URLLoader that would otherwise handle the HTML produced. | 57 // Mock for the URLLoader that would otherwise handle the HTML produced. |
| 55 base::scoped_nsobject<OCMockObject> mockLoader_; | 58 OCMockObject* mockLoader_; |
| 56 }; | 59 }; |
| 57 | 60 |
| 58 // Test with a timed out error. | 61 // Test with a timed out error. |
| 59 TEST_F(ErrorPageGeneratorTest, TimedOut) { | 62 TEST_F(ErrorPageGeneratorTest, TimedOut) { |
| 60 GURL errorURL("http://www.google.com"); | 63 GURL errorURL("http://www.google.com"); |
| 61 NSError* error = | 64 NSError* error = |
| 62 GenerateError(errorURL, NSURLErrorDomain, kCFURLErrorTimedOut); | 65 GenerateError(errorURL, NSURLErrorDomain, kCFURLErrorTimedOut); |
| 63 errorPageGenerator_.reset([[ErrorPageGenerator alloc] initWithError:error | 66 errorPageGenerator_ = |
| 64 isPost:NO | 67 [[ErrorPageGenerator alloc] initWithError:error isPost:NO isIncognito:NO]; |
| 65 isIncognito:NO]); | 68 [errorPageGenerator_ generateHtml:^(NSString* html) { |
| 66 [errorPageGenerator_.get() generateHtml:^(NSString* html) { | |
| 67 TestHTMLForError(html, @"ERR_CONNECTION_TIMED_OUT"); | 69 TestHTMLForError(html, @"ERR_CONNECTION_TIMED_OUT"); |
| 68 }]; | 70 }]; |
| 69 } | 71 } |
| 70 | 72 |
| 71 // Test with a bad URL scheme (see b/7448754). | 73 // Test with a bad URL scheme (see b/7448754). |
| 72 TEST_F(ErrorPageGeneratorTest, BadURLScheme) { | 74 TEST_F(ErrorPageGeneratorTest, BadURLScheme) { |
| 73 GURL errorURL( | 75 GURL errorURL( |
| 74 "itms-appss://itunes.apple.com/gb/app/google-search/id284815942?mt=8"); | 76 "itms-appss://itunes.apple.com/gb/app/google-search/id284815942?mt=8"); |
| 75 NSError* error = | 77 NSError* error = |
| 76 GenerateError(errorURL, NSURLErrorDomain, kCFURLErrorTimedOut); | 78 GenerateError(errorURL, NSURLErrorDomain, kCFURLErrorTimedOut); |
| 77 errorPageGenerator_.reset([[ErrorPageGenerator alloc] initWithError:error | 79 errorPageGenerator_ = |
| 78 isPost:NO | 80 [[ErrorPageGenerator alloc] initWithError:error isPost:NO isIncognito:NO]; |
| 79 isIncognito:NO]); | 81 [errorPageGenerator_ generateHtml:^(NSString* html) { |
| 80 [errorPageGenerator_.get() generateHtml:^(NSString* html) { | |
| 81 TestHTMLForError(html, @"ERR_CONNECTION_TIMED_OUT"); | 82 TestHTMLForError(html, @"ERR_CONNECTION_TIMED_OUT"); |
| 82 }]; | 83 }]; |
| 83 } | 84 } |
| 84 | 85 |
| 85 // Test with an empty GURL object. | 86 // Test with an empty GURL object. |
| 86 // TODO(ios): [merge 191784] b/8525110 fix the code or the test. | 87 // TODO(ios): [merge 191784] b/8525110 fix the code or the test. |
| 87 TEST_F(ErrorPageGeneratorTest, EmptyURLObject) { | 88 TEST_F(ErrorPageGeneratorTest, EmptyURLObject) { |
| 88 GURL errorURL; | 89 GURL errorURL; |
| 89 NSError* error = | 90 NSError* error = |
| 90 GenerateError(errorURL, NSURLErrorDomain, kCFURLErrorTimedOut); | 91 GenerateError(errorURL, NSURLErrorDomain, kCFURLErrorTimedOut); |
| 91 errorPageGenerator_.reset([[ErrorPageGenerator alloc] initWithError:error | 92 errorPageGenerator_ = |
| 92 isPost:NO | 93 [[ErrorPageGenerator alloc] initWithError:error isPost:NO isIncognito:NO]; |
| 93 isIncognito:NO]); | 94 [errorPageGenerator_ generateHtml:^(NSString* html) { |
| 94 [errorPageGenerator_.get() generateHtml:^(NSString* html) { | |
| 95 TestHTMLForError(html, @"ERR_CONNECTION_TIMED_OUT"); | 95 TestHTMLForError(html, @"ERR_CONNECTION_TIMED_OUT"); |
| 96 }]; | 96 }]; |
| 97 } | 97 } |
| OLD | NEW |