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 "chrome/browser/ui/views/location_bar/star_view.h" 6 7 #include "base/command_line.h" 8 #include "base/prefs/pref_service.h" 9 #include "base/strings/utf_string_conversions.h" 10 #include "chrome/browser/ui/tabs/tab_strip_model.h" 11 #include "chrome/browser/ui/views/bookmarks/bookmark_bubble_view.h" 12 #include "chrome/browser/ui/views/frame/browser_view.h" 13 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" 14 #include "chrome/common/pref_names.h" 15 #include "chrome/test/base/interactive_test_utils.h" 16 #include "chrome/test/base/in_process_browser_test.h" 17 #include "chrome/test/base/ui_test_utils.h" 18 #include "content/public/test/browser_test_utils.h" 19 #include "content/public/test/test_utils.h" 20 #include "ui/base/ui_base_switches.h" 21 22 #if defined(OS_WIN) 23 #include "ui/aura/window.h" 24 #include "ui/aura/window_tree_host.h" 25 #endif 26 27 namespace { 28 29 typedef InProcessBrowserTest StarViewTest; 30 31 // Verify that clicking the bookmark star a second time hides the bookmark 32 // bubble. 33 #if defined(OS_LINUX) && defined(USE_AURA) && !defined(OS_CHROMEOS) 34 #define MAYBE_HideOnSecondClick DISABLED_HideOnSecondClick 35 #else 36 #define MAYBE_HideOnSecondClick HideOnSecondClick 37 #endif 38 IN_PROC_BROWSER_TEST_F(StarViewTest, MAYBE_HideOnSecondClick) { 39 BrowserView* browser_view = reinterpret_cast<BrowserView*>( 40 browser()->window()); 41 views::View* star_view = 42 browser_view->GetToolbarView()->location_bar()->star_view(); 43 44 ui::MouseEvent pressed_event(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), 45 ui::EF_LEFT_MOUSE_BUTTON, 46 ui::EF_LEFT_MOUSE_BUTTON); 47 ui::MouseEvent released_event(ui::ET_MOUSE_RELEASED, gfx::Point(), 48 gfx::Point(), ui::EF_LEFT_MOUSE_BUTTON, 49 ui::EF_LEFT_MOUSE_BUTTON); 50 51 // Verify that clicking once shows the bookmark bubble. 52 EXPECT_FALSE(BookmarkBubbleView::IsShowing()); 53 star_view->OnMousePressed(pressed_event); 54 EXPECT_FALSE(BookmarkBubbleView::IsShowing()); 55 star_view->OnMouseReleased(released_event); 56 EXPECT_TRUE(BookmarkBubbleView::IsShowing()); 57 58 // Verify that clicking again doesn't reshow it. 59 star_view->OnMousePressed(pressed_event); 60 // Hide the bubble manually. In the browser this would normally happen during 61 // the event processing. 62 BookmarkBubbleView::Hide(); 63 base::MessageLoop::current()->RunUntilIdle(); 64 EXPECT_FALSE(BookmarkBubbleView::IsShowing()); 65 star_view->OnMouseReleased(released_event); 66 EXPECT_FALSE(BookmarkBubbleView::IsShowing()); 67 } 68 69 #if defined(OS_WIN) 70 71 class StarViewTestNoDWM : public InProcessBrowserTest { 72 public: 73 StarViewTestNoDWM() {} 74 75 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 76 command_line->AppendSwitch(switches::kDisableDwmComposition); 77 } 78 }; 79 80 BOOL CALLBACK EnumerateChildren(HWND hwnd, LPARAM l_param) { 81 HWND* child = reinterpret_cast<HWND*>(l_param); 82 *child = hwnd; 83 // The first child window is the plugin, then its children. So stop 84 // enumerating after the first callback. 85 return FALSE; 86 } 87 88 // Ensure that UIs like the star window, user profiler picker, omnibox 89 // popup and bookmark editor are always over a windowed NPAPI plugin even if 90 // kDisableDwmComposition is used. 91 // flaky: http://crbug.com/406631 92 IN_PROC_BROWSER_TEST_F(StarViewTestNoDWM, DISABLED_WindowedNPAPIPluginHidden) { 93 browser()->profile()->GetPrefs()->SetBoolean(prefs::kPluginsAlwaysAuthorize, 94 true); 95 96 // First switch to a new tab and back, to also test a scenario where we 97 // stopped watching the root window. 98 ui_test_utils::NavigateToURLWithDisposition( 99 browser(), GURL("about:blank"), NEW_FOREGROUND_TAB, 100 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); 101 browser()->tab_strip_model()->ActivateTabAt(0, true); 102 103 // First load the page and wait for the NPAPI plugin's window to display. 104 base::string16 expected_title(base::ASCIIToUTF16("ready")); 105 content::WebContents* tab = 106 browser()->tab_strip_model()->GetActiveWebContents(); 107 content::TitleWatcher title_watcher(tab, expected_title); 108 109 GURL url = ui_test_utils::GetTestUrl( 110 base::FilePath().AppendASCII("printing"), 111 base::FilePath().AppendASCII("npapi_plugin.html")); 112 ui_test_utils::NavigateToURL(browser(), url); 113 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle()); 114 115 // Now get the region of the plugin before the star view is shown. 116 HWND hwnd = tab->GetNativeView()->GetHost()->GetAcceleratedWidget(); 117 HWND child = NULL; 118 EnumChildWindows(hwnd, EnumerateChildren,reinterpret_cast<LPARAM>(&child)); 119 120 RECT region_before, region_after; 121 int result = GetWindowRgnBox(child, ®ion_before); 122 ASSERT_EQ(result, SIMPLEREGION); 123 124 // Now show the star view 125 BrowserView* browser_view = reinterpret_cast<BrowserView*>( 126 browser()->window()); 127 views::ImageView* star_view = 128 browser_view->GetToolbarView()->location_bar()->star_view(); 129 130 scoped_refptr<content::MessageLoopRunner> runner = 131 new content::MessageLoopRunner; 132 // Verify that clicking once shows the bookmark bubble. 133 ui_test_utils::MoveMouseToCenterAndPress( 134 star_view, 135 ui_controls::LEFT, 136 ui_controls::DOWN | ui_controls::UP, 137 runner->QuitClosure()); 138 runner->Run(); 139 140 EXPECT_TRUE(BookmarkBubbleView::IsShowing()); 141 142 result = GetWindowRgnBox(child, ®ion_after); 143 if (result == NULLREGION) { 144 // Depending on the browser window size, the plugin could be full covered. 145 return; 146 } 147 148 if (result == COMPLEXREGION) { 149 // Complex region, by definition not equal to the initial region. 150 return; 151 } 152 153 ASSERT_EQ(result, SIMPLEREGION); 154 bool rects_equal = 155 region_before.left == region_after.left && 156 region_before.top == region_after.top && 157 region_before.right == region_after.right && 158 region_before.bottom == region_after.bottom; 159 ASSERT_FALSE(rects_equal); 160 } 161 162 #endif 163 164 } // namespace 165