| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 | 5 |
| 6 #include <algorithm> | 6 #include <algorithm> |
| 7 #include <set> | 7 #include <set> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "chrome/app/chrome_command_ids.h" | 12 #include "chrome/app/chrome_command_ids.h" |
| 13 #include "chrome/browser/infobars/infobar_tab_helper.h" |
| 13 #include "chrome/browser/prefs/pref_change_registrar.h" | 14 #include "chrome/browser/prefs/pref_change_registrar.h" |
| 14 #include "chrome/browser/prefs/pref_service.h" | 15 #include "chrome/browser/prefs/pref_service.h" |
| 15 #include "chrome/browser/tab_contents/infobar.h" | 16 #include "chrome/browser/tab_contents/infobar.h" |
| 16 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 17 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
| 17 #include "chrome/browser/translate/translate_infobar_delegate.h" | 18 #include "chrome/browser/translate/translate_infobar_delegate.h" |
| 18 #include "chrome/browser/translate/translate_manager.h" | 19 #include "chrome/browser/translate/translate_manager.h" |
| 19 #include "chrome/browser/translate/translate_prefs.h" | 20 #include "chrome/browser/translate/translate_prefs.h" |
| 20 #include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h" | 21 #include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h" |
| 21 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 22 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 22 #include "chrome/common/chrome_notification_types.h" | 23 #include "chrome/common/chrome_notification_types.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 45 using testing::Property; | 46 using testing::Property; |
| 46 using WebKit::WebContextMenuData; | 47 using WebKit::WebContextMenuData; |
| 47 | 48 |
| 48 class TranslateManagerTest : public TabContentsWrapperTestHarness, | 49 class TranslateManagerTest : public TabContentsWrapperTestHarness, |
| 49 public NotificationObserver { | 50 public NotificationObserver { |
| 50 public: | 51 public: |
| 51 TranslateManagerTest() | 52 TranslateManagerTest() |
| 52 : ui_thread_(BrowserThread::UI, &message_loop_) { | 53 : ui_thread_(BrowserThread::UI, &message_loop_) { |
| 53 } | 54 } |
| 54 | 55 |
| 55 // Simluates navigating to a page and getting the page contents and language | 56 // Simulates navigating to a page and getting the page contents and language |
| 56 // for that navigation. | 57 // for that navigation. |
| 57 void SimulateNavigation(const GURL& url, | 58 void SimulateNavigation(const GURL& url, |
| 58 const std::string& lang, | 59 const std::string& lang, |
| 59 bool page_translatable) { | 60 bool page_translatable) { |
| 60 NavigateAndCommit(url); | 61 NavigateAndCommit(url); |
| 61 SimulateOnTranslateLanguageDetermined(lang, page_translatable); | 62 SimulateOnTranslateLanguageDetermined(lang, page_translatable); |
| 62 } | 63 } |
| 63 | 64 |
| 64 void SimulateOnTranslateLanguageDetermined(const std::string& lang, | 65 void SimulateOnTranslateLanguageDetermined(const std::string& lang, |
| 65 bool page_translatable) { | 66 bool page_translatable) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 80 if (page_id) | 81 if (page_id) |
| 81 *page_id = translate_param.a; | 82 *page_id = translate_param.a; |
| 82 // Ignore translate_param.b which is the script injected in the page. | 83 // Ignore translate_param.b which is the script injected in the page. |
| 83 if (original_lang) | 84 if (original_lang) |
| 84 *original_lang = translate_param.c; | 85 *original_lang = translate_param.c; |
| 85 if (target_lang) | 86 if (target_lang) |
| 86 *target_lang = translate_param.d; | 87 *target_lang = translate_param.d; |
| 87 return true; | 88 return true; |
| 88 } | 89 } |
| 89 | 90 |
| 91 InfoBarTabHelper* infobar_tab_helper() { |
| 92 return contents_wrapper()->infobar_tab_helper(); |
| 93 } |
| 94 |
| 90 // Returns the translate infobar if there is 1 infobar and it is a translate | 95 // Returns the translate infobar if there is 1 infobar and it is a translate |
| 91 // infobar. | 96 // infobar. |
| 92 TranslateInfoBarDelegate* GetTranslateInfoBar() { | 97 TranslateInfoBarDelegate* GetTranslateInfoBar() { |
| 93 return (contents_wrapper()->infobar_count() == 1) ? | 98 return (infobar_tab_helper()->infobar_count() == 1) ? |
| 94 contents_wrapper()->GetInfoBarDelegateAt(0)-> | 99 infobar_tab_helper()->GetInfoBarDelegateAt(0)-> |
| 95 AsTranslateInfoBarDelegate() : NULL; | 100 AsTranslateInfoBarDelegate() : NULL; |
| 96 } | 101 } |
| 97 | 102 |
| 98 // If there is 1 infobar and it is a translate infobar, closes it and returns | 103 // If there is 1 infobar and it is a translate infobar, closes it and returns |
| 99 // true. Returns false otherwise. | 104 // true. Returns false otherwise. |
| 100 bool CloseTranslateInfoBar() { | 105 bool CloseTranslateInfoBar() { |
| 101 InfoBarDelegate* infobar = GetTranslateInfoBar(); | 106 InfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 102 if (!infobar) | 107 if (!infobar) |
| 103 return false; | 108 return false; |
| 104 infobar->InfoBarDismissed(); // Simulates closing the infobar. | 109 infobar->InfoBarDismissed(); // Simulates closing the infobar. |
| 105 contents_wrapper()->RemoveInfoBar(infobar); | 110 infobar_tab_helper()->RemoveInfoBar(infobar); |
| 106 return true; | 111 return true; |
| 107 } | 112 } |
| 108 | 113 |
| 109 // Checks whether |infobar| has been removed and clears the removed infobar | 114 // Checks whether |infobar| has been removed and clears the removed infobar |
| 110 // list. | 115 // list. |
| 111 bool CheckInfoBarRemovedAndReset(InfoBarDelegate* delegate) { | 116 bool CheckInfoBarRemovedAndReset(InfoBarDelegate* delegate) { |
| 112 bool found = removed_infobars_.count(delegate) != 0; | 117 bool found = removed_infobars_.count(delegate) != 0; |
| 113 removed_infobars_.clear(); | 118 removed_infobars_.clear(); |
| 114 return found; | 119 return found; |
| 115 } | 120 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 128 TranslateManager::GetInstance()->set_translate_script_expiration_delay(0); | 133 TranslateManager::GetInstance()->set_translate_script_expiration_delay(0); |
| 129 } | 134 } |
| 130 | 135 |
| 131 // If there is 1 infobar and it is a translate infobar, deny translation and | 136 // If there is 1 infobar and it is a translate infobar, deny translation and |
| 132 // returns true. Returns false otherwise. | 137 // returns true. Returns false otherwise. |
| 133 bool DenyTranslation() { | 138 bool DenyTranslation() { |
| 134 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 139 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 135 if (!infobar) | 140 if (!infobar) |
| 136 return false; | 141 return false; |
| 137 infobar->TranslationDeclined(); | 142 infobar->TranslationDeclined(); |
| 138 contents_wrapper()->RemoveInfoBar(infobar); | 143 infobar_tab_helper()->RemoveInfoBar(infobar); |
| 139 return true; | 144 return true; |
| 140 } | 145 } |
| 141 | 146 |
| 142 virtual void Observe(int type, | 147 virtual void Observe(int type, |
| 143 const NotificationSource& source, | 148 const NotificationSource& source, |
| 144 const NotificationDetails& details) { | 149 const NotificationDetails& details) { |
| 145 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type); | 150 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type); |
| 146 removed_infobars_.insert(Details<InfoBarRemovedDetails>(details)->first); | 151 removed_infobars_.insert(Details<InfoBarRemovedDetails>(details)->first); |
| 147 } | 152 } |
| 148 | 153 |
| (...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 625 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 630 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 626 } | 631 } |
| 627 | 632 |
| 628 // Tests that multiple OnPageContents do not cause multiple infobars. | 633 // Tests that multiple OnPageContents do not cause multiple infobars. |
| 629 TEST_F(TranslateManagerTest, MultipleOnPageContents) { | 634 TEST_F(TranslateManagerTest, MultipleOnPageContents) { |
| 630 // Simulate navigating to a page and getting its language. | 635 // Simulate navigating to a page and getting its language. |
| 631 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 636 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 632 | 637 |
| 633 // Simulate clicking 'Nope' (don't translate). | 638 // Simulate clicking 'Nope' (don't translate). |
| 634 EXPECT_TRUE(DenyTranslation()); | 639 EXPECT_TRUE(DenyTranslation()); |
| 635 EXPECT_EQ(0U, contents_wrapper()->infobar_count()); | 640 EXPECT_EQ(0U, infobar_tab_helper()->infobar_count()); |
| 636 | 641 |
| 637 // Send a new PageContents, we should not show an infobar. | 642 // Send a new PageContents, we should not show an infobar. |
| 638 SimulateOnTranslateLanguageDetermined("fr", true); | 643 SimulateOnTranslateLanguageDetermined("fr", true); |
| 639 EXPECT_EQ(0U, contents_wrapper()->infobar_count()); | 644 EXPECT_EQ(0U, infobar_tab_helper()->infobar_count()); |
| 640 | 645 |
| 641 // Do the same steps but simulate closing the infobar this time. | 646 // Do the same steps but simulate closing the infobar this time. |
| 642 SimulateNavigation(GURL("http://www.youtube.fr"), "fr", true); | 647 SimulateNavigation(GURL("http://www.youtube.fr"), "fr", true); |
| 643 EXPECT_TRUE(CloseTranslateInfoBar()); | 648 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 644 EXPECT_EQ(0U, contents_wrapper()->infobar_count()); | 649 EXPECT_EQ(0U, infobar_tab_helper()->infobar_count()); |
| 645 SimulateOnTranslateLanguageDetermined("fr", true); | 650 SimulateOnTranslateLanguageDetermined("fr", true); |
| 646 EXPECT_EQ(0U, contents_wrapper()->infobar_count()); | 651 EXPECT_EQ(0U, infobar_tab_helper()->infobar_count()); |
| 647 } | 652 } |
| 648 | 653 |
| 649 // Test that reloading the page brings back the infobar. | 654 // Test that reloading the page brings back the infobar. |
| 650 TEST_F(TranslateManagerTest, Reload) { | 655 TEST_F(TranslateManagerTest, Reload) { |
| 651 // Simulate navigating to a page and getting its language. | 656 // Simulate navigating to a page and getting its language. |
| 652 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 657 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 653 | 658 |
| 654 // Close the infobar. | 659 // Close the infobar. |
| 655 EXPECT_TRUE(CloseTranslateInfoBar()); | 660 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 656 | 661 |
| (...skipping 671 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1328 NULL); | 1333 NULL); |
| 1329 // Now simulate the URL fetch. | 1334 // Now simulate the URL fetch. |
| 1330 SimulateTranslateScriptURLFetch(true); | 1335 SimulateTranslateScriptURLFetch(true); |
| 1331 // Now the message should have been sent. | 1336 // Now the message should have been sent. |
| 1332 int page_id = 0; | 1337 int page_id = 0; |
| 1333 std::string original_lang, target_lang; | 1338 std::string original_lang, target_lang; |
| 1334 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 1339 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1335 EXPECT_EQ("es", original_lang); | 1340 EXPECT_EQ("es", original_lang); |
| 1336 EXPECT_EQ("en", target_lang); | 1341 EXPECT_EQ("en", target_lang); |
| 1337 } | 1342 } |
| OLD | NEW |