| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <WebKit/WebKit.h> | 5 #import <WebKit/WebKit.h> |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #import "base/mac/scoped_nsobject.h" | 10 #import "base/mac/scoped_nsobject.h" |
| 11 #include "base/memory/ptr_util.h" |
| 11 #include "base/test/ios/wait_util.h" | 12 #include "base/test/ios/wait_util.h" |
| 12 #include "ios/web/public/browser_state.h" | 13 #include "ios/web/public/browser_state.h" |
| 13 #import "ios/web/public/test/http_server.h" | 14 #import "ios/web/public/test/http_server.h" |
| 14 #include "ios/web/public/test/response_providers/string_response_provider.h" | 15 #include "ios/web/public/test/response_providers/string_response_provider.h" |
| 15 #import "ios/web/public/web_view_creation_util.h" | 16 #import "ios/web/public/web_view_creation_util.h" |
| 16 #import "ios/web/test/web_int_test.h" | 17 #import "ios/web/test/web_int_test.h" |
| 17 #import "ios/web/web_state/ui/web_view_js_utils.h" | 18 #import "ios/web/web_state/ui/web_view_js_utils.h" |
| 18 #import "net/base/mac/url_conversions.h" | 19 #import "net/base/mac/url_conversions.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "testing/gtest_mac.h" | 21 #include "testing/gtest_mac.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 43 class BrowserStateWebViewPartitionTest : public web::WebIntTest { | 44 class BrowserStateWebViewPartitionTest : public web::WebIntTest { |
| 44 protected: | 45 protected: |
| 45 void SetUp() override { | 46 void SetUp() override { |
| 46 web::WebIntTest::SetUp(); | 47 web::WebIntTest::SetUp(); |
| 47 | 48 |
| 48 otr_browser_state_.SetOffTheRecord(true); | 49 otr_browser_state_.SetOffTheRecord(true); |
| 49 | 50 |
| 50 web::test::HttpServer& server = web::test::HttpServer::GetSharedInstance(); | 51 web::test::HttpServer& server = web::test::HttpServer::GetSharedInstance(); |
| 51 ASSERT_TRUE(server.IsRunning()); | 52 ASSERT_TRUE(server.IsRunning()); |
| 52 | 53 |
| 53 provider_.reset(new web::StringResponseProvider("Hello World")); | 54 auto provider = |
| 54 server.AddResponseProvider(provider_.get()); | 55 base::MakeUnique<web::StringResponseProvider>("Hello World"); |
| 56 provider_ = provider.get(); // Keep a weak copy to allow unregistration. |
| 57 server.AddResponseProvider(std::move(provider)); |
| 55 } | 58 } |
| 56 | 59 |
| 57 void TearDown() override { | 60 void TearDown() override { |
| 58 web::test::HttpServer& server = web::test::HttpServer::GetSharedInstance(); | 61 web::test::HttpServer& server = web::test::HttpServer::GetSharedInstance(); |
| 59 server.RemoveResponseProvider(provider_.release()); | 62 server.RemoveResponseProvider(provider_); |
| 63 provider_ = nullptr; |
| 60 | 64 |
| 61 web::WebIntTest::TearDown(); | 65 web::WebIntTest::TearDown(); |
| 62 } | 66 } |
| 63 | 67 |
| 64 // Runs the given JavaScript on |web_view| and returns the result as a string. | 68 // Runs the given JavaScript on |web_view| and returns the result as a string. |
| 65 NSString* EvaluateJavaScript(WKWebView* web_view, | 69 NSString* EvaluateJavaScript(WKWebView* web_view, |
| 66 NSString* js) { | 70 NSString* js) { |
| 67 __block base::scoped_nsobject<NSString> result; | 71 __block base::scoped_nsobject<NSString> result; |
| 68 __block bool block_was_called = false; | 72 __block bool block_was_called = false; |
| 69 web::EvaluateJavaScript(web_view, js, ^(NSString* js_result, NSError*) { | 73 web::EvaluateJavaScript(web_view, js, ^(NSString* js_result, NSError*) { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 }); | 131 }); |
| 128 | 132 |
| 129 web_view.navigationDelegate = old_navigation_delegate; | 133 web_view.navigationDelegate = old_navigation_delegate; |
| 130 } | 134 } |
| 131 | 135 |
| 132 // Returns the BrowserState that is used for testing. | 136 // Returns the BrowserState that is used for testing. |
| 133 web::BrowserState* GetOtrBrowserState() { return &otr_browser_state_; } | 137 web::BrowserState* GetOtrBrowserState() { return &otr_browser_state_; } |
| 134 | 138 |
| 135 private: | 139 private: |
| 136 // The ResponseProvider used to load a simple web page. | 140 // The ResponseProvider used to load a simple web page. |
| 137 std::unique_ptr<web::ResponseProvider> provider_; | 141 web::ResponseProvider* provider_; |
| 138 // The OTR browser state used in tests. | 142 // The OTR browser state used in tests. |
| 139 web::TestBrowserState otr_browser_state_; | 143 web::TestBrowserState otr_browser_state_; |
| 140 }; | 144 }; |
| 141 | 145 |
| 142 // Tests that cookies are partitioned between web views created with a | 146 // Tests that cookies are partitioned between web views created with a |
| 143 // non-OTR BrowserState and an OTR BrowserState. | 147 // non-OTR BrowserState and an OTR BrowserState. |
| 144 TEST_F(BrowserStateWebViewPartitionTest, Cookies) { | 148 TEST_F(BrowserStateWebViewPartitionTest, Cookies) { |
| 145 base::scoped_nsobject<WKWebView> web_view_1( | 149 base::scoped_nsobject<WKWebView> web_view_1( |
| 146 web::CreateWKWebView(CGRectZero, GetBrowserState())); | 150 web::CreateWKWebView(CGRectZero, GetBrowserState())); |
| 147 LoadTestWebPage(web_view_1); | 151 LoadTestWebPage(web_view_1); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 SetLocalStorageItem(@"someKey2", @"someValue2", web_view_2); | 186 SetLocalStorageItem(@"someKey2", @"someValue2", web_view_2); |
| 183 // Due to platform limitation, it's not possible to actually set localStorage | 187 // Due to platform limitation, it's not possible to actually set localStorage |
| 184 // item on an OTR BrowserState. Therefore, it's not possible to verify that a | 188 // item on an OTR BrowserState. Therefore, it's not possible to verify that a |
| 185 // localStorage item has been correctly set. | 189 // localStorage item has been correctly set. |
| 186 // Look at | 190 // Look at |
| 187 // http://stackoverflow.com/questions/14555347/html5-localstorage-error-with-s
afari-quota-exceeded-err-dom-exception-22-an | 191 // http://stackoverflow.com/questions/14555347/html5-localstorage-error-with-s
afari-quota-exceeded-err-dom-exception-22-an |
| 188 // for more details. | 192 // for more details. |
| 189 // Test that LocalStorage has not leaked over to |web_view_1|. | 193 // Test that LocalStorage has not leaked over to |web_view_1|. |
| 190 EXPECT_NSEQ(@"", GetLocalStorageItem(@"someKey2", web_view_1)); | 194 EXPECT_NSEQ(@"", GetLocalStorageItem(@"someKey2", web_view_1)); |
| 191 } | 195 } |
| OLD | NEW |