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