| OLD | NEW |
| 1 // Copyright 2013 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 #include "base/mac/scoped_nsobject.h" | 6 #include "base/mac/scoped_nsobject.h" |
| 6 #include "base/strings/sys_string_conversions.h" | 7 #include "base/strings/sys_string_conversions.h" |
| 7 #import "ios/chrome/browser/ui/url_loader.h" | |
| 8 #import "ios/chrome/browser/web/error_page_content.h" | |
| 9 #include "ios/web/public/test/web_test.h" | 8 #include "ios/web/public/test/web_test.h" |
| 10 #include "ios/web/web_state/error_translation_util.h" | 9 #include "ios/web/web_state/error_translation_util.h" |
| 11 #import "net/base/mac/url_conversions.h" | 10 #import "net/base/mac/url_conversions.h" |
| 12 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "testing/gtest_mac.h" | 13 #include "testing/gtest_mac.h" |
| 15 #import "third_party/ocmock/OCMock/OCMock.h" | 14 #import "third_party/ocmock/OCMock/OCMock.h" |
| 16 #import "third_party/ocmock/gtest_support.h" | 15 #import "third_party/ocmock/gtest_support.h" |
| 17 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 18 | 17 |
| 19 // From static_html_view_controller.mm: callback for the HtmlGenerator protocol. | 18 // From static_html_view_controller.mm: callback for the HtmlGenerator protocol. |
| 20 typedef void (^HtmlCallback)(NSString*); | 19 typedef void (^HtmlCallback)(NSString*); |
| 21 | 20 |
| 22 // To test the error page content code, generate a URL and error data, and | 21 // To test the error page generator code, generate error data, and examine the |
| 23 // examine the generated HTML to make sure the expected text has been | 22 // generated HTML to make sure the expected text has been included. In detail: |
| 24 // included. In detail: | 23 // 1. Use GenerateError to create an NSError object to pass in. |
| 25 // 1. Create a GURL with the URL to submit to the error page creator. | 24 // 2. Initialize errorPageGenerator_ with NSError objects. |
| 26 // 2. Use GenerateError to create an NSError object to pass in. | 25 // 3. Pass the |TestHTMLForError| function to errorPageContent_'s generateHTML |
| 27 // 3. Initialize errorPageContent_ with the URL and NSError objects. | |
| 28 // 4. Pass the |TestHTMLForError| function to errorPageContent_'s generateHTML | |
| 29 // function, along with the string that's expected to appear in the | 26 // function, along with the string that's expected to appear in the |
| 30 // generated HTML for this error. | 27 // generated HTML for this error. |
| 31 class ErrorPageContentTest : public web::WebTest { | 28 class ErrorPageGeneratorTest : public web::WebTest { |
| 32 protected: | 29 protected: |
| 33 void SetUp() override { | 30 void SetUp() override { web::WebTest::SetUp(); } |
| 34 web::WebTest::SetUp(); | |
| 35 mockLoader_.reset( | |
| 36 [[OCMockObject niceMockForProtocol:@protocol(UrlLoader)] retain]); | |
| 37 } | |
| 38 | 31 |
| 39 // Test the HTML generated in the test against the error we were supposed to | 32 // Test the HTML generated in the test against the error we were supposed to |
| 40 // find. | 33 // find. |
| 41 static void TestHTMLForError(NSString* html, NSString* errorToFind) { | 34 static void TestHTMLForError(NSString* html, NSString* errorToFind) { |
| 42 EXPECT_TRUE([html rangeOfString:errorToFind].length > 0); | 35 EXPECT_TRUE([html rangeOfString:errorToFind].length > 0); |
| 43 } | 36 } |
| 44 | 37 |
| 45 // Generate an NSError object with the format expected by LocalizedError, | 38 // Generate an NSError object with the format expected by LocalizedError, |
| 46 // given a URL, error domain, and error code. | 39 // given a URL, error domain, and error code. |
| 47 NSError* GenerateError(const GURL& errorURL, | 40 NSError* GenerateError(const GURL& errorURL, |
| 48 NSString* errorDomain, | 41 NSString* errorDomain, |
| 49 NSInteger errorCode) { | 42 NSInteger errorCode) { |
| 50 NSDictionary* info = @{ | 43 NSDictionary* info = @{ |
| 51 NSURLErrorFailingURLStringErrorKey : | 44 NSURLErrorFailingURLStringErrorKey : |
| 52 base::SysUTF8ToNSString(errorURL.spec()) | 45 base::SysUTF8ToNSString(errorURL.spec()) |
| 53 }; | 46 }; |
| 54 return web::NetErrorFromError( | 47 return web::NetErrorFromError( |
| 55 [NSError errorWithDomain:errorDomain code:errorCode userInfo:info]); | 48 [NSError errorWithDomain:errorDomain code:errorCode userInfo:info]); |
| 56 } | 49 } |
| 57 | 50 |
| 58 // ErrorPageContent object to be tested. | 51 // ErrorPageContent object to be tested. |
| 59 base::scoped_nsobject<ErrorPageContent> errorPageContent_; | 52 base::scoped_nsobject<ErrorPageGenerator> errorPageGenerator_; |
| 60 | 53 |
| 61 // Mock for the URLLoader that would otherwise handle the HTML produced. | 54 // Mock for the URLLoader that would otherwise handle the HTML produced. |
| 62 base::scoped_nsobject<OCMockObject> mockLoader_; | 55 base::scoped_nsobject<OCMockObject> mockLoader_; |
| 63 }; | 56 }; |
| 64 | 57 |
| 65 // Test with a timed out error. | 58 // Test with a timed out error. |
| 66 TEST_F(ErrorPageContentTest, TimedOut) { | 59 TEST_F(ErrorPageGeneratorTest, TimedOut) { |
| 67 GURL errorURL("http://www.google.com"); | 60 GURL errorURL("http://www.google.com"); |
| 68 NSError* error = | 61 NSError* error = |
| 69 GenerateError(errorURL, NSURLErrorDomain, kCFURLErrorTimedOut); | 62 GenerateError(errorURL, NSURLErrorDomain, kCFURLErrorTimedOut); |
| 70 errorPageContent_.reset([[ErrorPageContent alloc] | 63 errorPageGenerator_.reset([[ErrorPageGenerator alloc] initWithError:error |
| 71 initWithLoader:((id<UrlLoader>)mockLoader_.get()) | 64 isPost:NO |
| 72 browserState:GetBrowserState() | 65 isIncognito:NO]); |
| 73 url:errorURL | 66 [errorPageGenerator_.get() generateHtml:^(NSString* html) { |
| 74 error:error | |
| 75 isPost:NO | |
| 76 isIncognito:NO]); | |
| 77 [errorPageContent_.get() generateHtml:^(NSString* html) { | |
| 78 TestHTMLForError(html, @"ERR_CONNECTION_TIMED_OUT"); | 67 TestHTMLForError(html, @"ERR_CONNECTION_TIMED_OUT"); |
| 79 }]; | 68 }]; |
| 80 } | 69 } |
| 81 | 70 |
| 82 // Test with a bad URL scheme (see b/7448754). | 71 // Test with a bad URL scheme (see b/7448754). |
| 83 TEST_F(ErrorPageContentTest, BadURLScheme) { | 72 TEST_F(ErrorPageGeneratorTest, BadURLScheme) { |
| 84 GURL errorURL( | 73 GURL errorURL( |
| 85 "itms-appss://itunes.apple.com/gb/app/google-search/id284815942?mt=8"); | 74 "itms-appss://itunes.apple.com/gb/app/google-search/id284815942?mt=8"); |
| 86 NSError* error = | 75 NSError* error = |
| 87 GenerateError(errorURL, NSURLErrorDomain, kCFURLErrorTimedOut); | 76 GenerateError(errorURL, NSURLErrorDomain, kCFURLErrorTimedOut); |
| 88 errorPageContent_.reset([[ErrorPageContent alloc] | 77 errorPageGenerator_.reset([[ErrorPageGenerator alloc] initWithError:error |
| 89 initWithLoader:((id<UrlLoader>)mockLoader_.get()) | 78 isPost:NO |
| 90 browserState:GetBrowserState() | 79 isIncognito:NO]); |
| 91 url:errorURL | 80 [errorPageGenerator_.get() generateHtml:^(NSString* html) { |
| 92 error:error | |
| 93 isPost:NO | |
| 94 isIncognito:NO]); | |
| 95 [errorPageContent_.get() generateHtml:^(NSString* html) { | |
| 96 TestHTMLForError(html, @"ERR_CONNECTION_TIMED_OUT"); | 81 TestHTMLForError(html, @"ERR_CONNECTION_TIMED_OUT"); |
| 97 }]; | 82 }]; |
| 98 } | 83 } |
| 99 | 84 |
| 100 // Test with an empty GURL object. | 85 // Test with an empty GURL object. |
| 101 // TODO(ios): [merge 191784] b/8525110 fix the code or the test. | 86 // TODO(ios): [merge 191784] b/8525110 fix the code or the test. |
| 102 TEST_F(ErrorPageContentTest, EmptyURLObject) { | 87 TEST_F(ErrorPageGeneratorTest, EmptyURLObject) { |
| 103 GURL errorURL; | 88 GURL errorURL; |
| 104 NSError* error = | 89 NSError* error = |
| 105 GenerateError(errorURL, NSURLErrorDomain, kCFURLErrorTimedOut); | 90 GenerateError(errorURL, NSURLErrorDomain, kCFURLErrorTimedOut); |
| 106 errorPageContent_.reset([[ErrorPageContent alloc] | 91 errorPageGenerator_.reset([[ErrorPageGenerator alloc] initWithError:error |
| 107 initWithLoader:((id<UrlLoader>)mockLoader_.get()) | 92 isPost:NO |
| 108 browserState:GetBrowserState() | 93 isIncognito:NO]); |
| 109 url:errorURL | 94 [errorPageGenerator_.get() generateHtml:^(NSString* html) { |
| 110 error:error | |
| 111 isPost:NO | |
| 112 isIncognito:NO]); | |
| 113 [errorPageContent_.get() generateHtml:^(NSString* html) { | |
| 114 TestHTMLForError(html, @"ERR_CONNECTION_TIMED_OUT"); | 95 TestHTMLForError(html, @"ERR_CONNECTION_TIMED_OUT"); |
| 115 }]; | 96 }]; |
| 116 } | 97 } |
| OLD | NEW |