diff options
author | stuartmorgan <stuartmorgan@chromium.org> | 2015-05-07 20:14:38 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-05-08 03:15:09 +0000 |
commit | 275e50d9fa019a4155034420c865df7f2afcb0c9 (patch) | |
tree | 404dcb88bb284f0966fe93ae183226ed62521952 /ios | |
parent | a4100f62cf4820807390364d7dfe2c13e096ce71 (diff) | |
download | chromium_src-275e50d9fa019a4155034420c865df7f2afcb0c9.zip chromium_src-275e50d9fa019a4155034420c865df7f2afcb0c9.tar.gz chromium_src-275e50d9fa019a4155034420c865df7f2afcb0c9.tar.bz2 |
Upstream remaining ios/web unit tests
This upstreams and enables all the remaining unit tests in ios/web.
Also upstreams the web controller factory that was missed in a previous
CL; this completes the web upstreaming outside of app and web_shell.
BUG=464810
Review URL: https://codereview.chromium.org/1133573003
Cr-Commit-Position: refs/heads/master@{#328913}
Diffstat (limited to 'ios')
-rw-r--r-- | ios/web/ios_web.gyp | 2 | ||||
-rw-r--r-- | ios/web/ios_web_unittests.gyp | 7 | ||||
-rw-r--r-- | ios/web/net/clients/crw_csp_network_client_unittest.mm | 129 | ||||
-rw-r--r-- | ios/web/net/crw_url_verifying_protocol_handler_unittest.mm | 22 | ||||
-rw-r--r-- | ios/web/public/web_controller_factory.h | 35 | ||||
-rw-r--r-- | ios/web/public/web_controller_factory.mm | 36 | ||||
-rw-r--r-- | ios/web/public/web_state/js/crw_js_window_id_manager_unittest.mm | 60 |
7 files changed, 291 insertions, 0 deletions
diff --git a/ios/web/ios_web.gyp b/ios/web/ios_web.gyp index ae47fc5..b06beed69 100644 --- a/ios/web/ios_web.gyp +++ b/ios/web/ios_web.gyp @@ -121,6 +121,8 @@ 'public/web/url_data_source_ios.h', 'public/web_client.h', 'public/web_client.mm', + 'public/web_controller_factory.h', + 'public/web_controller_factory.mm', 'public/web_state/credential.h', 'public/web_state/crw_native_content.h', 'public/web_state/crw_native_content_provider.h', diff --git a/ios/web/ios_web_unittests.gyp b/ios/web/ios_web_unittests.gyp index b1e9d46..ad006e9 100644 --- a/ios/web/ios_web_unittests.gyp +++ b/ios/web/ios_web_unittests.gyp @@ -12,6 +12,7 @@ 'dependencies': [ '../../base/base.gyp:base', '../../base/base.gyp:test_support_base', + '../../net/net.gyp:net_test_support', '../../testing/gmock.gyp:gmock', '../../testing/gtest.gyp:gtest', '../../third_party/ocmock/ocmock.gyp:ocmock', @@ -25,16 +26,22 @@ 'browser_state_unittest.cc', 'crw_network_activity_indicator_manager_unittest.mm', 'history_state_util_unittest.mm', + 'navigation/crw_session_controller_unittest.mm', + 'navigation/crw_session_entry_unittest.mm', 'navigation/navigation_item_impl_unittest.mm', + 'navigation/navigation_manager_impl_unittest.mm', 'navigation/nscoder_util_unittest.mm', 'net/cert_policy_unittest.cc', + 'net/clients/crw_csp_network_client_unittest.mm', 'net/clients/crw_js_injection_network_client_unittest.mm', 'net/clients/crw_passkit_network_client_unittest.mm', + 'net/crw_url_verifying_protocol_handler_unittest.mm', 'net/request_group_util_unittest.mm', 'net/request_tracker_impl_unittest.mm', 'net/web_http_protocol_handler_delegate_unittest.mm', 'public/referrer_util_unittest.cc', 'public/test/http_server_unittest.mm', + 'public/web_state/js/crw_js_window_id_manager_unittest.mm', 'string_util_unittest.cc', 'test/crw_fake_web_controller_observer_unittest.mm', 'test/run_all_unittests.cc', diff --git a/ios/web/net/clients/crw_csp_network_client_unittest.mm b/ios/web/net/clients/crw_csp_network_client_unittest.mm new file mode 100644 index 0000000..29c5123 --- /dev/null +++ b/ios/web/net/clients/crw_csp_network_client_unittest.mm @@ -0,0 +1,129 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "ios/web/net/clients/crw_csp_network_client.h" + +#import <Foundation/Foundation.h> + +#include "base/mac/scoped_nsobject.h" +#import "ios/net/clients/crn_forwarding_network_client.h" +#import "ios/web/net/crw_url_verifying_protocol_handler.h" +#include "ios/web/public/test/test_web_thread_bundle.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/gtest_mac.h" + +@interface CRWCspMockClient : CRNForwardingNetworkClient +@end + +@implementation CRWCspMockClient { + base::scoped_nsobject<NSURLResponse> _response; +} + +- (void)didReceiveResponse:(NSURLResponse*)response { + _response.reset([response retain]); +} + +- (NSURLResponse*)response { + return _response; +} + +@end + +class CRWCspNetworkClientTest : public testing::Test { + public: + CRWCspNetworkClientTest() { + mock_client_.reset([[CRWCspMockClient alloc] init]); + csp_client_.reset([[CRWCspNetworkClient alloc] init]); + [csp_client_ setUnderlyingClient:mock_client_]; + } + + web::TestWebThreadBundle thread_bundle_; + base::scoped_nsobject<CRWCspNetworkClient> csp_client_; + base::scoped_nsobject<CRWCspMockClient> mock_client_; +}; + +TEST_F(CRWCspNetworkClientTest, FixCspHeaders) { + base::scoped_nsobject<NSDictionary> input_headers([@{ + @"Foo" : @"Bar", + @"coNteNt-seCuRity-POLicy" : @"coNNect-sRc foo.com; script-src 'self'", + @"X-WebKit-CSP" : @"frame-src 'self'" + } retain]); + + base::scoped_nsobject<NSHTTPURLResponse> input_response( + [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"http://foo"] + statusCode:200 + HTTPVersion:@"HTTP/1.1" + headerFields:input_headers]); + + [csp_client_ didReceiveResponse:input_response]; + ASSERT_TRUE( + [[mock_client_ response] isKindOfClass:[NSHTTPURLResponse class]]); + base::scoped_nsobject<NSDictionary> output_headers( + [[static_cast<NSHTTPURLResponse*>([mock_client_ response]) + allHeaderFields] retain]); + + // Check that unrelated headers are copied. + EXPECT_NSEQ(@"Bar", [output_headers objectForKey:@"Foo"]); + + base::scoped_nsobject<NSString> csp_header( + [[output_headers objectForKey:@"coNteNt-seCuRity-POLicy"] retain]); + + EXPECT_TRUE(csp_header.get()); + + // frame-src is not created because there were no frame-src and no + // default-src. + // 'self' and |kURLForVerification| are prepended to the connect-src value. + NSString* expected_csp_header = [NSString stringWithFormat: + @"coNNect-sRc 'self' %s foo.com; " @"script-src 'self'", + web::kURLForVerification]; + EXPECT_NSEQ(expected_csp_header, csp_header); + + // X-WebKit-CSP is handled as well. + // crwebinvoke: crwebinvokeimmediate: and crwebnull: are prepended to the + // existing frame-src value. + // connect-src is not created because there were no connect-src and no + // default-src. + csp_header.reset([[output_headers objectForKey:@"X-WebKit-CSP"] retain]); + EXPECT_NSEQ(@"frame-src crwebinvoke: crwebinvokeimmediate: crwebnull: 'self'", + csp_header); +} + +TEST_F(CRWCspNetworkClientTest, FixCspHeadersWithDefault) { + base::scoped_nsobject<NSDictionary> input_headers([@{ + @"Content-Security-Policy" : @"default-src foo.com; connect-src *" + } retain]); + + base::scoped_nsobject<NSHTTPURLResponse> input_response( + [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"http://foo"] + statusCode:200 + HTTPVersion:@"HTTP/1.1" + headerFields:input_headers]); + + [csp_client_ didReceiveResponse:input_response]; + ASSERT_TRUE( + [[mock_client_ response] isKindOfClass:[NSHTTPURLResponse class]]); + base::scoped_nsobject<NSDictionary> output_headers( + [[static_cast<NSHTTPURLResponse*>([mock_client_ response]) + allHeaderFields] retain]); + + base::scoped_nsobject<NSString> csp_header( + [[output_headers objectForKey:@"Content-Security-Policy"] retain]); + + EXPECT_TRUE(csp_header.get()); + + // crwebinvoke: crwebinvokeimmediate: and crwebnull: are prepended to the + // existing default-src value because there was no frame-src. + // 'self' and |kURLForVerification| are prepended to the connect-src value. + NSString* expected_csp_header = [NSString stringWithFormat: + @"default-src crwebinvoke: crwebinvokeimmediate: crwebnull: foo.com; " + @"connect-src 'self' %s *", web::kURLForVerification]; + EXPECT_NSEQ(expected_csp_header, csp_header); +} + +TEST_F(CRWCspNetworkClientTest, NonHTTPResponse) { + base::scoped_nsobject<NSURLResponse> response([[NSURLResponse alloc] init]); + [csp_client_ didReceiveResponse:response]; + // The client is a pass-through, compare the pointers. + EXPECT_EQ(response, [mock_client_ response]); +} diff --git a/ios/web/net/crw_url_verifying_protocol_handler_unittest.mm b/ios/web/net/crw_url_verifying_protocol_handler_unittest.mm new file mode 100644 index 0000000..d3bc835 --- /dev/null +++ b/ios/web/net/crw_url_verifying_protocol_handler_unittest.mm @@ -0,0 +1,22 @@ +// Copyright 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ios/web/net/crw_url_verifying_protocol_handler.h" + +#include "base/memory/scoped_ptr.h" +#import "ios/web/public/test/test_web_client.h" +#include "ios/web/public/web_client.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +// TODO(shreyasv): See if this can use the WebTest test fixture. +TEST(CRWURLVerifyingProtocolHandlerTest, NonLazyInitializer) { + scoped_ptr<web::TestWebClient> test_web_client(new web::TestWebClient()); + web::SetWebClient(test_web_client.get()); + EXPECT_TRUE([CRWURLVerifyingProtocolHandler preInitialize]); + web::SetWebClient(nullptr); +} + +} // namespace diff --git a/ios/web/public/web_controller_factory.h b/ios/web/public/web_controller_factory.h new file mode 100644 index 0000000..48fcf8e --- /dev/null +++ b/ios/web/public/web_controller_factory.h @@ -0,0 +1,35 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef IOS_WEB_PUBLIC_WEB_CONTROLLER_FACTORY_H_ +#define IOS_WEB_PUBLIC_WEB_CONTROLLER_FACTORY_H_ + +#include "base/memory/scoped_ptr.h" +#include "ios/web/public/web_view_type.h" + +@class CRWWebController; + +namespace web { + +class BrowserState; +class WebStateImpl; + +// Returns a new instance of CRWWebViewController. +// Note: Callers are responsible for releasing the returned web controller. +CRWWebController* CreateWebController(WebViewType web_view_type, + scoped_ptr<WebStateImpl> web_state); + +// Returns a new instance of CRWWebViewController. +// Temporary factory method for use in components that require a web controller. +// By requiring only the BrowserState, this eliminates the dependency on +// WebStateImpl from components. +// Note: Callers are responsible for releasing the returned web controller. +// TODO(kkhorimoto): Move factory method to WebState once the ownership of +// WebState and CRWWebController is reversed. +CRWWebController* CreateWebController(WebViewType web_view_type, + BrowserState* browser_state); + +} // namespace web + +#endif // IOS_WEB_PUBLIC_WEB_CONTROLLER_FACTORY_H_ diff --git a/ios/web/public/web_controller_factory.mm b/ios/web/public/web_controller_factory.mm new file mode 100644 index 0000000..fe4641b --- /dev/null +++ b/ios/web/public/web_controller_factory.mm @@ -0,0 +1,36 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "ios/web/public/web_controller_factory.h" + +#include "ios/web/public/browser_state.h" +#import "ios/web/web_state/ui/crw_ui_web_view_web_controller.h" +#import "ios/web/web_state/ui/crw_wk_web_view_web_controller.h" +#include "ios/web/web_state/web_state_impl.h" + +namespace web { + +CRWWebController* CreateWebController(WebViewType web_view_type, + scoped_ptr<WebStateImpl> web_state) { + switch (web_view_type) { + case UI_WEB_VIEW_TYPE: + return + [[CRWUIWebViewWebController alloc] initWithWebState:web_state.Pass()]; + case WK_WEB_VIEW_TYPE: + return + [[CRWWKWebViewWebController alloc] initWithWebState:web_state.Pass()]; + } + NOTREACHED(); + return nil; +} + +CRWWebController* CreateWebController(WebViewType web_view_type, + BrowserState* browser_state) { + DCHECK(browser_state); + scoped_ptr<web::WebStateImpl> web_state(new web::WebStateImpl(browser_state)); + web_state->GetNavigationManagerImpl().InitializeSession(nil, nil, NO, -1); + return CreateWebController(web_view_type, web_state.Pass()); +} + +} // namespace web diff --git a/ios/web/public/web_state/js/crw_js_window_id_manager_unittest.mm b/ios/web/public/web_state/js/crw_js_window_id_manager_unittest.mm new file mode 100644 index 0000000..4abc76a --- /dev/null +++ b/ios/web/public/web_state/js/crw_js_window_id_manager_unittest.mm @@ -0,0 +1,60 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "ios/web/web_state/js/crw_js_window_id_manager.h" + +#include "base/mac/scoped_nsobject.h" +#import "ios/web/public/test/crw_test_js_injection_receiver.h" +#include "ios/web/public/web_client.h" +#import "testing/gtest_mac.h" +#include "testing/platform_test.h" + +namespace { + +class JSWindowIDManagerTest : public PlatformTest { + protected: + void SetUp() override { + PlatformTest::SetUp(); + receiver_.reset([[CRWTestJSInjectionReceiver alloc] init]); + manager_.reset([[CRWJSWindowIdManager alloc] initWithReceiver:receiver_]); + web::SetWebClient(&web_client_); + } + void TearDown() override { + web::SetWebClient(nullptr); + PlatformTest::TearDown(); + } + // Required for CRWJSWindowIdManager creation. + base::scoped_nsobject<CRWTestJSInjectionReceiver> receiver_; + // Testable CRWJSWindowIdManager. + base::scoped_nsobject<CRWJSWindowIdManager> manager_; + // WebClient required for getting early page script, which must be injected + // before CRWJSWindowIdManager. + web::WebClient web_client_; +}; + +// TODO(jyquinn): enable this test (crbug.com/465898). +TEST_F(JSWindowIDManagerTest, DISABLED_WindowID) { + EXPECT_TRUE(manager_.get()); + [manager_ inject]; + NSString* windowID = [manager_ windowId]; + EXPECT_EQ(32U, [windowID length]); + // Inject a second time to check that the ID is different. + [manager_ inject]; + NSString* windowID2 = [manager_ windowId]; + EXPECT_FALSE([windowID isEqualToString:windowID2]); +} + +TEST_F(JSWindowIDManagerTest, WindowIDDifferent) { + [manager_ inject]; + NSString* windowID = [manager_ windowId]; + base::scoped_nsobject<CRWTestJSInjectionReceiver> receiver2( + [[CRWTestJSInjectionReceiver alloc] init]); + base::scoped_nsobject<CRWJSWindowIdManager> manager2( + [[CRWJSWindowIdManager alloc] initWithReceiver:receiver2]); + [manager2 inject]; + NSString* windowID2 = [manager2 windowId]; + EXPECT_NSNE(windowID, windowID2); +} + +} // namespace |