Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / translate / ios / browser / translate_controller_unittest.mm
blob0e572b998d515b98a52be740e296dac5e517ea08
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
3 // found in the LICENSE file.
5 #import "components/translate/ios/browser/translate_controller.h"
7 #include "base/values.h"
8 #import "components/translate/ios/browser/js_translate_manager.h"
9 #include "ios/web/public/test/test_web_state.h"
10 #include "testing/platform_test.h"
11 #import "third_party/ocmock/OCMock/OCMock.h"
12 #include "url/gurl.h"
14 namespace translate {
16 class TranslateControllerTest : public PlatformTest,
17                                 public TranslateController::Observer {
18  protected:
19   TranslateControllerTest()
20       : test_web_state_(new web::TestWebState),
21         success_(false),
22         ready_time_(0),
23         load_time_(0),
24         translation_time_(0),
25         on_script_ready_called_(false),
26         on_translate_complete_called_(false) {
27     mock_js_translate_manager_.reset(
28         [[OCMockObject niceMockForClass:[JsTranslateManager class]] retain]);
29     translate_controller_.reset(new TranslateController(
30         test_web_state_.get(), mock_js_translate_manager_));
31     translate_controller_->set_observer(this);
32   }
34   // TranslateController::Observer methods.
35   void OnTranslateScriptReady(bool success,
36                               double load_time,
37                               double ready_time) override {
38     on_script_ready_called_ = true;
39     success_ = success;
40     load_time_ = load_time;
41     ready_time_ = ready_time;
42   }
44   void OnTranslateComplete(bool success,
45                            const std::string& original_language,
46                            double translation_time) override {
47     on_translate_complete_called_ = true;
48     success_ = success;
49     original_language_ = original_language;
50     translation_time_ = translation_time;
51   }
53   scoped_ptr<web::TestWebState> test_web_state_;
54   base::scoped_nsobject<id> mock_js_translate_manager_;
55   scoped_ptr<TranslateController> translate_controller_;
56   bool success_;
57   double ready_time_;
58   double load_time_;
59   std::string original_language_;
60   double translation_time_;
61   bool on_script_ready_called_;
62   bool on_translate_complete_called_;
65 // Tests that OnJavascriptCommandReceived() returns false to malformed commands.
66 TEST_F(TranslateControllerTest, OnJavascriptCommandReceived) {
67   base::DictionaryValue malformed_command;
68   EXPECT_FALSE(translate_controller_->OnJavascriptCommandReceived(
69       malformed_command, GURL("http://google.com"), false));
72 // Tests that OnTranslateScriptReady() is called when a timeout message is
73 // recieved from the JS side.
74 TEST_F(TranslateControllerTest, OnTranslateScriptReadyTimeoutCalled) {
75   base::DictionaryValue command;
76   command.SetString("command", "translate.ready");
77   command.SetBoolean("timeout", true);
78   command.SetDouble("loadTime", .0);
79   command.SetDouble("readyTime", .0);
80   EXPECT_TRUE(translate_controller_->OnJavascriptCommandReceived(
81       command, GURL("http://google.com"), false));
82   EXPECT_TRUE(on_script_ready_called_);
83   EXPECT_FALSE(on_translate_complete_called_);
84   EXPECT_FALSE(success_);
87 // Tests that OnTranslateScriptReady() is called with the right parameters when
88 // a |translate.ready| message is recieved from the JS side.
89 TEST_F(TranslateControllerTest, OnTranslateScriptReadyCalled) {
90   // Arbitrary values.
91   double some_load_time = 23.1;
92   double some_ready_time = 12.2;
94   base::DictionaryValue command;
95   command.SetString("command", "translate.ready");
96   command.SetBoolean("timeout", false);
97   command.SetDouble("loadTime", some_load_time);
98   command.SetDouble("readyTime", some_ready_time);
99   EXPECT_TRUE(translate_controller_->OnJavascriptCommandReceived(
100       command, GURL("http://google.com"), false));
101   EXPECT_TRUE(on_script_ready_called_);
102   EXPECT_FALSE(on_translate_complete_called_);
103   EXPECT_TRUE(success_);
104   EXPECT_EQ(some_load_time, load_time_);
105   EXPECT_EQ(some_ready_time, ready_time_);
108 // Tests that OnTranslateComplete() is called with the right parameters when a
109 // |translate.status| message is recieved from the JS side.
110 TEST_F(TranslateControllerTest, TranslationSuccess) {
111   // Arbitrary values.
112   std::string some_original_language("en");
113   double some_translation_time = 12.9;
115   base::DictionaryValue command;
116   command.SetString("command", "translate.status");
117   command.SetBoolean("success", true);
118   command.SetString("originalPageLanguage", some_original_language);
119   command.SetDouble("translationTime", some_translation_time);
120   EXPECT_TRUE(translate_controller_->OnJavascriptCommandReceived(
121       command, GURL("http://google.com"), false));
122   EXPECT_FALSE(on_script_ready_called_);
123   EXPECT_TRUE(on_translate_complete_called_);
124   EXPECT_TRUE(success_);
125   EXPECT_EQ(some_original_language, original_language_);
126   EXPECT_EQ(some_translation_time, translation_time_);
129 // Tests that OnTranslateComplete() is called with the right parameters when a
130 // |translate.status| message is recieved from the JS side.
131 TEST_F(TranslateControllerTest, TranslationFailure) {
132   base::DictionaryValue command;
133   command.SetString("command", "translate.status");
134   command.SetBoolean("success", false);
135   EXPECT_TRUE(translate_controller_->OnJavascriptCommandReceived(
136       command, GURL("http://google.com"), false));
137   EXPECT_FALSE(on_script_ready_called_);
138   EXPECT_TRUE(on_translate_complete_called_);
139   EXPECT_FALSE(success_);
142 }  // namespace translate