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