OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/file_path.h" | |
6 #include "base/utf_string_conversions.h" | |
7 #include "chrome/browser/content_settings/host_content_settings_map.h" | |
8 #include "chrome/browser/ui/browser_finder.h" | |
9 #include "chrome/browser/ui/browser_tabstrip.h" | |
10 #include "chrome/common/chrome_notification_types.h" | |
11 #include "chrome/test/base/in_process_browser_test.h" | |
12 #include "chrome/test/base/ui_test_utils.h" | |
13 #include "content/public/browser/web_contents.h" | |
14 #include "content/public/browser/notification_service.h" | |
15 #include "content/public/test/browser_test_utils.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 namespace { | |
19 | |
20 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("click_modifier"); | |
21 static const char kFirstPageTitle[] = "First window"; | |
22 static const char kSecondPageTitle[] = "New window!"; | |
23 | |
24 class ClickModifierBrowserTest : public InProcessBrowserTest { | |
25 public: | |
26 ClickModifierBrowserTest() { | |
27 } | |
28 | |
29 // Returns a url that opens a new window or tab when clicked, via javascript. | |
30 GURL GetTestURL() { | |
31 return ui_test_utils::GetTestUrl( | |
32 FilePath(kTestDir), | |
33 FilePath(FILE_PATH_LITERAL("window_open.html"))); | |
34 } | |
35 | |
36 string16 getFirstPageTitle() { | |
37 return ASCIIToUTF16(kFirstPageTitle); | |
38 } | |
39 | |
40 string16 getSecondPageTitle() { | |
41 return ASCIIToUTF16(kSecondPageTitle); | |
42 } | |
43 | |
44 // Loads our test page and simulates a single click using the supplied button | |
45 // and modifiers. The test page will call window.open, creating either a new | |
46 // tab or a new window. If it's a tab, it will use active_title to verify | |
47 // whether it's in the foreground or background. Either way, it will use | |
48 // browser_count to determine | |
49 void RunTest( | |
50 Browser* browser, | |
51 int modifiers, | |
52 WebKit::WebMouseEvent::Button button, | |
53 bool expect_new_window, | |
54 bool expect_foreground) { | |
55 | |
56 GURL url(GetTestURL()); | |
57 ui_test_utils::NavigateToURL(browser, url); | |
58 EXPECT_EQ(1u, browser::GetBrowserCount(browser->profile())); | |
59 EXPECT_EQ(1, browser->tab_count()); | |
60 content::WebContents* web_contents = chrome::GetActiveWebContents(browser); | |
61 EXPECT_EQ(url, web_contents->GetURL()); | |
62 | |
63 content::WindowedNotificationObserver observer( | |
64 chrome::NOTIFICATION_TAB_ADDED, | |
65 content::NotificationService::AllSources()); | |
66 SimulateMouseClick(web_contents, modifiers, button); | |
67 observer.Wait(); | |
68 | |
69 unsigned expected_browser_count = expect_new_window ? 2 : 1; | |
70 EXPECT_EQ(expected_browser_count, | |
71 browser::GetBrowserCount(browser->profile())); | |
72 | |
73 // If we didn't pop up a new window, we need to make sure the right tab's in | |
74 // front. | |
75 if (!expect_new_window) { | |
76 EXPECT_EQ(2, browser->tab_count()); | |
77 web_contents = chrome::GetActiveWebContents(browser); | |
78 WaitForLoadStop(web_contents); | |
79 if (expect_foreground) { | |
80 EXPECT_EQ(getSecondPageTitle(), web_contents->GetTitle()); | |
81 } else { | |
82 EXPECT_EQ(getFirstPageTitle(), web_contents->GetTitle()); | |
83 } | |
84 } | |
85 } | |
86 private: | |
sky
2012/10/23 19:45:01
newline between 85/86.
ericu
2012/10/23 20:09:10
Done.
| |
87 DISALLOW_COPY_AND_ASSIGN(ClickModifierBrowserTest); | |
88 }; | |
89 | |
90 IN_PROC_BROWSER_TEST_F(ClickModifierBrowserTest, BasicClickTest) { | |
91 int modifiers = 0; | |
92 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonLeft; | |
93 bool expect_new_window = false; | |
94 bool expect_foreground = true; | |
95 RunTest(browser(), modifiers, button, expect_new_window, expect_foreground); | |
96 } | |
97 | |
98 // TODO(ericu): Alt-click behavior is platform-dependent and not well defined. | |
99 // Should we add tests so we know if it changes? | |
100 | |
101 // We ignore meta, so this should be just like BasicClickTest. | |
102 IN_PROC_BROWSER_TEST_F(ClickModifierBrowserTest, MetaClickTest) { | |
103 int modifiers = WebKit::WebInputEvent::MetaKey; | |
104 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonLeft; | |
105 bool expect_new_window = false; | |
106 bool expect_foreground = true; | |
107 RunTest(browser(), modifiers, button, expect_new_window, expect_foreground); | |
108 } | |
109 | |
110 // Shift-clicks open in a new window. | |
111 IN_PROC_BROWSER_TEST_F(ClickModifierBrowserTest, ShiftClickTest) { | |
112 int modifiers = WebKit::WebInputEvent::ShiftKey; | |
113 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonLeft; | |
114 bool expect_new_window = true; | |
115 bool expect_foreground = true; | |
116 RunTest(browser(), modifiers, button, expect_new_window, expect_foreground); | |
117 } | |
118 | |
119 // Shift-clicks open in a background tab. | |
120 IN_PROC_BROWSER_TEST_F(ClickModifierBrowserTest, ControlClickTest) { | |
121 int modifiers = WebKit::WebInputEvent::ControlKey; | |
122 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonLeft; | |
123 bool expect_new_window = false; | |
124 bool expect_foreground = false; | |
125 RunTest(browser(), modifiers, button, expect_new_window, expect_foreground); | |
126 } | |
127 | |
128 // Control-shift-clicks open in a foreground tab. | |
129 IN_PROC_BROWSER_TEST_F(ClickModifierBrowserTest, ControlShiftClickTest) { | |
130 int modifiers = WebKit::WebInputEvent::ControlKey | | |
131 WebKit::WebInputEvent::ShiftKey; | |
132 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonLeft; | |
133 bool expect_new_window = false; | |
134 bool expect_foreground = true; | |
135 RunTest(browser(), modifiers, button, expect_new_window, expect_foreground); | |
136 } | |
137 | |
138 // Middle-clicks open in a background tab. | |
139 IN_PROC_BROWSER_TEST_F(ClickModifierBrowserTest, MiddleClickTest) { | |
140 int modifiers = 0; | |
141 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonMiddle; | |
142 bool expect_new_window = false; | |
143 bool expect_foreground = false; | |
144 RunTest(browser(), modifiers, button, expect_new_window, expect_foreground); | |
145 } | |
146 | |
147 // Shift-middle-clicks open in a foreground tab. | |
148 IN_PROC_BROWSER_TEST_F(ClickModifierBrowserTest, ShiftMiddleClickTest) { | |
149 int modifiers = WebKit::WebInputEvent::ShiftKey; | |
150 WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonMiddle; | |
151 bool expect_new_window = false; | |
152 bool expect_foreground = true; | |
153 RunTest(browser(), modifiers, button, expect_new_window, expect_foreground); | |
154 } | |
155 | |
156 } // namespace | |
OLD | NEW |