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 #include "base/mac/foundation_util.h" |
10 #import "base/mac/scoped_nsobject.h" | 11 #import "base/mac/scoped_nsobject.h" |
11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
12 #include "base/test/ios/wait_util.h" | 13 #include "base/test/ios/wait_util.h" |
13 #include "ios/web/public/browser_state.h" | 14 #include "ios/web/public/browser_state.h" |
14 #import "ios/web/public/test/http_server.h" | 15 #import "ios/web/public/test/http_server.h" |
| 16 #import "ios/web/public/test/js_test_util.h" |
15 #include "ios/web/public/test/response_providers/string_response_provider.h" | 17 #include "ios/web/public/test/response_providers/string_response_provider.h" |
16 #import "ios/web/public/web_view_creation_util.h" | 18 #import "ios/web/public/web_view_creation_util.h" |
17 #import "ios/web/test/web_int_test.h" | 19 #import "ios/web/test/web_int_test.h" |
18 #import "ios/web/web_state/ui/web_view_js_utils.h" | |
19 #import "net/base/mac/url_conversions.h" | 20 #import "net/base/mac/url_conversions.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "testing/gtest_mac.h" | 22 #include "testing/gtest_mac.h" |
22 | 23 |
23 // A WKNavigationDelegate that is used to check if a WKWebView has finished | 24 // A WKNavigationDelegate that is used to check if a WKWebView has finished |
24 // a navigation. Used for testing purposes. | 25 // a navigation. Used for testing purposes. |
25 @interface TestNavigationDelegate : NSObject <WKNavigationDelegate> | 26 @interface TestNavigationDelegate : NSObject <WKNavigationDelegate> |
26 // YES if a navigation has finished. | 27 // YES if a navigation has finished. |
27 @property (nonatomic, assign) BOOL didFinishNavigation; | 28 @property (nonatomic, assign) BOOL didFinishNavigation; |
28 @end | 29 @end |
(...skipping 29 matching lines...) Expand all Loading... |
58 } | 59 } |
59 | 60 |
60 void TearDown() override { | 61 void TearDown() override { |
61 web::test::HttpServer& server = web::test::HttpServer::GetSharedInstance(); | 62 web::test::HttpServer& server = web::test::HttpServer::GetSharedInstance(); |
62 server.RemoveResponseProvider(provider_); | 63 server.RemoveResponseProvider(provider_); |
63 provider_ = nullptr; | 64 provider_ = nullptr; |
64 | 65 |
65 web::WebIntTest::TearDown(); | 66 web::WebIntTest::TearDown(); |
66 } | 67 } |
67 | 68 |
68 // Runs the given JavaScript on |web_view| and returns the result as a string. | |
69 NSString* EvaluateJavaScript(WKWebView* web_view, | |
70 NSString* js) { | |
71 __block base::scoped_nsobject<NSString> result; | |
72 __block bool block_was_called = false; | |
73 web::EvaluateJavaScript(web_view, js, ^(NSString* js_result, NSError*) { | |
74 result.reset([js_result copy]); | |
75 block_was_called = true; | |
76 }); | |
77 base::test::ios::WaitUntilCondition(^bool { | |
78 return block_was_called; | |
79 }); | |
80 return [[result copy] autorelease]; | |
81 } | |
82 | |
83 // Sets a persistent cookie with key, value on |web_view|. | 69 // Sets a persistent cookie with key, value on |web_view|. |
84 void SetCookie(NSString* key, NSString* value, WKWebView* web_view) { | 70 void SetCookie(NSString* key, NSString* value, WKWebView* web_view) { |
85 NSString* set_cookie = [NSString | 71 NSString* set_cookie = [NSString |
86 stringWithFormat:@"document.cookie='%@=%@;" | 72 stringWithFormat:@"document.cookie='%@=%@;" |
87 @"Expires=Tue, 05-May-9999 02:18:23 GMT; Path=/'", | 73 @"Expires=Tue, 05-May-9999 02:18:23 GMT; Path=/'", |
88 key, value]; | 74 key, value]; |
89 EvaluateJavaScript(web_view, set_cookie); | 75 web::ExecuteJavaScript(web_view, set_cookie); |
90 } | 76 } |
91 | 77 |
92 // Returns a csv list of all cookies from |web_view|. | 78 // Returns a csv list of all cookies from |web_view|. |
93 NSString* GetCookies(WKWebView* web_view) { | 79 NSString* GetCookies(WKWebView* web_view) { |
94 return EvaluateJavaScript(web_view, @"document.cookie"); | 80 id result = web::ExecuteJavaScript(web_view, @"document.cookie"); |
| 81 return base::mac::ObjCCastStrict<NSString>(result); |
95 } | 82 } |
96 | 83 |
97 // Sets a localstorage key, value pair on |web_view|. | 84 // Sets a localstorage key, value pair on |web_view|. |
98 void SetLocalStorageItem(NSString* key, | 85 void SetLocalStorageItem(NSString* key, |
99 NSString* value, | 86 NSString* value, |
100 WKWebView* web_view) { | 87 WKWebView* web_view) { |
101 NSString* set_local_storage_item = [NSString | 88 NSString* set_local_storage_item = [NSString |
102 stringWithFormat:@"localStorage.setItem('%@', '%@')", key, value]; | 89 stringWithFormat:@"localStorage.setItem('%@', '%@')", key, value]; |
103 EvaluateJavaScript(web_view, set_local_storage_item); | 90 NSError* unused_error = nil; |
| 91 web::ExecuteJavaScript(web_view, set_local_storage_item, &unused_error); |
104 } | 92 } |
105 | 93 |
106 // Returns the localstorage value associated with |key| from |web_view|. | 94 // Returns the localstorage value associated with |key| from |web_view|. |
107 NSString* GetLocalStorageItem(NSString* key, WKWebView* web_view) { | 95 id GetLocalStorageItem(NSString* key, WKWebView* web_view) { |
108 NSString* get_local_storage_value = | 96 NSString* get_local_storage_value = |
109 [NSString stringWithFormat:@"localStorage.getItem('%@');", key]; | 97 [NSString stringWithFormat:@"localStorage.getItem('%@');", key]; |
110 return EvaluateJavaScript(web_view, get_local_storage_value); | 98 return web::ExecuteJavaScript(web_view, get_local_storage_value); |
111 } | 99 } |
112 | 100 |
113 // Loads a test web page (that contains a small string) in |web_view| and | 101 // Loads a test web page (that contains a small string) in |web_view| and |
114 // waits until the web view has finished the navigation. | 102 // waits until the web view has finished the navigation. |
115 void LoadTestWebPage(WKWebView* web_view) { | 103 void LoadTestWebPage(WKWebView* web_view) { |
116 DCHECK(web_view); | 104 DCHECK(web_view); |
117 | 105 |
118 base::scoped_nsobject<TestNavigationDelegate> navigation_delegate( | 106 base::scoped_nsobject<TestNavigationDelegate> navigation_delegate( |
119 [[TestNavigationDelegate alloc] init]); | 107 [[TestNavigationDelegate alloc] init]); |
120 | 108 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 web::CreateWKWebView(CGRectZero, GetBrowserState())); | 162 web::CreateWKWebView(CGRectZero, GetBrowserState())); |
175 LoadTestWebPage(web_view_1); | 163 LoadTestWebPage(web_view_1); |
176 SetLocalStorageItem(@"someKey1", @"someValue1", web_view_1); | 164 SetLocalStorageItem(@"someKey1", @"someValue1", web_view_1); |
177 EXPECT_NSEQ(@"someValue1", GetLocalStorageItem(@"someKey1", web_view_1)); | 165 EXPECT_NSEQ(@"someValue1", GetLocalStorageItem(@"someKey1", web_view_1)); |
178 | 166 |
179 base::scoped_nsobject<WKWebView> web_view_2( | 167 base::scoped_nsobject<WKWebView> web_view_2( |
180 web::CreateWKWebView(CGRectZero, GetOtrBrowserState())); | 168 web::CreateWKWebView(CGRectZero, GetOtrBrowserState())); |
181 LoadTestWebPage(web_view_2); | 169 LoadTestWebPage(web_view_2); |
182 | 170 |
183 // Test that LocalStorage has not leaked over to |web_view_2|. | 171 // Test that LocalStorage has not leaked over to |web_view_2|. |
184 EXPECT_NSEQ(@"", GetLocalStorageItem(@"someKey1", web_view_2)); | 172 EXPECT_NSEQ([NSNull null], GetLocalStorageItem(@"someKey1", web_view_2)); |
185 | 173 |
186 SetLocalStorageItem(@"someKey2", @"someValue2", web_view_2); | 174 SetLocalStorageItem(@"someKey2", @"someValue2", web_view_2); |
187 // Due to platform limitation, it's not possible to actually set localStorage | 175 // Due to platform limitation, it's not possible to actually set localStorage |
188 // item on an OTR BrowserState. Therefore, it's not possible to verify that a | 176 // item on an OTR BrowserState. Therefore, it's not possible to verify that a |
189 // localStorage item has been correctly set. | 177 // localStorage item has been correctly set. |
190 // Look at | 178 // Look at |
191 // http://stackoverflow.com/questions/14555347/html5-localstorage-error-with-s
afari-quota-exceeded-err-dom-exception-22-an | 179 // http://stackoverflow.com/questions/14555347/html5-localstorage-error-with-s
afari-quota-exceeded-err-dom-exception-22-an |
192 // for more details. | 180 // for more details. |
193 // Test that LocalStorage has not leaked over to |web_view_1|. | 181 // Test that LocalStorage has not leaked over to |web_view_1|. |
194 EXPECT_NSEQ(@"", GetLocalStorageItem(@"someKey2", web_view_1)); | 182 EXPECT_NSEQ([NSNull null], GetLocalStorageItem(@"someKey2", web_view_1)); |
195 } | 183 } |
OLD | NEW |