Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/js/crw_js_window_id_manager.h" | 5 #import "ios/web/web_state/js/crw_js_window_id_manager.h" |
| 6 | 6 |
| 7 #import <WebKit/WebKit.h> | 7 #import <WebKit/WebKit.h> |
| 8 | 8 |
| 9 #include "ios/web/public/test/fakes/test_browser_state.h" | |
| 9 #import "ios/web/public/test/js_test_util.h" | 10 #import "ios/web/public/test/js_test_util.h" |
| 10 #import "ios/web/web_state/js/page_script_util.h" | 11 #import "ios/web/web_state/js/page_script_util.h" |
| 11 #import "testing/gtest_mac.h" | 12 #import "testing/gtest_mac.h" |
| 13 #import "testing/platform_test.h" | |
| 12 | 14 |
| 13 namespace web { | 15 namespace web { |
| 14 | 16 |
|
Eugene But (OOO till 7-30)
2017/03/16 16:12:40
nit: Please add comment: "Test fixture for testing
Hiroshi Ichikawa
2017/03/17 06:09:24
Done.
| |
| 17 class JSWindowIDManagerTest : public PlatformTest { | |
| 18 protected: | |
| 19 TestBrowserState browser_state_; | |
| 20 }; | |
| 21 | |
| 15 // Tests that window ID injection by a second manager results in a different | 22 // Tests that window ID injection by a second manager results in a different |
| 16 // window ID. | 23 // window ID. |
| 17 TEST(JSWindowIDManagerTest, WindowIDDifferentManager) { | 24 TEST_F(JSWindowIDManagerTest, WindowIDDifferentManager) { |
| 18 // Inject the first manager. | 25 // Inject the first manager. |
| 19 WKWebView* web_view = [[[WKWebView alloc] init] autorelease]; | 26 WKWebView* web_view = [[[WKWebView alloc] init] autorelease]; |
| 20 ExecuteJavaScript(web_view, GetEarlyPageScript()); | 27 ExecuteJavaScript(web_view, GetEarlyPageScript(&browser_state_)); |
| 21 | 28 |
| 22 CRWJSWindowIDManager* manager = | 29 CRWJSWindowIDManager* manager = |
| 23 [[[CRWJSWindowIDManager alloc] initWithWebView:web_view] autorelease]; | 30 [[[CRWJSWindowIDManager alloc] initWithWebView:web_view] autorelease]; |
| 24 [manager inject]; | 31 [manager inject]; |
| 25 EXPECT_NSEQ([manager windowID], | 32 EXPECT_NSEQ([manager windowID], |
| 26 ExecuteJavaScript(web_view, @"window.__gCrWeb.windowId")); | 33 ExecuteJavaScript(web_view, @"window.__gCrWeb.windowId")); |
| 27 | 34 |
| 28 // Inject the second manager. | 35 // Inject the second manager. |
| 29 WKWebView* web_view2 = [[[WKWebView alloc] init] autorelease]; | 36 WKWebView* web_view2 = [[[WKWebView alloc] init] autorelease]; |
| 30 ExecuteJavaScript(web_view2, GetEarlyPageScript()); | 37 ExecuteJavaScript(web_view2, GetEarlyPageScript(&browser_state_)); |
| 31 | 38 |
| 32 CRWJSWindowIDManager* manager2 = | 39 CRWJSWindowIDManager* manager2 = |
| 33 [[[CRWJSWindowIDManager alloc] initWithWebView:web_view2] autorelease]; | 40 [[[CRWJSWindowIDManager alloc] initWithWebView:web_view2] autorelease]; |
| 34 [manager2 inject]; | 41 [manager2 inject]; |
| 35 EXPECT_NSEQ([manager2 windowID], | 42 EXPECT_NSEQ([manager2 windowID], |
| 36 ExecuteJavaScript(web_view2, @"window.__gCrWeb.windowId")); | 43 ExecuteJavaScript(web_view2, @"window.__gCrWeb.windowId")); |
| 37 | 44 |
| 38 // Window IDs must be different. | 45 // Window IDs must be different. |
| 39 EXPECT_NSNE([manager windowID], [manager2 windowID]); | 46 EXPECT_NSNE([manager windowID], [manager2 windowID]); |
| 40 } | 47 } |
| 41 | 48 |
| 42 // Tests that injecting multiple times creates a new window ID. | 49 // Tests that injecting multiple times creates a new window ID. |
| 43 TEST(JSWindowIDManagerTest, MultipleInjections) { | 50 TEST_F(JSWindowIDManagerTest, MultipleInjections) { |
| 44 WKWebView* web_view = [[[WKWebView alloc] init] autorelease]; | 51 WKWebView* web_view = [[[WKWebView alloc] init] autorelease]; |
| 45 ExecuteJavaScript(web_view, GetEarlyPageScript()); | 52 ExecuteJavaScript(web_view, GetEarlyPageScript(&browser_state_)); |
| 46 | 53 |
| 47 // First injection. | 54 // First injection. |
| 48 CRWJSWindowIDManager* manager = | 55 CRWJSWindowIDManager* manager = |
| 49 [[[CRWJSWindowIDManager alloc] initWithWebView:web_view] autorelease]; | 56 [[[CRWJSWindowIDManager alloc] initWithWebView:web_view] autorelease]; |
| 50 [manager inject]; | 57 [manager inject]; |
| 51 NSString* windowID = [manager windowID]; | 58 NSString* windowID = [manager windowID]; |
| 52 EXPECT_NSEQ(windowID, | 59 EXPECT_NSEQ(windowID, |
| 53 ExecuteJavaScript(web_view, @"window.__gCrWeb.windowId")); | 60 ExecuteJavaScript(web_view, @"window.__gCrWeb.windowId")); |
| 54 | 61 |
| 55 // Second injection. | 62 // Second injection. |
| 56 [manager inject]; | 63 [manager inject]; |
| 57 EXPECT_NSEQ([manager windowID], | 64 EXPECT_NSEQ([manager windowID], |
| 58 ExecuteJavaScript(web_view, @"window.__gCrWeb.windowId")); | 65 ExecuteJavaScript(web_view, @"window.__gCrWeb.windowId")); |
| 59 | 66 |
| 60 EXPECT_NSNE(windowID, [manager windowID]); | 67 EXPECT_NSNE(windowID, [manager windowID]); |
| 61 } | 68 } |
| 62 | 69 |
| 63 // Tests that injection will retry if |window.__gCrWeb| is not present. | 70 // Tests that injection will retry if |window.__gCrWeb| is not present. |
| 64 TEST(JSWindowIDManagerTest, InjectionRetry) { | 71 TEST_F(JSWindowIDManagerTest, InjectionRetry) { |
| 65 WKWebView* web_view = [[[WKWebView alloc] init] autorelease]; | 72 WKWebView* web_view = [[[WKWebView alloc] init] autorelease]; |
| 66 | 73 |
| 67 CRWJSWindowIDManager* manager = | 74 CRWJSWindowIDManager* manager = |
| 68 [[[CRWJSWindowIDManager alloc] initWithWebView:web_view] autorelease]; | 75 [[[CRWJSWindowIDManager alloc] initWithWebView:web_view] autorelease]; |
| 69 [manager inject]; | 76 [manager inject]; |
| 70 EXPECT_TRUE([manager windowID]); | 77 EXPECT_TRUE([manager windowID]); |
| 71 EXPECT_FALSE(ExecuteJavaScript(web_view, @"window.__gCrWeb")); | 78 EXPECT_FALSE(ExecuteJavaScript(web_view, @"window.__gCrWeb")); |
| 72 | 79 |
| 73 // Now inject window.__gCrWeb and check if window ID injection retried. | 80 // Now inject window.__gCrWeb and check if window ID injection retried. |
| 74 ExecuteJavaScript(web_view, GetEarlyPageScript()); | 81 ExecuteJavaScript(web_view, GetEarlyPageScript(&browser_state_)); |
| 75 EXPECT_NSEQ([manager windowID], | 82 EXPECT_NSEQ([manager windowID], |
| 76 ExecuteJavaScript(web_view, @"window.__gCrWeb.windowId")); | 83 ExecuteJavaScript(web_view, @"window.__gCrWeb.windowId")); |
| 77 } | 84 } |
| 78 | 85 |
| 79 } // namespace web | 86 } // namespace web |
| OLD | NEW |