| 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" | |
| 18 #include "chrome/browser/infobars/infobar_service.h" | 17 #include "chrome/browser/infobars/infobar_service.h" |
| 19 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_uti
l.h" | 18 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_uti
l.h" |
| 20 #include "chrome/browser/translate/translate_infobar_delegate.h" | 19 #include "chrome/browser/translate/translate_infobar_delegate.h" |
| 21 #include "chrome/browser/translate/translate_service.h" | 20 #include "chrome/browser/translate/translate_service.h" |
| 22 #include "chrome/browser/translate/translate_tab_helper.h" | 21 #include "chrome/browser/translate/translate_tab_helper.h" |
| 23 #include "chrome/browser/ui/translate/translate_bubble_factory.h" | 22 #include "chrome/browser/ui/translate/translate_bubble_factory.h" |
| 24 #include "chrome/browser/ui/translate/translate_bubble_model.h" | 23 #include "chrome/browser/ui/translate/translate_bubble_model.h" |
| 25 #include "chrome/browser/ui/translate/translate_bubble_model_impl.h" | 24 #include "chrome/browser/ui/translate/translate_bubble_model_impl.h" |
| 26 #include "chrome/common/chrome_switches.h" | 25 #include "chrome/common/chrome_switches.h" |
| 27 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 *original_lang = translate_param.c; | 140 *original_lang = translate_param.c; |
| 142 if (target_lang) | 141 if (target_lang) |
| 143 *target_lang = translate_param.d; | 142 *target_lang = translate_param.d; |
| 144 return true; | 143 return true; |
| 145 } | 144 } |
| 146 | 145 |
| 147 InfoBarService* infobar_service() { | 146 InfoBarService* infobar_service() { |
| 148 return InfoBarService::FromWebContents(web_contents()); | 147 return InfoBarService::FromWebContents(web_contents()); |
| 149 } | 148 } |
| 150 | 149 |
| 151 InfoBarManager* infobar_manager() { | |
| 152 return infobar_service()->infobar_manager(); | |
| 153 } | |
| 154 | |
| 155 // Returns the translate infobar if there is 1 infobar and it is a translate | 150 // Returns the translate infobar if there is 1 infobar and it is a translate |
| 156 // infobar. | 151 // infobar. |
| 157 TranslateInfoBarDelegate* GetTranslateInfoBar() { | 152 TranslateInfoBarDelegate* GetTranslateInfoBar() { |
| 158 return (infobar_manager()->infobar_count() == 1) | 153 return (infobar_service()->infobar_count() == 1) ? |
| 159 ? infobar_manager() | 154 infobar_service()->infobar_at(0)->delegate()-> |
| 160 ->infobar_at(0) | 155 AsTranslateInfoBarDelegate() : |
| 161 ->delegate() | 156 NULL; |
| 162 ->AsTranslateInfoBarDelegate() | |
| 163 : NULL; | |
| 164 } | 157 } |
| 165 | 158 |
| 166 // If there is 1 infobar and it is a translate infobar, closes it and returns | 159 // If there is 1 infobar and it is a translate infobar, closes it and returns |
| 167 // true. Returns false otherwise. | 160 // true. Returns false otherwise. |
| 168 bool CloseTranslateInfoBar() { | 161 bool CloseTranslateInfoBar() { |
| 169 InfoBarDelegate* infobar = GetTranslateInfoBar(); | 162 InfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 170 if (!infobar) | 163 if (!infobar) |
| 171 return false; | 164 return false; |
| 172 infobar->InfoBarDismissed(); // Simulates closing the infobar. | 165 infobar->InfoBarDismissed(); // Simulates closing the infobar. |
| 173 infobar_manager()->RemoveInfoBar(infobar_manager()->infobar_at(0)); | 166 infobar_service()->RemoveInfoBar(infobar_service()->infobar_at(0)); |
| 174 return true; | 167 return true; |
| 175 } | 168 } |
| 176 | 169 |
| 177 // Checks whether |infobar| has been removed and clears the removed infobar | 170 // Checks whether |infobar| has been removed and clears the removed infobar |
| 178 // list. | 171 // list. |
| 179 bool CheckInfoBarRemovedAndReset(InfoBarDelegate* delegate) { | 172 bool CheckInfoBarRemovedAndReset(InfoBarDelegate* delegate) { |
| 180 bool found = removed_infobars_.count(delegate) != 0; | 173 bool found = removed_infobars_.count(delegate) != 0; |
| 181 removed_infobars_.clear(); | 174 removed_infobars_.clear(); |
| 182 return found; | 175 return found; |
| 183 } | 176 } |
| 184 | 177 |
| 185 void ExpireTranslateScriptImmediately() { | 178 void ExpireTranslateScriptImmediately() { |
| 186 TranslateDownloadManager::GetInstance()->SetTranslateScriptExpirationDelay( | 179 TranslateDownloadManager::GetInstance()->SetTranslateScriptExpirationDelay( |
| 187 0); | 180 0); |
| 188 } | 181 } |
| 189 | 182 |
| 190 // If there is 1 infobar and it is a translate infobar, deny translation and | 183 // If there is 1 infobar and it is a translate infobar, deny translation and |
| 191 // returns true. Returns false otherwise. | 184 // returns true. Returns false otherwise. |
| 192 bool DenyTranslation() { | 185 bool DenyTranslation() { |
| 193 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 186 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 194 if (!infobar) | 187 if (!infobar) |
| 195 return false; | 188 return false; |
| 196 infobar->TranslationDeclined(); | 189 infobar->TranslationDeclined(); |
| 197 infobar_manager()->RemoveInfoBar(infobar_manager()->infobar_at(0)); | 190 infobar_service()->RemoveInfoBar(infobar_service()->infobar_at(0)); |
| 198 return true; | 191 return true; |
| 199 } | 192 } |
| 200 | 193 |
| 201 void ReloadAndWait(bool successful_reload) { | 194 void ReloadAndWait(bool successful_reload) { |
| 202 NavEntryCommittedObserver nav_observer(web_contents()); | 195 NavEntryCommittedObserver nav_observer(web_contents()); |
| 203 if (successful_reload) | 196 if (successful_reload) |
| 204 Reload(); | 197 Reload(); |
| 205 else | 198 else |
| 206 FailedReload(); | 199 FailedReload(); |
| 207 | 200 |
| (...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 739 // This should not have triggered a translate. | 732 // This should not have triggered a translate. |
| 740 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 733 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 741 } | 734 } |
| 742 | 735 |
| 743 // Tests that multiple OnPageContents do not cause multiple infobars. | 736 // Tests that multiple OnPageContents do not cause multiple infobars. |
| 744 TEST_F(TranslateManagerRenderViewHostTest, MultipleOnPageContents) { | 737 TEST_F(TranslateManagerRenderViewHostTest, MultipleOnPageContents) { |
| 745 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 738 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 746 | 739 |
| 747 // Simulate clicking 'Nope' (don't translate). | 740 // Simulate clicking 'Nope' (don't translate). |
| 748 EXPECT_TRUE(DenyTranslation()); | 741 EXPECT_TRUE(DenyTranslation()); |
| 749 EXPECT_EQ(0U, infobar_manager()->infobar_count()); | 742 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 750 | 743 |
| 751 // Send a new PageContents, we should not show an infobar. | 744 // Send a new PageContents, we should not show an infobar. |
| 752 SimulateOnTranslateLanguageDetermined("fr", true); | 745 SimulateOnTranslateLanguageDetermined("fr", true); |
| 753 EXPECT_EQ(0U, infobar_manager()->infobar_count()); | 746 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 754 | 747 |
| 755 // Do the same steps but simulate closing the infobar this time. | 748 // Do the same steps but simulate closing the infobar this time. |
| 756 SimulateNavigation(GURL("http://www.youtube.fr"), "fr", true); | 749 SimulateNavigation(GURL("http://www.youtube.fr"), "fr", true); |
| 757 EXPECT_TRUE(CloseTranslateInfoBar()); | 750 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 758 EXPECT_EQ(0U, infobar_manager()->infobar_count()); | 751 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 759 SimulateOnTranslateLanguageDetermined("fr", true); | 752 SimulateOnTranslateLanguageDetermined("fr", true); |
| 760 EXPECT_EQ(0U, infobar_manager()->infobar_count()); | 753 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 761 } | 754 } |
| 762 | 755 |
| 763 // Test that reloading the page brings back the infobar if the | 756 // Test that reloading the page brings back the infobar if the |
| 764 // reload succeeded and does not bring it back the reload fails. | 757 // reload succeeded and does not bring it back the reload fails. |
| 765 TEST_F(TranslateManagerRenderViewHostTest, Reload) { | 758 TEST_F(TranslateManagerRenderViewHostTest, Reload) { |
| 766 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 759 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 767 | 760 |
| 768 EXPECT_TRUE(CloseTranslateInfoBar()); | 761 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 769 | 762 |
| 770 // Reload should bring back the infobar if the reload succeeds. | 763 // Reload should bring back the infobar if the reload succeeds. |
| (...skipping 776 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1547 // Check the bubble exists instead of the infobar. | 1540 // Check the bubble exists instead of the infobar. |
| 1548 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 1541 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 1549 ASSERT_TRUE(infobar == NULL); | 1542 ASSERT_TRUE(infobar == NULL); |
| 1550 TranslateBubbleModel* bubble = factory->model(); | 1543 TranslateBubbleModel* bubble = factory->model(); |
| 1551 ASSERT_TRUE(bubble != NULL); | 1544 ASSERT_TRUE(bubble != NULL); |
| 1552 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING, | 1545 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING, |
| 1553 bubble->GetViewState()); | 1546 bubble->GetViewState()); |
| 1554 } | 1547 } |
| 1555 | 1548 |
| 1556 #endif // defined(USE_AURA) | 1549 #endif // defined(USE_AURA) |
| OLD | NEW |