| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <set> | 6 #include <set> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/prefs/pref_change_registrar.h" | 10 #include "base/prefs/pref_change_registrar.h" |
| 11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "chrome/app/chrome_command_ids.h" | 13 #include "chrome/app/chrome_command_ids.h" |
| 14 #include "chrome/browser/chrome_notification_types.h" | 14 #include "chrome/browser/chrome_notification_types.h" |
| 15 #include "chrome/browser/extensions/test_extension_system.h" | 15 #include "chrome/browser/extensions/test_extension_system.h" |
| 16 #include "chrome/browser/infobars/infobar.h" | 16 #include "chrome/browser/infobars/infobar.h" |
| 17 #include "chrome/browser/infobars/infobar_manager.h" |
| 17 #include "chrome/browser/infobars/infobar_service.h" | 18 #include "chrome/browser/infobars/infobar_service.h" |
| 18 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_uti
l.h" | 19 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_uti
l.h" |
| 19 #include "chrome/browser/translate/translate_infobar_delegate.h" | 20 #include "chrome/browser/translate/translate_infobar_delegate.h" |
| 20 #include "chrome/browser/translate/translate_manager.h" | 21 #include "chrome/browser/translate/translate_manager.h" |
| 21 #include "chrome/browser/translate/translate_service.h" | 22 #include "chrome/browser/translate/translate_service.h" |
| 22 #include "chrome/browser/translate/translate_tab_helper.h" | 23 #include "chrome/browser/translate/translate_tab_helper.h" |
| 23 #include "chrome/browser/ui/translate/translate_bubble_factory.h" | 24 #include "chrome/browser/ui/translate/translate_bubble_factory.h" |
| 24 #include "chrome/browser/ui/translate/translate_bubble_model.h" | 25 #include "chrome/browser/ui/translate/translate_bubble_model.h" |
| 25 #include "chrome/browser/ui/translate/translate_bubble_model_impl.h" | 26 #include "chrome/browser/ui/translate/translate_bubble_model_impl.h" |
| 26 #include "chrome/common/chrome_switches.h" | 27 #include "chrome/common/chrome_switches.h" |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 return true; | 144 return true; |
| 144 } | 145 } |
| 145 | 146 |
| 146 InfoBarService* infobar_service() { | 147 InfoBarService* infobar_service() { |
| 147 return InfoBarService::FromWebContents(web_contents()); | 148 return InfoBarService::FromWebContents(web_contents()); |
| 148 } | 149 } |
| 149 | 150 |
| 150 // Returns the translate infobar if there is 1 infobar and it is a translate | 151 // Returns the translate infobar if there is 1 infobar and it is a translate |
| 151 // infobar. | 152 // infobar. |
| 152 TranslateInfoBarDelegate* GetTranslateInfoBar() { | 153 TranslateInfoBarDelegate* GetTranslateInfoBar() { |
| 153 return (infobar_service()->infobar_count() == 1) | 154 InfoBarManager& infobar_manager = infobar_service()->infobar_manager(); |
| 154 ? infobar_service() | 155 return (infobar_manager.infobar_count() == 1) |
| 155 ->infobar_at(0) | 156 ? infobar_manager.infobar_at(0) |
| 156 ->delegate() | 157 ->delegate() |
| 157 ->AsTranslateInfoBarDelegate() | 158 ->AsTranslateInfoBarDelegate() |
| 158 : NULL; | 159 : NULL; |
| 159 } | 160 } |
| 160 | 161 |
| 161 // If there is 1 infobar and it is a translate infobar, closes it and returns | 162 // If there is 1 infobar and it is a translate infobar, closes it and returns |
| 162 // true. Returns false otherwise. | 163 // true. Returns false otherwise. |
| 163 bool CloseTranslateInfoBar() { | 164 bool CloseTranslateInfoBar() { |
| 164 InfoBarDelegate* infobar = GetTranslateInfoBar(); | 165 InfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 165 if (!infobar) | 166 if (!infobar) |
| 166 return false; | 167 return false; |
| 167 infobar->InfoBarDismissed(); // Simulates closing the infobar. | 168 infobar->InfoBarDismissed(); // Simulates closing the infobar. |
| 168 infobar_service()->RemoveInfoBar(infobar_service()->infobar_at(0)); | 169 InfoBarManager& infobar_manager = infobar_service()->infobar_manager(); |
| 170 infobar_manager.RemoveInfoBar(infobar_manager.infobar_at(0)); |
| 169 return true; | 171 return true; |
| 170 } | 172 } |
| 171 | 173 |
| 172 // Checks whether |infobar| has been removed and clears the removed infobar | 174 // Checks whether |infobar| has been removed and clears the removed infobar |
| 173 // list. | 175 // list. |
| 174 bool CheckInfoBarRemovedAndReset(InfoBarDelegate* delegate) { | 176 bool CheckInfoBarRemovedAndReset(InfoBarDelegate* delegate) { |
| 175 bool found = removed_infobars_.count(delegate) != 0; | 177 bool found = removed_infobars_.count(delegate) != 0; |
| 176 removed_infobars_.clear(); | 178 removed_infobars_.clear(); |
| 177 return found; | 179 return found; |
| 178 } | 180 } |
| 179 | 181 |
| 180 void ExpireTranslateScriptImmediately() { | 182 void ExpireTranslateScriptImmediately() { |
| 181 TranslateDownloadManager::GetInstance()->SetTranslateScriptExpirationDelay( | 183 TranslateDownloadManager::GetInstance()->SetTranslateScriptExpirationDelay( |
| 182 0); | 184 0); |
| 183 } | 185 } |
| 184 | 186 |
| 185 // If there is 1 infobar and it is a translate infobar, deny translation and | 187 // If there is 1 infobar and it is a translate infobar, deny translation and |
| 186 // returns true. Returns false otherwise. | 188 // returns true. Returns false otherwise. |
| 187 bool DenyTranslation() { | 189 bool DenyTranslation() { |
| 188 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 190 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 189 if (!infobar) | 191 if (!infobar) |
| 190 return false; | 192 return false; |
| 191 infobar->TranslationDeclined(); | 193 infobar->TranslationDeclined(); |
| 192 infobar_service()->RemoveInfoBar(infobar_service()->infobar_at(0)); | 194 InfoBarManager& infobar_manager = infobar_service()->infobar_manager(); |
| 195 infobar_manager.RemoveInfoBar(infobar_manager.infobar_at(0)); |
| 193 return true; | 196 return true; |
| 194 } | 197 } |
| 195 | 198 |
| 196 void ReloadAndWait(bool successful_reload) { | 199 void ReloadAndWait(bool successful_reload) { |
| 197 NavEntryCommittedObserver nav_observer(web_contents()); | 200 NavEntryCommittedObserver nav_observer(web_contents()); |
| 198 if (successful_reload) | 201 if (successful_reload) |
| 199 Reload(); | 202 Reload(); |
| 200 else | 203 else |
| 201 FailedReload(); | 204 FailedReload(); |
| 202 | 205 |
| (...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 723 process()->sink().ClearMessages(); | 726 process()->sink().ClearMessages(); |
| 724 SimulateNavigation(GURL("http://news.google.es"), "es", true); | 727 SimulateNavigation(GURL("http://news.google.es"), "es", true); |
| 725 | 728 |
| 726 // This should not have triggered a translate. | 729 // This should not have triggered a translate. |
| 727 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 730 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 728 } | 731 } |
| 729 | 732 |
| 730 // Tests that multiple OnPageContents do not cause multiple infobars. | 733 // Tests that multiple OnPageContents do not cause multiple infobars. |
| 731 TEST_F(TranslateManagerRenderViewHostTest, MultipleOnPageContents) { | 734 TEST_F(TranslateManagerRenderViewHostTest, MultipleOnPageContents) { |
| 732 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 735 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 736 InfoBarManager& infobar_manager = infobar_service()->infobar_manager(); |
| 733 | 737 |
| 734 // Simulate clicking 'Nope' (don't translate). | 738 // Simulate clicking 'Nope' (don't translate). |
| 735 EXPECT_TRUE(DenyTranslation()); | 739 EXPECT_TRUE(DenyTranslation()); |
| 736 EXPECT_EQ(0U, infobar_service()->infobar_count()); | 740 EXPECT_EQ(0U, infobar_manager.infobar_count()); |
| 737 | 741 |
| 738 // Send a new PageContents, we should not show an infobar. | 742 // Send a new PageContents, we should not show an infobar. |
| 739 SimulateOnTranslateLanguageDetermined("fr", true); | 743 SimulateOnTranslateLanguageDetermined("fr", true); |
| 740 EXPECT_EQ(0U, infobar_service()->infobar_count()); | 744 EXPECT_EQ(0U, infobar_manager.infobar_count()); |
| 741 | 745 |
| 742 // Do the same steps but simulate closing the infobar this time. | 746 // Do the same steps but simulate closing the infobar this time. |
| 743 SimulateNavigation(GURL("http://www.youtube.fr"), "fr", true); | 747 SimulateNavigation(GURL("http://www.youtube.fr"), "fr", true); |
| 744 EXPECT_TRUE(CloseTranslateInfoBar()); | 748 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 745 EXPECT_EQ(0U, infobar_service()->infobar_count()); | 749 EXPECT_EQ(0U, infobar_manager.infobar_count()); |
| 746 SimulateOnTranslateLanguageDetermined("fr", true); | 750 SimulateOnTranslateLanguageDetermined("fr", true); |
| 747 EXPECT_EQ(0U, infobar_service()->infobar_count()); | 751 EXPECT_EQ(0U, infobar_manager.infobar_count()); |
| 748 } | 752 } |
| 749 | 753 |
| 750 // Test that reloading the page brings back the infobar if the | 754 // Test that reloading the page brings back the infobar if the |
| 751 // reload succeeded and does not bring it back the reload fails. | 755 // reload succeeded and does not bring it back the reload fails. |
| 752 TEST_F(TranslateManagerRenderViewHostTest, Reload) { | 756 TEST_F(TranslateManagerRenderViewHostTest, Reload) { |
| 753 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 757 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 754 | 758 |
| 755 EXPECT_TRUE(CloseTranslateInfoBar()); | 759 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 756 | 760 |
| 757 // Reload should bring back the infobar if the reload succeeds. | 761 // Reload should bring back the infobar if the reload succeeds. |
| (...skipping 773 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1531 // Check the bubble exists instead of the infobar. | 1535 // Check the bubble exists instead of the infobar. |
| 1532 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 1536 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 1533 ASSERT_TRUE(infobar == NULL); | 1537 ASSERT_TRUE(infobar == NULL); |
| 1534 TranslateBubbleModel* bubble = factory->model(); | 1538 TranslateBubbleModel* bubble = factory->model(); |
| 1535 ASSERT_TRUE(bubble != NULL); | 1539 ASSERT_TRUE(bubble != NULL); |
| 1536 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING, | 1540 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING, |
| 1537 bubble->GetViewState()); | 1541 bubble->GetViewState()); |
| 1538 } | 1542 } |
| 1539 | 1543 |
| 1540 #endif // defined(USE_AURA) | 1544 #endif // defined(USE_AURA) |
| OLD | NEW |