| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/web/web_state/ui/web_view_js_utils.h" | 5 #import "ios/web/web_state/ui/web_view_js_utils.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #import "base/mac/scoped_nsobject.h" | 8 #import "base/mac/scoped_nsobject.h" |
| 9 #include "base/test/ios/wait_util.h" | 9 #include "base/test/ios/wait_util.h" |
| 10 #include "ios/web/public/test/test_browser_state.h" | 10 #include "ios/web/public/test/test_browser_state.h" |
| 11 #import "ios/web/public/test/test_web_client.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" | 12 #import "ios/web/public/web_view_creation_util.h" |
| 14 #import "ios/web/web_state/web_view_internal_creation_util.h" | 13 #import "ios/web/web_state/web_view_internal_creation_util.h" |
| 15 #import "ios/web/test/web_test.h" | 14 #import "ios/web/test/web_test.h" |
| 16 #include "testing/gtest_mac.h" | 15 #include "testing/gtest_mac.h" |
| 17 #include "testing/platform_test.h" | 16 #include "testing/platform_test.h" |
| 18 | 17 |
| 19 namespace { | 18 namespace web { |
| 20 | 19 |
| 21 // Synchronously returns result of web::EvaluateJavaScript call. | 20 // Test fixture for web::EvaluateJavaScript testing. |
| 22 template <typename WebView> | 21 class WebViewJsUtilsTest : public web::WebTest { |
| 23 NSString* EvaluateJavaScript(WebView web_view, NSString* js) { | 22 protected: |
| 24 __block bool evaluation_completed = false; | 23 void SetUp() override { |
| 25 __block base::scoped_nsobject<NSString> evaluation_result; | 24 web::WebTest::SetUp(); |
| 26 web::EvaluateJavaScript(web_view, js, ^(NSString* result, NSError* error) { | 25 web_view_.reset(web::CreateWKWebView(CGRectZero, GetBrowserState())); |
| 26 } |
| 27 // Synchronously returns result of web::EvaluateJavaScript call. |
| 28 NSString* EvaluateJavaScript(NSString* js) { |
| 29 __block bool evaluation_completed = false; |
| 30 __block base::scoped_nsobject<NSString> evaluation_result; |
| 31 web::EvaluateJavaScript(web_view_, js, ^(NSString* result, NSError* error) { |
| 27 DCHECK(!error); | 32 DCHECK(!error); |
| 28 evaluation_result.reset([result copy]); | 33 evaluation_result.reset([result copy]); |
| 29 evaluation_completed = true; | 34 evaluation_completed = true; |
| 30 }); | 35 }); |
| 31 base::test::ios::WaitUntilCondition(^bool() { | 36 base::test::ios::WaitUntilCondition(^{ |
| 32 return evaluation_completed; | 37 return evaluation_completed; |
| 33 }); | 38 }); |
| 34 return [[evaluation_result copy] autorelease]; | 39 return [[evaluation_result copy] autorelease]; |
| 35 } | 40 } |
| 36 | 41 |
| 37 // Base test fixture for web::EvaluateJavaScript testing. | 42 private: |
| 38 typedef web::WebTest WebViewJSUtilsTest; | |
| 39 | |
| 40 // Test fixture for web::EvaluateJavaScript(UIWebView*..) testing. | |
| 41 class UIWebViewJSUtilsTest : public WebViewJSUtilsTest { | |
| 42 protected: | |
| 43 void SetUp() override { | |
| 44 WebViewJSUtilsTest::SetUp(); | |
| 45 web_view_.reset(web::CreateWebView(CGRectZero)); | |
| 46 } | |
| 47 // UIWebView created for testing. | |
| 48 base::scoped_nsobject<UIWebView> web_view_; | |
| 49 }; | |
| 50 | |
| 51 // Test fixture for web::EvaluateJavaScript(WKWebView*..) testing. | |
| 52 class WKWebViewJSUtilsTest : public WebViewJSUtilsTest { | |
| 53 protected: | |
| 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())); | |
| 59 } | |
| 60 // WKWebView created for testing. | 43 // WKWebView created for testing. |
| 61 base::scoped_nsobject<WKWebView> web_view_; | 44 base::scoped_nsobject<WKWebView> web_view_; |
| 62 }; | 45 }; |
| 63 | 46 |
| 64 // Tests that a script with undefined result correctly evaluates to string. | 47 // Tests that a script with undefined result correctly evaluates to string. |
| 65 WEB_TEST_F(UIWebViewJSUtilsTest, WKWebViewJSUtilsTest, UndefinedEvaluation) { | 48 TEST_F(WebViewJsUtilsTest, UndefinedEvaluation) { |
| 66 EXPECT_NSEQ(@"", EvaluateJavaScript(this->web_view_, @"{}")); | 49 EXPECT_NSEQ(@"", EvaluateJavaScript(@"{}")); |
| 67 } | 50 } |
| 68 | 51 |
| 69 // Tests that a script with string result correctly evaluates to string. | 52 // Tests that a script with string result correctly evaluates to string. |
| 70 WEB_TEST_F(UIWebViewJSUtilsTest, WKWebViewJSUtilsTest, StringEvaluation) { | 53 TEST_F(WebViewJsUtilsTest, StringEvaluation) { |
| 71 EXPECT_NSEQ(@"test", EvaluateJavaScript(this->web_view_, @"'test'")); | 54 EXPECT_NSEQ(@"test", EvaluateJavaScript(@"'test'")); |
| 72 } | 55 } |
| 73 | 56 |
| 74 // Tests that a script with number result correctly evaluates to string. | 57 // Tests that a script with number result correctly evaluates to string. |
| 75 WEB_TEST_F(UIWebViewJSUtilsTest, WKWebViewJSUtilsTest, NumberEvaluation) { | 58 TEST_F(WebViewJsUtilsTest, NumberEvaluation) { |
| 76 EXPECT_NSEQ(@"-1", EvaluateJavaScript(this->web_view_, @"-1")); | 59 EXPECT_NSEQ(@"-1", EvaluateJavaScript(@"-1")); |
| 77 EXPECT_NSEQ(@"0", EvaluateJavaScript(this->web_view_, @"0")); | 60 EXPECT_NSEQ(@"0", EvaluateJavaScript(@"0")); |
| 78 EXPECT_NSEQ(@"1", EvaluateJavaScript(this->web_view_, @"1")); | 61 EXPECT_NSEQ(@"1", EvaluateJavaScript(@"1")); |
| 79 EXPECT_NSEQ(@"3.14", EvaluateJavaScript(this->web_view_, @"3.14")); | 62 EXPECT_NSEQ(@"3.14", EvaluateJavaScript(@"3.14")); |
| 80 } | 63 } |
| 81 | 64 |
| 82 // Tests that a script with bool result correctly evaluates to string. | 65 // Tests that a script with bool result correctly evaluates to string. |
| 83 WEB_TEST_F(UIWebViewJSUtilsTest, WKWebViewJSUtilsTest, BoolEvaluation) { | 66 TEST_F(WebViewJsUtilsTest, BoolEvaluation) { |
| 84 EXPECT_NSEQ(@"true", EvaluateJavaScript(this->web_view_, @"true")); | 67 EXPECT_NSEQ(@"true", EvaluateJavaScript(@"true")); |
| 85 EXPECT_NSEQ(@"false", EvaluateJavaScript(this->web_view_, @"false")); | 68 EXPECT_NSEQ(@"false", EvaluateJavaScript(@"false")); |
| 86 } | 69 } |
| 87 | 70 |
| 88 // Tests that a script with null result correctly evaluates to empty string. | 71 // Tests that a script with null result correctly evaluates to empty string. |
| 89 WEB_TEST_F(UIWebViewJSUtilsTest, WKWebViewJSUtilsTest, NullEvaluation) { | 72 TEST_F(WebViewJsUtilsTest, NullEvaluation) { |
| 90 EXPECT_NSEQ(@"", EvaluateJavaScript(this->web_view_, @"null")); | 73 EXPECT_NSEQ(@"", EvaluateJavaScript(@"null")); |
| 91 } | 74 } |
| 92 | 75 |
| 93 } // namespace | 76 } // namespace web |
| OLD | NEW |