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

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

Issue 7810002: Move infobar handling to a tab helper. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: update Created 9 years, 3 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) 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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
90 // Returns the translate infobar if there is 1 infobar and it is a translate 91 // Returns the translate infobar if there is 1 infobar and it is a translate
91 // infobar. 92 // infobar.
92 TranslateInfoBarDelegate* GetTranslateInfoBar() { 93 TranslateInfoBarDelegate* GetTranslateInfoBar() {
93 return (contents_wrapper()->infobar_count() == 1) ? 94 return (contents_wrapper()->infobar_tab_helper()->infobar_count() == 1) ?
Peter Kasting 2011/08/31 18:47:32 Nit: Factor this infobar_tab_helper() out to a tem
94 contents_wrapper()->GetInfoBarDelegateAt(0)-> 95 contents_wrapper()->infobar_tab_helper()->GetInfoBarDelegateAt(0)->
95 AsTranslateInfoBarDelegate() : NULL; 96 AsTranslateInfoBarDelegate() : NULL;
96 } 97 }
97 98
98 // If there is 1 infobar and it is a translate infobar, closes it and returns 99 // If there is 1 infobar and it is a translate infobar, closes it and returns
99 // true. Returns false otherwise. 100 // true. Returns false otherwise.
100 bool CloseTranslateInfoBar() { 101 bool CloseTranslateInfoBar() {
101 InfoBarDelegate* infobar = GetTranslateInfoBar(); 102 InfoBarDelegate* infobar = GetTranslateInfoBar();
102 if (!infobar) 103 if (!infobar)
103 return false; 104 return false;
104 infobar->InfoBarDismissed(); // Simulates closing the infobar. 105 infobar->InfoBarDismissed(); // Simulates closing the infobar.
105 contents_wrapper()->RemoveInfoBar(infobar); 106 contents_wrapper()->infobar_tab_helper()->RemoveInfoBar(infobar);
106 return true; 107 return true;
107 } 108 }
108 109
109 // Checks whether |infobar| has been removed and clears the removed infobar 110 // Checks whether |infobar| has been removed and clears the removed infobar
110 // list. 111 // list.
111 bool CheckInfoBarRemovedAndReset(InfoBarDelegate* delegate) { 112 bool CheckInfoBarRemovedAndReset(InfoBarDelegate* delegate) {
112 bool found = removed_infobars_.count(delegate) != 0; 113 bool found = removed_infobars_.count(delegate) != 0;
113 removed_infobars_.clear(); 114 removed_infobars_.clear();
114 return found; 115 return found;
115 } 116 }
(...skipping 12 matching lines...) Expand all
128 TranslateManager::GetInstance()->set_translate_script_expiration_delay(0); 129 TranslateManager::GetInstance()->set_translate_script_expiration_delay(0);
129 } 130 }
130 131
131 // If there is 1 infobar and it is a translate infobar, deny translation and 132 // If there is 1 infobar and it is a translate infobar, deny translation and
132 // returns true. Returns false otherwise. 133 // returns true. Returns false otherwise.
133 bool DenyTranslation() { 134 bool DenyTranslation() {
134 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); 135 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
135 if (!infobar) 136 if (!infobar)
136 return false; 137 return false;
137 infobar->TranslationDeclined(); 138 infobar->TranslationDeclined();
138 contents_wrapper()->RemoveInfoBar(infobar); 139 contents_wrapper()->infobar_tab_helper()->RemoveInfoBar(infobar);
139 return true; 140 return true;
140 } 141 }
141 142
142 virtual void Observe(int type, 143 virtual void Observe(int type,
143 const NotificationSource& source, 144 const NotificationSource& source,
144 const NotificationDetails& details) { 145 const NotificationDetails& details) {
145 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type); 146 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type);
146 removed_infobars_.insert(Details<InfoBarRemovedDetails>(details)->first); 147 removed_infobars_.insert(Details<InfoBarRemovedDetails>(details)->first);
147 } 148 }
148 149
(...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after
625 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); 626 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
626 } 627 }
627 628
628 // Tests that multiple OnPageContents do not cause multiple infobars. 629 // Tests that multiple OnPageContents do not cause multiple infobars.
629 TEST_F(TranslateManagerTest, MultipleOnPageContents) { 630 TEST_F(TranslateManagerTest, MultipleOnPageContents) {
630 // Simulate navigating to a page and getting its language. 631 // Simulate navigating to a page and getting its language.
631 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 632 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
632 633
633 // Simulate clicking 'Nope' (don't translate). 634 // Simulate clicking 'Nope' (don't translate).
634 EXPECT_TRUE(DenyTranslation()); 635 EXPECT_TRUE(DenyTranslation());
635 EXPECT_EQ(0U, contents_wrapper()->infobar_count()); 636 EXPECT_EQ(0U, contents_wrapper()->infobar_tab_helper()->infobar_count());
Peter Kasting 2011/08/31 18:47:32 Nit: Factor this infobar_tab_helper() out to a tem
636 637
637 // Send a new PageContents, we should not show an infobar. 638 // Send a new PageContents, we should not show an infobar.
638 SimulateOnTranslateLanguageDetermined("fr", true); 639 SimulateOnTranslateLanguageDetermined("fr", true);
639 EXPECT_EQ(0U, contents_wrapper()->infobar_count()); 640 EXPECT_EQ(0U, contents_wrapper()->infobar_tab_helper()->infobar_count());
640 641
641 // Do the same steps but simulate closing the infobar this time. 642 // Do the same steps but simulate closing the infobar this time.
642 SimulateNavigation(GURL("http://www.youtube.fr"), "fr", true); 643 SimulateNavigation(GURL("http://www.youtube.fr"), "fr", true);
643 EXPECT_TRUE(CloseTranslateInfoBar()); 644 EXPECT_TRUE(CloseTranslateInfoBar());
644 EXPECT_EQ(0U, contents_wrapper()->infobar_count()); 645 EXPECT_EQ(0U, contents_wrapper()->infobar_tab_helper()->infobar_count());
645 SimulateOnTranslateLanguageDetermined("fr", true); 646 SimulateOnTranslateLanguageDetermined("fr", true);
646 EXPECT_EQ(0U, contents_wrapper()->infobar_count()); 647 EXPECT_EQ(0U, contents_wrapper()->infobar_tab_helper()->infobar_count());
647 } 648 }
648 649
649 // Test that reloading the page brings back the infobar. 650 // Test that reloading the page brings back the infobar.
650 TEST_F(TranslateManagerTest, Reload) { 651 TEST_F(TranslateManagerTest, Reload) {
651 // Simulate navigating to a page and getting its language. 652 // Simulate navigating to a page and getting its language.
652 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 653 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
653 654
654 // Close the infobar. 655 // Close the infobar.
655 EXPECT_TRUE(CloseTranslateInfoBar()); 656 EXPECT_TRUE(CloseTranslateInfoBar());
656 657
(...skipping 671 matching lines...) Expand 10 before | Expand all | Expand 10 after
1328 NULL); 1329 NULL);
1329 // Now simulate the URL fetch. 1330 // Now simulate the URL fetch.
1330 SimulateTranslateScriptURLFetch(true); 1331 SimulateTranslateScriptURLFetch(true);
1331 // Now the message should have been sent. 1332 // Now the message should have been sent.
1332 int page_id = 0; 1333 int page_id = 0;
1333 std::string original_lang, target_lang; 1334 std::string original_lang, target_lang;
1334 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); 1335 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1335 EXPECT_EQ("es", original_lang); 1336 EXPECT_EQ("es", original_lang);
1336 EXPECT_EQ("en", target_lang); 1337 EXPECT_EQ("en", target_lang);
1337 } 1338 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698