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
3 // found in the LICENSE file.
5 #import "components/translate/ios/browser/js_translate_manager.h"
7 #import "base/mac/scoped_nsobject.h"
8 #include "base/strings/sys_string_conversions.h"
9 #include "base/time/time.h"
10 #include "grit/components_resources.h"
11 #import "ios/web/public/test/crw_test_js_injection_receiver.h"
12 #import "ios/web/public/test/js_test_util.h"
13 #import "testing/gtest_mac.h"
14 #include "testing/platform_test.h"
15 #include "ui/base/resource/resource_bundle.h"
18 using base::TimeDelta;
20 @interface JsTranslateManager (Testing)
21 - (double)performanceNow;
24 @implementation JsTranslateManager (Testing)
25 // Returns the time in milliseconds.
26 - (double)performanceNow {
28 web::EvaluateJavaScriptAsString(self.receiver, @"performance.now()");
29 return [result doubleValue];
33 class JsTranslateManagerTest : public PlatformTest {
35 JsTranslateManagerTest() {
36 receiver_.reset([[CRWTestJSInjectionReceiver alloc] init]);
37 manager_.reset([[JsTranslateManager alloc] initWithReceiver:receiver_]);
38 base::StringPiece script =
39 ResourceBundle::GetSharedInstance().GetRawDataResource(
41 [manager_ setScript:base::SysUTF8ToNSString(script.as_string() +
45 bool IsDefined(NSString* name) {
47 [NSString stringWithFormat:@"typeof %@ != 'undefined'", name];
48 return [web::EvaluateJavaScriptAsString(receiver_, script) isEqual:@"true"];
51 base::scoped_nsobject<CRWTestJSInjectionReceiver> receiver_;
52 base::scoped_nsobject<JsTranslateManager> manager_;
55 TEST_F(JsTranslateManagerTest, PerformancePlaceholder) {
57 EXPECT_TRUE(IsDefined(@"performance"));
58 EXPECT_TRUE(IsDefined(@"performance.now"));
60 // Check that performance.now returns correct values.
61 NSTimeInterval intervalInSeconds = 0.3;
62 double startTime = [manager_ performanceNow];
63 [NSThread sleepForTimeInterval:intervalInSeconds];
64 double endTime = [manager_ performanceNow];
65 double timeElapsed = endTime - startTime;
66 // The tolerance is high to avoid flake.
67 EXPECT_NEAR(timeElapsed, intervalInSeconds * 1000, 100);
70 TEST_F(JsTranslateManagerTest, Inject) {
72 EXPECT_TRUE([manager_ hasBeenInjected]);
73 EXPECT_EQ(nil, [manager_ script]);
74 // TODO(shreyasv): Switch to the util function in web/ once that CL lands.
75 __block BOOL block_was_called = NO;
76 [manager_ evaluate:@"cr.googleTranslate.libReady"
77 stringResultHandler:^(NSString* result, NSError*) {
78 block_was_called = YES;
79 EXPECT_NSEQ(@"false", result);
81 // TODO(shreyasv): Move to |WaitUntilCondition| once that is moved to ios/.
82 const NSTimeInterval kTimeout = 5.0;
83 Time startTime = Time::Now();
84 while (!block_was_called &&
85 (Time::Now() - startTime < TimeDelta::FromSeconds(kTimeout))) {
86 NSDate* beforeDate = [NSDate dateWithTimeIntervalSinceNow:.01];
87 [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
88 beforeDate:beforeDate];
91 EXPECT_TRUE(block_was_called);