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

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

Issue 10917042: Revert 154453 - Remove the translate pyauto test to chrome tests and all the supporting automation … (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 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) 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 5
6 #include <algorithm> 6 #include <algorithm>
7 #include <set> 7 #include <set>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/json/json_writer.h" 10 #include "base/json/json_writer.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/stringprintf.h" 12 #include "base/stringprintf.h"
13 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
14 #include "base/values.h" 14 #include "base/values.h"
15 #include "chrome/app/chrome_command_ids.h" 15 #include "chrome/app/chrome_command_ids.h"
16 #include "chrome/browser/api/prefs/pref_change_registrar.h" 16 #include "chrome/browser/api/prefs/pref_change_registrar.h"
17 #include "chrome/browser/extensions/test_extension_system.h" 17 #include "chrome/browser/extensions/test_extension_system.h"
18 #include "chrome/browser/infobars/infobar.h" 18 #include "chrome/browser/infobars/infobar.h"
19 #include "chrome/browser/infobars/infobar_tab_helper.h" 19 #include "chrome/browser/infobars/infobar_tab_helper.h"
20 #include "chrome/browser/prefs/pref_service.h" 20 #include "chrome/browser/prefs/pref_service.h"
21 #include "chrome/browser/prefs/session_startup_pref.h"
22 #include "chrome/browser/tab_contents/render_view_context_menu.h" 21 #include "chrome/browser/tab_contents/render_view_context_menu.h"
23 #include "chrome/browser/translate/translate_infobar_delegate.h" 22 #include "chrome/browser/translate/translate_infobar_delegate.h"
24 #include "chrome/browser/translate/translate_manager.h" 23 #include "chrome/browser/translate/translate_manager.h"
25 #include "chrome/browser/translate/translate_prefs.h" 24 #include "chrome/browser/translate/translate_prefs.h"
26 #include "chrome/browser/translate/translate_tab_helper.h"
27 #include "chrome/browser/ui/browser.h"
28 #include "chrome/browser/ui/browser_tabstrip.h"
29 #include "chrome/browser/ui/tab_contents/tab_contents.h" 25 #include "chrome/browser/ui/tab_contents/tab_contents.h"
30 #include "chrome/browser/ui/tab_contents/test_tab_contents.h" 26 #include "chrome/browser/ui/tab_contents/test_tab_contents.h"
31 #include "chrome/common/chrome_notification_types.h" 27 #include "chrome/common/chrome_notification_types.h"
32 #include "chrome/common/pref_names.h" 28 #include "chrome/common/pref_names.h"
33 #include "chrome/common/render_messages.h" 29 #include "chrome/common/render_messages.h"
34 #include "chrome/common/url_constants.h"
35 #include "chrome/test/base/in_process_browser_test.h"
36 #include "chrome/test/base/testing_browser_process.h" 30 #include "chrome/test/base/testing_browser_process.h"
37 #include "chrome/test/base/testing_profile.h" 31 #include "chrome/test/base/testing_profile.h"
38 #include "chrome/test/base/ui_test_utils.h"
39 #include "content/public/browser/navigation_details.h" 32 #include "content/public/browser/navigation_details.h"
40 #include "content/public/browser/navigation_entry.h" 33 #include "content/public/browser/navigation_entry.h"
41 #include "content/public/browser/notification_details.h" 34 #include "content/public/browser/notification_details.h"
42 #include "content/public/browser/notification_registrar.h" 35 #include "content/public/browser/notification_registrar.h"
43 #include "content/public/browser/web_contents.h" 36 #include "content/public/browser/web_contents.h"
44 #include "content/public/test/mock_notification_observer.h" 37 #include "content/public/test/mock_notification_observer.h"
45 #include "content/public/test/mock_render_process_host.h" 38 #include "content/public/test/mock_render_process_host.h"
46 #include "content/public/test/render_view_test.h" 39 #include "content/public/test/render_view_test.h"
47 #include "content/public/test/test_browser_thread.h" 40 #include "content/public/test/test_browser_thread.h"
48 #include "content/public/test/test_renderer_host.h" 41 #include "content/public/test/test_renderer_host.h"
49 #include "grit/generated_resources.h" 42 #include "grit/generated_resources.h"
50 #include "ipc/ipc_test_sink.h" 43 #include "ipc/ipc_test_sink.h"
51 #include "net/url_request/test_url_fetcher_factory.h" 44 #include "net/url_request/test_url_fetcher_factory.h"
52 #include "testing/gmock/include/gmock/gmock.h" 45 #include "testing/gmock/include/gmock/gmock.h"
53 #include "third_party/cld/languages/public/languages.h" 46 #include "third_party/cld/languages/public/languages.h"
54 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h" 47 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h"
55 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" 48 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
56 49
57 using content::BrowserThread; 50 using content::BrowserThread;
58 using content::NavigationController; 51 using content::NavigationController;
59 using content::RenderViewHostTester; 52 using content::RenderViewHostTester;
60 using content::WebContents; 53 using content::WebContents;
61 using testing::_; 54 using testing::_;
62 using testing::Pointee; 55 using testing::Pointee;
63 using testing::Property; 56 using testing::Property;
64 using WebKit::WebContextMenuData; 57 using WebKit::WebContextMenuData;
65 58
66 // An observer that keeps track of whether a navigation entry was committed.
67 class NavEntryCommittedObserver : public content::NotificationObserver {
68 public:
69 explicit NavEntryCommittedObserver(WebContents* web_contents) {
70 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
71 content::Source<NavigationController>(
72 &web_contents->GetController()));
73 }
74
75 virtual void Observe(int type,
76 const content::NotificationSource& source,
77 const content::NotificationDetails& details) {
78 DCHECK(type == content::NOTIFICATION_NAV_ENTRY_COMMITTED);
79 details_ =
80 *(content::Details<content::LoadCommittedDetails>(details).ptr());
81 }
82
83 const content::LoadCommittedDetails&
84 get_load_commited_details() const {
85 return details_;
86 }
87
88 private:
89 content::LoadCommittedDetails details_;
90 content::NotificationRegistrar registrar_;
91
92 DISALLOW_COPY_AND_ASSIGN(NavEntryCommittedObserver);
93 };
94
95 class TranslateManagerTest : public TabContentsTestHarness, 59 class TranslateManagerTest : public TabContentsTestHarness,
96 public content::NotificationObserver { 60 public content::NotificationObserver {
97 public: 61 public:
98 TranslateManagerTest() 62 TranslateManagerTest()
99 : ui_thread_(BrowserThread::UI, &message_loop_) { 63 : ui_thread_(BrowserThread::UI, &message_loop_) {
100 } 64 }
101 65
102 // Simulates navigating to a page and getting the page contents and language 66 // Simulates navigating to a page and getting the page contents and language
103 // for that navigation. 67 // for that navigation.
104 void SimulateNavigation(const GURL& url, 68 void SimulateNavigation(const GURL& url,
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 // returns true. Returns false otherwise. 149 // returns true. Returns false otherwise.
186 bool DenyTranslation() { 150 bool DenyTranslation() {
187 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); 151 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
188 if (!infobar) 152 if (!infobar)
189 return false; 153 return false;
190 infobar->TranslationDeclined(); 154 infobar->TranslationDeclined();
191 infobar_tab_helper()->RemoveInfoBar(infobar); 155 infobar_tab_helper()->RemoveInfoBar(infobar);
192 return true; 156 return true;
193 } 157 }
194 158
195 void ReloadAndWait() {
196 NavEntryCommittedObserver nav_observer(contents());
197 Reload();
198
199 // Ensures it is really handled a reload.
200 const content::LoadCommittedDetails& nav_details =
201 nav_observer.get_load_commited_details();
202 EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation.
203 EXPECT_EQ(content::NAVIGATION_TYPE_EXISTING_PAGE, nav_details.type);
204
205 // The TranslateManager class processes the navigation entry committed
206 // notification in a posted task; process that task.
207 MessageLoop::current()->RunAllPending();
208 }
209
210 virtual void Observe(int type, 159 virtual void Observe(int type,
211 const content::NotificationSource& source, 160 const content::NotificationSource& source,
212 const content::NotificationDetails& details) { 161 const content::NotificationDetails& details) {
213 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type); 162 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type);
214 removed_infobars_.insert( 163 removed_infobars_.insert(
215 content::Details<InfoBarRemovedDetails>(details)->first); 164 content::Details<InfoBarRemovedDetails>(details)->first);
216 } 165 }
217 166
218 protected: 167 protected:
219 virtual void SetUp() { 168 virtual void SetUp() {
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 content::RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox 257 content::RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox
309 webkit_platform_support_; 258 webkit_platform_support_;
310 259
311 // The infobars that have been removed. 260 // The infobars that have been removed.
312 // WARNING: the pointers point to deleted objects, use only for comparison. 261 // WARNING: the pointers point to deleted objects, use only for comparison.
313 std::set<InfoBarDelegate*> removed_infobars_; 262 std::set<InfoBarDelegate*> removed_infobars_;
314 263
315 DISALLOW_COPY_AND_ASSIGN(TranslateManagerTest); 264 DISALLOW_COPY_AND_ASSIGN(TranslateManagerTest);
316 }; 265 };
317 266
267 // An observer that keeps track of whether a navigation entry was committed.
268 class NavEntryCommittedObserver : public content::NotificationObserver {
269 public:
270 explicit NavEntryCommittedObserver(WebContents* web_contents) {
271 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
272 content::Source<NavigationController>(
273 &web_contents->GetController()));
274 }
275
276 virtual void Observe(int type,
277 const content::NotificationSource& source,
278 const content::NotificationDetails& details) {
279 DCHECK(type == content::NOTIFICATION_NAV_ENTRY_COMMITTED);
280 details_ =
281 *(content::Details<content::LoadCommittedDetails>(details).ptr());
282 }
283
284 const content::LoadCommittedDetails&
285 get_load_commited_details() const {
286 return details_;
287 }
288
289 private:
290 content::LoadCommittedDetails details_;
291 content::NotificationRegistrar registrar_;
292
293 DISALLOW_COPY_AND_ASSIGN(NavEntryCommittedObserver);
294 };
295
318 namespace { 296 namespace {
319 297
320 class TestRenderViewContextMenu : public RenderViewContextMenu { 298 class TestRenderViewContextMenu : public RenderViewContextMenu {
321 public: 299 public:
322 static TestRenderViewContextMenu* CreateContextMenu( 300 static TestRenderViewContextMenu* CreateContextMenu(
323 WebContents* web_contents) { 301 WebContents* web_contents) {
324 content::ContextMenuParams params; 302 content::ContextMenuParams params;
325 params.media_type = WebKit::WebContextMenuData::MediaTypeNone; 303 params.media_type = WebKit::WebContextMenuData::MediaTypeNone;
326 params.x = 0; 304 params.x = 0;
327 params.y = 0; 305 params.y = 0;
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 EXPECT_EQ(new_original_lang, original_lang); 403 EXPECT_EQ(new_original_lang, original_lang);
426 EXPECT_EQ(new_target_lang, target_lang); 404 EXPECT_EQ(new_target_lang, target_lang);
427 // Simulate the render notifying the translation has been done. 405 // Simulate the render notifying the translation has been done.
428 RenderViewHostTester::TestOnMessageReceived( 406 RenderViewHostTester::TestOnMessageReceived(
429 rvh(), 407 rvh(),
430 ChromeViewHostMsg_PageTranslated( 408 ChromeViewHostMsg_PageTranslated(
431 0, 0, new_original_lang, new_target_lang, TranslateErrors::NONE)); 409 0, 0, new_original_lang, new_target_lang, TranslateErrors::NONE));
432 // infobar is now invalid. 410 // infobar is now invalid.
433 new_infobar = GetTranslateInfoBar(); 411 new_infobar = GetTranslateInfoBar();
434 ASSERT_TRUE(new_infobar != NULL); 412 ASSERT_TRUE(new_infobar != NULL);
435
436 // Verify reload keeps the same settings.
437 ReloadAndWait();
438 new_infobar = GetTranslateInfoBar();
439 ASSERT_TRUE(new_infobar != NULL);
440 ASSERT_EQ(new_target_lang, infobar->GetTargetLanguageCode());
441 } 413 }
442 414
443 TEST_F(TranslateManagerTest, TranslateScriptNotAvailable) { 415 TEST_F(TranslateManagerTest, TranslateScriptNotAvailable) {
444 // Simulate navigating to a page. 416 // Simulate navigating to a page.
445 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 417 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
446 418
447 // We should have an infobar. 419 // We should have an infobar.
448 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); 420 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
449 ASSERT_TRUE(infobar != NULL); 421 ASSERT_TRUE(infobar != NULL);
450 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar->type()); 422 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar->type());
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after
775 747
776 // Test that reloading the page brings back the infobar. 748 // Test that reloading the page brings back the infobar.
777 TEST_F(TranslateManagerTest, Reload) { 749 TEST_F(TranslateManagerTest, Reload) {
778 // Simulate navigating to a page and getting its language. 750 // Simulate navigating to a page and getting its language.
779 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 751 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
780 752
781 // Close the infobar. 753 // Close the infobar.
782 EXPECT_TRUE(CloseTranslateInfoBar()); 754 EXPECT_TRUE(CloseTranslateInfoBar());
783 755
784 // Reload should bring back the infobar. 756 // Reload should bring back the infobar.
785 ReloadAndWait(); 757 NavEntryCommittedObserver nav_observer(contents());
758 Reload();
759
760 // Ensures it is really handled a reload.
761 const content::LoadCommittedDetails& nav_details =
762 nav_observer.get_load_commited_details();
763 EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation.
764 EXPECT_EQ(content::NAVIGATION_TYPE_EXISTING_PAGE, nav_details.type);
765
766 // The TranslateManager class processes the navigation entry committed
767 // notification in a posted task; process that task.
768 MessageLoop::current()->RunAllPending();
786 EXPECT_TRUE(GetTranslateInfoBar() != NULL); 769 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
787 } 770 }
788 771
789 // Test that reloading the page by way of typing again the URL in the 772 // Test that reloading the page by way of typing again the URL in the
790 // location bar brings back the infobar. 773 // location bar brings back the infobar.
791 TEST_F(TranslateManagerTest, ReloadFromLocationBar) { 774 TEST_F(TranslateManagerTest, ReloadFromLocationBar) {
792 GURL url("http://www.google.fr"); 775 GURL url("http://www.google.fr");
793 776
794 // Simulate navigating to a page and getting its language. 777 // Simulate navigating to a page and getting its language.
795 SimulateNavigation(url, "fr", true); 778 SimulateNavigation(url, "fr", true);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
854 // Simulate the user navigating in a sub-frame. 837 // Simulate the user navigating in a sub-frame.
855 rvh_tester()->SendNavigateWithTransition( 838 rvh_tester()->SendNavigateWithTransition(
856 2, GURL("http://pub.com"), content::PAGE_TRANSITION_MANUAL_SUBFRAME); 839 2, GURL("http://pub.com"), content::PAGE_TRANSITION_MANUAL_SUBFRAME);
857 EXPECT_TRUE(GetTranslateInfoBar() == NULL); 840 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
858 841
859 // Navigate out of page, a new infobar should show. 842 // Navigate out of page, a new infobar should show.
860 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true); 843 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true);
861 EXPECT_TRUE(GetTranslateInfoBar() != NULL); 844 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
862 } 845 }
863 846
847
848
864 // Tests that denying translation is sticky when navigating in page. 849 // Tests that denying translation is sticky when navigating in page.
865 TEST_F(TranslateManagerTest, DenyTranslateInPageNavigation) { 850 TEST_F(TranslateManagerTest, DenyTranslateInPageNavigation) {
866 // Simulate navigating to a page and getting its language. 851 // Simulate navigating to a page and getting its language.
867 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 852 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
868 853
869 // Simulate clicking 'Nope' (don't translate). 854 // Simulate clicking 'Nope' (don't translate).
870 EXPECT_TRUE(DenyTranslation()); 855 EXPECT_TRUE(DenyTranslation());
871 856
872 // Navigate in page, no infobar should be shown. 857 // Navigate in page, no infobar should be shown.
873 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true); 858 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true);
(...skipping 600 matching lines...) Expand 10 before | Expand all | Expand 10 after
1474 NULL); 1459 NULL);
1475 // Now simulate the URL fetch. 1460 // Now simulate the URL fetch.
1476 SimulateTranslateScriptURLFetch(true); 1461 SimulateTranslateScriptURLFetch(true);
1477 // Now the message should have been sent. 1462 // Now the message should have been sent.
1478 int page_id = 0; 1463 int page_id = 0;
1479 std::string original_lang, target_lang; 1464 std::string original_lang, target_lang;
1480 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); 1465 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1481 EXPECT_EQ("es", original_lang); 1466 EXPECT_EQ("es", original_lang);
1482 EXPECT_EQ("en", target_lang); 1467 EXPECT_EQ("en", target_lang);
1483 } 1468 }
1484
1485 TEST_F(TranslateManagerTest, DownloadsAndHistoryNotTranslated) {
1486 ASSERT_FALSE(TranslateManager::IsTranslatableURL(
1487 GURL(chrome::kChromeUIDownloadsURL)));
1488 ASSERT_FALSE(TranslateManager::IsTranslatableURL(
1489 GURL(chrome::kChromeUIHistoryURL)));
1490 }
1491
1492 // Test that session restore restores the translate infobar and other translate
1493 // settings.
1494 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, PRE_TranslateSessionRestore) {
1495 SessionStartupPref pref(SessionStartupPref::LAST);
1496 SessionStartupPref::SetStartupPref(browser()->profile(), pref);
1497
1498 WebContents* current_web_contents = chrome::GetActiveWebContents(browser());
1499 TabContents* current_tab_contents = chrome::GetActiveTabContents(browser());
1500 TranslateTabHelper* helper = current_tab_contents->translate_tab_helper();
1501 content::Source<WebContents> source(current_web_contents);
1502
1503 ui_test_utils::WindowedNotificationObserverWithDetails<std::string>
1504 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
1505 source);
1506
1507 GURL french_url = ui_test_utils::GetTestUrl(
1508 FilePath(), FilePath(FILE_PATH_LITERAL("french_page.html")));
1509 ui_test_utils::NavigateToURL(browser(), french_url);
1510 fr_language_detected_signal.Wait();
1511 std::string lang;
1512 EXPECT_TRUE(fr_language_detected_signal.GetDetailsFor(
1513 source.map_key(), &lang));
1514 EXPECT_EQ("fr", lang);
1515 EXPECT_EQ("fr", helper->language_state().original_language());
1516 }
1517
1518 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, TranslateSessionRestore) {
1519 WebContents* current_web_contents = chrome::GetActiveWebContents(browser());
1520 content::Source<WebContents> source(current_web_contents);
1521
1522 ui_test_utils::WindowedNotificationObserverWithDetails<std::string>
1523 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
1524 source);
1525 fr_language_detected_signal.Wait();
1526 }
OLDNEW
« no previous file with comments | « chrome/browser/automation/testing_automation_provider.cc ('k') | chrome/test/functional/PYAUTO_TESTS » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698