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

Side by Side Diff: chrome/browser/ui/browser_browsertest.cc

Issue 11249004: Tests for dispositions of various modified clicks on a normal link. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Tweak comments Created 8 years, 1 month 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
« no previous file with comments | « no previous file | chrome/test/data/click_modifier/href.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <string> 5 #include <string>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/file_path.h" 9 #include "base/file_path.h"
10 #include "base/sys_info.h" 10 #include "base/sys_info.h"
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 #include "content/public/browser/notification_source.h" 53 #include "content/public/browser/notification_source.h"
54 #include "content/public/browser/render_process_host.h" 54 #include "content/public/browser/render_process_host.h"
55 #include "content/public/browser/render_view_host.h" 55 #include "content/public/browser/render_view_host.h"
56 #include "content/public/browser/render_widget_host_view.h" 56 #include "content/public/browser/render_widget_host_view.h"
57 #include "content/public/browser/web_contents.h" 57 #include "content/public/browser/web_contents.h"
58 #include "content/public/browser/web_contents_observer.h" 58 #include "content/public/browser/web_contents_observer.h"
59 #include "content/public/common/page_transition_types.h" 59 #include "content/public/common/page_transition_types.h"
60 #include "content/public/common/renderer_preferences.h" 60 #include "content/public/common/renderer_preferences.h"
61 #include "content/public/common/url_constants.h" 61 #include "content/public/common/url_constants.h"
62 #include "content/public/test/browser_test_utils.h" 62 #include "content/public/test/browser_test_utils.h"
63 #include "content/public/test/test_navigation_observer.h"
63 #include "grit/chromium_strings.h" 64 #include "grit/chromium_strings.h"
64 #include "grit/generated_resources.h" 65 #include "grit/generated_resources.h"
65 #include "net/base/mock_host_resolver.h" 66 #include "net/base/mock_host_resolver.h"
66 #include "net/test/test_server.h" 67 #include "net/test/test_server.h"
67 #include "ui/base/l10n/l10n_util.h" 68 #include "ui/base/l10n/l10n_util.h"
68 69
69 #if defined(OS_MACOSX) 70 #if defined(OS_MACOSX)
70 #include "base/mac/mac_util.h" 71 #include "base/mac/mac_util.h"
71 #include "base/mac/scoped_nsautorelease_pool.h" 72 #include "base/mac/scoped_nsautorelease_pool.h"
72 #include "chrome/browser/ui/cocoa/run_loop_testing.h" 73 #include "chrome/browser/ui/cocoa/run_loop_testing.h"
(...skipping 1633 matching lines...) Expand 10 before | Expand all | Expand 10 after
1706 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("click_modifier"); 1707 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("click_modifier");
1707 static const char kFirstPageTitle[] = "First window"; 1708 static const char kFirstPageTitle[] = "First window";
1708 static const char kSecondPageTitle[] = "New window!"; 1709 static const char kSecondPageTitle[] = "New window!";
1709 1710
1710 class ClickModifierTest : public InProcessBrowserTest { 1711 class ClickModifierTest : public InProcessBrowserTest {
1711 public: 1712 public:
1712 ClickModifierTest() { 1713 ClickModifierTest() {
1713 } 1714 }
1714 1715
1715 // Returns a url that opens a new window or tab when clicked, via javascript. 1716 // Returns a url that opens a new window or tab when clicked, via javascript.
1716 GURL GetTestURL() { 1717 GURL GetWindowOpenURL() {
1717 return ui_test_utils::GetTestUrl( 1718 return ui_test_utils::GetTestUrl(
1718 FilePath(kTestDir), 1719 FilePath(kTestDir),
1719 FilePath(FILE_PATH_LITERAL("window_open.html"))); 1720 FilePath(FILE_PATH_LITERAL("window_open.html")));
1720 } 1721 }
1721 1722
1723 // Returns a url that follows a simple link when clicked, unless affected by
1724 // modifiers.
1725 GURL GetHrefURL() {
1726 return ui_test_utils::GetTestUrl(
1727 FilePath(kTestDir),
1728 FilePath(FILE_PATH_LITERAL("href.html")));
1729 }
1730
1722 string16 getFirstPageTitle() { 1731 string16 getFirstPageTitle() {
1723 return ASCIIToUTF16(kFirstPageTitle); 1732 return ASCIIToUTF16(kFirstPageTitle);
1724 } 1733 }
1725 1734
1726 string16 getSecondPageTitle() { 1735 string16 getSecondPageTitle() {
1727 return ASCIIToUTF16(kSecondPageTitle); 1736 return ASCIIToUTF16(kSecondPageTitle);
1728 } 1737 }
1729 1738
1730 // Loads our test page and simulates a single click using the supplied button 1739 // Loads our test page and simulates a single click using the supplied button
1731 // and modifiers. The test page will call window.open, creating either a new 1740 // and modifiers. The click will cause either a navigation or the creation of
1732 // tab or a new window. In the case of a tab, it will verify whether the 1741 // a new window or foreground or background tab. We verify that the expected
1733 // tab's in the foreground or background. 1742 // disposition occurs.
1734 void RunTest(Browser* browser, 1743 void RunTest(Browser* browser,
1744 const GURL& url,
1735 int modifiers, 1745 int modifiers,
1736 WebKit::WebMouseEvent::Button button, 1746 WebKit::WebMouseEvent::Button button,
1737 bool expect_new_window, 1747 WindowOpenDisposition disposition) {
1738 bool expect_foreground) {
1739 GURL url(GetTestURL());
1740 ui_test_utils::NavigateToURL(browser, url); 1748 ui_test_utils::NavigateToURL(browser, url);
1741 EXPECT_EQ(1u, browser::GetBrowserCount(browser->profile())); 1749 EXPECT_EQ(1u, browser::GetBrowserCount(browser->profile()));
1742 EXPECT_EQ(1, browser->tab_count()); 1750 EXPECT_EQ(1, browser->tab_count());
1743 content::WebContents* web_contents = chrome::GetActiveWebContents(browser); 1751 content::WebContents* web_contents = chrome::GetActiveWebContents(browser);
1744 EXPECT_EQ(url, web_contents->GetURL()); 1752 EXPECT_EQ(url, web_contents->GetURL());
1745 1753
1754 if (disposition == CURRENT_TAB) {
1755 NavigationController* controller =
1756 chrome::GetActiveWebContents(browser) ?
1757 &chrome::GetActiveWebContents(browser)->GetController() : NULL;
1758 content::TestNavigationObserver same_tab_observer(
1759 content::Source<NavigationController>(controller),
1760 NULL,
1761 1);
1762 SimulateMouseClick(web_contents, modifiers, button);
1763 base::RunLoop run_loop;
1764 same_tab_observer.WaitForObservation(
1765 base::Bind(&content::RunThisRunLoop, base::Unretained(&run_loop)),
1766 content::GetQuitTaskForRunLoop(&run_loop));
1767 EXPECT_EQ(1u, browser::GetBrowserCount(browser->profile()));
1768 EXPECT_EQ(1, browser->tab_count());
1769 EXPECT_EQ(getSecondPageTitle(), web_contents->GetTitle());
1770 return;
1771 }
1772
1746 content::WindowedNotificationObserver observer( 1773 content::WindowedNotificationObserver observer(
1747 chrome::NOTIFICATION_TAB_ADDED, 1774 chrome::NOTIFICATION_TAB_ADDED,
1748 content::NotificationService::AllSources()); 1775 content::NotificationService::AllSources());
1749 SimulateMouseClick(web_contents, modifiers, button); 1776 SimulateMouseClick(web_contents, modifiers, button);
1750 observer.Wait(); 1777 observer.Wait();
1751 1778
1752 unsigned expected_browser_count = expect_new_window ? 2 : 1; 1779 if (disposition == NEW_WINDOW) {
1753 EXPECT_EQ(expected_browser_count, 1780 EXPECT_EQ(2u, browser::GetBrowserCount(browser->profile()));
1754 browser::GetBrowserCount(browser->profile())); 1781 return;
1782 }
1755 1783
1756 // If we didn't pop up a new window, we need to make sure the right tab's in 1784 EXPECT_EQ(1u, browser::GetBrowserCount(browser->profile()));
1757 // front. 1785 EXPECT_EQ(2, browser->tab_count());
1758 if (!expect_new_window) { 1786 web_contents = chrome::GetActiveWebContents(browser);
1759 EXPECT_EQ(2, browser->tab_count()); 1787 WaitForLoadStop(web_contents);
1760 web_contents = chrome::GetActiveWebContents(browser); 1788 if (disposition == NEW_FOREGROUND_TAB) {
1761 WaitForLoadStop(web_contents); 1789 EXPECT_EQ(getSecondPageTitle(), web_contents->GetTitle());
1762 if (expect_foreground) { 1790 } else {
1763 EXPECT_EQ(getSecondPageTitle(), web_contents->GetTitle()); 1791 ASSERT_EQ(NEW_BACKGROUND_TAB, disposition);
1764 } else { 1792 EXPECT_EQ(getFirstPageTitle(), web_contents->GetTitle());
1765 EXPECT_EQ(getFirstPageTitle(), web_contents->GetTitle());
1766 }
1767 } 1793 }
1768 } 1794 }
1769 1795
1770 private: 1796 private:
1771 DISALLOW_COPY_AND_ASSIGN(ClickModifierTest); 1797 DISALLOW_COPY_AND_ASSIGN(ClickModifierTest);
1772 }; 1798 };
1773 1799
1774 IN_PROC_BROWSER_TEST_F(ClickModifierTest, BasicClickTest) { 1800 // Tests for clicking on elements with handlers that run window.open.
1801
1802 IN_PROC_BROWSER_TEST_F(ClickModifierTest, WindowOpenBasicClickTest) {
1775 int modifiers = 0; 1803 int modifiers = 0;
1776 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonLeft; 1804 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonLeft;
1777 bool expect_new_window = false; 1805 WindowOpenDisposition disposition = NEW_FOREGROUND_TAB;
1778 bool expect_foreground = true; 1806 RunTest(browser(), GetWindowOpenURL(), modifiers, button, disposition);
1779 RunTest(browser(), modifiers, button, expect_new_window, expect_foreground);
1780 } 1807 }
1781 1808
1782 // TODO(ericu): Alt-click behavior is platform-dependent and not well defined. 1809 // TODO(ericu): Alt-click behavior on window.open is platform-dependent and not
1783 // Should we add tests so we know if it changes? 1810 // well defined. Should we add tests so we know if it changes?
1784 1811
1785 // Shift-clicks open in a new window. 1812 // Shift-clicks open in a new window.
1786 // TODO(ericu): Disabled until https://bugs.webkit.org/show_bug.cgi?id=99202 1813 // TODO(ericu): Disabled until https://bugs.webkit.org/show_bug.cgi?id=99202
1787 // lands, fixing http://crbug.com/31631. 1814 // lands, fixing http://crbug.com/31631.
1788 IN_PROC_BROWSER_TEST_F(ClickModifierTest, DISABLED_ShiftClickTest) { 1815 IN_PROC_BROWSER_TEST_F(ClickModifierTest, DISABLED_WindowOpenShiftClickTest) {
1789 int modifiers = WebKit::WebInputEvent::ShiftKey; 1816 int modifiers = WebKit::WebInputEvent::ShiftKey;
1790 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonLeft; 1817 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonLeft;
1791 bool expect_new_window = true; 1818 WindowOpenDisposition disposition = NEW_WINDOW;
1792 bool expect_foreground = true; 1819 RunTest(browser(), GetWindowOpenURL(), modifiers, button, disposition);
1793 RunTest(browser(), modifiers, button, expect_new_window, expect_foreground);
1794 } 1820 }
1795 1821
1796 // Control-clicks open in a background tab. 1822 // Control-clicks open in a background tab.
1797 // On OSX meta [the command key] takes the place of control. 1823 // On OSX meta [the command key] takes the place of control.
1798 IN_PROC_BROWSER_TEST_F(ClickModifierTest, ControlClickTest) { 1824 IN_PROC_BROWSER_TEST_F(ClickModifierTest, WindowOpenControlClickTest) {
1799 #if defined(OS_MACOSX) 1825 #if defined(OS_MACOSX)
1800 int modifiers = WebKit::WebInputEvent::MetaKey; 1826 int modifiers = WebKit::WebInputEvent::MetaKey;
1801 #else 1827 #else
1802 int modifiers = WebKit::WebInputEvent::ControlKey; 1828 int modifiers = WebKit::WebInputEvent::ControlKey;
1803 #endif 1829 #endif
1804 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonLeft; 1830 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonLeft;
1805 bool expect_new_window = false; 1831 WindowOpenDisposition disposition = NEW_BACKGROUND_TAB;
1806 bool expect_foreground = false; 1832 RunTest(browser(), GetWindowOpenURL(), modifiers, button, disposition);
1807 RunTest(browser(), modifiers, button, expect_new_window, expect_foreground);
1808 } 1833 }
1809 1834
1810 // Control-shift-clicks open in a foreground tab. 1835 // Control-shift-clicks open in a foreground tab.
1811 // On OSX meta [the command key] takes the place of control. 1836 // On OSX meta [the command key] takes the place of control.
1812 IN_PROC_BROWSER_TEST_F(ClickModifierTest, ControlShiftClickTest) { 1837 IN_PROC_BROWSER_TEST_F(ClickModifierTest, WindowOpenControlShiftClickTest) {
1813 #if defined(OS_MACOSX) 1838 #if defined(OS_MACOSX)
1814 int modifiers = WebKit::WebInputEvent::MetaKey; 1839 int modifiers = WebKit::WebInputEvent::MetaKey;
1815 #else 1840 #else
1841 int modifiers = WebKit::WebInputEvent::ControlKey;
1842 #endif
1843 modifiers |= WebKit::WebInputEvent::ShiftKey;
1844 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonLeft;
1845 WindowOpenDisposition disposition = NEW_FOREGROUND_TAB;
1846 RunTest(browser(), GetWindowOpenURL(), modifiers, button, disposition);
1847 }
1848
1849 // Middle-clicks open in a background tab.
1850 IN_PROC_BROWSER_TEST_F(ClickModifierTest, WindowOpenMiddleClickTest) {
1851 int modifiers = 0;
1852 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonMiddle;
1853 WindowOpenDisposition disposition = NEW_BACKGROUND_TAB;
1854 RunTest(browser(), GetWindowOpenURL(), modifiers, button, disposition);
1855 }
1856
1857 // Shift-middle-clicks open in a foreground tab.
1858 IN_PROC_BROWSER_TEST_F(ClickModifierTest, WindowOpenShiftMiddleClickTest) {
1859 int modifiers = WebKit::WebInputEvent::ShiftKey;
1860 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonMiddle;
1861 WindowOpenDisposition disposition = NEW_FOREGROUND_TAB;
1862 RunTest(browser(), GetWindowOpenURL(), modifiers, button, disposition);
1863 }
1864
1865 // Tests for clicking on normal links.
1866
1867 IN_PROC_BROWSER_TEST_F(ClickModifierTest, HrefBasicClickTest) {
1868 int modifiers = 0;
1869 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonLeft;
1870 WindowOpenDisposition disposition = CURRENT_TAB;
1871 RunTest(browser(), GetHrefURL(), modifiers, button, disposition);
1872 }
1873
1874 // TODO(ericu): Alt-click behavior on links is platform-dependent and not well
1875 // defined. Should we add tests so we know if it changes?
1876
1877 // Shift-clicks open in a new window.
1878 IN_PROC_BROWSER_TEST_F(ClickModifierTest, HrefShiftClickTest) {
1879 int modifiers = WebKit::WebInputEvent::ShiftKey;
1880 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonLeft;
1881 WindowOpenDisposition disposition = NEW_WINDOW;
1882 RunTest(browser(), GetHrefURL(), modifiers, button, disposition);
1883 }
1884
1885 // Control-clicks open in a background tab.
1886 // On OSX meta [the command key] takes the place of control.
1887 IN_PROC_BROWSER_TEST_F(ClickModifierTest, HrefControlClickTest) {
1888 #if defined(OS_MACOSX)
1889 int modifiers = WebKit::WebInputEvent::MetaKey;
1890 #else
1891 int modifiers = WebKit::WebInputEvent::ControlKey;
1892 #endif
1893 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonLeft;
1894 WindowOpenDisposition disposition = NEW_BACKGROUND_TAB;
1895 RunTest(browser(), GetHrefURL(), modifiers, button, disposition);
1896 }
1897
1898 // Control-shift-clicks open in a foreground tab.
1899 // On OSX meta [the command key] takes the place of control.
1900 IN_PROC_BROWSER_TEST_F(ClickModifierTest, HrefControlShiftClickTest) {
1901 #if defined(OS_MACOSX)
1902 int modifiers = WebKit::WebInputEvent::MetaKey;
1903 #else
1816 int modifiers = WebKit::WebInputEvent::ControlKey; 1904 int modifiers = WebKit::WebInputEvent::ControlKey;
1817 #endif 1905 #endif
1818 modifiers |= WebKit::WebInputEvent::ShiftKey; 1906 modifiers |= WebKit::WebInputEvent::ShiftKey;
1819 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonLeft; 1907 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonLeft;
1820 bool expect_new_window = false; 1908 WindowOpenDisposition disposition = NEW_FOREGROUND_TAB;
1821 bool expect_foreground = true; 1909 RunTest(browser(), GetHrefURL(), modifiers, button, disposition);
1822 RunTest(browser(), modifiers, button, expect_new_window, expect_foreground);
1823 } 1910 }
1824 1911
1825 // Middle-clicks open in a background tab. 1912 // Middle-clicks open in a background tab.
1826 IN_PROC_BROWSER_TEST_F(ClickModifierTest, MiddleClickTest) { 1913 IN_PROC_BROWSER_TEST_F(ClickModifierTest, HrefMiddleClickTest) {
1827 int modifiers = 0; 1914 int modifiers = 0;
1828 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonMiddle; 1915 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonMiddle;
1829 bool expect_new_window = false; 1916 WindowOpenDisposition disposition = NEW_BACKGROUND_TAB;
1830 bool expect_foreground = false; 1917 RunTest(browser(), GetHrefURL(), modifiers, button, disposition);
1831 RunTest(browser(), modifiers, button, expect_new_window, expect_foreground);
1832 } 1918 }
1833 1919
1834 // Shift-middle-clicks open in a foreground tab. 1920 // Shift-middle-clicks open in a foreground tab.
1835 IN_PROC_BROWSER_TEST_F(ClickModifierTest, ShiftMiddleClickTest) { 1921 IN_PROC_BROWSER_TEST_F(ClickModifierTest, HrefShiftMiddleClickTest) {
1836 int modifiers = WebKit::WebInputEvent::ShiftKey; 1922 int modifiers = WebKit::WebInputEvent::ShiftKey;
1837 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonMiddle; 1923 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonMiddle;
1838 bool expect_new_window = false; 1924 WindowOpenDisposition disposition = NEW_FOREGROUND_TAB;
1839 bool expect_foreground = true; 1925 RunTest(browser(), GetHrefURL(), modifiers, button, disposition);
1840 RunTest(browser(), modifiers, button, expect_new_window, expect_foreground);
1841 } 1926 }
OLDNEW
« no previous file with comments | « no previous file | chrome/test/data/click_modifier/href.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698