diff options
author | droger <droger@chromium.org> | 2014-12-16 13:58:13 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-12-16 21:58:38 +0000 |
commit | bfba8a470717488726b909c8208f15c6a4512673 (patch) | |
tree | 56df55800551a343f58e3f46207971d58c1ae645 /ios | |
parent | 634a76e456748851c4d51a89a6b466b3f2d972cc (diff) | |
download | chromium_src-bfba8a470717488726b909c8208f15c6a4512673.zip chromium_src-bfba8a470717488726b909c8208f15c6a4512673.tar.gz chromium_src-bfba8a470717488726b909c8208f15c6a4512673.tar.bz2 |
Upstream components/translate/ios
Review URL: https://codereview.chromium.org/809693003
Cr-Commit-Position: refs/heads/master@{#308676}
Diffstat (limited to 'ios')
-rw-r--r-- | ios/ios_tests_unit.gyp | 1 | ||||
-rw-r--r-- | ios/web/ios_web.gyp | 4 | ||||
-rw-r--r-- | ios/web/public/test/js_test_util.h | 21 | ||||
-rw-r--r-- | ios/web/public/test/js_test_util.mm | 55 | ||||
-rw-r--r-- | ios/web/public/url_scheme_util.h | 25 | ||||
-rw-r--r-- | ios/web/url_scheme_util.mm | 31 | ||||
-rw-r--r-- | ios/web/url_scheme_util_unittest.mm | 31 |
7 files changed, 168 insertions, 0 deletions
diff --git a/ios/ios_tests_unit.gyp b/ios/ios_tests_unit.gyp index ed1b9b7..64da0d6 100644 --- a/ios/ios_tests_unit.gyp +++ b/ios/ios_tests_unit.gyp @@ -24,6 +24,7 @@ 'web/navigation/navigation_item_impl_unittest.mm', 'web/public/webp_decoder_unittest.mm', 'web/string_util_unittest.cc', + 'web/url_scheme_util_unittest.mm', ], 'actions': [ { diff --git a/ios/web/ios_web.gyp b/ios/web/ios_web.gyp index 0f5d4d3..d256398 100644 --- a/ios/web/ios_web.gyp +++ b/ios/web/ios_web.gyp @@ -36,6 +36,7 @@ 'public/ssl_status.cc', 'public/ssl_status.h', 'public/string_util.h', + 'public/url_scheme_util.h', 'public/user_agent.h', 'public/user_agent.mm', 'public/web_state/js/crw_js_base_manager.h', @@ -46,6 +47,7 @@ 'public/web_state/web_state_observer.h', 'public/web_thread.h', 'string_util.cc', + 'url_scheme_util.mm', 'web_state/js/crw_js_base_manager.mm', 'web_state/js/crw_js_common_manager.h', 'web_state/js/crw_js_common_manager.mm', @@ -113,6 +115,8 @@ 'sources': [ 'public/test/crw_test_js_injection_receiver.h', 'public/test/crw_test_js_injection_receiver.mm', + 'public/test/js_test_util.h', + 'public/test/js_test_util.mm', 'public/test/test_browser_state.cc', 'public/test/test_browser_state.h', 'public/test/test_web_state.cc', diff --git a/ios/web/public/test/js_test_util.h b/ios/web/public/test/js_test_util.h new file mode 100644 index 0000000..7dd4607 --- /dev/null +++ b/ios/web/public/test/js_test_util.h @@ -0,0 +1,21 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef IOS_WEB_PUBLIC_TEST_JS_TEST_UTIL_H_ +#define IOS_WEB_PUBLIC_TEST_JS_TEST_UTIL_H_ + +@class CRWJSInjectionManager; +@class CRWJSInjectionReceiver; +@class NSString; + +namespace web { +// Evaluates JavaScript on the |manager| and returns the result as a string. +NSString* EvaluateJavaScriptAsString(CRWJSInjectionManager* manager, + NSString* script); +// Evaluates JavaScript on the |receiver| and returns the result as a string. +NSString* EvaluateJavaScriptAsString(CRWJSInjectionReceiver* receiver, + NSString* script); +} // namespace web + +#endif // IOS_WEB_PUBLIC_TEST_JS_TEST_UTIL_H_ diff --git a/ios/web/public/test/js_test_util.mm b/ios/web/public/test/js_test_util.mm new file mode 100644 index 0000000..74b0298 --- /dev/null +++ b/ios/web/public/test/js_test_util.mm @@ -0,0 +1,55 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "ios/web/public/test/js_test_util.h" + +#import <Foundation/Foundation.h> + +#import "base/logging.h" +#import "base/mac/scoped_nsobject.h" +#import "base/time/time.h" +#import "ios/web/public/web_state/js/crw_js_injection_manager.h" +#import "ios/web/public/web_state/js/crw_js_injection_receiver.h" + +using base::Time; +using base::TimeDelta; + +namespace { +// The amount of time (in secs) |evaluate:StringResultHandler:| is given time to +// process until the test fails. +const NSTimeInterval kTimeout = 5.0; +} + +namespace web { + +NSString* EvaluateJavaScriptAsString(CRWJSInjectionManager* manager, + NSString* script) { + __block base::scoped_nsobject<NSString> evaluationResult; + [manager evaluate:script + stringResultHandler:^(NSString* result, NSError* error) { + DCHECK(result); + evaluationResult.reset([result copy]); + }]; + // TODO(shreyasv): This is a temporary solution to have some duplicated code. + // The right way to implement this is to use |WaitUntilCondition|. Move to + // that when that function lives in ios/. + Time startTime = Time::Now(); + while (!evaluationResult.get() && + (Time::Now() - startTime < TimeDelta::FromSeconds(kTimeout))) { + NSDate* beforeDate = [NSDate dateWithTimeIntervalSinceNow:.01]; + [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode + beforeDate:beforeDate]; + } + DCHECK(evaluationResult); + return [[evaluationResult retain] autorelease]; +} + +NSString* EvaluateJavaScriptAsString(CRWJSInjectionReceiver* receiver, + NSString* script) { + base::scoped_nsobject<CRWJSInjectionManager> manager( + [[CRWJSInjectionManager alloc] initWithReceiver:receiver]); + return EvaluateJavaScriptAsString(manager, script); +} + +} // namespace web diff --git a/ios/web/public/url_scheme_util.h b/ios/web/public/url_scheme_util.h new file mode 100644 index 0000000..7300b2f --- /dev/null +++ b/ios/web/public/url_scheme_util.h @@ -0,0 +1,25 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef IOS_WEB_PUBLIC_URL_SCHEME_UTIL_H_ +#define IOS_WEB_PUBLIC_URL_SCHEME_UTIL_H_ + +class GURL; +@class NSURL; + +namespace web { + +// Returns true if the URL has a www content scheme, i.e. http, https or data. +bool UrlHasWebScheme(const GURL& url); + +// NOTE: Use these methods only in an NSURL-only context. Otherwise, use +// GURL's IsScheme, or the GURL version of UrlHasWebScheme. +// These functions will always return the same thing that the equivalent +// GURL call would return (assuming the URL is a valid GURL). +// Returns true if the URL has a www content scheme, i.e. http, https or data. +bool UrlHasWebScheme(NSURL* url); + +} // namespace web + +#endif // IOS_WEB_PUBLIC_URL_SCHEME_UTIL_H_ diff --git a/ios/web/url_scheme_util.mm b/ios/web/url_scheme_util.mm new file mode 100644 index 0000000..945a09a --- /dev/null +++ b/ios/web/url_scheme_util.mm @@ -0,0 +1,31 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "ios/web/public/url_scheme_util.h" + +#import <Foundation/Foundation.h> + +#include "url/gurl.h" + +namespace web { + +bool UrlHasWebScheme(const GURL& url) { + return url.SchemeIs(url::kHttpScheme) || + url.SchemeIs(url::kHttpsScheme) || + url.SchemeIs(url::kDataScheme); +} + +bool UrlHasWebScheme(NSURL* url) { + NSString* scheme = [url scheme]; + if (![scheme length]) + return false; + + // Use the GURL implementation, but with a scheme-only URL to avoid + // unnecessary parsing in GURL construction. + NSString* schemeURLString = [scheme stringByAppendingString:@":"]; + GURL gurl([schemeURLString UTF8String]); + return UrlHasWebScheme(gurl); +} + +} // namespace web diff --git a/ios/web/url_scheme_util_unittest.mm b/ios/web/url_scheme_util_unittest.mm new file mode 100644 index 0000000..1492088 --- /dev/null +++ b/ios/web/url_scheme_util_unittest.mm @@ -0,0 +1,31 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "ios/web/public/url_scheme_util.h" + +#import <Foundation/Foundation.h> + +#import "testing/gtest_mac.h" +#include "url/gurl.h" + +namespace web { + +TEST(URLSchemeUtilTest, UrlHasWebScheme) { + EXPECT_TRUE(UrlHasWebScheme(GURL("http://foo.com"))); + EXPECT_TRUE(UrlHasWebScheme(GURL("https://foo.com"))); + EXPECT_TRUE(UrlHasWebScheme(GURL("data:text/html;charset=utf-8,Hello"))); + EXPECT_FALSE(UrlHasWebScheme(GURL("about:blank"))); + EXPECT_FALSE(UrlHasWebScheme(GURL("chrome://settings"))); +} + +TEST(URLSchemeUtilTest, NSURLHasWebScheme) { + EXPECT_TRUE(UrlHasWebScheme([NSURL URLWithString:@"http://foo.com"])); + EXPECT_TRUE(UrlHasWebScheme([NSURL URLWithString:@"https://foo.com"])); + EXPECT_TRUE(UrlHasWebScheme( + [NSURL URLWithString:@"data:text/html;charset=utf-8,Hello"])); + EXPECT_FALSE(UrlHasWebScheme([NSURL URLWithString:@"about:blank"])); + EXPECT_FALSE(UrlHasWebScheme([NSURL URLWithString:@"chrome://settings"])); +} + +} // namespace web |