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