Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / translate / ios / browser / js_translate_manager_unittest.mm
blobe0115203c5f4aa141121078231b336d373ea6ae2
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"
17 using base::Time;
18 using base::TimeDelta;
20 @interface JsTranslateManager (Testing)
21 - (double)performanceNow;
22 @end
24 @implementation JsTranslateManager (Testing)
25 // Returns the time in milliseconds.
26 - (double)performanceNow {
27   NSString* result =
28       web::EvaluateJavaScriptAsString(self.receiver, @"performance.now()");
29   return [result doubleValue];
31 @end
33 class JsTranslateManagerTest : public PlatformTest {
34  protected:
35   JsTranslateManagerTest() {
36     receiver_.reset([[CRWTestJSInjectionReceiver alloc] init]);
37     manager_.reset([[JsTranslateManager alloc] initWithReceiver:receiver_]);
38     base::StringPiece script =
39         ResourceBundle::GetSharedInstance().GetRawDataResource(
40             IDR_TRANSLATE_JS);
41     [manager_ setScript:base::SysUTF8ToNSString(script.as_string() +
42                                                 "('DummyKey');")];
43   }
45   bool IsDefined(NSString* name) {
46     NSString* script =
47         [NSString stringWithFormat:@"typeof %@ != 'undefined'", name];
48     return [web::EvaluateJavaScriptAsString(receiver_, script) isEqual:@"true"];
49   }
51   base::scoped_nsobject<CRWTestJSInjectionReceiver> receiver_;
52   base::scoped_nsobject<JsTranslateManager> manager_;
55 TEST_F(JsTranslateManagerTest, PerformancePlaceholder) {
56   [manager_ inject];
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) {
71   [manager_ 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);
80       }];
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];
89   }
91   EXPECT_TRUE(block_was_called);