Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / translate / translate_manager_render_view_host_unittest.cc
blob3690af47e6f5dd34f8cc4179a55e7b0aed5b53df
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 #include <algorithm>
6 #include <set>
7 #include <vector>
9 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/prefs/pref_change_registrar.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/strings/stringprintf.h"
14 #include "chrome/app/chrome_command_ids.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/extensions/test_extension_system.h"
17 #include "chrome/browser/infobars/infobar_service.h"
18 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h"
19 #include "chrome/browser/translate/chrome_translate_client.h"
20 #include "chrome/browser/translate/translate_service.h"
21 #include "chrome/browser/ui/translate/translate_bubble_factory.h"
22 #include "chrome/browser/ui/translate/translate_bubble_model.h"
23 #include "chrome/browser/ui/translate/translate_bubble_model_impl.h"
24 #include "chrome/common/chrome_switches.h"
25 #include "chrome/common/pref_names.h"
26 #include "chrome/common/url_constants.h"
27 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
28 #include "chrome/test/base/testing_browser_process.h"
29 #include "chrome/test/base/testing_profile.h"
30 #include "components/infobars/core/infobar.h"
31 #include "components/translate/content/browser/content_translate_driver.h"
32 #include "components/translate/content/common/translate_messages.h"
33 #include "components/translate/core/browser/translate_accept_languages.h"
34 #include "components/translate/core/browser/translate_download_manager.h"
35 #include "components/translate/core/browser/translate_infobar_delegate.h"
36 #include "components/translate/core/browser/translate_language_list.h"
37 #include "components/translate/core/browser/translate_manager.h"
38 #include "components/translate/core/browser/translate_prefs.h"
39 #include "components/translate/core/browser/translate_script.h"
40 #include "components/translate/core/common/language_detection_details.h"
41 #include "components/translate/core/common/translate_pref_names.h"
42 #include "content/public/browser/navigation_details.h"
43 #include "content/public/browser/navigation_entry.h"
44 #include "content/public/browser/notification_details.h"
45 #include "content/public/browser/notification_registrar.h"
46 #include "content/public/browser/web_contents.h"
47 #include "content/public/common/url_constants.h"
48 #include "content/public/test/mock_render_process_host.h"
49 #include "content/public/test/test_renderer_host.h"
50 #include "net/base/net_errors.h"
51 #include "net/url_request/test_url_fetcher_factory.h"
52 #include "net/url_request/url_fetcher_delegate.h"
53 #include "net/url_request/url_request_status.h"
54 #include "testing/gmock/include/gmock/gmock.h"
55 #include "third_party/WebKit/public/web/WebContextMenuData.h"
56 #include "url/gurl.h"
58 // An observer that keeps track of whether a navigation entry was committed.
59 class NavEntryCommittedObserver : public content::NotificationObserver {
60 public:
61 explicit NavEntryCommittedObserver(content::WebContents* web_contents) {
62 registrar_.Add(this,
63 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
64 content::Source<content::NavigationController>(
65 &web_contents->GetController()));
68 void Observe(int type,
69 const content::NotificationSource& source,
70 const content::NotificationDetails& details) override {
71 DCHECK(type == content::NOTIFICATION_NAV_ENTRY_COMMITTED);
72 details_ =
73 *(content::Details<content::LoadCommittedDetails>(details).ptr());
76 const content::LoadCommittedDetails& load_committed_details() const {
77 return details_;
80 private:
81 content::LoadCommittedDetails details_;
82 content::NotificationRegistrar registrar_;
84 DISALLOW_COPY_AND_ASSIGN(NavEntryCommittedObserver);
87 class TranslateManagerRenderViewHostTest
88 : public ChromeRenderViewHostTestHarness,
89 public content::NotificationObserver {
90 public:
91 TranslateManagerRenderViewHostTest()
92 : pref_callback_(
93 base::Bind(&TranslateManagerRenderViewHostTest::OnPreferenceChanged,
94 base::Unretained(this))) {}
96 // Simulates navigating to a page and getting the page contents and language
97 // for that navigation.
98 void SimulateNavigation(const GURL& url,
99 const std::string& lang,
100 bool page_translatable) {
101 if (rvh()->GetMainFrame()->GetLastCommittedURL() == url)
102 Reload();
103 else
104 NavigateAndCommit(url);
105 SimulateOnTranslateLanguageDetermined(lang, page_translatable);
108 void SimulateOnTranslateLanguageDetermined(const std::string& lang,
109 bool page_translatable) {
110 translate::LanguageDetectionDetails details;
111 details.adopted_language = lang;
112 content::RenderViewHostTester::TestOnMessageReceived(
113 rvh(),
114 ChromeViewHostMsg_TranslateLanguageDetermined(
115 0, details, page_translatable));
118 void SimulateOnPageTranslated(int routing_id,
119 const std::string& source_lang,
120 const std::string& target_lang,
121 translate::TranslateErrors::Type error) {
122 content::RenderViewHostTester::TestOnMessageReceived(
123 rvh(),
124 ChromeViewHostMsg_PageTranslated(
125 routing_id, source_lang, target_lang, error));
128 void SimulateOnPageTranslated(const std::string& source_lang,
129 const std::string& target_lang) {
130 SimulateOnPageTranslated(
131 0, source_lang, target_lang, translate::TranslateErrors::NONE);
134 bool GetTranslateMessage(std::string* original_lang,
135 std::string* target_lang) {
136 const IPC::Message* message = process()->sink().GetFirstMessageMatching(
137 ChromeViewMsg_TranslatePage::ID);
138 if (!message)
139 return false;
140 base::Tuple<int, std::string, std::string, std::string> translate_param;
141 ChromeViewMsg_TranslatePage::Read(message, &translate_param);
142 // Ignore get<0>(translate_param) which is the page seq no.
143 // Ignore get<1>(translate_param) which is the script injected in the page.
144 if (original_lang)
145 *original_lang = base::get<2>(translate_param);
146 if (target_lang)
147 *target_lang = base::get<3>(translate_param);
148 return true;
151 InfoBarService* infobar_service() {
152 return InfoBarService::FromWebContents(web_contents());
155 // Returns the translate infobar if there is 1 infobar and it is a translate
156 // infobar.
157 translate::TranslateInfoBarDelegate* GetTranslateInfoBar() {
158 return (infobar_service()->infobar_count() == 1) ?
159 infobar_service()->infobar_at(0)->delegate()->
160 AsTranslateInfoBarDelegate() :
161 NULL;
164 // If there is 1 infobar and it is a translate infobar, closes it and returns
165 // true. Returns false otherwise.
166 bool CloseTranslateInfoBar() {
167 infobars::InfoBarDelegate* infobar = GetTranslateInfoBar();
168 if (!infobar)
169 return false;
170 infobar->InfoBarDismissed(); // Simulates closing the infobar.
171 infobar_service()->RemoveInfoBar(infobar_service()->infobar_at(0));
172 return true;
175 // Checks whether |infobar| has been removed and clears the removed infobar
176 // list.
177 bool CheckInfoBarRemovedAndReset(infobars::InfoBarDelegate* delegate) {
178 bool found = removed_infobars_.count(delegate) != 0;
179 removed_infobars_.clear();
180 return found;
183 void ExpireTranslateScriptImmediately() {
184 translate::TranslateDownloadManager::GetInstance()
185 ->SetTranslateScriptExpirationDelay(0);
188 // If there is 1 infobar and it is a translate infobar, deny translation and
189 // returns true. Returns false otherwise.
190 bool DenyTranslation() {
191 translate::TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
192 if (!infobar)
193 return false;
194 infobar->TranslationDeclined();
195 infobar_service()->RemoveInfoBar(infobar_service()->infobar_at(0));
196 return true;
199 void ReloadAndWait(bool successful_reload) {
200 NavEntryCommittedObserver nav_observer(web_contents());
201 if (successful_reload)
202 Reload();
203 else
204 FailedReload();
206 // Ensures it is really handled a reload.
207 const content::LoadCommittedDetails& nav_details =
208 nav_observer.load_committed_details();
209 EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation.
210 EXPECT_EQ(content::NAVIGATION_TYPE_EXISTING_PAGE, nav_details.type);
212 // The TranslateManager class processes the navigation entry committed
213 // notification in a posted task; process that task.
214 base::MessageLoop::current()->RunUntilIdle();
217 TestRenderViewContextMenu* CreateContextMenu() {
218 content::ContextMenuParams params;
219 params.media_type = blink::WebContextMenuData::MediaTypeNone;
220 params.x = 0;
221 params.y = 0;
222 params.has_image_contents = true;
223 params.media_flags = 0;
224 params.spellcheck_enabled = false;
225 params.is_editable = false;
226 params.page_url =
227 web_contents()->GetController().GetActiveEntry()->GetURL();
228 #if defined(OS_MACOSX)
229 params.writing_direction_default = 0;
230 params.writing_direction_left_to_right = 0;
231 params.writing_direction_right_to_left = 0;
232 #endif // OS_MACOSX
233 params.edit_flags = blink::WebContextMenuData::CanTranslate;
234 return new TestRenderViewContextMenu(web_contents()->GetMainFrame(),
235 params);
238 virtual void Observe(int type,
239 const content::NotificationSource& source,
240 const content::NotificationDetails& details) {
241 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type);
242 removed_infobars_.insert(
243 content::Details<infobars::InfoBar::RemovedDetails>(
244 details)->first->delegate());
247 MOCK_METHOD1(OnPreferenceChanged, void(const std::string&));
249 protected:
250 virtual void SetUp() {
251 TranslateService::InitializeForTesting();
253 // Clears the translate script so it is fetched everytime and sets the
254 // expiration delay to a large value by default (in case it was zeroed in a
255 // previous test).
256 translate::TranslateDownloadManager* download_manager =
257 translate::TranslateDownloadManager::GetInstance();
258 download_manager->ClearTranslateScriptForTesting();
259 download_manager->SetTranslateScriptExpirationDelay(60 * 60 * 1000);
261 ChromeRenderViewHostTestHarness::SetUp();
262 InfoBarService::CreateForWebContents(web_contents());
263 ChromeTranslateClient::CreateForWebContents(web_contents());
264 ChromeTranslateClient::FromWebContents(web_contents())
265 ->translate_driver()
266 .set_translate_max_reload_attempts(0);
268 notification_registrar_.Add(
269 this,
270 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
271 content::Source<InfoBarService>(infobar_service()));
274 virtual void TearDown() {
275 process()->sink().ClearMessages();
277 notification_registrar_.Remove(
278 this,
279 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
280 content::Source<InfoBarService>(infobar_service()));
282 ChromeRenderViewHostTestHarness::TearDown();
283 TranslateService::ShutdownForTesting();
286 void SetApplicationLocale(const std::string& locale) {
287 g_browser_process->SetApplicationLocale(locale);
288 translate::TranslateDownloadManager::GetInstance()->set_application_locale(
289 g_browser_process->GetApplicationLocale());
292 void SimulateTranslateScriptURLFetch(bool success) {
293 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(
294 translate::TranslateScript::kFetcherId);
295 ASSERT_TRUE(fetcher);
296 net::Error error = success ? net::OK : net::ERR_FAILED;
297 fetcher->set_url(fetcher->GetOriginalURL());
298 fetcher->set_status(net::URLRequestStatus::FromError(error));
299 fetcher->set_response_code(success ? 200 : 500);
300 fetcher->delegate()->OnURLFetchComplete(fetcher);
303 void SimulateSupportedLanguagesURLFetch(
304 bool success,
305 const std::vector<std::string>& languages,
306 bool use_alpha_languages,
307 const std::vector<std::string>& alpha_languages) {
308 net::Error error = success ? net::OK : net::ERR_FAILED;
310 std::string data;
311 if (success) {
312 data = base::StringPrintf(
313 "{\"sl\": {\"bla\": \"bla\"}, \"%s\": {",
314 translate::TranslateLanguageList::kTargetLanguagesKey);
315 const char* comma = "";
316 for (size_t i = 0; i < languages.size(); ++i) {
317 data += base::StringPrintf(
318 "%s\"%s\": \"UnusedFullName\"", comma, languages[i].c_str());
319 if (i == 0)
320 comma = ",";
323 if (use_alpha_languages) {
324 data += base::StringPrintf(
325 "},\"%s\": {",
326 translate::TranslateLanguageList::kAlphaLanguagesKey);
327 comma = "";
328 for (size_t i = 0; i < alpha_languages.size(); ++i) {
329 data += base::StringPrintf(
330 "%s\"%s\": 1", comma, alpha_languages[i].c_str());
331 if (i == 0)
332 comma = ",";
336 data += "}}";
338 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(
339 translate::TranslateLanguageList::kFetcherId);
340 ASSERT_TRUE(fetcher != NULL);
341 fetcher->set_url(fetcher->GetOriginalURL());
342 fetcher->set_status(net::URLRequestStatus::FromError(error));
343 fetcher->set_response_code(success ? 200 : 500);
344 fetcher->SetResponseString(data);
345 fetcher->delegate()->OnURLFetchComplete(fetcher);
348 void SetPrefObserverExpectation(const char* path) {
349 EXPECT_CALL(*this, OnPreferenceChanged(std::string(path)));
352 PrefChangeRegistrar::NamedChangeCallback pref_callback_;
354 private:
355 content::NotificationRegistrar notification_registrar_;
356 net::TestURLFetcherFactory url_fetcher_factory_;
358 // The infobars that have been removed.
359 // WARNING: the pointers point to deleted objects, use only for comparison.
360 std::set<infobars::InfoBarDelegate*> removed_infobars_;
362 DISALLOW_COPY_AND_ASSIGN(TranslateManagerRenderViewHostTest);
365 class MockTranslateBubbleFactory : public TranslateBubbleFactory {
366 public:
367 MockTranslateBubbleFactory() {}
369 void ShowImplementation(
370 BrowserWindow* window,
371 content::WebContents* web_contents,
372 translate::TranslateStep step,
373 translate::TranslateErrors::Type error_type) override {
374 if (model_) {
375 model_->SetViewState(
376 TranslateBubbleModelImpl::TranslateStepToViewState(step));
377 return;
380 ChromeTranslateClient* chrome_translate_client =
381 ChromeTranslateClient::FromWebContents(web_contents);
382 std::string source_language =
383 chrome_translate_client->GetLanguageState().original_language();
384 std::string target_language =
385 translate::TranslateDownloadManager::GetLanguageCode(
386 g_browser_process->GetApplicationLocale());
388 scoped_ptr<translate::TranslateUIDelegate> ui_delegate(
389 new translate::TranslateUIDelegate(
390 chrome_translate_client->GetTranslateManager()->GetWeakPtr(),
391 source_language,
392 target_language));
393 model_.reset(new TranslateBubbleModelImpl(step, ui_delegate.Pass()));
396 TranslateBubbleModel* model() { return model_.get(); }
398 private:
399 scoped_ptr<TranslateBubbleModel> model_;
401 DISALLOW_COPY_AND_ASSIGN(MockTranslateBubbleFactory);
405 TEST_F(TranslateManagerRenderViewHostTest, NormalTranslate) {
406 // See BubbleNormalTranslate for corresponding bubble UX testing.
407 if (TranslateService::IsTranslateBubbleEnabled())
408 return;
410 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
412 // We should have an infobar.
413 translate::TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
414 ASSERT_TRUE(infobar != NULL);
415 EXPECT_EQ(translate::TRANSLATE_STEP_BEFORE_TRANSLATE,
416 infobar->translate_step());
418 // Simulate clicking translate.
419 process()->sink().ClearMessages();
420 infobar->Translate();
422 // The "Translating..." infobar should be showing.
423 infobar = GetTranslateInfoBar();
424 ASSERT_TRUE(infobar != NULL);
425 EXPECT_EQ(translate::TRANSLATE_STEP_TRANSLATING, infobar->translate_step());
427 // Simulate the translate script being retrieved (it only needs to be done
428 // once in the test as it is cached).
429 SimulateTranslateScriptURLFetch(true);
431 // Test that we sent the right message to the renderer.
432 std::string original_lang, target_lang;
433 EXPECT_TRUE(GetTranslateMessage(&original_lang, &target_lang));
434 EXPECT_EQ("fr", original_lang);
435 EXPECT_EQ("en", target_lang);
437 // Simulate the render notifying the translation has been done.
438 SimulateOnPageTranslated("fr", "en");
440 // The after translate infobar should be showing.
441 infobar = GetTranslateInfoBar();
442 ASSERT_TRUE(infobar != NULL);
443 EXPECT_EQ(translate::TRANSLATE_STEP_AFTER_TRANSLATE,
444 infobar->translate_step());
446 // Simulate changing the original language and translating.
447 process()->sink().ClearMessages();
448 std::string new_original_lang = infobar->language_code_at(0);
449 infobar->UpdateOriginalLanguageIndex(0);
450 infobar->Translate();
451 EXPECT_TRUE(GetTranslateMessage(&original_lang, &target_lang));
452 EXPECT_EQ(new_original_lang, original_lang);
453 EXPECT_EQ("en", target_lang);
454 // Simulate the render notifying the translation has been done.
455 SimulateOnPageTranslated(new_original_lang, "en");
456 infobar = GetTranslateInfoBar();
457 ASSERT_TRUE(infobar != NULL);
459 // Simulate changing the target language and translating.
460 process()->sink().ClearMessages();
461 std::string new_target_lang = infobar->language_code_at(1);
462 infobar->UpdateTargetLanguageIndex(1);
463 infobar->Translate();
464 EXPECT_TRUE(GetTranslateMessage(&original_lang, &target_lang));
465 EXPECT_EQ(new_original_lang, original_lang);
466 EXPECT_EQ(new_target_lang, target_lang);
467 // Simulate the render notifying the translation has been done.
468 SimulateOnPageTranslated(new_original_lang, new_target_lang);
469 infobar = GetTranslateInfoBar();
470 ASSERT_TRUE(infobar != NULL);
471 EXPECT_EQ(new_target_lang, infobar->target_language_code());
473 // Reloading should trigger translation iff Always Translate is on.
474 ReloadAndWait(true);
475 infobar = GetTranslateInfoBar();
476 ASSERT_TRUE(infobar != NULL);
477 EXPECT_EQ(translate::TRANSLATE_STEP_BEFORE_TRANSLATE,
478 infobar->translate_step());
479 infobar->UpdateTargetLanguageIndex(1);
480 infobar->ToggleAlwaysTranslate();
481 ReloadAndWait(true);
482 infobar = GetTranslateInfoBar();
483 ASSERT_TRUE(infobar != NULL);
484 EXPECT_EQ(translate::TRANSLATE_STEP_TRANSLATING, infobar->translate_step());
485 EXPECT_EQ(new_target_lang, infobar->target_language_code());
488 TEST_F(TranslateManagerRenderViewHostTest, TranslateScriptNotAvailable) {
489 // See BubbleTranslateScriptNotAvailable for corresponding bubble UX testing.
490 if (TranslateService::IsTranslateBubbleEnabled())
491 return;
493 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
495 // We should have an infobar.
496 translate::TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
497 ASSERT_TRUE(infobar != NULL);
498 EXPECT_EQ(translate::TRANSLATE_STEP_BEFORE_TRANSLATE,
499 infobar->translate_step());
501 // Simulate clicking translate.
502 process()->sink().ClearMessages();
503 infobar->Translate();
504 SimulateTranslateScriptURLFetch(false);
506 // We should not have sent any message to translate to the renderer.
507 EXPECT_FALSE(GetTranslateMessage(NULL, NULL));
509 // And we should have an error infobar showing.
510 infobar = GetTranslateInfoBar();
511 ASSERT_TRUE(infobar != NULL);
512 EXPECT_EQ(translate::TRANSLATE_STEP_TRANSLATE_ERROR,
513 infobar->translate_step());
516 // Ensures we deal correctly with pages for which the browser does not recognize
517 // the language (the translate server may or not detect the language).
518 TEST_F(TranslateManagerRenderViewHostTest, TranslateUnknownLanguage) {
519 // See BubbleUnknownLanguage for corresponding bubble UX testing.
520 if (TranslateService::IsTranslateBubbleEnabled())
521 return;
523 // Simulate navigating to a page ("und" is the string returned by the CLD for
524 // languages it does not recognize).
525 SimulateNavigation(GURL("http://www.google.mys"), "und", true);
527 // We should not have an infobar as we don't know the language.
528 ASSERT_TRUE(GetTranslateInfoBar() == NULL);
530 // Translate the page anyway throught the context menu.
531 scoped_ptr<TestRenderViewContextMenu> menu(CreateContextMenu());
532 menu->Init();
533 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
535 // To test that bug #49018 if fixed, make sure we deal correctly with errors.
536 // Simulate a failure to fetch the translate script.
537 SimulateTranslateScriptURLFetch(false);
538 translate::TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
539 ASSERT_TRUE(infobar != NULL);
540 EXPECT_EQ(translate::TRANSLATE_STEP_TRANSLATE_ERROR,
541 infobar->translate_step());
542 EXPECT_TRUE(infobar->is_error());
543 infobar->MessageInfoBarButtonPressed();
544 SimulateTranslateScriptURLFetch(true); // This time succeed.
546 // Simulate the render notifying the translation has been done, the server
547 // having detected the page was in a known and supported language.
548 SimulateOnPageTranslated("fr", "en");
550 // The after translate infobar should be showing.
551 infobar = GetTranslateInfoBar();
552 ASSERT_TRUE(infobar != NULL);
553 EXPECT_EQ(translate::TRANSLATE_STEP_AFTER_TRANSLATE,
554 infobar->translate_step());
555 EXPECT_EQ("fr", infobar->original_language_code());
556 EXPECT_EQ("en", infobar->target_language_code());
558 // Let's run the same steps but this time the server detects the page is
559 // already in English.
560 SimulateNavigation(GURL("http://www.google.com"), "und", true);
561 menu.reset(CreateContextMenu());
562 menu->Init();
563 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
564 SimulateOnPageTranslated(
565 1, "en", "en", translate::TranslateErrors::IDENTICAL_LANGUAGES);
566 infobar = GetTranslateInfoBar();
567 ASSERT_TRUE(infobar != NULL);
568 EXPECT_EQ(translate::TRANSLATE_STEP_TRANSLATE_ERROR,
569 infobar->translate_step());
570 EXPECT_EQ(translate::TranslateErrors::IDENTICAL_LANGUAGES,
571 infobar->error_type());
573 // Let's run the same steps again but this time the server fails to detect the
574 // page's language (it returns an empty string).
575 SimulateNavigation(GURL("http://www.google.com"), "und", true);
576 menu.reset(CreateContextMenu());
577 menu->Init();
578 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
579 SimulateOnPageTranslated(
580 2, std::string(), "en", translate::TranslateErrors::UNKNOWN_LANGUAGE);
581 infobar = GetTranslateInfoBar();
582 ASSERT_TRUE(infobar != NULL);
583 EXPECT_EQ(translate::TRANSLATE_STEP_TRANSLATE_ERROR,
584 infobar->translate_step());
585 EXPECT_EQ(translate::TranslateErrors::UNKNOWN_LANGUAGE,
586 infobar->error_type());
589 // Tests that we show/don't show an info-bar for the languages.
590 TEST_F(TranslateManagerRenderViewHostTest, TestLanguages) {
591 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
592 if (TranslateService::IsTranslateBubbleEnabled())
593 return;
595 std::vector<std::string> languages;
596 languages.push_back("en");
597 languages.push_back("ja");
598 languages.push_back("fr");
599 languages.push_back("ht");
600 languages.push_back("xx");
601 languages.push_back("zh");
602 languages.push_back("zh-CN");
603 languages.push_back("und");
605 GURL url("http://www.google.com");
606 for (size_t i = 0; i < languages.size(); ++i) {
607 std::string lang = languages[i];
608 SCOPED_TRACE(::testing::Message() << "Iteration " << i
609 << " language=" << lang);
611 // We should not have a translate infobar.
612 translate::TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
613 ASSERT_TRUE(infobar == NULL);
615 SimulateNavigation(url, lang, true);
617 // Verify we have/don't have an info-bar as expected.
618 infobar = GetTranslateInfoBar();
619 bool expected =
620 translate::TranslateDownloadManager::IsSupportedLanguage(lang) &&
621 lang != "en";
622 EXPECT_EQ(expected, infobar != NULL);
624 if (infobar != NULL)
625 EXPECT_TRUE(CloseTranslateInfoBar());
629 // A list of languages to fake being returned by the translate server.
630 // Use only langauges for which Chrome's copy of ICU has
631 // display names in English locale. To save space, Chrome's copy of ICU
632 // does not have the display name for a language unless it's in the
633 // Accept-Language list.
634 static const char* server_language_list[] =
635 {"ach", "ak", "af", "en-CA", "zh", "yi", "fr-FR", "tl", "iw", "in", "xx"};
636 static const char* alpha_language_list[] = {"ach", "yi"};
638 // Test the fetching of languages from the translate server
639 TEST_F(TranslateManagerRenderViewHostTest, FetchLanguagesFromTranslateServer) {
640 std::vector<std::string> server_languages;
641 for (size_t i = 0; i < arraysize(server_language_list); ++i)
642 server_languages.push_back(server_language_list[i]);
644 std::vector<std::string> alpha_languages;
645 for (size_t i = 0; i < arraysize(alpha_language_list); ++i)
646 alpha_languages.push_back(alpha_language_list[i]);
648 // First, get the default languages list. Note that calling
649 // GetSupportedLanguages() invokes RequestLanguageList() internally.
650 std::vector<std::string> default_supported_languages;
651 translate::TranslateDownloadManager::GetSupportedLanguages(
652 &default_supported_languages);
653 // To make sure we got the defaults and don't confuse them with the mocks.
654 ASSERT_NE(default_supported_languages.size(), server_languages.size());
656 // Check that we still get the defaults until the URLFetch has completed.
657 std::vector<std::string> current_supported_languages;
658 translate::TranslateDownloadManager::GetSupportedLanguages(
659 &current_supported_languages);
660 EXPECT_EQ(default_supported_languages, current_supported_languages);
662 // Also check that it didn't change if we failed the URL fetch.
663 SimulateSupportedLanguagesURLFetch(
664 false, std::vector<std::string>(), true, std::vector<std::string>());
665 current_supported_languages.clear();
666 translate::TranslateDownloadManager::GetSupportedLanguages(
667 &current_supported_languages);
668 EXPECT_EQ(default_supported_languages, current_supported_languages);
670 // Now check that we got the appropriate set of languages from the server.
671 SimulateSupportedLanguagesURLFetch(
672 true, server_languages, true, alpha_languages);
673 current_supported_languages.clear();
674 translate::TranslateDownloadManager::GetSupportedLanguages(
675 &current_supported_languages);
676 // "xx" can't be displayed in the Translate infobar, so this is eliminated.
677 EXPECT_EQ(server_languages.size() - 1, current_supported_languages.size());
678 // Not sure we need to guarantee the order of languages, so we find them.
679 for (size_t i = 0; i < server_languages.size(); ++i) {
680 const std::string& lang = server_languages[i];
681 if (lang == "xx")
682 continue;
683 EXPECT_NE(current_supported_languages.end(),
684 std::find(current_supported_languages.begin(),
685 current_supported_languages.end(),
686 lang)) << "lang=" << lang;
687 bool is_alpha =
688 std::find(alpha_languages.begin(), alpha_languages.end(), lang) !=
689 alpha_languages.end();
690 EXPECT_EQ(translate::TranslateDownloadManager::IsAlphaLanguage(lang),
691 is_alpha)
692 << "lang=" << lang;
696 // Test the fetching of languages from the translate server without 'al'
697 // parameter.
698 TEST_F(TranslateManagerRenderViewHostTest,
699 FetchLanguagesFromTranslateServerWithoutAlpha) {
700 std::vector<std::string> server_languages;
701 for (size_t i = 0; i < arraysize(server_language_list); ++i)
702 server_languages.push_back(server_language_list[i]);
704 std::vector<std::string> alpha_languages;
705 for (size_t i = 0; i < arraysize(alpha_language_list); ++i)
706 alpha_languages.push_back(alpha_language_list[i]);
708 // call GetSupportedLanguages to call RequestLanguageList internally.
709 std::vector<std::string> default_supported_languages;
710 translate::TranslateDownloadManager::GetSupportedLanguages(
711 &default_supported_languages);
713 SimulateSupportedLanguagesURLFetch(
714 true, server_languages, false, alpha_languages);
716 std::vector<std::string> current_supported_languages;
717 translate::TranslateDownloadManager::GetSupportedLanguages(
718 &current_supported_languages);
720 // "xx" can't be displayed in the Translate infobar, so this is eliminated.
721 EXPECT_EQ(server_languages.size() - 1, current_supported_languages.size());
723 for (size_t i = 0; i < server_languages.size(); ++i) {
724 const std::string& lang = server_languages[i];
725 if (lang == "xx")
726 continue;
727 EXPECT_NE(current_supported_languages.end(),
728 std::find(current_supported_languages.begin(),
729 current_supported_languages.end(),
730 lang)) << "lang=" << lang;
731 EXPECT_FALSE(translate::TranslateDownloadManager::IsAlphaLanguage(lang))
732 << "lang=" << lang;
736 // Tests auto-translate on page.
737 TEST_F(TranslateManagerRenderViewHostTest, AutoTranslateOnNavigate) {
738 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
739 if (TranslateService::IsTranslateBubbleEnabled())
740 return;
742 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
744 // Simulate the user translating.
745 translate::TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
746 ASSERT_TRUE(infobar != NULL);
747 infobar->Translate();
748 // Simulate the translate script being retrieved.
749 SimulateTranslateScriptURLFetch(true);
750 SimulateOnPageTranslated("fr", "en");
752 // Now navigate to a new page in the same language.
753 process()->sink().ClearMessages();
754 SimulateNavigation(GURL("http://news.google.fr"), "fr", true);
756 // This should have automatically triggered a translation.
757 std::string original_lang, target_lang;
758 EXPECT_TRUE(GetTranslateMessage(&original_lang, &target_lang));
759 EXPECT_EQ("fr", original_lang);
760 EXPECT_EQ("en", target_lang);
762 // Now navigate to a page in a different language.
763 process()->sink().ClearMessages();
764 SimulateNavigation(GURL("http://news.google.es"), "es", true);
766 // This should not have triggered a translate.
767 EXPECT_FALSE(GetTranslateMessage(&original_lang, &target_lang));
770 // Tests that multiple OnPageContents do not cause multiple infobars.
771 TEST_F(TranslateManagerRenderViewHostTest, MultipleOnPageContents) {
772 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
773 if (TranslateService::IsTranslateBubbleEnabled())
774 return;
776 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
778 // Simulate clicking 'Nope' (don't translate).
779 EXPECT_TRUE(DenyTranslation());
780 EXPECT_EQ(0U, infobar_service()->infobar_count());
782 // Send a new PageContents, we should not show an infobar.
783 SimulateOnTranslateLanguageDetermined("fr", true);
784 EXPECT_EQ(0U, infobar_service()->infobar_count());
786 // Do the same steps but simulate closing the infobar this time.
787 SimulateNavigation(GURL("http://www.youtube.fr"), "fr", true);
788 EXPECT_TRUE(CloseTranslateInfoBar());
789 EXPECT_EQ(0U, infobar_service()->infobar_count());
790 SimulateOnTranslateLanguageDetermined("fr", true);
791 EXPECT_EQ(0U, infobar_service()->infobar_count());
794 // Test that reloading the page brings back the infobar if the
795 // reload succeeded and does not bring it back the reload fails.
796 TEST_F(TranslateManagerRenderViewHostTest, Reload) {
797 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
798 if (TranslateService::IsTranslateBubbleEnabled())
799 return;
801 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
803 EXPECT_TRUE(CloseTranslateInfoBar());
805 // Reload should bring back the infobar if the reload succeeds.
806 ReloadAndWait(true);
807 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
808 EXPECT_TRUE(CloseTranslateInfoBar());
810 // ...But not show it if the reload fails.
811 ReloadAndWait(false);
812 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
814 // If we set reload attempts to a high value, we will not see the infobar
815 // immediately.
816 ChromeTranslateClient::FromWebContents(web_contents())
817 ->translate_driver()
818 .set_translate_max_reload_attempts(100);
819 ReloadAndWait(true);
820 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
823 // Test that reloading the page by way of typing again the URL in the
824 // location bar brings back the infobar.
825 TEST_F(TranslateManagerRenderViewHostTest, ReloadFromLocationBar) {
826 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
827 if (TranslateService::IsTranslateBubbleEnabled())
828 return;
830 GURL url("http://www.google.fr");
831 SimulateNavigation(url, "fr", true);
833 EXPECT_TRUE(CloseTranslateInfoBar());
835 // Create a pending navigation and simulate a page load. That should be the
836 // equivalent of typing the URL again in the location bar.
837 NavEntryCommittedObserver nav_observer(web_contents());
838 web_contents()->GetController().LoadURL(
839 url, content::Referrer(), ui::PAGE_TRANSITION_TYPED, std::string());
840 int pending_id =
841 web_contents()->GetController().GetPendingEntry()->GetUniqueID();
842 content::RenderFrameHostTester::For(web_contents()->GetMainFrame())
843 ->SendNavigateWithTransition(0, pending_id, false, url,
844 ui::PAGE_TRANSITION_TYPED);
846 // Test that we are really getting a same page navigation, the test would be
847 // useless if it was not the case.
848 const content::LoadCommittedDetails& nav_details =
849 nav_observer.load_committed_details();
850 EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation.
851 EXPECT_EQ(content::NAVIGATION_TYPE_SAME_PAGE, nav_details.type);
853 // The TranslateManager class processes the navigation entry committed
854 // notification in a posted task; process that task.
855 base::MessageLoop::current()->RunUntilIdle();
856 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
859 // Tests that a closed translate infobar does not reappear when navigating
860 // in-page.
861 TEST_F(TranslateManagerRenderViewHostTest, CloseInfoBarInPageNavigation) {
862 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
863 if (TranslateService::IsTranslateBubbleEnabled())
864 return;
866 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
868 EXPECT_TRUE(CloseTranslateInfoBar());
870 // Navigate in page, no infobar should be shown.
871 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true);
872 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
874 // Navigate out of page, a new infobar should show.
875 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true);
876 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
879 // Tests that a closed translate infobar does not reappear when navigating
880 // in a subframe. (http://crbug.com/48215)
881 TEST_F(TranslateManagerRenderViewHostTest, CloseInfoBarInSubframeNavigation) {
882 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
883 if (TranslateService::IsTranslateBubbleEnabled())
884 return;
886 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
888 EXPECT_TRUE(CloseTranslateInfoBar());
890 content::RenderFrameHostTester* subframe_tester =
891 content::RenderFrameHostTester::For(
892 content::RenderFrameHostTester::For(main_rfh())
893 ->AppendChild("subframe"));
895 // Simulate a sub-frame auto-navigating.
896 subframe_tester->SendNavigateWithTransition(
897 0, 0, false, GURL("http://pub.com"), ui::PAGE_TRANSITION_AUTO_SUBFRAME);
898 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
900 // Simulate the user navigating in a sub-frame.
901 subframe_tester->SendNavigateWithTransition(
902 1, 0, true, GURL("http://pub.com"), ui::PAGE_TRANSITION_MANUAL_SUBFRAME);
903 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
905 // Navigate out of page, a new infobar should show.
906 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true);
907 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
910 // Tests that denying translation is sticky when navigating in page.
911 TEST_F(TranslateManagerRenderViewHostTest, DenyTranslateInPageNavigation) {
912 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
913 if (TranslateService::IsTranslateBubbleEnabled())
914 return;
916 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
918 // Simulate clicking 'Nope' (don't translate).
919 EXPECT_TRUE(DenyTranslation());
921 // Navigate in page, no infobar should be shown.
922 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true);
923 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
925 // Navigate out of page, a new infobar should show.
926 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true);
927 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
930 // Tests that after translating and closing the infobar, the infobar does not
931 // return when navigating in page.
932 TEST_F(TranslateManagerRenderViewHostTest,
933 TranslateCloseInfoBarInPageNavigation) {
934 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
935 if (TranslateService::IsTranslateBubbleEnabled())
936 return;
938 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
940 // Simulate the user translating.
941 translate::TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
942 ASSERT_TRUE(infobar != NULL);
943 infobar->Translate();
944 // Simulate the translate script being retrieved.
945 SimulateTranslateScriptURLFetch(true);
946 SimulateOnPageTranslated("fr", "en");
948 EXPECT_TRUE(CloseTranslateInfoBar());
950 // Navigate in page, no infobar should be shown.
951 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true);
952 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
954 // Navigate out of page, a new infobar should show.
955 // Note that we navigate to a page in a different language so we don't trigger
956 // the auto-translate feature (it would translate the page automatically and
957 // the before translate infobar would not be shown).
958 SimulateNavigation(GURL("http://www.google.de"), "de", true);
959 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
962 // Tests that the after translate the infobar still shows when navigating
963 // in-page.
964 TEST_F(TranslateManagerRenderViewHostTest, TranslateInPageNavigation) {
965 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
966 if (TranslateService::IsTranslateBubbleEnabled())
967 return;
969 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
971 // Simulate the user translating.
972 translate::TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
973 ASSERT_TRUE(infobar != NULL);
974 infobar->Translate();
975 SimulateTranslateScriptURLFetch(true);
976 SimulateOnPageTranslated("fr", "en");
977 // The after translate infobar is showing.
978 infobar = GetTranslateInfoBar();
979 ASSERT_TRUE(infobar != NULL);
981 // Navigate out of page, a new infobar should show.
982 // See note in TranslateCloseInfoBarInPageNavigation test on why it is
983 // important to navigate to a page in a different language for this test.
984 SimulateNavigation(GURL("http://www.google.de"), "de", true);
985 // The old infobar is gone.
986 EXPECT_TRUE(CheckInfoBarRemovedAndReset(infobar));
987 // And there is a new one.
988 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
991 // Tests that no translate infobar is shown when navigating to a page in an
992 // unsupported language.
993 TEST_F(TranslateManagerRenderViewHostTest, CLDReportsUnsupportedPageLanguage) {
994 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
995 if (TranslateService::IsTranslateBubbleEnabled())
996 return;
998 // Simulate navigating to a page and getting an unsupported language.
999 SimulateNavigation(GURL("http://www.google.com"), "qbz", true);
1001 // No info-bar should be shown.
1002 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
1005 // Tests that we deal correctly with unsupported languages returned by the
1006 // server.
1007 // The translation server might return a language we don't support.
1008 TEST_F(TranslateManagerRenderViewHostTest, ServerReportsUnsupportedLanguage) {
1009 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
1010 if (TranslateService::IsTranslateBubbleEnabled())
1011 return;
1013 SimulateNavigation(GURL("http://mail.google.fr"), "fr", true);
1014 translate::TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1015 ASSERT_TRUE(infobar != NULL);
1016 process()->sink().ClearMessages();
1017 infobar->Translate();
1018 SimulateTranslateScriptURLFetch(true);
1019 // Simulate the render notifying the translation has been done, but it
1020 // reports a language we don't support.
1021 SimulateOnPageTranslated("qbz", "en");
1023 // An error infobar should be showing to report that we don't support this
1024 // language.
1025 infobar = GetTranslateInfoBar();
1026 ASSERT_TRUE(infobar != NULL);
1027 EXPECT_EQ(translate::TRANSLATE_STEP_TRANSLATE_ERROR,
1028 infobar->translate_step());
1030 // This infobar should have a button (so the string should not be empty).
1031 ASSERT_FALSE(infobar->GetMessageInfoBarButtonText().empty());
1033 // Pressing the button on that infobar should revert to the original language.
1034 process()->sink().ClearMessages();
1035 infobar->MessageInfoBarButtonPressed();
1036 const IPC::Message* message = process()->sink().GetFirstMessageMatching(
1037 ChromeViewMsg_RevertTranslation::ID);
1038 EXPECT_TRUE(message != NULL);
1039 // And it should have removed the infobar.
1040 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
1043 // Tests that no translate infobar is shown and context menu is disabled, when
1044 // Chrome is in a language that the translate server does not support.
1045 TEST_F(TranslateManagerRenderViewHostTest, UnsupportedUILanguage) {
1046 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
1047 if (TranslateService::IsTranslateBubbleEnabled())
1048 return;
1050 std::string original_lang = g_browser_process->GetApplicationLocale();
1051 SetApplicationLocale("qbz");
1053 // Make sure that the accept language list only contains unsupported languages
1054 Profile* profile =
1055 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
1056 PrefService* prefs = profile->GetPrefs();
1057 prefs->SetString(prefs::kAcceptLanguages, "qbz");
1059 // Simulate navigating to a page in a language supported by the translate
1060 // server.
1061 SimulateNavigation(GURL("http://www.google.com"), "en", true);
1063 // No info-bar should be shown.
1064 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
1066 // And the context menu option should be disabled too.
1067 scoped_ptr<TestRenderViewContextMenu> menu(CreateContextMenu());
1068 menu->Init();
1069 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
1070 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
1072 SetApplicationLocale(original_lang);
1075 // Tests that the first supported accept language is selected
1076 TEST_F(TranslateManagerRenderViewHostTest, TranslateAcceptLanguage) {
1077 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
1078 if (TranslateService::IsTranslateBubbleEnabled())
1079 return;
1081 // Set locate to non-existant language
1082 std::string original_lang = g_browser_process->GetApplicationLocale();
1083 SetApplicationLocale("qbz");
1085 // Set Qbz and French as the only accepted languages
1086 Profile* profile =
1087 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
1088 PrefService* prefs = profile->GetPrefs();
1089 prefs->SetString(prefs::kAcceptLanguages, "qbz,fr");
1091 // Go to a German page
1092 SimulateNavigation(GURL("http://google.de"), "de", true);
1094 // Expect the infobar to pop up
1095 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1097 // Set Qbz and English-US as the only accepted languages to test the country
1098 // code removal code which was causing a crash as filed in Issue 90106,
1099 // a crash caused by a language with a country code that wasn't recognized.
1100 prefs->SetString(prefs::kAcceptLanguages, "qbz,en-us");
1102 // Go to a German page
1103 SimulateNavigation(GURL("http://google.de"), "de", true);
1105 // Expect the infobar to pop up
1106 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1109 // Tests that the translate enabled preference is honored.
1110 TEST_F(TranslateManagerRenderViewHostTest, TranslateEnabledPref) {
1111 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
1112 if (TranslateService::IsTranslateBubbleEnabled())
1113 return;
1115 // Make sure the pref allows translate.
1116 Profile* profile =
1117 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
1118 PrefService* prefs = profile->GetPrefs();
1119 prefs->SetBoolean(prefs::kEnableTranslate, true);
1121 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1123 // An infobar should be shown.
1124 translate::TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1125 EXPECT_TRUE(infobar != NULL);
1127 // Disable translate.
1128 prefs->SetBoolean(prefs::kEnableTranslate, false);
1130 // Navigate to a new page, that should close the previous infobar.
1131 GURL url("http://www.youtube.fr");
1132 NavigateAndCommit(url);
1133 infobar = GetTranslateInfoBar();
1134 EXPECT_TRUE(infobar == NULL);
1136 // Simulate getting the page contents and language, that should not trigger
1137 // a translate infobar.
1138 SimulateOnTranslateLanguageDetermined("fr", true);
1139 infobar = GetTranslateInfoBar();
1140 EXPECT_TRUE(infobar == NULL);
1143 // Tests the "Never translate <language>" pref.
1144 TEST_F(TranslateManagerRenderViewHostTest, NeverTranslateLanguagePref) {
1145 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
1146 if (TranslateService::IsTranslateBubbleEnabled())
1147 return;
1149 GURL url("http://www.google.fr");
1150 SimulateNavigation(url, "fr", true);
1152 // An infobar should be shown.
1153 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1155 // Select never translate this language.
1156 Profile* profile =
1157 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
1158 PrefService* prefs = profile->GetPrefs();
1159 PrefChangeRegistrar registrar;
1160 registrar.Init(prefs);
1161 registrar.Add(translate::TranslatePrefs::kPrefTranslateBlockedLanguages,
1162 pref_callback_);
1163 scoped_ptr<translate::TranslatePrefs> translate_prefs(
1164 ChromeTranslateClient::CreateTranslatePrefs(prefs));
1165 EXPECT_FALSE(translate_prefs->IsBlockedLanguage("fr"));
1166 translate::TranslateAcceptLanguages* accept_languages =
1167 ChromeTranslateClient::GetTranslateAcceptLanguages(profile);
1168 EXPECT_TRUE(translate_prefs->CanTranslateLanguage(accept_languages, "fr"));
1169 SetPrefObserverExpectation(
1170 translate::TranslatePrefs::kPrefTranslateBlockedLanguages);
1171 translate_prefs->BlockLanguage("fr");
1172 EXPECT_TRUE(translate_prefs->IsBlockedLanguage("fr"));
1173 EXPECT_FALSE(translate_prefs->IsSiteBlacklisted(url.host()));
1174 EXPECT_FALSE(translate_prefs->CanTranslateLanguage(accept_languages, "fr"));
1176 EXPECT_TRUE(CloseTranslateInfoBar());
1178 // Navigate to a new page also in French.
1179 SimulateNavigation(GURL("http://wwww.youtube.fr"), "fr", true);
1181 // There should not be a translate infobar.
1182 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
1184 // Remove the language from the blacklist.
1185 SetPrefObserverExpectation(
1186 translate::TranslatePrefs::kPrefTranslateBlockedLanguages);
1187 translate_prefs->UnblockLanguage("fr");
1188 EXPECT_FALSE(translate_prefs->IsBlockedLanguage("fr"));
1189 EXPECT_FALSE(translate_prefs->IsSiteBlacklisted(url.host()));
1190 EXPECT_TRUE(translate_prefs->CanTranslateLanguage(accept_languages, "fr"));
1192 // Navigate to a page in French.
1193 SimulateNavigation(url, "fr", true);
1195 // There should be a translate infobar.
1196 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1199 // Tests the "Never translate this site" pref.
1200 TEST_F(TranslateManagerRenderViewHostTest, NeverTranslateSitePref) {
1201 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
1202 if (TranslateService::IsTranslateBubbleEnabled())
1203 return;
1205 GURL url("http://www.google.fr");
1206 std::string host(url.host());
1207 SimulateNavigation(url, "fr", true);
1209 // An infobar should be shown.
1210 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1212 // Select never translate this site.
1213 Profile* profile =
1214 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
1215 PrefService* prefs = profile->GetPrefs();
1216 PrefChangeRegistrar registrar;
1217 registrar.Init(prefs);
1218 registrar.Add(translate::TranslatePrefs::kPrefTranslateSiteBlacklist,
1219 pref_callback_);
1220 scoped_ptr<translate::TranslatePrefs> translate_prefs(
1221 ChromeTranslateClient::CreateTranslatePrefs(prefs));
1222 EXPECT_FALSE(translate_prefs->IsSiteBlacklisted(host));
1223 translate::TranslateAcceptLanguages* accept_languages =
1224 ChromeTranslateClient::GetTranslateAcceptLanguages(profile);
1225 EXPECT_TRUE(translate_prefs->CanTranslateLanguage(accept_languages, "fr"));
1226 SetPrefObserverExpectation(
1227 translate::TranslatePrefs::kPrefTranslateSiteBlacklist);
1228 translate_prefs->BlacklistSite(host);
1229 EXPECT_TRUE(translate_prefs->IsSiteBlacklisted(host));
1230 EXPECT_TRUE(translate_prefs->CanTranslateLanguage(accept_languages, "fr"));
1232 EXPECT_TRUE(CloseTranslateInfoBar());
1234 // Navigate to a new page also on the same site.
1235 SimulateNavigation(GURL("http://www.google.fr/hello"), "fr", true);
1237 // There should not be a translate infobar.
1238 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
1240 // Remove the site from the blacklist.
1241 SetPrefObserverExpectation(
1242 translate::TranslatePrefs::kPrefTranslateSiteBlacklist);
1243 translate_prefs->RemoveSiteFromBlacklist(host);
1244 EXPECT_FALSE(translate_prefs->IsSiteBlacklisted(host));
1245 EXPECT_TRUE(translate_prefs->CanTranslateLanguage(accept_languages, "fr"));
1247 // Navigate to a page in French.
1248 SimulateNavigation(url, "fr", true);
1250 // There should be a translate infobar.
1251 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1254 // Tests the "Always translate this language" pref.
1255 TEST_F(TranslateManagerRenderViewHostTest, AlwaysTranslateLanguagePref) {
1256 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
1257 if (TranslateService::IsTranslateBubbleEnabled())
1258 return;
1260 // Select always translate French to English.
1261 Profile* profile =
1262 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
1263 PrefService* prefs = profile->GetPrefs();
1264 PrefChangeRegistrar registrar;
1265 registrar.Init(prefs);
1266 registrar.Add(translate::TranslatePrefs::kPrefTranslateWhitelists,
1267 pref_callback_);
1268 scoped_ptr<translate::TranslatePrefs> translate_prefs(
1269 ChromeTranslateClient::CreateTranslatePrefs(prefs));
1270 SetPrefObserverExpectation(
1271 translate::TranslatePrefs::kPrefTranslateWhitelists);
1272 translate_prefs->WhitelistLanguagePair("fr", "en");
1274 // Load a page in French.
1275 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1277 // It should have triggered an automatic translation to English.
1279 // The translating infobar should be showing.
1280 translate::TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1281 ASSERT_TRUE(infobar != NULL);
1282 EXPECT_EQ(translate::TRANSLATE_STEP_TRANSLATING, infobar->translate_step());
1283 SimulateTranslateScriptURLFetch(true);
1284 std::string original_lang, target_lang;
1285 EXPECT_TRUE(GetTranslateMessage(&original_lang, &target_lang));
1286 EXPECT_EQ("fr", original_lang);
1287 EXPECT_EQ("en", target_lang);
1288 process()->sink().ClearMessages();
1290 // Try another language, it should not be autotranslated.
1291 SimulateNavigation(GURL("http://www.google.es"), "es", true);
1292 EXPECT_FALSE(GetTranslateMessage(&original_lang, &target_lang));
1293 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1294 EXPECT_TRUE(CloseTranslateInfoBar());
1296 // Let's switch to incognito mode, it should not be autotranslated in that
1297 // case either.
1298 TestingProfile* test_profile =
1299 static_cast<TestingProfile*>(web_contents()->GetBrowserContext());
1300 test_profile->ForceIncognito(true);
1301 SimulateNavigation(GURL("http://www.youtube.fr"), "fr", true);
1302 EXPECT_FALSE(GetTranslateMessage(&original_lang, &target_lang));
1303 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1304 EXPECT_TRUE(CloseTranslateInfoBar());
1305 test_profile->ForceIncognito(false); // Get back to non incognito.
1307 // Now revert the always translate pref and make sure we go back to expected
1308 // behavior, which is show a "before translate" infobar.
1309 SetPrefObserverExpectation(
1310 translate::TranslatePrefs::kPrefTranslateWhitelists);
1311 translate_prefs->RemoveLanguagePairFromWhitelist("fr", "en");
1312 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1313 EXPECT_FALSE(GetTranslateMessage(&original_lang, &target_lang));
1314 infobar = GetTranslateInfoBar();
1315 ASSERT_TRUE(infobar != NULL);
1316 EXPECT_EQ(translate::TRANSLATE_STEP_BEFORE_TRANSLATE,
1317 infobar->translate_step());
1320 // Context menu.
1321 TEST_F(TranslateManagerRenderViewHostTest, ContextMenu) {
1322 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
1323 if (TranslateService::IsTranslateBubbleEnabled())
1324 return;
1326 // Blacklist www.google.fr and French for translation.
1327 GURL url("http://www.google.fr");
1328 Profile* profile =
1329 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
1330 scoped_ptr<translate::TranslatePrefs> translate_prefs(
1331 ChromeTranslateClient::CreateTranslatePrefs(profile->GetPrefs()));
1332 translate_prefs->BlockLanguage("fr");
1333 translate_prefs->BlacklistSite(url.host());
1334 EXPECT_TRUE(translate_prefs->IsBlockedLanguage("fr"));
1335 EXPECT_TRUE(translate_prefs->IsSiteBlacklisted(url.host()));
1337 // Simulate navigating to a page in French. The translate menu should show but
1338 // should only be enabled when the page language has been received.
1339 NavigateAndCommit(url);
1340 scoped_ptr<TestRenderViewContextMenu> menu(CreateContextMenu());
1341 menu->Init();
1342 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
1343 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
1345 // Simulate receiving the language.
1346 SimulateOnTranslateLanguageDetermined("fr", true);
1347 menu.reset(CreateContextMenu());
1348 menu->Init();
1349 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
1350 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
1352 // Use the menu to translate the page.
1353 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
1355 // That should have triggered a translation.
1356 // The "translating..." infobar should be showing.
1357 translate::TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1358 ASSERT_TRUE(infobar != NULL);
1359 EXPECT_EQ(translate::TRANSLATE_STEP_TRANSLATING, infobar->translate_step());
1360 SimulateTranslateScriptURLFetch(true);
1361 std::string original_lang, target_lang;
1362 EXPECT_TRUE(GetTranslateMessage(&original_lang, &target_lang));
1363 EXPECT_EQ("fr", original_lang);
1364 EXPECT_EQ("en", target_lang);
1365 process()->sink().ClearMessages();
1367 // This should also have reverted the blacklisting of this site and language.
1368 EXPECT_FALSE(translate_prefs->IsBlockedLanguage("fr"));
1369 EXPECT_FALSE(translate_prefs->IsSiteBlacklisted(url.host()));
1371 // Let's simulate the page being translated.
1372 SimulateOnPageTranslated("fr", "en");
1374 // The translate menu should now be disabled.
1375 menu.reset(CreateContextMenu());
1376 menu->Init();
1377 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
1378 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
1380 // Test that selecting translate in the context menu WHILE the page is being
1381 // translated does nothing (this could happen if autotranslate kicks-in and
1382 // the user selects the menu while the translation is being performed).
1383 SimulateNavigation(GURL("http://www.google.es"), "es", true);
1384 infobar = GetTranslateInfoBar();
1385 ASSERT_TRUE(infobar != NULL);
1386 infobar->Translate();
1387 EXPECT_TRUE(GetTranslateMessage(&original_lang, &target_lang));
1388 process()->sink().ClearMessages();
1389 menu.reset(CreateContextMenu());
1390 menu->Init();
1391 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
1392 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
1393 // No message expected since the translation should have been ignored.
1394 EXPECT_FALSE(GetTranslateMessage(&original_lang, &target_lang));
1396 // Now test that selecting translate in the context menu AFTER the page has
1397 // been translated does nothing.
1398 SimulateNavigation(GURL("http://www.google.de"), "de", true);
1399 infobar = GetTranslateInfoBar();
1400 ASSERT_TRUE(infobar != NULL);
1401 infobar->Translate();
1402 EXPECT_TRUE(GetTranslateMessage(&original_lang, &target_lang));
1403 process()->sink().ClearMessages();
1404 menu.reset(CreateContextMenu());
1405 menu->Init();
1406 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
1407 SimulateOnPageTranslated("de", "en");
1408 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
1409 // No message expected since the translation should have been ignored.
1410 EXPECT_FALSE(GetTranslateMessage(&original_lang, &target_lang));
1412 // Test that the translate context menu is enabled when the page is in an
1413 // unknown language.
1414 SimulateNavigation(url, "und", true);
1415 menu.reset(CreateContextMenu());
1416 menu->Init();
1417 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
1418 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
1420 // Test that the translate context menu is enabled even if the page is in an
1421 // unsupported language.
1422 SimulateNavigation(url, "qbz", true);
1423 menu.reset(CreateContextMenu());
1424 menu->Init();
1425 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
1426 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
1429 // Tests that an extra always/never translate button is shown on the "before
1430 // translate" infobar when the translation is accepted/declined 3 times,
1431 // only when not in incognito mode.
1432 TEST_F(TranslateManagerRenderViewHostTest, BeforeTranslateExtraButtons) {
1433 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
1434 if (TranslateService::IsTranslateBubbleEnabled())
1435 return;
1437 Profile* profile =
1438 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
1439 scoped_ptr<translate::TranslatePrefs> translate_prefs(
1440 ChromeTranslateClient::CreateTranslatePrefs(profile->GetPrefs()));
1441 translate_prefs->ResetTranslationAcceptedCount("fr");
1442 translate_prefs->ResetTranslationDeniedCount("fr");
1443 translate_prefs->ResetTranslationAcceptedCount("de");
1444 translate_prefs->ResetTranslationDeniedCount("de");
1446 // We'll do 4 times in incognito mode first to make sure the button is not
1447 // shown in that case, then 4 times in normal mode.
1448 translate::TranslateInfoBarDelegate* infobar;
1449 TestingProfile* test_profile =
1450 static_cast<TestingProfile*>(web_contents()->GetBrowserContext());
1451 test_profile->ForceIncognito(true);
1452 for (int i = 0; i < 8; ++i) {
1453 SCOPED_TRACE(::testing::Message() << "Iteration " << i << " incognito mode="
1454 << test_profile->IsOffTheRecord());
1455 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1456 infobar = GetTranslateInfoBar();
1457 ASSERT_TRUE(infobar != NULL);
1458 EXPECT_EQ(translate::TRANSLATE_STEP_BEFORE_TRANSLATE,
1459 infobar->translate_step());
1460 if (i < 7) {
1461 EXPECT_FALSE(infobar->ShouldShowAlwaysTranslateShortcut());
1462 infobar->Translate();
1463 process()->sink().ClearMessages();
1464 } else {
1465 EXPECT_TRUE(infobar->ShouldShowAlwaysTranslateShortcut());
1467 if (i == 3)
1468 test_profile->ForceIncognito(false);
1470 // Simulate the user pressing "Always translate French".
1471 infobar->AlwaysTranslatePageLanguage();
1472 EXPECT_TRUE(translate_prefs->IsLanguagePairWhitelisted("fr", "en"));
1473 // Simulate the translate script being retrieved (it only needs to be done
1474 // once in the test as it is cached).
1475 SimulateTranslateScriptURLFetch(true);
1476 // That should have triggered a page translate.
1477 std::string original_lang, target_lang;
1478 EXPECT_TRUE(GetTranslateMessage(&original_lang, &target_lang));
1479 process()->sink().ClearMessages();
1481 // Now test that declining the translation causes a "never translate" button
1482 // to be shown (in non incognito mode only).
1483 test_profile->ForceIncognito(true);
1484 for (int i = 0; i < 8; ++i) {
1485 SCOPED_TRACE(::testing::Message() << "Iteration " << i << " incognito mode="
1486 << test_profile->IsOffTheRecord());
1487 SimulateNavigation(GURL("http://www.google.de"), "de", true);
1488 infobar = GetTranslateInfoBar();
1489 ASSERT_TRUE(infobar != NULL);
1490 EXPECT_EQ(translate::TRANSLATE_STEP_BEFORE_TRANSLATE,
1491 infobar->translate_step());
1492 if (i < 7) {
1493 EXPECT_FALSE(infobar->ShouldShowNeverTranslateShortcut());
1494 infobar->TranslationDeclined();
1495 } else {
1496 EXPECT_TRUE(infobar->ShouldShowNeverTranslateShortcut());
1498 if (i == 3)
1499 test_profile->ForceIncognito(false);
1501 // Simulate the user pressing "Never translate French".
1502 infobar->NeverTranslatePageLanguage();
1503 EXPECT_TRUE(translate_prefs->IsBlockedLanguage("de"));
1504 // No translation should have occured and the infobar should be gone.
1505 EXPECT_FALSE(GetTranslateMessage(&original_lang, &target_lang));
1506 process()->sink().ClearMessages();
1507 ASSERT_TRUE(GetTranslateInfoBar() == NULL);
1510 // Tests that we don't show a translate infobar when a page instructs that it
1511 // should not be translated.
1512 TEST_F(TranslateManagerRenderViewHostTest, NonTranslatablePage) {
1513 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
1514 if (TranslateService::IsTranslateBubbleEnabled())
1515 return;
1517 SimulateNavigation(GURL("http://mail.google.fr"), "fr", false);
1519 // We should not have an infobar.
1520 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
1522 // The context menu is enabled to allow users to force translation.
1523 scoped_ptr<TestRenderViewContextMenu> menu(CreateContextMenu());
1524 menu->Init();
1525 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
1526 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
1529 // Tests that the script is expired and refetched as expected.
1530 TEST_F(TranslateManagerRenderViewHostTest, ScriptExpires) {
1531 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
1532 if (TranslateService::IsTranslateBubbleEnabled())
1533 return;
1535 ExpireTranslateScriptImmediately();
1537 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1538 translate::TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1539 ASSERT_TRUE(infobar != NULL);
1540 process()->sink().ClearMessages();
1541 infobar->Translate();
1542 SimulateTranslateScriptURLFetch(true);
1543 SimulateOnPageTranslated("fr", "en");
1545 // A task should have been posted to clear the script, run it.
1546 base::MessageLoop::current()->RunUntilIdle();
1548 // Do another navigation and translation.
1549 SimulateNavigation(GURL("http://www.google.es"), "es", true);
1550 infobar = GetTranslateInfoBar();
1551 ASSERT_TRUE(infobar != NULL);
1552 process()->sink().ClearMessages();
1553 infobar->Translate();
1554 // If we don't simulate the URL fetch, the TranslateManager should be waiting
1555 // for the script and no message should have been sent to the renderer.
1556 EXPECT_TRUE(process()->sink().GetFirstMessageMatching(
1557 ChromeViewMsg_TranslatePage::ID) == NULL);
1558 // Now simulate the URL fetch.
1559 SimulateTranslateScriptURLFetch(true);
1560 // Now the message should have been sent.
1561 std::string original_lang, target_lang;
1562 EXPECT_TRUE(GetTranslateMessage(&original_lang, &target_lang));
1563 EXPECT_EQ("es", original_lang);
1564 EXPECT_EQ("en", target_lang);
1567 TEST_F(TranslateManagerRenderViewHostTest, DownloadsAndHistoryNotTranslated) {
1568 ASSERT_FALSE(
1569 TranslateService::IsTranslatableURL(GURL(chrome::kChromeUIDownloadsURL)));
1570 ASSERT_FALSE(
1571 TranslateService::IsTranslatableURL(GURL(chrome::kChromeUIHistoryURL)));
1574 TEST_F(TranslateManagerRenderViewHostTest, BubbleNormalTranslate) {
1575 // See NormalTranslate for corresponding infobar UX testing.
1576 if (!TranslateService::IsTranslateBubbleEnabled())
1577 return;
1579 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory;
1580 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory);
1581 TranslateBubbleFactory::SetFactory(factory);
1583 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1585 // Check the bubble exists instead of the infobar.
1586 translate::TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1587 ASSERT_TRUE(infobar == NULL);
1588 TranslateBubbleModel* bubble = factory->model();
1589 ASSERT_TRUE(bubble != NULL);
1590 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE,
1591 bubble->GetViewState());
1593 // Simulate clicking translate.
1594 process()->sink().ClearMessages();
1595 bubble->Translate();
1597 // Check the bubble shows "Translating...".
1598 bubble = factory->model();
1599 ASSERT_TRUE(bubble != NULL);
1600 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING,
1601 bubble->GetViewState());
1603 // Simulate the translate script being retrieved (it only needs to be done
1604 // once in the test as it is cached).
1605 SimulateTranslateScriptURLFetch(true);
1607 // Simulate the render notifying the translation has been done.
1608 SimulateOnPageTranslated("fr", "en");
1610 // Check the bubble shows "Translated."
1611 bubble = factory->model();
1612 ASSERT_TRUE(bubble != NULL);
1613 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_AFTER_TRANSLATE,
1614 bubble->GetViewState());
1617 TEST_F(TranslateManagerRenderViewHostTest, BubbleTranslateScriptNotAvailable) {
1618 // See TranslateScriptNotAvailable for corresponding infobar UX testing.
1619 if (!TranslateService::IsTranslateBubbleEnabled())
1620 return;
1622 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory;
1623 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory);
1624 TranslateBubbleFactory::SetFactory(factory);
1626 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1628 // Check the bubble exists instead of the infobar.
1629 translate::TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1630 ASSERT_TRUE(infobar == NULL);
1631 TranslateBubbleModel* bubble = factory->model();
1632 ASSERT_TRUE(bubble != NULL);
1633 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE,
1634 bubble->GetViewState());
1636 // Simulate clicking translate.
1637 process()->sink().ClearMessages();
1638 bubble->Translate();
1639 SimulateTranslateScriptURLFetch(false);
1641 // We should not have sent any message to translate to the renderer.
1642 EXPECT_FALSE(GetTranslateMessage(NULL, NULL));
1644 // And we should have an error infobar showing.
1645 bubble = factory->model();
1646 ASSERT_TRUE(bubble != NULL);
1647 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_ERROR, bubble->GetViewState());
1650 TEST_F(TranslateManagerRenderViewHostTest, BubbleUnknownLanguage) {
1651 // See TranslateUnknownLanguage for corresponding infobar UX testing.
1652 if (!TranslateService::IsTranslateBubbleEnabled())
1653 return;
1655 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory;
1656 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory);
1657 TranslateBubbleFactory::SetFactory(factory);
1659 // Simulate navigating to a page ("und" is the string returned by the CLD for
1660 // languages it does not recognize).
1661 SimulateNavigation(GURL("http://www.google.mys"), "und", true);
1663 // We should not have a bubble as we don't know the language.
1664 ASSERT_TRUE(factory->model() == NULL);
1666 // Translate the page anyway throught the context menu.
1667 scoped_ptr<TestRenderViewContextMenu> menu(CreateContextMenu());
1668 menu->Init();
1669 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
1671 // Check the bubble exists instead of the infobar.
1672 translate::TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1673 ASSERT_TRUE(infobar == NULL);
1674 TranslateBubbleModel* bubble = factory->model();
1675 ASSERT_TRUE(bubble != NULL);
1676 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING,
1677 bubble->GetViewState());