| 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 #include "ios/web/public/test/fakes/test_browser_state.h" |
| 10 #import "ios/web/public/test/js_test_util.h" | 10 #import "ios/web/public/test/js_test_util.h" |
| 11 #import "ios/web/web_state/js/page_script_util.h" | 11 #import "ios/web/web_state/js/page_script_util.h" |
| 12 #import "testing/gtest_mac.h" | 12 #import "testing/gtest_mac.h" |
| 13 #import "testing/platform_test.h" | 13 #import "testing/platform_test.h" |
| 14 | 14 |
| 15 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 16 #error "This file requires ARC support." |
| 17 #endif |
| 18 |
| 15 namespace web { | 19 namespace web { |
| 16 | 20 |
| 17 // Test fixture for testing CRWJSWindowIDManager class. | 21 // Test fixture for testing CRWJSWindowIDManager class. |
| 18 class JSWindowIDManagerTest : public PlatformTest { | 22 class JSWindowIDManagerTest : public PlatformTest { |
| 19 protected: | 23 protected: |
| 20 TestBrowserState browser_state_; | 24 TestBrowserState browser_state_; |
| 21 }; | 25 }; |
| 22 | 26 |
| 23 // Tests that window ID injection by a second manager results in a different | 27 // Tests that window ID injection by a second manager results in a different |
| 24 // window ID. | 28 // window ID. |
| 25 TEST_F(JSWindowIDManagerTest, WindowIDDifferentManager) { | 29 TEST_F(JSWindowIDManagerTest, WindowIDDifferentManager) { |
| 26 // Inject the first manager. | 30 // Inject the first manager. |
| 27 WKWebView* web_view = [[[WKWebView alloc] init] autorelease]; | 31 WKWebView* web_view = [[WKWebView alloc] init]; |
| 28 ExecuteJavaScript(web_view, GetEarlyPageScript(&browser_state_)); | 32 ExecuteJavaScript(web_view, GetEarlyPageScript(&browser_state_)); |
| 29 | 33 |
| 30 CRWJSWindowIDManager* manager = | 34 CRWJSWindowIDManager* manager = |
| 31 [[[CRWJSWindowIDManager alloc] initWithWebView:web_view] autorelease]; | 35 [[CRWJSWindowIDManager alloc] initWithWebView:web_view]; |
| 32 [manager inject]; | 36 [manager inject]; |
| 33 EXPECT_NSEQ([manager windowID], | 37 EXPECT_NSEQ([manager windowID], |
| 34 ExecuteJavaScript(web_view, @"window.__gCrWeb.windowId")); | 38 ExecuteJavaScript(web_view, @"window.__gCrWeb.windowId")); |
| 35 | 39 |
| 36 // Inject the second manager. | 40 // Inject the second manager. |
| 37 WKWebView* web_view2 = [[[WKWebView alloc] init] autorelease]; | 41 WKWebView* web_view2 = [[WKWebView alloc] init]; |
| 38 ExecuteJavaScript(web_view2, GetEarlyPageScript(&browser_state_)); | 42 ExecuteJavaScript(web_view2, GetEarlyPageScript(&browser_state_)); |
| 39 | 43 |
| 40 CRWJSWindowIDManager* manager2 = | 44 CRWJSWindowIDManager* manager2 = |
| 41 [[[CRWJSWindowIDManager alloc] initWithWebView:web_view2] autorelease]; | 45 [[CRWJSWindowIDManager alloc] initWithWebView:web_view2]; |
| 42 [manager2 inject]; | 46 [manager2 inject]; |
| 43 EXPECT_NSEQ([manager2 windowID], | 47 EXPECT_NSEQ([manager2 windowID], |
| 44 ExecuteJavaScript(web_view2, @"window.__gCrWeb.windowId")); | 48 ExecuteJavaScript(web_view2, @"window.__gCrWeb.windowId")); |
| 45 | 49 |
| 46 // Window IDs must be different. | 50 // Window IDs must be different. |
| 47 EXPECT_NSNE([manager windowID], [manager2 windowID]); | 51 EXPECT_NSNE([manager windowID], [manager2 windowID]); |
| 48 } | 52 } |
| 49 | 53 |
| 50 // Tests that injecting multiple times creates a new window ID. | 54 // Tests that injecting multiple times creates a new window ID. |
| 51 TEST_F(JSWindowIDManagerTest, MultipleInjections) { | 55 TEST_F(JSWindowIDManagerTest, MultipleInjections) { |
| 52 WKWebView* web_view = [[[WKWebView alloc] init] autorelease]; | 56 WKWebView* web_view = [[WKWebView alloc] init]; |
| 53 ExecuteJavaScript(web_view, GetEarlyPageScript(&browser_state_)); | 57 ExecuteJavaScript(web_view, GetEarlyPageScript(&browser_state_)); |
| 54 | 58 |
| 55 // First injection. | 59 // First injection. |
| 56 CRWJSWindowIDManager* manager = | 60 CRWJSWindowIDManager* manager = |
| 57 [[[CRWJSWindowIDManager alloc] initWithWebView:web_view] autorelease]; | 61 [[CRWJSWindowIDManager alloc] initWithWebView:web_view]; |
| 58 [manager inject]; | 62 [manager inject]; |
| 59 NSString* windowID = [manager windowID]; | 63 NSString* windowID = [manager windowID]; |
| 60 EXPECT_NSEQ(windowID, | 64 EXPECT_NSEQ(windowID, |
| 61 ExecuteJavaScript(web_view, @"window.__gCrWeb.windowId")); | 65 ExecuteJavaScript(web_view, @"window.__gCrWeb.windowId")); |
| 62 | 66 |
| 63 // Second injection. | 67 // Second injection. |
| 64 [manager inject]; | 68 [manager inject]; |
| 65 EXPECT_NSEQ([manager windowID], | 69 EXPECT_NSEQ([manager windowID], |
| 66 ExecuteJavaScript(web_view, @"window.__gCrWeb.windowId")); | 70 ExecuteJavaScript(web_view, @"window.__gCrWeb.windowId")); |
| 67 | 71 |
| 68 EXPECT_NSNE(windowID, [manager windowID]); | 72 EXPECT_NSNE(windowID, [manager windowID]); |
| 69 } | 73 } |
| 70 | 74 |
| 71 // Tests that injection will retry if |window.__gCrWeb| is not present. | 75 // Tests that injection will retry if |window.__gCrWeb| is not present. |
| 72 TEST_F(JSWindowIDManagerTest, InjectionRetry) { | 76 TEST_F(JSWindowIDManagerTest, InjectionRetry) { |
| 73 WKWebView* web_view = [[[WKWebView alloc] init] autorelease]; | 77 WKWebView* web_view = [[WKWebView alloc] init]; |
| 74 | 78 |
| 75 CRWJSWindowIDManager* manager = | 79 CRWJSWindowIDManager* manager = |
| 76 [[[CRWJSWindowIDManager alloc] initWithWebView:web_view] autorelease]; | 80 [[CRWJSWindowIDManager alloc] initWithWebView:web_view]; |
| 77 [manager inject]; | 81 [manager inject]; |
| 78 EXPECT_TRUE([manager windowID]); | 82 EXPECT_TRUE([manager windowID]); |
| 79 EXPECT_FALSE(ExecuteJavaScript(web_view, @"window.__gCrWeb")); | 83 EXPECT_FALSE(ExecuteJavaScript(web_view, @"window.__gCrWeb")); |
| 80 | 84 |
| 81 // Now inject window.__gCrWeb and check if window ID injection retried. | 85 // Now inject window.__gCrWeb and check if window ID injection retried. |
| 82 ExecuteJavaScript(web_view, GetEarlyPageScript(&browser_state_)); | 86 ExecuteJavaScript(web_view, GetEarlyPageScript(&browser_state_)); |
| 83 EXPECT_NSEQ([manager windowID], | 87 EXPECT_NSEQ([manager windowID], |
| 84 ExecuteJavaScript(web_view, @"window.__gCrWeb.windowId")); | 88 ExecuteJavaScript(web_view, @"window.__gCrWeb.windowId")); |
| 85 } | 89 } |
| 86 | 90 |
| 87 } // namespace web | 91 } // namespace web |
| OLD | NEW |