| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/translate/ios/browser/js_translate_manager.h" | 5 #import "components/translate/ios/browser/js_translate_manager.h" |
| 6 | 6 |
| 7 #import "base/mac/scoped_nsobject.h" | |
| 8 #include "base/strings/sys_string_conversions.h" | 7 #include "base/strings/sys_string_conversions.h" |
| 9 #include "components/grit/components_resources.h" | 8 #include "components/grit/components_resources.h" |
| 10 #import "ios/web/public/test/fakes/crw_test_js_injection_receiver.h" | 9 #import "ios/web/public/test/fakes/crw_test_js_injection_receiver.h" |
| 11 #import "ios/web/public/test/js_test_util.h" | 10 #import "ios/web/public/test/js_test_util.h" |
| 12 #import "testing/gtest_mac.h" | 11 #import "testing/gtest_mac.h" |
| 13 #include "testing/platform_test.h" | 12 #include "testing/platform_test.h" |
| 14 #include "ui/base/resource/resource_bundle.h" | 13 #include "ui/base/resource/resource_bundle.h" |
| 15 | 14 |
| 15 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 16 #error "This file requires ARC support." |
| 17 #endif |
| 18 |
| 16 @interface JsTranslateManager (Testing) | 19 @interface JsTranslateManager (Testing) |
| 17 - (double)performanceNow; | 20 - (double)performanceNow; |
| 18 @end | 21 @end |
| 19 | 22 |
| 20 @implementation JsTranslateManager (Testing) | 23 @implementation JsTranslateManager (Testing) |
| 21 // Returns the time in milliseconds. | 24 // Returns the time in milliseconds. |
| 22 - (double)performanceNow { | 25 - (double)performanceNow { |
| 23 id result = web::ExecuteJavaScript(self.receiver, @"performance.now()"); | 26 id result = web::ExecuteJavaScript(self.receiver, @"performance.now()"); |
| 24 return [result doubleValue]; | 27 return [result doubleValue]; |
| 25 } | 28 } |
| 26 @end | 29 @end |
| 27 | 30 |
| 28 class JsTranslateManagerTest : public PlatformTest { | 31 class JsTranslateManagerTest : public PlatformTest { |
| 29 protected: | 32 protected: |
| 30 JsTranslateManagerTest() { | 33 JsTranslateManagerTest() { |
| 31 receiver_.reset([[CRWTestJSInjectionReceiver alloc] init]); | 34 receiver_ = [[CRWTestJSInjectionReceiver alloc] init]; |
| 32 manager_.reset([[JsTranslateManager alloc] initWithReceiver:receiver_]); | 35 manager_ = [[JsTranslateManager alloc] initWithReceiver:receiver_]; |
| 33 base::StringPiece script = | 36 base::StringPiece script = |
| 34 ResourceBundle::GetSharedInstance().GetRawDataResource( | 37 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 35 IDR_TRANSLATE_JS); | 38 IDR_TRANSLATE_JS); |
| 36 [manager_ setScript:base::SysUTF8ToNSString(script.as_string() + | 39 [manager_ setScript:base::SysUTF8ToNSString(script.as_string() + |
| 37 "('DummyKey');")]; | 40 "('DummyKey');")]; |
| 38 } | 41 } |
| 39 | 42 |
| 40 bool IsDefined(NSString* name) { | 43 bool IsDefined(NSString* name) { |
| 41 NSString* script = | 44 NSString* script = |
| 42 [NSString stringWithFormat:@"typeof %@ != 'undefined'", name]; | 45 [NSString stringWithFormat:@"typeof %@ != 'undefined'", name]; |
| 43 return [web::ExecuteJavaScript(receiver_, script) boolValue]; | 46 return [web::ExecuteJavaScript(receiver_, script) boolValue]; |
| 44 } | 47 } |
| 45 | 48 |
| 46 base::scoped_nsobject<CRWTestJSInjectionReceiver> receiver_; | 49 CRWTestJSInjectionReceiver* receiver_; |
| 47 base::scoped_nsobject<JsTranslateManager> manager_; | 50 JsTranslateManager* manager_; |
| 48 }; | 51 }; |
| 49 | 52 |
| 50 // TODO(crbug.com/658619#c47): Test reported as flaky. | 53 // TODO(crbug.com/658619#c47): Test reported as flaky. |
| 51 TEST_F(JsTranslateManagerTest, DISABLED_PerformancePlaceholder) { | 54 TEST_F(JsTranslateManagerTest, DISABLED_PerformancePlaceholder) { |
| 52 [manager_ inject]; | 55 [manager_ inject]; |
| 53 EXPECT_TRUE(IsDefined(@"performance")); | 56 EXPECT_TRUE(IsDefined(@"performance")); |
| 54 EXPECT_TRUE(IsDefined(@"performance.now")); | 57 EXPECT_TRUE(IsDefined(@"performance.now")); |
| 55 | 58 |
| 56 // Check that performance.now returns correct values. | 59 // Check that performance.now returns correct values. |
| 57 NSTimeInterval intervalInSeconds = 0.3; | 60 NSTimeInterval intervalInSeconds = 0.3; |
| 58 double startTime = [manager_ performanceNow]; | 61 double startTime = [manager_ performanceNow]; |
| 59 [NSThread sleepForTimeInterval:intervalInSeconds]; | 62 [NSThread sleepForTimeInterval:intervalInSeconds]; |
| 60 double endTime = [manager_ performanceNow]; | 63 double endTime = [manager_ performanceNow]; |
| 61 double timeElapsed = endTime - startTime; | 64 double timeElapsed = endTime - startTime; |
| 62 // The tolerance is high to avoid flake. | 65 // The tolerance is high to avoid flake. |
| 63 EXPECT_NEAR(timeElapsed, intervalInSeconds * 1000, 100); | 66 EXPECT_NEAR(timeElapsed, intervalInSeconds * 1000, 100); |
| 64 } | 67 } |
| 65 | 68 |
| 66 TEST_F(JsTranslateManagerTest, Inject) { | 69 TEST_F(JsTranslateManagerTest, Inject) { |
| 67 [manager_ inject]; | 70 [manager_ inject]; |
| 68 EXPECT_TRUE([manager_ hasBeenInjected]); | 71 EXPECT_TRUE([manager_ hasBeenInjected]); |
| 69 EXPECT_EQ(nil, [manager_ script]); | 72 EXPECT_EQ(nil, [manager_ script]); |
| 70 EXPECT_NSEQ(@NO, | 73 EXPECT_NSEQ(@NO, |
| 71 web::ExecuteJavaScript(manager_, @"cr.googleTranslate.libReady")); | 74 web::ExecuteJavaScript(manager_, @"cr.googleTranslate.libReady")); |
| 72 } | 75 } |
| OLD | NEW |