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