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

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

Issue 10914041: Revert 154464 - Revert 154453 - Remove the translate pyauto test to chrome tests and all the suppor… (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"
21 #include "chrome/browser/tab_contents/render_view_context_menu.h" 22 #include "chrome/browser/tab_contents/render_view_context_menu.h"
22 #include "chrome/browser/translate/translate_infobar_delegate.h" 23 #include "chrome/browser/translate/translate_infobar_delegate.h"
23 #include "chrome/browser/translate/translate_manager.h" 24 #include "chrome/browser/translate/translate_manager.h"
24 #include "chrome/browser/translate/translate_prefs.h" 25 #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"
25 #include "chrome/browser/ui/tab_contents/tab_contents.h" 29 #include "chrome/browser/ui/tab_contents/tab_contents.h"
26 #include "chrome/browser/ui/tab_contents/test_tab_contents.h" 30 #include "chrome/browser/ui/tab_contents/test_tab_contents.h"
27 #include "chrome/common/chrome_notification_types.h" 31 #include "chrome/common/chrome_notification_types.h"
28 #include "chrome/common/pref_names.h" 32 #include "chrome/common/pref_names.h"
29 #include "chrome/common/render_messages.h" 33 #include "chrome/common/render_messages.h"
34 #include "chrome/common/url_constants.h"
35 #include "chrome/test/base/in_process_browser_test.h"
30 #include "chrome/test/base/testing_browser_process.h" 36 #include "chrome/test/base/testing_browser_process.h"
31 #include "chrome/test/base/testing_profile.h" 37 #include "chrome/test/base/testing_profile.h"
38 #include "chrome/test/base/ui_test_utils.h"
32 #include "content/public/browser/navigation_details.h" 39 #include "content/public/browser/navigation_details.h"
33 #include "content/public/browser/navigation_entry.h" 40 #include "content/public/browser/navigation_entry.h"
34 #include "content/public/browser/notification_details.h" 41 #include "content/public/browser/notification_details.h"
35 #include "content/public/browser/notification_registrar.h" 42 #include "content/public/browser/notification_registrar.h"
36 #include "content/public/browser/web_contents.h" 43 #include "content/public/browser/web_contents.h"
37 #include "content/public/test/mock_notification_observer.h" 44 #include "content/public/test/mock_notification_observer.h"
38 #include "content/public/test/mock_render_process_host.h" 45 #include "content/public/test/mock_render_process_host.h"
39 #include "content/public/test/render_view_test.h" 46 #include "content/public/test/render_view_test.h"
40 #include "content/public/test/test_browser_thread.h" 47 #include "content/public/test/test_browser_thread.h"
41 #include "content/public/test/test_renderer_host.h" 48 #include "content/public/test/test_renderer_host.h"
42 #include "grit/generated_resources.h" 49 #include "grit/generated_resources.h"
43 #include "ipc/ipc_test_sink.h" 50 #include "ipc/ipc_test_sink.h"
44 #include "net/url_request/test_url_fetcher_factory.h" 51 #include "net/url_request/test_url_fetcher_factory.h"
45 #include "testing/gmock/include/gmock/gmock.h" 52 #include "testing/gmock/include/gmock/gmock.h"
46 #include "third_party/cld/languages/public/languages.h" 53 #include "third_party/cld/languages/public/languages.h"
47 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h" 54 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h"
48 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" 55 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
49 56
50 using content::BrowserThread; 57 using content::BrowserThread;
51 using content::NavigationController; 58 using content::NavigationController;
52 using content::RenderViewHostTester; 59 using content::RenderViewHostTester;
53 using content::WebContents; 60 using content::WebContents;
54 using testing::_; 61 using testing::_;
55 using testing::Pointee; 62 using testing::Pointee;
56 using testing::Property; 63 using testing::Property;
57 using WebKit::WebContextMenuData; 64 using WebKit::WebContextMenuData;
58 65
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
59 class TranslateManagerTest : public TabContentsTestHarness, 95 class TranslateManagerTest : public TabContentsTestHarness,
60 public content::NotificationObserver { 96 public content::NotificationObserver {
61 public: 97 public:
62 TranslateManagerTest() 98 TranslateManagerTest()
63 : ui_thread_(BrowserThread::UI, &message_loop_) { 99 : ui_thread_(BrowserThread::UI, &message_loop_) {
64 } 100 }
65 101
66 // Simulates navigating to a page and getting the page contents and language 102 // Simulates navigating to a page and getting the page contents and language
67 // for that navigation. 103 // for that navigation.
68 void SimulateNavigation(const GURL& url, 104 void SimulateNavigation(const GURL& url,
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 // returns true. Returns false otherwise. 185 // returns true. Returns false otherwise.
150 bool DenyTranslation() { 186 bool DenyTranslation() {
151 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); 187 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
152 if (!infobar) 188 if (!infobar)
153 return false; 189 return false;
154 infobar->TranslationDeclined(); 190 infobar->TranslationDeclined();
155 infobar_tab_helper()->RemoveInfoBar(infobar); 191 infobar_tab_helper()->RemoveInfoBar(infobar);
156 return true; 192 return true;
157 } 193 }
158 194
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
159 virtual void Observe(int type, 210 virtual void Observe(int type,
160 const content::NotificationSource& source, 211 const content::NotificationSource& source,
161 const content::NotificationDetails& details) { 212 const content::NotificationDetails& details) {
162 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type); 213 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type);
163 removed_infobars_.insert( 214 removed_infobars_.insert(
164 content::Details<InfoBarRemovedDetails>(details)->first); 215 content::Details<InfoBarRemovedDetails>(details)->first);
165 } 216 }
166 217
167 protected: 218 protected:
168 virtual void SetUp() { 219 virtual void SetUp() {
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 content::RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox 308 content::RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox
258 webkit_platform_support_; 309 webkit_platform_support_;
259 310
260 // The infobars that have been removed. 311 // The infobars that have been removed.
261 // WARNING: the pointers point to deleted objects, use only for comparison. 312 // WARNING: the pointers point to deleted objects, use only for comparison.
262 std::set<InfoBarDelegate*> removed_infobars_; 313 std::set<InfoBarDelegate*> removed_infobars_;
263 314
264 DISALLOW_COPY_AND_ASSIGN(TranslateManagerTest); 315 DISALLOW_COPY_AND_ASSIGN(TranslateManagerTest);
265 }; 316 };
266 317
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
296 namespace { 318 namespace {
297 319
298 class TestRenderViewContextMenu : public RenderViewContextMenu { 320 class TestRenderViewContextMenu : public RenderViewContextMenu {
299 public: 321 public:
300 static TestRenderViewContextMenu* CreateContextMenu( 322 static TestRenderViewContextMenu* CreateContextMenu(
301 WebContents* web_contents) { 323 WebContents* web_contents) {
302 content::ContextMenuParams params; 324 content::ContextMenuParams params;
303 params.media_type = WebKit::WebContextMenuData::MediaTypeNone; 325 params.media_type = WebKit::WebContextMenuData::MediaTypeNone;
304 params.x = 0; 326 params.x = 0;
305 params.y = 0; 327 params.y = 0;
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 EXPECT_EQ(new_original_lang, original_lang); 425 EXPECT_EQ(new_original_lang, original_lang);
404 EXPECT_EQ(new_target_lang, target_lang); 426 EXPECT_EQ(new_target_lang, target_lang);
405 // Simulate the render notifying the translation has been done. 427 // Simulate the render notifying the translation has been done.
406 RenderViewHostTester::TestOnMessageReceived( 428 RenderViewHostTester::TestOnMessageReceived(
407 rvh(), 429 rvh(),
408 ChromeViewHostMsg_PageTranslated( 430 ChromeViewHostMsg_PageTranslated(
409 0, 0, new_original_lang, new_target_lang, TranslateErrors::NONE)); 431 0, 0, new_original_lang, new_target_lang, TranslateErrors::NONE));
410 // infobar is now invalid. 432 // infobar is now invalid.
411 new_infobar = GetTranslateInfoBar(); 433 new_infobar = GetTranslateInfoBar();
412 ASSERT_TRUE(new_infobar != NULL); 434 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());
413 } 441 }
414 442
415 TEST_F(TranslateManagerTest, TranslateScriptNotAvailable) { 443 TEST_F(TranslateManagerTest, TranslateScriptNotAvailable) {
416 // Simulate navigating to a page. 444 // Simulate navigating to a page.
417 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 445 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
418 446
419 // We should have an infobar. 447 // We should have an infobar.
420 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); 448 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
421 ASSERT_TRUE(infobar != NULL); 449 ASSERT_TRUE(infobar != NULL);
422 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar->type()); 450 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar->type());
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after
747 775
748 // Test that reloading the page brings back the infobar. 776 // Test that reloading the page brings back the infobar.
749 TEST_F(TranslateManagerTest, Reload) { 777 TEST_F(TranslateManagerTest, Reload) {
750 // Simulate navigating to a page and getting its language. 778 // Simulate navigating to a page and getting its language.
751 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 779 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
752 780
753 // Close the infobar. 781 // Close the infobar.
754 EXPECT_TRUE(CloseTranslateInfoBar()); 782 EXPECT_TRUE(CloseTranslateInfoBar());
755 783
756 // Reload should bring back the infobar. 784 // Reload should bring back the infobar.
757 NavEntryCommittedObserver nav_observer(contents()); 785 ReloadAndWait();
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();
769 EXPECT_TRUE(GetTranslateInfoBar() != NULL); 786 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
770 } 787 }
771 788
772 // Test that reloading the page by way of typing again the URL in the 789 // Test that reloading the page by way of typing again the URL in the
773 // location bar brings back the infobar. 790 // location bar brings back the infobar.
774 TEST_F(TranslateManagerTest, ReloadFromLocationBar) { 791 TEST_F(TranslateManagerTest, ReloadFromLocationBar) {
775 GURL url("http://www.google.fr"); 792 GURL url("http://www.google.fr");
776 793
777 // Simulate navigating to a page and getting its language. 794 // Simulate navigating to a page and getting its language.
778 SimulateNavigation(url, "fr", true); 795 SimulateNavigation(url, "fr", true);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
837 // Simulate the user navigating in a sub-frame. 854 // Simulate the user navigating in a sub-frame.
838 rvh_tester()->SendNavigateWithTransition( 855 rvh_tester()->SendNavigateWithTransition(
839 2, GURL("http://pub.com"), content::PAGE_TRANSITION_MANUAL_SUBFRAME); 856 2, GURL("http://pub.com"), content::PAGE_TRANSITION_MANUAL_SUBFRAME);
840 EXPECT_TRUE(GetTranslateInfoBar() == NULL); 857 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
841 858
842 // Navigate out of page, a new infobar should show. 859 // Navigate out of page, a new infobar should show.
843 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true); 860 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true);
844 EXPECT_TRUE(GetTranslateInfoBar() != NULL); 861 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
845 } 862 }
846 863
847
848
849 // Tests that denying translation is sticky when navigating in page. 864 // Tests that denying translation is sticky when navigating in page.
850 TEST_F(TranslateManagerTest, DenyTranslateInPageNavigation) { 865 TEST_F(TranslateManagerTest, DenyTranslateInPageNavigation) {
851 // Simulate navigating to a page and getting its language. 866 // Simulate navigating to a page and getting its language.
852 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 867 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
853 868
854 // Simulate clicking 'Nope' (don't translate). 869 // Simulate clicking 'Nope' (don't translate).
855 EXPECT_TRUE(DenyTranslation()); 870 EXPECT_TRUE(DenyTranslation());
856 871
857 // Navigate in page, no infobar should be shown. 872 // Navigate in page, no infobar should be shown.
858 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true); 873 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true);
(...skipping 600 matching lines...) Expand 10 before | Expand all | Expand 10 after
1459 NULL); 1474 NULL);
1460 // Now simulate the URL fetch. 1475 // Now simulate the URL fetch.
1461 SimulateTranslateScriptURLFetch(true); 1476 SimulateTranslateScriptURLFetch(true);
1462 // Now the message should have been sent. 1477 // Now the message should have been sent.
1463 int page_id = 0; 1478 int page_id = 0;
1464 std::string original_lang, target_lang; 1479 std::string original_lang, target_lang;
1465 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); 1480 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1466 EXPECT_EQ("es", original_lang); 1481 EXPECT_EQ("es", original_lang);
1467 EXPECT_EQ("en", target_lang); 1482 EXPECT_EQ("en", target_lang);
1468 } 1483 }
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