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
3 // found in the LICENSE file.
5 #import "ios/web/web_state/js/page_script_util.h"
7 #import <UIKit/UIKit.h>
8 #import <WebKit/WebKit.h>
10 #import "base/mac/scoped_nsobject.h"
11 #include "base/strings/sys_string_conversions.h"
12 #import "base/test/ios/wait_util.h"
13 #import "ios/web/public/test/js_test_util.h"
14 #include "ios/web/public/test/test_browser_state.h"
15 #import "ios/web/public/test/test_web_client.h"
16 #include "ios/web/public/test/web_test_util.h"
17 #import "ios/web/public/web_view_creation_util.h"
18 #include "ios/web/test/web_test.h"
19 #include "testing/gtest_mac.h"
24 // A test fixture for testing the page_script_util methods.
25 typedef WebTest PageScriptUtilTest;
27 // Tests that UIWebView early page script is a valid script that injects global
29 TEST_F(PageScriptUtilTest, UIWebViewEarlyPageScript) {
30 base::scoped_nsobject<UIWebView> web_view([[UIWebView alloc] init]);
31 EvaluateJavaScriptAsString(web_view, GetEarlyPageScript(UI_WEB_VIEW_TYPE));
32 EXPECT_NSEQ(@"object",
33 EvaluateJavaScriptAsString(web_view, @"typeof __gCrWeb"));
36 // Tests that WKWebView early page script is a valid script that injects global
38 TEST_F(PageScriptUtilTest, WKWebViewEarlyPageScript) {
39 CR_TEST_REQUIRES_WK_WEB_VIEW();
40 base::scoped_nsobject<WKWebView> web_view(
41 CreateWKWebView(CGRectZero, GetBrowserState()));
42 EvaluateJavaScript(web_view, GetEarlyPageScript(WK_WEB_VIEW_TYPE));
43 EXPECT_NSEQ(@"object", EvaluateJavaScript(web_view, @"typeof __gCrWeb"));
46 // Tests that embedder's UIWebView script is included into early script.
47 TEST_F(PageScriptUtilTest, UIEmbedderScript) {
48 GetWebClient()->SetEarlyPageScript(@"__gCrEmbedder = {};", UI_WEB_VIEW_TYPE);
49 base::scoped_nsobject<UIWebView> web_view([[UIWebView alloc] init]);
50 EvaluateJavaScriptAsString(web_view, GetEarlyPageScript(UI_WEB_VIEW_TYPE));
51 EXPECT_NSEQ(@"object",
52 EvaluateJavaScriptAsString(web_view, @"typeof __gCrEmbedder"));
55 // Tests that embedder's WKWebView script is included into early script.
56 TEST_F(PageScriptUtilTest, WKEmbedderScript) {
57 CR_TEST_REQUIRES_WK_WEB_VIEW();
58 GetWebClient()->SetEarlyPageScript(@"__gCrEmbedder = {};", WK_WEB_VIEW_TYPE);
59 base::scoped_nsobject<WKWebView> web_view(
60 CreateWKWebView(CGRectZero, GetBrowserState()));
61 EvaluateJavaScript(web_view, GetEarlyPageScript(WK_WEB_VIEW_TYPE));
62 EXPECT_NSEQ(@"object", EvaluateJavaScript(web_view, @"typeof __gCrEmbedder"));