Chromium Code Reviews| Index: chrome/browser/ui/click_modifier_browsertest.cc |
| diff --git a/chrome/browser/ui/click_modifier_browsertest.cc b/chrome/browser/ui/click_modifier_browsertest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1c327120628eb1b025523d09d8522230c1fbb179 |
| --- /dev/null |
| +++ b/chrome/browser/ui/click_modifier_browsertest.cc |
| @@ -0,0 +1,156 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/file_path.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "chrome/browser/content_settings/host_content_settings_map.h" |
| +#include "chrome/browser/ui/browser_finder.h" |
| +#include "chrome/browser/ui/browser_tabstrip.h" |
| +#include "chrome/common/chrome_notification_types.h" |
| +#include "chrome/test/base/in_process_browser_test.h" |
| +#include "chrome/test/base/ui_test_utils.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "content/public/test/browser_test_utils.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace { |
| + |
| +static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("click_modifier"); |
| +static const char kFirstPageTitle[] = "First window"; |
| +static const char kSecondPageTitle[] = "New window!"; |
| + |
| +class ClickModifierBrowserTest : public InProcessBrowserTest { |
| + public: |
| + ClickModifierBrowserTest() { |
| + } |
| + |
| + // Returns a url that opens a new window or tab when clicked, via javascript. |
| + GURL GetTestURL() { |
| + return ui_test_utils::GetTestUrl( |
| + FilePath(kTestDir), |
| + FilePath(FILE_PATH_LITERAL("window_open.html"))); |
| + } |
| + |
| + string16 getFirstPageTitle() { |
| + return ASCIIToUTF16(kFirstPageTitle); |
| + } |
| + |
| + string16 getSecondPageTitle() { |
| + return ASCIIToUTF16(kSecondPageTitle); |
| + } |
| + |
| + // Loads our test page and simulates a single click using the supplied button |
| + // and modifiers. The test page will call window.open, creating either a new |
| + // tab or a new window. If it's a tab, it will use active_title to verify |
| + // whether it's in the foreground or background. Either way, it will use |
| + // browser_count to determine |
| + void RunTest( |
| + Browser* browser, |
| + int modifiers, |
| + WebKit::WebMouseEvent::Button button, |
| + bool expect_new_window, |
| + bool expect_foreground) { |
| + |
| + GURL url(GetTestURL()); |
| + ui_test_utils::NavigateToURL(browser, url); |
| + EXPECT_EQ(1u, browser::GetBrowserCount(browser->profile())); |
| + EXPECT_EQ(1, browser->tab_count()); |
| + content::WebContents* web_contents = chrome::GetActiveWebContents(browser); |
| + EXPECT_EQ(url, web_contents->GetURL()); |
| + |
| + content::WindowedNotificationObserver observer( |
| + chrome::NOTIFICATION_TAB_ADDED, |
| + content::NotificationService::AllSources()); |
| + SimulateMouseClick(web_contents, modifiers, button); |
| + observer.Wait(); |
| + |
| + unsigned expected_browser_count = expect_new_window ? 2 : 1; |
| + EXPECT_EQ(expected_browser_count, |
| + browser::GetBrowserCount(browser->profile())); |
| + |
| + // If we didn't pop up a new window, we need to make sure the right tab's in |
| + // front. |
| + if (!expect_new_window) { |
| + EXPECT_EQ(2, browser->tab_count()); |
| + web_contents = chrome::GetActiveWebContents(browser); |
| + WaitForLoadStop(web_contents); |
| + if (expect_foreground) { |
| + EXPECT_EQ(getSecondPageTitle(), web_contents->GetTitle()); |
| + } else { |
| + EXPECT_EQ(getFirstPageTitle(), web_contents->GetTitle()); |
| + } |
| + } |
| + } |
| + private: |
|
sky
2012/10/23 19:45:01
newline between 85/86.
ericu
2012/10/23 20:09:10
Done.
|
| + DISALLOW_COPY_AND_ASSIGN(ClickModifierBrowserTest); |
| +}; |
| + |
| +IN_PROC_BROWSER_TEST_F(ClickModifierBrowserTest, BasicClickTest) { |
| + int modifiers = 0; |
| + WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonLeft; |
| + bool expect_new_window = false; |
| + bool expect_foreground = true; |
| + RunTest(browser(), modifiers, button, expect_new_window, expect_foreground); |
| +} |
| + |
| +// TODO(ericu): Alt-click behavior is platform-dependent and not well defined. |
| +// Should we add tests so we know if it changes? |
| + |
| +// We ignore meta, so this should be just like BasicClickTest. |
| +IN_PROC_BROWSER_TEST_F(ClickModifierBrowserTest, MetaClickTest) { |
| + int modifiers = WebKit::WebInputEvent::MetaKey; |
| + WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonLeft; |
| + bool expect_new_window = false; |
| + bool expect_foreground = true; |
| + RunTest(browser(), modifiers, button, expect_new_window, expect_foreground); |
| +} |
| + |
| +// Shift-clicks open in a new window. |
| +IN_PROC_BROWSER_TEST_F(ClickModifierBrowserTest, ShiftClickTest) { |
| + int modifiers = WebKit::WebInputEvent::ShiftKey; |
| + WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonLeft; |
| + bool expect_new_window = true; |
| + bool expect_foreground = true; |
| + RunTest(browser(), modifiers, button, expect_new_window, expect_foreground); |
| +} |
| + |
| +// Shift-clicks open in a background tab. |
| +IN_PROC_BROWSER_TEST_F(ClickModifierBrowserTest, ControlClickTest) { |
| + int modifiers = WebKit::WebInputEvent::ControlKey; |
| + WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonLeft; |
| + bool expect_new_window = false; |
| + bool expect_foreground = false; |
| + RunTest(browser(), modifiers, button, expect_new_window, expect_foreground); |
| +} |
| + |
| +// Control-shift-clicks open in a foreground tab. |
| +IN_PROC_BROWSER_TEST_F(ClickModifierBrowserTest, ControlShiftClickTest) { |
| + int modifiers = WebKit::WebInputEvent::ControlKey | |
| + WebKit::WebInputEvent::ShiftKey; |
| + WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonLeft; |
| + bool expect_new_window = false; |
| + bool expect_foreground = true; |
| + RunTest(browser(), modifiers, button, expect_new_window, expect_foreground); |
| +} |
| + |
| +// Middle-clicks open in a background tab. |
| +IN_PROC_BROWSER_TEST_F(ClickModifierBrowserTest, MiddleClickTest) { |
| + int modifiers = 0; |
| + WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonMiddle; |
| + bool expect_new_window = false; |
| + bool expect_foreground = false; |
| + RunTest(browser(), modifiers, button, expect_new_window, expect_foreground); |
| +} |
| + |
| +// Shift-middle-clicks open in a foreground tab. |
| +IN_PROC_BROWSER_TEST_F(ClickModifierBrowserTest, ShiftMiddleClickTest) { |
| + int modifiers = WebKit::WebInputEvent::ShiftKey; |
| + WebKit::WebMouseEvent::Button button = WebKit::WebMouseEvent::ButtonMiddle; |
| + bool expect_new_window = false; |
| + bool expect_foreground = true; |
| + RunTest(browser(), modifiers, button, expect_new_window, expect_foreground); |
| +} |
| + |
| +} // namespace |