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/public/web_view_creation_util.h"
14 #import "ios/web/web_state/web_view_internal_creation_util.h"
15 #import "ios/web/test/web_test.h"
16 #include "testing/gtest_mac.h"
17 #include "testing/platform_test.h"
21 // Synchronously returns result of web::EvaluateJavaScript call.
22 template <typename WebView>
23 NSString* EvaluateJavaScript(WebView web_view, NSString* js) {
24 __block bool evaluation_completed = false;
25 __block base::scoped_nsobject<NSString> evaluation_result;
26 web::EvaluateJavaScript(web_view, js, ^(NSString* result, NSError* error) {
28 evaluation_result.reset([result copy]);
29 evaluation_completed = true;
31 base::test::ios::WaitUntilCondition(^bool() {
32 return evaluation_completed;
34 return [[evaluation_result copy] autorelease];
37 // Base test fixture for web::EvaluateJavaScript testing.
38 typedef web::WebTest WebViewJSUtilsTest;
40 // Test fixture for web::EvaluateJavaScript(UIWebView*..) testing.
41 class UIWebViewJSUtilsTest : public WebViewJSUtilsTest {
43 void SetUp() override {
44 WebViewJSUtilsTest::SetUp();
45 web_view_.reset(web::CreateWebView(CGRectZero));
47 // UIWebView created for testing.
48 base::scoped_nsobject<UIWebView> web_view_;
51 // Test fixture for web::EvaluateJavaScript(WKWebView*..) testing.
52 class WKWebViewJSUtilsTest : public WebViewJSUtilsTest {
54 void SetUp() override {
55 // SetUp crashes on iOS 7.
56 CR_TEST_REQUIRES_WK_WEB_VIEW();
57 WebViewJSUtilsTest::SetUp();
58 web_view_.reset(web::CreateWKWebView(CGRectZero, GetBrowserState()));
60 // WKWebView created for testing.
61 base::scoped_nsobject<WKWebView> web_view_;
64 // Tests that a script with undefined result correctly evaluates to string.
65 WEB_TEST_F(UIWebViewJSUtilsTest, WKWebViewJSUtilsTest, UndefinedEvaluation) {
66 EXPECT_NSEQ(@"", EvaluateJavaScript(this->web_view_, @"{}"));
69 // Tests that a script with string result correctly evaluates to string.
70 WEB_TEST_F(UIWebViewJSUtilsTest, WKWebViewJSUtilsTest, StringEvaluation) {
71 EXPECT_NSEQ(@"test", EvaluateJavaScript(this->web_view_, @"'test'"));
74 // Tests that a script with number result correctly evaluates to string.
75 WEB_TEST_F(UIWebViewJSUtilsTest, WKWebViewJSUtilsTest, NumberEvaluation) {
76 EXPECT_NSEQ(@"-1", EvaluateJavaScript(this->web_view_, @"-1"));
77 EXPECT_NSEQ(@"0", EvaluateJavaScript(this->web_view_, @"0"));
78 EXPECT_NSEQ(@"1", EvaluateJavaScript(this->web_view_, @"1"));
79 EXPECT_NSEQ(@"3.14", EvaluateJavaScript(this->web_view_, @"3.14"));
82 // Tests that a script with bool result correctly evaluates to string.
83 WEB_TEST_F(UIWebViewJSUtilsTest, WKWebViewJSUtilsTest, BoolEvaluation) {
84 EXPECT_NSEQ(@"true", EvaluateJavaScript(this->web_view_, @"true"));
85 EXPECT_NSEQ(@"false", EvaluateJavaScript(this->web_view_, @"false"));
88 // Tests that a script with null result correctly evaluates to empty string.
89 WEB_TEST_F(UIWebViewJSUtilsTest, WKWebViewJSUtilsTest, NullEvaluation) {
90 EXPECT_NSEQ(@"", EvaluateJavaScript(this->web_view_, @"null"));