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.
5 #import "ios/web/web_state/ui/web_view_js_utils.h"
7 #include "base/logging.h"
8 #import "base/mac/scoped_nsobject.h"
9 #include "base/test/ios/wait_util.h"
10 #include "ios/web/public/test/test_browser_state.h"
11 #import "ios/web/public/test/test_web_client.h"
12 #include "ios/web/public/test/web_test_util.h"
13 #import "ios/web/web_state/web_view_creation_utils.h"
14 #include "testing/gtest_mac.h"
15 #include "testing/platform_test.h"
19 // Synchronously returns result of web::EvaluateJavaScript call.
20 template <typename WebView>
21 NSString* EvaluateJavaScript(WebView web_view, NSString* js) {
22 __block bool evaluation_completed = false;
23 __block base::scoped_nsobject<NSString> evaluation_result;
24 web::EvaluateJavaScript(web_view, js, ^(NSString* result, NSError* error) {
26 evaluation_result.reset([result copy]);
27 evaluation_completed = true;
29 base::test::ios::WaitUntilCondition(^bool() {
30 return evaluation_completed;
32 return [[evaluation_result copy] autorelease];
35 // Base test fixture for web::EvaluateJavaScript testing.
36 class WebViewJSUtilsTest : public PlatformTest {
38 void SetUp() override {
39 PlatformTest::SetUp();
40 web::SetWebClient(&test_web_client_);
42 void TearDown() override {
43 web::SetWebClient(nullptr);
44 PlatformTest::TearDown();
46 // BrowserState required for web view creation.
47 web::TestBrowserState browser_state_;
50 // Required by web::CreateWebView/web::CreateWKWebView functions.
51 web::TestWebClient test_web_client_;
54 // Test fixture for web::EvaluateJavaScript(UIWebView*..) testing.
55 class UIWebViewJSUtilsTest : public WebViewJSUtilsTest {
57 void SetUp() override {
58 WebViewJSUtilsTest::SetUp();
59 web_view_.reset(web::CreateWebView(CGRectZero));
61 // UIWebView created for testing.
62 base::scoped_nsobject<UIWebView> web_view_;
65 // Test fixture for web::EvaluateJavaScript(WKWebView*..) testing.
66 class WKWebViewJSUtilsTest : public WebViewJSUtilsTest {
68 void SetUp() override {
69 // SetUp crashes on iOS 7.
70 CR_TEST_REQUIRES_WK_WEB_VIEW();
71 WebViewJSUtilsTest::SetUp();
72 web_view_.reset(web::CreateWKWebView(CGRectZero, &browser_state_));
74 // WKWebView created for testing.
75 base::scoped_nsobject<WKWebView> web_view_;
78 // Tests that a script with undefined result correctly evaluates to string.
79 WEB_TEST_F(UIWebViewJSUtilsTest, WKWebViewJSUtilsTest, UndefinedEvaluation) {
80 EXPECT_NSEQ(@"", EvaluateJavaScript(this->web_view_, @"{}"));
83 // Tests that a script with string result correctly evaluates to string.
84 WEB_TEST_F(UIWebViewJSUtilsTest, WKWebViewJSUtilsTest, StringEvaluation) {
85 EXPECT_NSEQ(@"test", EvaluateJavaScript(this->web_view_, @"'test'"));
88 // Tests that a script with number result correctly evaluates to string.
89 WEB_TEST_F(UIWebViewJSUtilsTest, WKWebViewJSUtilsTest, NumberEvaluation) {
90 EXPECT_NSEQ(@"-1", EvaluateJavaScript(this->web_view_, @"-1"));
91 EXPECT_NSEQ(@"0", EvaluateJavaScript(this->web_view_, @"0"));
92 EXPECT_NSEQ(@"1", EvaluateJavaScript(this->web_view_, @"1"));
93 EXPECT_NSEQ(@"3.14", EvaluateJavaScript(this->web_view_, @"3.14"));
96 // Tests that a script with bool result correctly evaluates to string.
97 WEB_TEST_F(UIWebViewJSUtilsTest, WKWebViewJSUtilsTest, BoolEvaluation) {
98 EXPECT_NSEQ(@"true", EvaluateJavaScript(this->web_view_, @"true"));
99 EXPECT_NSEQ(@"false", EvaluateJavaScript(this->web_view_, @"false"));