OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "base/time/time.h" | 5 #include "base/time/time.h" |
6 #include "chrome/renderer/translate/translate_helper.h" | 6 #include "chrome/renderer/translate/translate_helper.h" |
7 #include "chrome/test/base/chrome_render_view_test.h" | 7 #include "chrome/test/base/chrome_render_view_test.h" |
8 #include "components/translate/content/common/translate_messages.h" | 8 #include "components/translate/content/common/translate_messages.h" |
9 #include "components/translate/core/common/translate_constants.h" | 9 #include "components/translate/core/common/translate_constants.h" |
10 #include "content/public/renderer/render_view.h" | 10 #include "content/public/renderer/render_view.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 translate_helper_ = new TestTranslateHelper(view_); | 60 translate_helper_ = new TestTranslateHelper(view_); |
61 } | 61 } |
62 | 62 |
63 virtual void TearDown() OVERRIDE { | 63 virtual void TearDown() OVERRIDE { |
64 delete translate_helper_; | 64 delete translate_helper_; |
65 ChromeRenderViewTest::TearDown(); | 65 ChromeRenderViewTest::TearDown(); |
66 } | 66 } |
67 | 67 |
68 bool GetPageTranslatedMessage(std::string* original_lang, | 68 bool GetPageTranslatedMessage(std::string* original_lang, |
69 std::string* target_lang, | 69 std::string* target_lang, |
70 TranslateErrors::Type* error) { | 70 translate::TranslateErrors::Type* error) { |
71 const IPC::Message* message = render_thread_->sink(). | 71 const IPC::Message* message = render_thread_->sink(). |
72 GetUniqueMessageMatching(ChromeViewHostMsg_PageTranslated::ID); | 72 GetUniqueMessageMatching(ChromeViewHostMsg_PageTranslated::ID); |
73 if (!message) | 73 if (!message) |
74 return false; | 74 return false; |
75 Tuple3<std::string, std::string, TranslateErrors::Type> translate_param; | 75 Tuple3<std::string, std::string, translate::TranslateErrors::Type> |
| 76 translate_param; |
76 ChromeViewHostMsg_PageTranslated::Read(message, &translate_param); | 77 ChromeViewHostMsg_PageTranslated::Read(message, &translate_param); |
77 if (original_lang) | 78 if (original_lang) |
78 *original_lang = translate_param.a; | 79 *original_lang = translate_param.a; |
79 if (target_lang) | 80 if (target_lang) |
80 *target_lang = translate_param.b; | 81 *target_lang = translate_param.b; |
81 if (error) | 82 if (error) |
82 *error = translate_param.c; | 83 *error = translate_param.c; |
83 return true; | 84 return true; |
84 } | 85 } |
85 | 86 |
(...skipping 13 matching lines...) Expand all Loading... |
99 .WillRepeatedly(Return(true)); | 100 .WillRepeatedly(Return(true)); |
100 | 101 |
101 EXPECT_CALL(*translate_helper_, IsTranslateLibReady()) | 102 EXPECT_CALL(*translate_helper_, IsTranslateLibReady()) |
102 .Times(AtLeast(5)) // See kMaxTranslateInitCheckAttempts in | 103 .Times(AtLeast(5)) // See kMaxTranslateInitCheckAttempts in |
103 // translate_helper.cc | 104 // translate_helper.cc |
104 .WillRepeatedly(Return(false)); | 105 .WillRepeatedly(Return(false)); |
105 | 106 |
106 translate_helper_->TranslatePage("en", "fr", std::string()); | 107 translate_helper_->TranslatePage("en", "fr", std::string()); |
107 base::MessageLoop::current()->RunUntilIdle(); | 108 base::MessageLoop::current()->RunUntilIdle(); |
108 | 109 |
109 TranslateErrors::Type error; | 110 translate::TranslateErrors::Type error; |
110 ASSERT_TRUE(GetPageTranslatedMessage(NULL, NULL, &error)); | 111 ASSERT_TRUE(GetPageTranslatedMessage(NULL, NULL, &error)); |
111 EXPECT_EQ(TranslateErrors::INITIALIZATION_ERROR, error); | 112 EXPECT_EQ(translate::TranslateErrors::INITIALIZATION_ERROR, error); |
112 } | 113 } |
113 | 114 |
114 // Tests that the browser gets notified of the translation success when the | 115 // Tests that the browser gets notified of the translation success when the |
115 // translation succeeds. | 116 // translation succeeds. |
116 TEST_F(TranslateHelperBrowserTest, TranslateSuccess) { | 117 TEST_F(TranslateHelperBrowserTest, TranslateSuccess) { |
117 // We make IsTranslateLibAvailable true so we don't attempt to inject the | 118 // We make IsTranslateLibAvailable true so we don't attempt to inject the |
118 // library. | 119 // library. |
119 EXPECT_CALL(*translate_helper_, IsTranslateLibAvailable()) | 120 EXPECT_CALL(*translate_helper_, IsTranslateLibAvailable()) |
120 .Times(AtLeast(1)) | 121 .Times(AtLeast(1)) |
121 .WillRepeatedly(Return(true)); | 122 .WillRepeatedly(Return(true)); |
(...skipping 16 matching lines...) Expand all Loading... |
138 EXPECT_CALL(*translate_helper_, | 139 EXPECT_CALL(*translate_helper_, |
139 ExecuteScriptAndGetDoubleResult(_)).Times(3); | 140 ExecuteScriptAndGetDoubleResult(_)).Times(3); |
140 | 141 |
141 std::string original_lang("en"); | 142 std::string original_lang("en"); |
142 std::string target_lang("fr"); | 143 std::string target_lang("fr"); |
143 translate_helper_->TranslatePage(original_lang, target_lang, std::string()); | 144 translate_helper_->TranslatePage(original_lang, target_lang, std::string()); |
144 base::MessageLoop::current()->RunUntilIdle(); | 145 base::MessageLoop::current()->RunUntilIdle(); |
145 | 146 |
146 std::string received_original_lang; | 147 std::string received_original_lang; |
147 std::string received_target_lang; | 148 std::string received_target_lang; |
148 TranslateErrors::Type error; | 149 translate::TranslateErrors::Type error; |
149 ASSERT_TRUE(GetPageTranslatedMessage(&received_original_lang, | 150 ASSERT_TRUE(GetPageTranslatedMessage(&received_original_lang, |
150 &received_target_lang, | 151 &received_target_lang, |
151 &error)); | 152 &error)); |
152 EXPECT_EQ(original_lang, received_original_lang); | 153 EXPECT_EQ(original_lang, received_original_lang); |
153 EXPECT_EQ(target_lang, received_target_lang); | 154 EXPECT_EQ(target_lang, received_target_lang); |
154 EXPECT_EQ(TranslateErrors::NONE, error); | 155 EXPECT_EQ(translate::TranslateErrors::NONE, error); |
155 } | 156 } |
156 | 157 |
157 // Tests that the browser gets notified of the translation failure when the | 158 // Tests that the browser gets notified of the translation failure when the |
158 // translation fails. | 159 // translation fails. |
159 TEST_F(TranslateHelperBrowserTest, TranslateFailure) { | 160 TEST_F(TranslateHelperBrowserTest, TranslateFailure) { |
160 // We make IsTranslateLibAvailable true so we don't attempt to inject the | 161 // We make IsTranslateLibAvailable true so we don't attempt to inject the |
161 // library. | 162 // library. |
162 EXPECT_CALL(*translate_helper_, IsTranslateLibAvailable()) | 163 EXPECT_CALL(*translate_helper_, IsTranslateLibAvailable()) |
163 .Times(AtLeast(1)) | 164 .Times(AtLeast(1)) |
164 .WillRepeatedly(Return(true)); | 165 .WillRepeatedly(Return(true)); |
(...skipping 14 matching lines...) Expand all Loading... |
179 .Times(AtLeast(1)) | 180 .Times(AtLeast(1)) |
180 .WillRepeatedly(Return(false)); | 181 .WillRepeatedly(Return(false)); |
181 | 182 |
182 // V8 call for performance monitoring should be ignored. | 183 // V8 call for performance monitoring should be ignored. |
183 EXPECT_CALL(*translate_helper_, | 184 EXPECT_CALL(*translate_helper_, |
184 ExecuteScriptAndGetDoubleResult(_)).Times(2); | 185 ExecuteScriptAndGetDoubleResult(_)).Times(2); |
185 | 186 |
186 translate_helper_->TranslatePage("en", "fr", std::string()); | 187 translate_helper_->TranslatePage("en", "fr", std::string()); |
187 base::MessageLoop::current()->RunUntilIdle(); | 188 base::MessageLoop::current()->RunUntilIdle(); |
188 | 189 |
189 TranslateErrors::Type error; | 190 translate::TranslateErrors::Type error; |
190 ASSERT_TRUE(GetPageTranslatedMessage(NULL, NULL, &error)); | 191 ASSERT_TRUE(GetPageTranslatedMessage(NULL, NULL, &error)); |
191 EXPECT_EQ(TranslateErrors::TRANSLATION_ERROR, error); | 192 EXPECT_EQ(translate::TranslateErrors::TRANSLATION_ERROR, error); |
192 } | 193 } |
193 | 194 |
194 // Tests that when the browser translate a page for which the language is | 195 // Tests that when the browser translate a page for which the language is |
195 // undefined we query the translate element to get the language. | 196 // undefined we query the translate element to get the language. |
196 TEST_F(TranslateHelperBrowserTest, UndefinedSourceLang) { | 197 TEST_F(TranslateHelperBrowserTest, UndefinedSourceLang) { |
197 // We make IsTranslateLibAvailable true so we don't attempt to inject the | 198 // We make IsTranslateLibAvailable true so we don't attempt to inject the |
198 // library. | 199 // library. |
199 EXPECT_CALL(*translate_helper_, IsTranslateLibAvailable()) | 200 EXPECT_CALL(*translate_helper_, IsTranslateLibAvailable()) |
200 .Times(AtLeast(1)) | 201 .Times(AtLeast(1)) |
201 .WillRepeatedly(Return(true)); | 202 .WillRepeatedly(Return(true)); |
(...skipping 13 matching lines...) Expand all Loading... |
215 | 216 |
216 // V8 call for performance monitoring should be ignored. | 217 // V8 call for performance monitoring should be ignored. |
217 EXPECT_CALL(*translate_helper_, | 218 EXPECT_CALL(*translate_helper_, |
218 ExecuteScriptAndGetDoubleResult(_)).Times(3); | 219 ExecuteScriptAndGetDoubleResult(_)).Times(3); |
219 | 220 |
220 translate_helper_->TranslatePage(translate::kUnknownLanguageCode, | 221 translate_helper_->TranslatePage(translate::kUnknownLanguageCode, |
221 "fr", | 222 "fr", |
222 std::string()); | 223 std::string()); |
223 base::MessageLoop::current()->RunUntilIdle(); | 224 base::MessageLoop::current()->RunUntilIdle(); |
224 | 225 |
225 TranslateErrors::Type error; | 226 translate::TranslateErrors::Type error; |
226 std::string original_lang; | 227 std::string original_lang; |
227 std::string target_lang; | 228 std::string target_lang; |
228 ASSERT_TRUE(GetPageTranslatedMessage(&original_lang, &target_lang, &error)); | 229 ASSERT_TRUE(GetPageTranslatedMessage(&original_lang, &target_lang, &error)); |
229 EXPECT_EQ("de", original_lang); | 230 EXPECT_EQ("de", original_lang); |
230 EXPECT_EQ("fr", target_lang); | 231 EXPECT_EQ("fr", target_lang); |
231 EXPECT_EQ(TranslateErrors::NONE, error); | 232 EXPECT_EQ(translate::TranslateErrors::NONE, error); |
232 } | 233 } |
233 | 234 |
234 // Tests that starting a translation while a similar one is pending does not | 235 // Tests that starting a translation while a similar one is pending does not |
235 // break anything. | 236 // break anything. |
236 TEST_F(TranslateHelperBrowserTest, MultipleSimilarTranslations) { | 237 TEST_F(TranslateHelperBrowserTest, MultipleSimilarTranslations) { |
237 // We make IsTranslateLibAvailable true so we don't attempt to inject the | 238 // We make IsTranslateLibAvailable true so we don't attempt to inject the |
238 // library. | 239 // library. |
239 EXPECT_CALL(*translate_helper_, IsTranslateLibAvailable()) | 240 EXPECT_CALL(*translate_helper_, IsTranslateLibAvailable()) |
240 .Times(AtLeast(1)) | 241 .Times(AtLeast(1)) |
241 .WillRepeatedly(Return(true)); | 242 .WillRepeatedly(Return(true)); |
(...skipping 14 matching lines...) Expand all Loading... |
256 std::string original_lang("en"); | 257 std::string original_lang("en"); |
257 std::string target_lang("fr"); | 258 std::string target_lang("fr"); |
258 translate_helper_->TranslatePage(original_lang, target_lang, std::string()); | 259 translate_helper_->TranslatePage(original_lang, target_lang, std::string()); |
259 // While this is running call again TranslatePage to make sure noting bad | 260 // While this is running call again TranslatePage to make sure noting bad |
260 // happens. | 261 // happens. |
261 translate_helper_->TranslatePage(original_lang, target_lang, std::string()); | 262 translate_helper_->TranslatePage(original_lang, target_lang, std::string()); |
262 base::MessageLoop::current()->RunUntilIdle(); | 263 base::MessageLoop::current()->RunUntilIdle(); |
263 | 264 |
264 std::string received_original_lang; | 265 std::string received_original_lang; |
265 std::string received_target_lang; | 266 std::string received_target_lang; |
266 TranslateErrors::Type error; | 267 translate::TranslateErrors::Type error; |
267 ASSERT_TRUE(GetPageTranslatedMessage(&received_original_lang, | 268 ASSERT_TRUE(GetPageTranslatedMessage(&received_original_lang, |
268 &received_target_lang, | 269 &received_target_lang, |
269 &error)); | 270 &error)); |
270 EXPECT_EQ(original_lang, received_original_lang); | 271 EXPECT_EQ(original_lang, received_original_lang); |
271 EXPECT_EQ(target_lang, received_target_lang); | 272 EXPECT_EQ(target_lang, received_target_lang); |
272 EXPECT_EQ(TranslateErrors::NONE, error); | 273 EXPECT_EQ(translate::TranslateErrors::NONE, error); |
273 } | 274 } |
274 | 275 |
275 // Tests that starting a translation while a different one is pending works. | 276 // Tests that starting a translation while a different one is pending works. |
276 TEST_F(TranslateHelperBrowserTest, MultipleDifferentTranslations) { | 277 TEST_F(TranslateHelperBrowserTest, MultipleDifferentTranslations) { |
277 EXPECT_CALL(*translate_helper_, IsTranslateLibAvailable()) | 278 EXPECT_CALL(*translate_helper_, IsTranslateLibAvailable()) |
278 .Times(AtLeast(1)) | 279 .Times(AtLeast(1)) |
279 .WillRepeatedly(Return(true)); | 280 .WillRepeatedly(Return(true)); |
280 EXPECT_CALL(*translate_helper_, IsTranslateLibReady()) | 281 EXPECT_CALL(*translate_helper_, IsTranslateLibReady()) |
281 .WillRepeatedly(Return(true)); | 282 .WillRepeatedly(Return(true)); |
282 EXPECT_CALL(*translate_helper_, StartTranslation()) | 283 EXPECT_CALL(*translate_helper_, StartTranslation()) |
(...skipping 11 matching lines...) Expand all Loading... |
294 std::string target_lang("fr"); | 295 std::string target_lang("fr"); |
295 translate_helper_->TranslatePage(original_lang, target_lang, std::string()); | 296 translate_helper_->TranslatePage(original_lang, target_lang, std::string()); |
296 // While this is running call again TranslatePage with a new target lang. | 297 // While this is running call again TranslatePage with a new target lang. |
297 std::string new_target_lang("de"); | 298 std::string new_target_lang("de"); |
298 translate_helper_->TranslatePage( | 299 translate_helper_->TranslatePage( |
299 original_lang, new_target_lang, std::string()); | 300 original_lang, new_target_lang, std::string()); |
300 base::MessageLoop::current()->RunUntilIdle(); | 301 base::MessageLoop::current()->RunUntilIdle(); |
301 | 302 |
302 std::string received_original_lang; | 303 std::string received_original_lang; |
303 std::string received_target_lang; | 304 std::string received_target_lang; |
304 TranslateErrors::Type error; | 305 translate::TranslateErrors::Type error; |
305 ASSERT_TRUE(GetPageTranslatedMessage(&received_original_lang, | 306 ASSERT_TRUE(GetPageTranslatedMessage(&received_original_lang, |
306 &received_target_lang, | 307 &received_target_lang, |
307 &error)); | 308 &error)); |
308 EXPECT_EQ(original_lang, received_original_lang); | 309 EXPECT_EQ(original_lang, received_original_lang); |
309 EXPECT_EQ(new_target_lang, received_target_lang); | 310 EXPECT_EQ(new_target_lang, received_target_lang); |
310 EXPECT_EQ(TranslateErrors::NONE, error); | 311 EXPECT_EQ(translate::TranslateErrors::NONE, error); |
311 } | 312 } |
312 | 313 |
313 // Tests that we send the right translate language message for a page and that | 314 // Tests that we send the right translate language message for a page and that |
314 // we respect the "no translate" meta-tag. | 315 // we respect the "no translate" meta-tag. |
315 TEST_F(ChromeRenderViewTest, TranslatablePage) { | 316 TEST_F(ChromeRenderViewTest, TranslatablePage) { |
316 // Suppress the normal delay that occurs when the page is loaded before which | 317 // Suppress the normal delay that occurs when the page is loaded before which |
317 // the renderer sends the page contents to the browser. | 318 // the renderer sends the page contents to the browser. |
318 SendContentStateImmediately(); | 319 SendContentStateImmediately(); |
319 | 320 |
320 LoadHTML("<html><body>A random page with random content.</body></html>"); | 321 LoadHTML("<html><body>A random page with random content.</body></html>"); |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
447 | 448 |
448 GoBack(back_state); | 449 GoBack(back_state); |
449 | 450 |
450 message = render_thread_->sink().GetUniqueMessageMatching( | 451 message = render_thread_->sink().GetUniqueMessageMatching( |
451 ChromeViewHostMsg_TranslateLanguageDetermined::ID); | 452 ChromeViewHostMsg_TranslateLanguageDetermined::ID); |
452 ASSERT_NE(static_cast<IPC::Message*>(NULL), message); | 453 ASSERT_NE(static_cast<IPC::Message*>(NULL), message); |
453 ChromeViewHostMsg_TranslateLanguageDetermined::Read(message, ¶ms); | 454 ChromeViewHostMsg_TranslateLanguageDetermined::Read(message, ¶ms); |
454 EXPECT_EQ("zh", params.a.adopted_language); | 455 EXPECT_EQ("zh", params.a.adopted_language); |
455 render_thread_->sink().ClearMessages(); | 456 render_thread_->sink().ClearMessages(); |
456 } | 457 } |
OLD | NEW |