Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(278)

Side by Side Diff: chrome/browser/translate/translate_manager_browsertest.cc

Issue 22694006: Infobar system refactor. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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"
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 } 141 }
142 142
143 InfoBarService* infobar_service() { 143 InfoBarService* infobar_service() {
144 return InfoBarService::FromWebContents(web_contents()); 144 return InfoBarService::FromWebContents(web_contents());
145 } 145 }
146 146
147 // Returns the translate infobar if there is 1 infobar and it is a translate 147 // Returns the translate infobar if there is 1 infobar and it is a translate
148 // infobar. 148 // infobar.
149 TranslateInfoBarDelegate* GetTranslateInfoBar() { 149 TranslateInfoBarDelegate* GetTranslateInfoBar() {
150 return (infobar_service()->infobar_count() == 1) ? 150 return (infobar_service()->infobar_count() == 1) ?
151 infobar_service()->infobar_at(0)->AsTranslateInfoBarDelegate() : NULL; 151 infobar_service()->infobar_at(0)->delegate()->
152 AsTranslateInfoBarDelegate() : NULL;
152 } 153 }
153 154
154 // If there is 1 infobar and it is a translate infobar, closes it and returns 155 // If there is 1 infobar and it is a translate infobar, closes it and returns
155 // true. Returns false otherwise. 156 // true. Returns false otherwise.
156 bool CloseTranslateInfoBar() { 157 bool CloseTranslateInfoBar() {
157 InfoBarDelegate* infobar = GetTranslateInfoBar(); 158 InfoBarDelegate* infobar = GetTranslateInfoBar();
158 if (!infobar) 159 if (!infobar)
159 return false; 160 return false;
160 infobar->InfoBarDismissed(); // Simulates closing the infobar. 161 infobar->InfoBarDismissed(); // Simulates closing the infobar.
161 infobar_service()->RemoveInfoBar(infobar); 162 infobar_service()->RemoveInfoBar(infobar_service()->infobar_at(0));
162 return true; 163 return true;
163 } 164 }
164 165
165 // Checks whether |infobar| has been removed and clears the removed infobar 166 // Checks whether |infobar| has been removed and clears the removed infobar
166 // list. 167 // list.
167 bool CheckInfoBarRemovedAndReset(InfoBarDelegate* delegate) { 168 bool CheckInfoBarRemovedAndReset(InfoBarDelegate* delegate) {
168 bool found = removed_infobars_.count(delegate) != 0; 169 bool found = removed_infobars_.count(delegate) != 0;
169 removed_infobars_.clear(); 170 removed_infobars_.clear();
170 return found; 171 return found;
171 } 172 }
172 173
173 void ExpireTranslateScriptImmediately() { 174 void ExpireTranslateScriptImmediately() {
174 TranslateManager::GetInstance()->SetTranslateScriptExpirationDelay(0); 175 TranslateManager::GetInstance()->SetTranslateScriptExpirationDelay(0);
175 } 176 }
176 177
177 // If there is 1 infobar and it is a translate infobar, deny translation and 178 // If there is 1 infobar and it is a translate infobar, deny translation and
178 // returns true. Returns false otherwise. 179 // returns true. Returns false otherwise.
179 bool DenyTranslation() { 180 bool DenyTranslation() {
180 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); 181 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
181 if (!infobar) 182 if (!infobar)
182 return false; 183 return false;
183 infobar->TranslationDeclined(); 184 infobar->TranslationDeclined();
184 infobar_service()->RemoveInfoBar(infobar); 185 infobar_service()->RemoveInfoBar(infobar_service()->infobar_at(0));
185 return true; 186 return true;
186 } 187 }
187 188
188 void ReloadAndWait(bool successful_reload) { 189 void ReloadAndWait(bool successful_reload) {
189 NavEntryCommittedObserver nav_observer(web_contents()); 190 NavEntryCommittedObserver nav_observer(web_contents());
190 if (successful_reload) 191 if (successful_reload)
191 Reload(); 192 Reload();
192 else 193 else
193 FailedReload(); 194 FailedReload();
194 195
195 // Ensures it is really handled a reload. 196 // Ensures it is really handled a reload.
196 const content::LoadCommittedDetails& nav_details = 197 const content::LoadCommittedDetails& nav_details =
197 nav_observer.load_committed_details(); 198 nav_observer.load_committed_details();
198 EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation. 199 EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation.
199 EXPECT_EQ(content::NAVIGATION_TYPE_EXISTING_PAGE, nav_details.type); 200 EXPECT_EQ(content::NAVIGATION_TYPE_EXISTING_PAGE, nav_details.type);
200 201
201 // The TranslateManager class processes the navigation entry committed 202 // The TranslateManager class processes the navigation entry committed
202 // notification in a posted task; process that task. 203 // notification in a posted task; process that task.
203 base::MessageLoop::current()->RunUntilIdle(); 204 base::MessageLoop::current()->RunUntilIdle();
204 } 205 }
205 206
206 virtual void Observe(int type, 207 virtual void Observe(int type,
207 const content::NotificationSource& source, 208 const content::NotificationSource& source,
208 const content::NotificationDetails& details) { 209 const content::NotificationDetails& details) {
209 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type); 210 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type);
210 removed_infobars_.insert( 211 removed_infobars_.insert(
211 content::Details<InfoBarRemovedDetails>(details)->first); 212 content::Details<InfoBar::RemovedDetails>(details)->first->delegate());
212 } 213 }
213 214
214 MOCK_METHOD1(OnPreferenceChanged, void(const std::string&)); 215 MOCK_METHOD1(OnPreferenceChanged, void(const std::string&));
215 216
216 protected: 217 protected:
217 virtual void SetUp() { 218 virtual void SetUp() {
218 // Access the TranslateManager singleton so it is created before we call 219 // Access the TranslateManager singleton so it is created before we call
219 // ChromeRenderViewHostTestHarness::SetUp() to match what's done in Chrome, 220 // ChromeRenderViewHostTestHarness::SetUp() to match what's done in Chrome,
220 // where the TranslateManager is created before the WebContents. This 221 // where the TranslateManager is created before the WebContents. This
221 // matters as they both register for similar events and we want the 222 // matters as they both register for similar events and we want the
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 // Simulate changing the original language and translating. 410 // Simulate changing the original language and translating.
410 process()->sink().ClearMessages(); 411 process()->sink().ClearMessages();
411 std::string new_original_lang = infobar->language_code_at(0); 412 std::string new_original_lang = infobar->language_code_at(0);
412 infobar->set_original_language_index(0); 413 infobar->set_original_language_index(0);
413 infobar->Translate(); 414 infobar->Translate();
414 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); 415 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
415 EXPECT_EQ(new_original_lang, original_lang); 416 EXPECT_EQ(new_original_lang, original_lang);
416 EXPECT_EQ("en", target_lang); 417 EXPECT_EQ("en", target_lang);
417 // Simulate the render notifying the translation has been done. 418 // Simulate the render notifying the translation has been done.
418 SimulateOnPageTranslated(new_original_lang, "en"); 419 SimulateOnPageTranslated(new_original_lang, "en");
419 // infobar is now invalid. 420 infobar = GetTranslateInfoBar();
420 TranslateInfoBarDelegate* new_infobar = GetTranslateInfoBar(); 421 ASSERT_TRUE(infobar != NULL);
421 ASSERT_TRUE(new_infobar != NULL);
422 infobar = new_infobar;
423 422
424 // Simulate changing the target language and translating. 423 // Simulate changing the target language and translating.
425 process()->sink().ClearMessages(); 424 process()->sink().ClearMessages();
426 std::string new_target_lang = infobar->language_code_at(1); 425 std::string new_target_lang = infobar->language_code_at(1);
427 infobar->set_target_language_index(1); 426 infobar->set_target_language_index(1);
428 infobar->Translate(); 427 infobar->Translate();
429 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); 428 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
430 EXPECT_EQ(new_original_lang, original_lang); 429 EXPECT_EQ(new_original_lang, original_lang);
431 EXPECT_EQ(new_target_lang, target_lang); 430 EXPECT_EQ(new_target_lang, target_lang);
432 // Simulate the render notifying the translation has been done. 431 // Simulate the render notifying the translation has been done.
433 SimulateOnPageTranslated(new_original_lang, new_target_lang); 432 SimulateOnPageTranslated(new_original_lang, new_target_lang);
434 // infobar is now invalid. 433 infobar = GetTranslateInfoBar();
435 new_infobar = GetTranslateInfoBar(); 434 ASSERT_TRUE(infobar != NULL);
436 ASSERT_TRUE(new_infobar != NULL); 435 EXPECT_EQ(new_target_lang, infobar->target_language_code());
437 436
438 // Verify reload keeps the same settings. 437 // Reloading should trigger translation iff Always Translate is on.
439 ReloadAndWait(true); 438 ReloadAndWait(true);
440 new_infobar = GetTranslateInfoBar(); 439 infobar = GetTranslateInfoBar();
441 ASSERT_TRUE(new_infobar != NULL); 440 ASSERT_TRUE(infobar != NULL);
442 ASSERT_EQ(new_target_lang, infobar->target_language_code()); 441 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE,
442 infobar->infobar_type());
443 infobar->set_target_language_index(1);
444 infobar->ToggleAlwaysTranslate();
445 ReloadAndWait(true);
446 infobar = GetTranslateInfoBar();
447 ASSERT_TRUE(infobar != NULL);
448 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->infobar_type());
449 EXPECT_EQ(new_target_lang, infobar->target_language_code());
443 } 450 }
444 451
445 TEST_F(TranslateManagerBrowserTest, TranslateScriptNotAvailable) { 452 TEST_F(TranslateManagerBrowserTest, TranslateScriptNotAvailable) {
446 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 453 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
447 454
448 // We should have an infobar. 455 // We should have an infobar.
449 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); 456 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
450 ASSERT_TRUE(infobar != NULL); 457 ASSERT_TRUE(infobar != NULL);
451 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, 458 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE,
452 infobar->infobar_type()); 459 infobar->infobar_type());
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
721 EXPECT_EQ(0U, infobar_service()->infobar_count()); 728 EXPECT_EQ(0U, infobar_service()->infobar_count());
722 } 729 }
723 730
724 // Test that reloading the page brings back the infobar if the 731 // Test that reloading the page brings back the infobar if the
725 // reload succeeded and does not bring it back the reload fails. 732 // reload succeeded and does not bring it back the reload fails.
726 TEST_F(TranslateManagerBrowserTest, Reload) { 733 TEST_F(TranslateManagerBrowserTest, Reload) {
727 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 734 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
728 735
729 EXPECT_TRUE(CloseTranslateInfoBar()); 736 EXPECT_TRUE(CloseTranslateInfoBar());
730 737
731 // Reload should bring back the infobar if the page succeds 738 // Reload should bring back the infobar if the reload succeeds.
732 ReloadAndWait(true); 739 ReloadAndWait(true);
733 EXPECT_TRUE(GetTranslateInfoBar() != NULL); 740 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
734
735 EXPECT_TRUE(CloseTranslateInfoBar()); 741 EXPECT_TRUE(CloseTranslateInfoBar());
736 742
737 // And not show it if the reload fails 743 // ...But not show it if the reload fails.
738 ReloadAndWait(false); 744 ReloadAndWait(false);
739 EXPECT_EQ(NULL, GetTranslateInfoBar()); 745 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
740 746
741 // Set reload attempts to a high value, we will not see the infobar 747 // If we set reload attempts to a high value, we will not see the infobar
742 // immediatly. 748 // immediately.
743 TranslateManager::GetInstance()->set_translate_max_reload_attemps(100); 749 TranslateManager::GetInstance()->set_translate_max_reload_attemps(100);
744 ReloadAndWait(true); 750 ReloadAndWait(true);
745 EXPECT_TRUE(GetTranslateInfoBar() == NULL); 751 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
746 } 752 }
747 753
748 // Test that reloading the page by way of typing again the URL in the 754 // Test that reloading the page by way of typing again the URL in the
749 // location bar brings back the infobar. 755 // location bar brings back the infobar.
750 TEST_F(TranslateManagerBrowserTest, ReloadFromLocationBar) { 756 TEST_F(TranslateManagerBrowserTest, ReloadFromLocationBar) {
751 GURL url("http://www.google.fr"); 757 GURL url("http://www.google.fr");
752 SimulateNavigation(url, "fr", true); 758 SimulateNavigation(url, "fr", true);
(...skipping 701 matching lines...) Expand 10 before | Expand all | Expand 10 after
1454 content::WebContents* current_web_contents = 1460 content::WebContents* current_web_contents =
1455 browser()->tab_strip_model()->GetActiveWebContents(); 1461 browser()->tab_strip_model()->GetActiveWebContents();
1456 content::Source<content::WebContents> source(current_web_contents); 1462 content::Source<content::WebContents> source(current_web_contents);
1457 1463
1458 ui_test_utils::WindowedNotificationObserverWithDetails< 1464 ui_test_utils::WindowedNotificationObserverWithDetails<
1459 LanguageDetectionDetails> 1465 LanguageDetectionDetails>
1460 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, 1466 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
1461 source); 1467 source);
1462 fr_language_detected_signal.Wait(); 1468 fr_language_detected_signal.Wait();
1463 } 1469 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698