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