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 #include "testing/gtest_mac.h"
16 #include "testing/platform_test.h"
20 // Synchronously returns result of web::EvaluateJavaScript call.
21 template <typename WebView>
22 NSString* EvaluateJavaScript(WebView web_view, NSString* js) {
23 __block bool evaluation_completed = false;
24 __block base::scoped_nsobject<NSString> evaluation_result;
25 web::EvaluateJavaScript(web_view, js, ^(NSString* result, NSError* error) {
27 evaluation_result.reset([result copy]);
28 evaluation_completed = true;
30 base::test::ios::WaitUntilCondition(^bool() {
31 return evaluation_completed;
33 return [[evaluation_result copy] autorelease];
36 // Base test fixture for web::EvaluateJavaScript testing.
37 class WebViewJSUtilsTest : public PlatformTest {
39 void SetUp() override {
40 PlatformTest::SetUp();
41 web::SetWebClient(&test_web_client_);
43 void TearDown() override {
44 web::SetWebClient(nullptr);
45 PlatformTest::TearDown();
47 // BrowserState required for web view creation.
48 web::TestBrowserState browser_state_;
51 // Required by web::CreateWebView/web::CreateWKWebView functions.
52 web::TestWebClient test_web_client_;
55 // Test fixture for web::EvaluateJavaScript(UIWebView*..) testing.
56 class UIWebViewJSUtilsTest : public WebViewJSUtilsTest {
58 void SetUp() override {
59 WebViewJSUtilsTest::SetUp();
60 web_view_.reset(web::CreateWebView(CGRectZero));
62 // UIWebView created for testing.
63 base::scoped_nsobject<UIWebView> web_view_;
66 // Test fixture for web::EvaluateJavaScript(WKWebView*..) testing.
67 class WKWebViewJSUtilsTest : public WebViewJSUtilsTest {
69 void SetUp() override {
70 // SetUp crashes on iOS 7.
71 CR_TEST_REQUIRES_WK_WEB_VIEW();
72 WebViewJSUtilsTest::SetUp();
73 web_view_.reset(web::CreateWKWebView(CGRectZero, &browser_state_));
75 // WKWebView created for testing.
76 base::scoped_nsobject<WKWebView> web_view_;
79 // Tests that a script with undefined result correctly evaluates to string.
80 WEB_TEST_F(UIWebViewJSUtilsTest, WKWebViewJSUtilsTest, UndefinedEvaluation) {
81 EXPECT_NSEQ(@"", EvaluateJavaScript(this->web_view_, @"{}"));
84 // Tests that a script with string result correctly evaluates to string.
85 WEB_TEST_F(UIWebViewJSUtilsTest, WKWebViewJSUtilsTest, StringEvaluation) {
86 EXPECT_NSEQ(@"test", EvaluateJavaScript(this->web_view_, @"'test'"));
89 // Tests that a script with number result correctly evaluates to string.
90 WEB_TEST_F(UIWebViewJSUtilsTest, WKWebViewJSUtilsTest, NumberEvaluation) {
91 EXPECT_NSEQ(@"-1", EvaluateJavaScript(this->web_view_, @"-1"));
92 EXPECT_NSEQ(@"0", EvaluateJavaScript(this->web_view_, @"0"));
93 EXPECT_NSEQ(@"1", EvaluateJavaScript(this->web_view_, @"1"));
94 EXPECT_NSEQ(@"3.14", EvaluateJavaScript(this->web_view_, @"3.14"));
97 // Tests that a script with bool result correctly evaluates to string.
98 WEB_TEST_F(UIWebViewJSUtilsTest, WKWebViewJSUtilsTest, BoolEvaluation) {
99 EXPECT_NSEQ(@"true", EvaluateJavaScript(this->web_view_, @"true"));
100 EXPECT_NSEQ(@"false", EvaluateJavaScript(this->web_view_, @"false"));
103 // Tests that a script with null result correctly evaluates to empty string.
104 WEB_TEST_F(UIWebViewJSUtilsTest, WKWebViewJSUtilsTest, NullEvaluation) {
105 EXPECT_NSEQ(@"", EvaluateJavaScript(this->web_view_, @"null"));