| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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/web/web_state/ui/crw_web_controller.h" | 5 #import "ios/web/web_state/ui/crw_web_controller.h" |
| 6 | 6 |
| 7 #import <WebKit/WebKit.h> | 7 #import <WebKit/WebKit.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/ios/ios_util.h" | 11 #include "base/ios/ios_util.h" |
| 12 #import "base/ios/weak_nsobject.h" | 12 #include "base/mac/foundation_util.h" |
| 13 #import "base/mac/scoped_nsobject.h" | |
| 14 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 15 #import "base/test/ios/wait_util.h" | 14 #import "base/test/ios/wait_util.h" |
| 16 #import "ios/testing/ocmock_complex_type_helper.h" | 15 #import "ios/testing/ocmock_complex_type_helper.h" |
| 17 #import "ios/web/navigation/crw_session_controller.h" | 16 #import "ios/web/navigation/crw_session_controller.h" |
| 18 #import "ios/web/navigation/navigation_item_impl.h" | 17 #import "ios/web/navigation/navigation_item_impl.h" |
| 19 #import "ios/web/navigation/navigation_manager_impl.h" | 18 #import "ios/web/navigation/navigation_manager_impl.h" |
| 20 #include "ios/web/public/referrer.h" | 19 #include "ios/web/public/referrer.h" |
| 21 #import "ios/web/public/test/fakes/test_native_content.h" | 20 #import "ios/web/public/test/fakes/test_native_content.h" |
| 22 #import "ios/web/public/test/fakes/test_native_content_provider.h" | 21 #import "ios/web/public/test/fakes/test_native_content_provider.h" |
| 23 #import "ios/web/public/test/fakes/test_web_client.h" | 22 #import "ios/web/public/test/fakes/test_web_client.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 42 #include "net/ssl/ssl_info.h" | 41 #include "net/ssl/ssl_info.h" |
| 43 #include "net/test/cert_test_util.h" | 42 #include "net/test/cert_test_util.h" |
| 44 #include "net/test/test_data_directory.h" | 43 #include "net/test/test_data_directory.h" |
| 45 #include "testing/gtest/include/gtest/gtest.h" | 44 #include "testing/gtest/include/gtest/gtest.h" |
| 46 #import "testing/gtest_mac.h" | 45 #import "testing/gtest_mac.h" |
| 47 #include "third_party/ocmock/OCMock/OCMock.h" | 46 #include "third_party/ocmock/OCMock/OCMock.h" |
| 48 #include "third_party/ocmock/gtest_support.h" | 47 #include "third_party/ocmock/gtest_support.h" |
| 49 #include "third_party/ocmock/ocmock_extensions.h" | 48 #include "third_party/ocmock/ocmock_extensions.h" |
| 50 #import "ui/base/test/ios/ui_view_test_utils.h" | 49 #import "ui/base/test/ios/ui_view_test_utils.h" |
| 51 | 50 |
| 51 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 52 #error "This file requires ARC support." |
| 53 #endif |
| 54 |
| 52 using web::NavigationManagerImpl; | 55 using web::NavigationManagerImpl; |
| 53 | 56 |
| 54 @interface CRWWebController (PrivateAPI) | 57 @interface CRWWebController (PrivateAPI) |
| 55 @property(nonatomic, readwrite) web::PageDisplayState pageDisplayState; | 58 @property(nonatomic, readwrite) web::PageDisplayState pageDisplayState; |
| 56 @end | 59 @end |
| 57 | 60 |
| 58 @interface CountingObserver : NSObject<CRWWebControllerObserver> | 61 @interface CountingObserver : NSObject<CRWWebControllerObserver> |
| 59 | 62 |
| 60 @property(nonatomic, readonly) int pageLoadedCount; | 63 @property(nonatomic, readonly) int pageLoadedCount; |
| 61 @end | 64 @end |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 base::test::ios::WaitUntilCondition(^bool() { | 115 base::test::ios::WaitUntilCondition(^bool() { |
| 113 return webController.pageDisplayState.zoom_state() == zoom_state; | 116 return webController.pageDisplayState.zoom_state() == zoom_state; |
| 114 }); | 117 }); |
| 115 } | 118 } |
| 116 | 119 |
| 117 // Test fixture for testing CRWWebController. Stubs out web view. | 120 // Test fixture for testing CRWWebController. Stubs out web view. |
| 118 class CRWWebControllerTest : public web::WebTestWithWebController { | 121 class CRWWebControllerTest : public web::WebTestWithWebController { |
| 119 protected: | 122 protected: |
| 120 void SetUp() override { | 123 void SetUp() override { |
| 121 web::WebTestWithWebController::SetUp(); | 124 web::WebTestWithWebController::SetUp(); |
| 122 mock_web_view_.reset([CreateMockWebView() retain]); | 125 mock_web_view_ = CreateMockWebView(); |
| 123 scroll_view_.reset([[UIScrollView alloc] init]); | 126 scroll_view_ = [[UIScrollView alloc] init]; |
| 124 [[[mock_web_view_ stub] andReturn:scroll_view_.get()] scrollView]; | 127 [[[mock_web_view_ stub] andReturn:scroll_view_] scrollView]; |
| 125 | 128 |
| 126 base::scoped_nsobject<TestWebViewContentView> webViewContentView( | 129 TestWebViewContentView* web_view_content_view = |
| 127 [[TestWebViewContentView alloc] initWithMockWebView:mock_web_view_ | 130 [[TestWebViewContentView alloc] initWithMockWebView:mock_web_view_ |
| 128 scrollView:scroll_view_]); | 131 scrollView:scroll_view_]; |
| 129 [web_controller() injectWebViewContentView:webViewContentView]; | 132 [web_controller() injectWebViewContentView:web_view_content_view]; |
| 130 } | 133 } |
| 131 | 134 |
| 132 void TearDown() override { | 135 void TearDown() override { |
| 133 EXPECT_OCMOCK_VERIFY(mock_web_view_); | 136 EXPECT_OCMOCK_VERIFY(mock_web_view_); |
| 134 [web_controller() resetInjectedWebViewContentView]; | 137 [web_controller() resetInjectedWebViewContentView]; |
| 135 web::WebTestWithWebController::TearDown(); | 138 web::WebTestWithWebController::TearDown(); |
| 136 } | 139 } |
| 137 | 140 |
| 138 // The value for web view OCMock objects to expect for |-setFrame:|. | 141 // The value for web view OCMock objects to expect for |-setFrame:|. |
| 139 CGRect GetExpectedWebViewFrame() const { | 142 CGRect GetExpectedWebViewFrame() const { |
| 140 CGSize container_view_size = [UIScreen mainScreen].bounds.size; | 143 CGSize container_view_size = [UIScreen mainScreen].bounds.size; |
| 141 container_view_size.height -= | 144 container_view_size.height -= |
| 142 CGRectGetHeight([UIApplication sharedApplication].statusBarFrame); | 145 CGRectGetHeight([UIApplication sharedApplication].statusBarFrame); |
| 143 return {CGPointZero, container_view_size}; | 146 return {CGPointZero, container_view_size}; |
| 144 } | 147 } |
| 145 | 148 |
| 146 // Creates WebView mock. | 149 // Creates WebView mock. |
| 147 UIView* CreateMockWebView() { | 150 UIView* CreateMockWebView() { |
| 148 id result = [OCMockObject mockForClass:[WKWebView class]]; | 151 id result = [OCMockObject mockForClass:[WKWebView class]]; |
| 149 | 152 |
| 150 if (base::ios::IsRunningOnIOS10OrLater()) { | 153 if (base::ios::IsRunningOnIOS10OrLater()) { |
| 151 [[result stub] serverTrust]; | 154 [[result stub] serverTrust]; |
| 152 } else { | 155 } else { |
| 153 [[result stub] certificateChain]; | 156 [[result stub] certificateChain]; |
| 154 } | 157 } |
| 155 | 158 |
| 156 [[result stub] backForwardList]; | 159 [[result stub] backForwardList]; |
| 157 [[[result stub] andReturn:[NSURL URLWithString:@(kTestURLString)]] URL]; | 160 [[[result stub] andReturn:[NSURL URLWithString:@(kTestURLString)]] URL]; |
| 158 [[result stub] setNavigationDelegate:[OCMArg checkWithBlock:^(id delegate) { | 161 [[result stub] setNavigationDelegate:[OCMArg checkWithBlock:^(id delegate) { |
| 159 navigation_delegate_.reset(delegate); | 162 navigation_delegate_ = delegate; |
| 160 return YES; | 163 return YES; |
| 161 }]]; | 164 }]]; |
| 162 [[result stub] setUIDelegate:OCMOCK_ANY]; | 165 [[result stub] setUIDelegate:OCMOCK_ANY]; |
| 163 [[result stub] setFrame:GetExpectedWebViewFrame()]; | 166 [[result stub] setFrame:GetExpectedWebViewFrame()]; |
| 164 [[result stub] addObserver:web_controller() | 167 [[result stub] addObserver:web_controller() |
| 165 forKeyPath:OCMOCK_ANY | 168 forKeyPath:OCMOCK_ANY |
| 166 options:0 | 169 options:0 |
| 167 context:nullptr]; | 170 context:nullptr]; |
| 168 [[result stub] removeObserver:web_controller() forKeyPath:OCMOCK_ANY]; | 171 [[result stub] removeObserver:web_controller() forKeyPath:OCMOCK_ANY]; |
| 169 | 172 |
| 170 return result; | 173 return result; |
| 171 } | 174 } |
| 172 | 175 |
| 173 base::WeakNSProtocol<id<WKNavigationDelegate>> navigation_delegate_; | 176 __weak id<WKNavigationDelegate> navigation_delegate_; |
| 174 base::scoped_nsobject<UIScrollView> scroll_view_; | 177 UIScrollView* scroll_view_; |
| 175 base::scoped_nsobject<id> mock_web_view_; | 178 id mock_web_view_; |
| 176 }; | 179 }; |
| 177 | 180 |
| 178 // Tests that AllowCertificateError is called with correct arguments if | 181 // Tests that AllowCertificateError is called with correct arguments if |
| 179 // WKWebView fails to load a page with bad SSL cert. | 182 // WKWebView fails to load a page with bad SSL cert. |
| 180 TEST_F(CRWWebControllerTest, SslCertError) { | 183 TEST_F(CRWWebControllerTest, SslCertError) { |
| 181 web::TestWebStateObserver observer(web_state()); | 184 web::TestWebStateObserver observer(web_state()); |
| 182 ASSERT_FALSE(observer.did_change_visible_security_state_info()); | 185 ASSERT_FALSE(observer.did_change_visible_security_state_info()); |
| 183 | 186 |
| 184 // Last arguments passed to AllowCertificateError must be in default state. | 187 // Last arguments passed to AllowCertificateError must be in default state. |
| 185 ASSERT_FALSE(GetWebClient()->last_cert_error_code()); | 188 ASSERT_FALSE(GetWebClient()->last_cert_error_code()); |
| 186 ASSERT_FALSE(GetWebClient()->last_cert_error_ssl_info().is_valid()); | 189 ASSERT_FALSE(GetWebClient()->last_cert_error_ssl_info().is_valid()); |
| 187 ASSERT_FALSE(GetWebClient()->last_cert_error_ssl_info().cert_status); | 190 ASSERT_FALSE(GetWebClient()->last_cert_error_ssl_info().cert_status); |
| 188 ASSERT_FALSE(GetWebClient()->last_cert_error_request_url().is_valid()); | 191 ASSERT_FALSE(GetWebClient()->last_cert_error_request_url().is_valid()); |
| 189 ASSERT_TRUE(GetWebClient()->last_cert_error_overridable()); | 192 ASSERT_TRUE(GetWebClient()->last_cert_error_overridable()); |
| 190 | 193 |
| 191 scoped_refptr<net::X509Certificate> cert = | 194 scoped_refptr<net::X509Certificate> cert = |
| 192 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); | 195 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); |
| 193 ASSERT_TRUE(cert); | 196 ASSERT_TRUE(cert); |
| 194 base::ScopedCFTypeRef<CFMutableArrayRef> chain( | 197 base::ScopedCFTypeRef<CFMutableArrayRef> chain( |
| 195 net::x509_util::CreateSecCertificateArrayForX509Certificate(cert.get())); | 198 net::x509_util::CreateSecCertificateArrayForX509Certificate(cert.get())); |
| 196 ASSERT_TRUE(chain); | 199 ASSERT_TRUE(chain); |
| 197 | 200 |
| 198 GURL url("https://chromium.test"); | 201 GURL url("https://chromium.test"); |
| 199 NSError* error = | 202 NSError* error = |
| 200 [NSError errorWithDomain:NSURLErrorDomain | 203 [NSError errorWithDomain:NSURLErrorDomain |
| 201 code:NSURLErrorServerCertificateHasUnknownRoot | 204 code:NSURLErrorServerCertificateHasUnknownRoot |
| 202 userInfo:@{ | 205 userInfo:@{ |
| 203 web::kNSErrorPeerCertificateChainKey : | 206 web::kNSErrorPeerCertificateChainKey : |
| 204 static_cast<NSArray*>(chain.get()), | 207 base::mac::CFToNSCast(chain.get()), |
| 205 web::kNSErrorFailingURLKey : net::NSURLWithGURL(url), | 208 web::kNSErrorFailingURLKey : net::NSURLWithGURL(url), |
| 206 }]; | 209 }]; |
| 207 base::scoped_nsobject<NSObject> navigation([[NSObject alloc] init]); | 210 NSObject* navigation = [[NSObject alloc] init]; |
| 208 [navigation_delegate_ webView:mock_web_view_ | 211 [navigation_delegate_ webView:mock_web_view_ |
| 209 didStartProvisionalNavigation:static_cast<WKNavigation*>(navigation)]; | 212 didStartProvisionalNavigation:static_cast<WKNavigation*>(navigation)]; |
| 210 [navigation_delegate_ webView:mock_web_view_ | 213 [navigation_delegate_ webView:mock_web_view_ |
| 211 didFailProvisionalNavigation:static_cast<WKNavigation*>(navigation) | 214 didFailProvisionalNavigation:static_cast<WKNavigation*>(navigation) |
| 212 withError:error]; | 215 withError:error]; |
| 213 | 216 |
| 214 // Verify correctness of AllowCertificateError method call. | 217 // Verify correctness of AllowCertificateError method call. |
| 215 EXPECT_EQ(net::ERR_CERT_INVALID, GetWebClient()->last_cert_error_code()); | 218 EXPECT_EQ(net::ERR_CERT_INVALID, GetWebClient()->last_cert_error_code()); |
| 216 EXPECT_TRUE(GetWebClient()->last_cert_error_ssl_info().is_valid()); | 219 EXPECT_TRUE(GetWebClient()->last_cert_error_ssl_info().is_valid()); |
| 217 EXPECT_EQ(net::CERT_STATUS_INVALID, | 220 EXPECT_EQ(net::CERT_STATUS_INVALID, |
| (...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 642 | 645 |
| 643 EXPECT_EQ(GURL(kTestURLString), url); | 646 EXPECT_EQ(GURL(kTestURLString), url); |
| 644 EXPECT_EQ(web::kAbsolute, trust_level); | 647 EXPECT_EQ(web::kAbsolute, trust_level); |
| 645 } | 648 } |
| 646 | 649 |
| 647 // Test fixture for testing CRWWebController presenting native content. | 650 // Test fixture for testing CRWWebController presenting native content. |
| 648 class CRWWebControllerNativeContentTest : public web::WebTestWithWebController { | 651 class CRWWebControllerNativeContentTest : public web::WebTestWithWebController { |
| 649 protected: | 652 protected: |
| 650 void SetUp() override { | 653 void SetUp() override { |
| 651 web::WebTestWithWebController::SetUp(); | 654 web::WebTestWithWebController::SetUp(); |
| 652 mock_native_provider_.reset([[TestNativeContentProvider alloc] init]); | 655 mock_native_provider_ = [[TestNativeContentProvider alloc] init]; |
| 653 [web_controller() setNativeProvider:mock_native_provider_]; | 656 [web_controller() setNativeProvider:mock_native_provider_]; |
| 654 } | 657 } |
| 655 | 658 |
| 656 void Load(const GURL& URL) { | 659 void Load(const GURL& URL) { |
| 657 NavigationManagerImpl& navigation_manager = | 660 NavigationManagerImpl& navigation_manager = |
| 658 [web_controller() webStateImpl]->GetNavigationManagerImpl(); | 661 [web_controller() webStateImpl]->GetNavigationManagerImpl(); |
| 659 navigation_manager.AddPendingItem( | 662 navigation_manager.AddPendingItem( |
| 660 URL, web::Referrer(), ui::PAGE_TRANSITION_TYPED, | 663 URL, web::Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 661 web::NavigationInitiationType::USER_INITIATED, | 664 web::NavigationInitiationType::USER_INITIATED, |
| 662 web::NavigationManager::UserAgentOverrideOption::INHERIT); | 665 web::NavigationManager::UserAgentOverrideOption::INHERIT); |
| 663 [web_controller() loadCurrentURL]; | 666 [web_controller() loadCurrentURL]; |
| 664 } | 667 } |
| 665 | 668 |
| 666 base::scoped_nsobject<TestNativeContentProvider> mock_native_provider_; | 669 TestNativeContentProvider* mock_native_provider_; |
| 667 }; | 670 }; |
| 668 | 671 |
| 669 // Tests WebState and NavigationManager correctly return native content URL. | 672 // Tests WebState and NavigationManager correctly return native content URL. |
| 670 TEST_F(CRWWebControllerNativeContentTest, NativeContentURL) { | 673 TEST_F(CRWWebControllerNativeContentTest, NativeContentURL) { |
| 671 GURL url_to_load(kTestAppSpecificURL); | 674 GURL url_to_load(kTestAppSpecificURL); |
| 672 base::scoped_nsobject<TestNativeContent> content( | 675 TestNativeContent* content = |
| 673 [[TestNativeContent alloc] initWithURL:url_to_load virtualURL:GURL()]); | 676 [[TestNativeContent alloc] initWithURL:url_to_load virtualURL:GURL()]; |
| 674 [mock_native_provider_ setController:content forURL:url_to_load]; | 677 [mock_native_provider_ setController:content forURL:url_to_load]; |
| 675 Load(url_to_load); | 678 Load(url_to_load); |
| 676 web::URLVerificationTrustLevel trust_level = web::kNone; | 679 web::URLVerificationTrustLevel trust_level = web::kNone; |
| 677 GURL gurl = [web_controller() currentURLWithTrustLevel:&trust_level]; | 680 GURL gurl = [web_controller() currentURLWithTrustLevel:&trust_level]; |
| 678 EXPECT_EQ(gurl, url_to_load); | 681 EXPECT_EQ(gurl, url_to_load); |
| 679 EXPECT_EQ(web::kAbsolute, trust_level); | 682 EXPECT_EQ(web::kAbsolute, trust_level); |
| 680 EXPECT_EQ([web_controller() webState]->GetVisibleURL(), url_to_load); | 683 EXPECT_EQ([web_controller() webState]->GetVisibleURL(), url_to_load); |
| 681 NavigationManagerImpl& navigationManager = | 684 NavigationManagerImpl& navigationManager = |
| 682 [web_controller() webStateImpl]->GetNavigationManagerImpl(); | 685 [web_controller() webStateImpl]->GetNavigationManagerImpl(); |
| 683 EXPECT_EQ(navigationManager.GetVisibleItem()->GetURL(), url_to_load); | 686 EXPECT_EQ(navigationManager.GetVisibleItem()->GetURL(), url_to_load); |
| 684 EXPECT_EQ(navigationManager.GetVisibleItem()->GetVirtualURL(), url_to_load); | 687 EXPECT_EQ(navigationManager.GetVisibleItem()->GetVirtualURL(), url_to_load); |
| 685 EXPECT_EQ(navigationManager.GetLastCommittedItem()->GetURL(), url_to_load); | 688 EXPECT_EQ(navigationManager.GetLastCommittedItem()->GetURL(), url_to_load); |
| 686 EXPECT_EQ(navigationManager.GetLastCommittedItem()->GetVirtualURL(), | 689 EXPECT_EQ(navigationManager.GetLastCommittedItem()->GetVirtualURL(), |
| 687 url_to_load); | 690 url_to_load); |
| 688 } | 691 } |
| 689 | 692 |
| 690 // Tests WebState and NavigationManager correctly return native content URL and | 693 // Tests WebState and NavigationManager correctly return native content URL and |
| 691 // VirtualURL | 694 // VirtualURL |
| 692 TEST_F(CRWWebControllerNativeContentTest, NativeContentVirtualURL) { | 695 TEST_F(CRWWebControllerNativeContentTest, NativeContentVirtualURL) { |
| 693 GURL url_to_load(kTestAppSpecificURL); | 696 GURL url_to_load(kTestAppSpecificURL); |
| 694 GURL virtual_url(kTestURLString); | 697 GURL virtual_url(kTestURLString); |
| 695 base::scoped_nsobject<TestNativeContent> content([[TestNativeContent alloc] | 698 TestNativeContent* content = |
| 696 initWithURL:virtual_url | 699 [[TestNativeContent alloc] initWithURL:virtual_url |
| 697 virtualURL:virtual_url]); | 700 virtualURL:virtual_url]; |
| 698 [mock_native_provider_ setController:content forURL:url_to_load]; | 701 [mock_native_provider_ setController:content forURL:url_to_load]; |
| 699 Load(url_to_load); | 702 Load(url_to_load); |
| 700 web::URLVerificationTrustLevel trust_level = web::kNone; | 703 web::URLVerificationTrustLevel trust_level = web::kNone; |
| 701 GURL gurl = [web_controller() currentURLWithTrustLevel:&trust_level]; | 704 GURL gurl = [web_controller() currentURLWithTrustLevel:&trust_level]; |
| 702 EXPECT_EQ(gurl, virtual_url); | 705 EXPECT_EQ(gurl, virtual_url); |
| 703 EXPECT_EQ(web::kAbsolute, trust_level); | 706 EXPECT_EQ(web::kAbsolute, trust_level); |
| 704 EXPECT_EQ([web_controller() webState]->GetVisibleURL(), virtual_url); | 707 EXPECT_EQ([web_controller() webState]->GetVisibleURL(), virtual_url); |
| 705 NavigationManagerImpl& navigationManager = | 708 NavigationManagerImpl& navigationManager = |
| 706 [web_controller() webStateImpl]->GetNavigationManagerImpl(); | 709 [web_controller() webStateImpl]->GetNavigationManagerImpl(); |
| 707 EXPECT_EQ(navigationManager.GetVisibleItem()->GetURL(), url_to_load); | 710 EXPECT_EQ(navigationManager.GetVisibleItem()->GetURL(), url_to_load); |
| 708 EXPECT_EQ(navigationManager.GetVisibleItem()->GetVirtualURL(), virtual_url); | 711 EXPECT_EQ(navigationManager.GetVisibleItem()->GetVirtualURL(), virtual_url); |
| 709 EXPECT_EQ(navigationManager.GetLastCommittedItem()->GetURL(), url_to_load); | 712 EXPECT_EQ(navigationManager.GetLastCommittedItem()->GetURL(), url_to_load); |
| 710 EXPECT_EQ(navigationManager.GetLastCommittedItem()->GetVirtualURL(), | 713 EXPECT_EQ(navigationManager.GetLastCommittedItem()->GetVirtualURL(), |
| 711 virtual_url); | 714 virtual_url); |
| 712 } | 715 } |
| 713 | 716 |
| 714 // A separate test class, as none of the |CRWUIWebViewWebControllerTest| setup | 717 // A separate test class, as none of the |CRWUIWebViewWebControllerTest| setup |
| 715 // is needed; | 718 // is needed; |
| 716 typedef web::WebTestWithWebController CRWWebControllerObserversTest; | 719 typedef web::WebTestWithWebController CRWWebControllerObserversTest; |
| 717 | 720 |
| 718 // Tests that CRWWebControllerObservers are called. | 721 // Tests that CRWWebControllerObservers are called. |
| 719 TEST_F(CRWWebControllerObserversTest, Observers) { | 722 TEST_F(CRWWebControllerObserversTest, Observers) { |
| 720 base::scoped_nsobject<CountingObserver> observer( | 723 CountingObserver* observer = [[CountingObserver alloc] init]; |
| 721 [[CountingObserver alloc] init]); | |
| 722 EXPECT_EQ(0u, [web_controller() observerCount]); | 724 EXPECT_EQ(0u, [web_controller() observerCount]); |
| 723 [web_controller() addObserver:observer]; | 725 [web_controller() addObserver:observer]; |
| 724 EXPECT_EQ(1u, [web_controller() observerCount]); | 726 EXPECT_EQ(1u, [web_controller() observerCount]); |
| 725 | 727 |
| 726 EXPECT_EQ(0, [observer pageLoadedCount]); | 728 EXPECT_EQ(0, [observer pageLoadedCount]); |
| 727 [web_controller() webStateImpl]->OnPageLoaded(GURL("http://test"), false); | 729 [web_controller() webStateImpl]->OnPageLoaded(GURL("http://test"), false); |
| 728 EXPECT_EQ(0, [observer pageLoadedCount]); | 730 EXPECT_EQ(0, [observer pageLoadedCount]); |
| 729 [web_controller() webStateImpl]->OnPageLoaded(GURL("http://test"), true); | 731 [web_controller() webStateImpl]->OnPageLoaded(GURL("http://test"), true); |
| 730 EXPECT_EQ(1, [observer pageLoadedCount]); | 732 EXPECT_EQ(1, [observer pageLoadedCount]); |
| 731 | 733 |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 869 protected: | 871 protected: |
| 870 // Calls |executeUserJavaScript:completionHandler:|, waits for script | 872 // Calls |executeUserJavaScript:completionHandler:|, waits for script |
| 871 // execution completion, and synchronously returns the result. | 873 // execution completion, and synchronously returns the result. |
| 872 id ExecuteUserJavaScript(NSString* java_script, NSError** error) { | 874 id ExecuteUserJavaScript(NSString* java_script, NSError** error) { |
| 873 __block id script_result = nil; | 875 __block id script_result = nil; |
| 874 __block NSError* script_error = nil; | 876 __block NSError* script_error = nil; |
| 875 __block bool script_executed = false; | 877 __block bool script_executed = false; |
| 876 [web_controller() | 878 [web_controller() |
| 877 executeUserJavaScript:java_script | 879 executeUserJavaScript:java_script |
| 878 completionHandler:^(id local_result, NSError* local_error) { | 880 completionHandler:^(id local_result, NSError* local_error) { |
| 879 script_result = [local_result retain]; | 881 script_result = local_result; |
| 880 script_error = [local_error retain]; | 882 script_error = local_error; |
| 881 script_executed = true; | 883 script_executed = true; |
| 882 }]; | 884 }]; |
| 883 | 885 |
| 884 WaitForCondition(^{ | 886 WaitForCondition(^{ |
| 885 return script_executed; | 887 return script_executed; |
| 886 }); | 888 }); |
| 887 | 889 |
| 888 if (error) { | 890 if (error) { |
| 889 *error = script_error; | 891 *error = script_error; |
| 890 } | 892 } |
| 891 [script_error autorelease]; | 893 return script_result; |
| 892 return [script_result autorelease]; | |
| 893 } | 894 } |
| 894 }; | 895 }; |
| 895 | 896 |
| 896 // Tests evaluating user script on an http page. | 897 // Tests evaluating user script on an http page. |
| 897 TEST_F(ScriptExecutionTest, UserScriptOnHttpPage) { | 898 TEST_F(ScriptExecutionTest, UserScriptOnHttpPage) { |
| 898 LoadHtml(@"<html></html>", GURL(kTestURLString)); | 899 LoadHtml(@"<html></html>", GURL(kTestURLString)); |
| 899 NSError* error = nil; | 900 NSError* error = nil; |
| 900 EXPECT_NSEQ(@0, ExecuteUserJavaScript(@"window.w = 0;", &error)); | 901 EXPECT_NSEQ(@0, ExecuteUserJavaScript(@"window.w = 0;", &error)); |
| 901 EXPECT_FALSE(error); | 902 EXPECT_FALSE(error); |
| 902 | 903 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 925 EXPECT_EQ(web::JS_EVALUATION_ERROR_CODE_NO_WEB_VIEW, error.code); | 926 EXPECT_EQ(web::JS_EVALUATION_ERROR_CODE_NO_WEB_VIEW, error.code); |
| 926 | 927 |
| 927 EXPECT_FALSE(ExecuteJavaScript(@"window.w")); | 928 EXPECT_FALSE(ExecuteJavaScript(@"window.w")); |
| 928 }; | 929 }; |
| 929 | 930 |
| 930 // Fixture class to test WKWebView crashes. | 931 // Fixture class to test WKWebView crashes. |
| 931 class CRWWebControllerWebProcessTest : public web::WebTestWithWebController { | 932 class CRWWebControllerWebProcessTest : public web::WebTestWithWebController { |
| 932 protected: | 933 protected: |
| 933 void SetUp() override { | 934 void SetUp() override { |
| 934 web::WebTestWithWebController::SetUp(); | 935 web::WebTestWithWebController::SetUp(); |
| 935 webView_.reset([web::BuildTerminatedWKWebView() retain]); | 936 webView_ = web::BuildTerminatedWKWebView(); |
| 936 base::scoped_nsobject<TestWebViewContentView> webViewContentView( | 937 TestWebViewContentView* webViewContentView = [[TestWebViewContentView alloc] |
| 937 [[TestWebViewContentView alloc] | 938 initWithMockWebView:webView_ |
| 938 initWithMockWebView:webView_ | 939 scrollView:[webView_ scrollView]]; |
| 939 scrollView:[webView_ scrollView]]); | |
| 940 [web_controller() injectWebViewContentView:webViewContentView]; | 940 [web_controller() injectWebViewContentView:webViewContentView]; |
| 941 | 941 |
| 942 // This test intentionally crashes the render process. | 942 // This test intentionally crashes the render process. |
| 943 SetIgnoreRenderProcessCrashesDuringTesting(true); | 943 SetIgnoreRenderProcessCrashesDuringTesting(true); |
| 944 } | 944 } |
| 945 base::scoped_nsobject<WKWebView> webView_; | 945 WKWebView* webView_; |
| 946 }; | 946 }; |
| 947 | 947 |
| 948 // Tests that WebStateDelegate::RenderProcessGone is called when WKWebView web | 948 // Tests that WebStateDelegate::RenderProcessGone is called when WKWebView web |
| 949 // process has crashed. | 949 // process has crashed. |
| 950 TEST_F(CRWWebControllerWebProcessTest, Crash) { | 950 TEST_F(CRWWebControllerWebProcessTest, Crash) { |
| 951 web::TestWebStateObserver observer(web_state()); | 951 web::TestWebStateObserver observer(web_state()); |
| 952 web::TestWebStateObserver* observer_ptr = &observer; | 952 web::TestWebStateObserver* observer_ptr = &observer; |
| 953 web::SimulateWKWebViewCrash(webView_); | 953 web::SimulateWKWebViewCrash(webView_); |
| 954 base::test::ios::WaitUntilCondition(^bool() { | 954 base::test::ios::WaitUntilCondition(^bool() { |
| 955 return observer_ptr->render_process_gone_info(); | 955 return observer_ptr->render_process_gone_info(); |
| 956 }); | 956 }); |
| 957 EXPECT_EQ(web_state(), observer.render_process_gone_info()->web_state); | 957 EXPECT_EQ(web_state(), observer.render_process_gone_info()->web_state); |
| 958 EXPECT_FALSE([web_controller() isViewAlive]); | 958 EXPECT_FALSE([web_controller() isViewAlive]); |
| 959 }; | 959 }; |
| 960 | 960 |
| 961 } // namespace | 961 } // namespace |
| OLD | NEW |