OLD | NEW |
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 "base/mac/foundation_util.h" | 5 #include "base/mac/foundation_util.h" |
| 6 #include "base/strings/utf_string_conversions.h" |
6 #include "chrome/browser/ui/browser.h" | 7 #include "chrome/browser/ui/browser.h" |
7 #include "chrome/browser/ui/browser_window.h" | 8 #include "chrome/browser/ui/browser_window.h" |
8 #import "chrome/browser/ui/cocoa/browser_window_controller.h" | 9 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
| 10 #include "chrome/browser/ui/cocoa/find_bar/find_bar_bridge.h" |
| 11 #include "chrome/browser/ui/cocoa/find_bar/find_bar_cocoa_controller.h" |
9 #include "chrome/browser/ui/cocoa/find_bar/find_bar_text_field.h" | 12 #include "chrome/browser/ui/cocoa/find_bar/find_bar_text_field.h" |
10 #import "chrome/browser/ui/cocoa/test/run_loop_testing.h" | 13 #import "chrome/browser/ui/cocoa/test/run_loop_testing.h" |
11 #include "chrome/browser/ui/exclusive_access/fullscreen_controller_test.h" | 14 #include "chrome/browser/ui/exclusive_access/fullscreen_controller_test.h" |
12 #include "chrome/browser/ui/find_bar/find_bar.h" | 15 #include "chrome/browser/ui/find_bar/find_bar.h" |
13 #include "chrome/browser/ui/find_bar/find_bar_controller.h" | 16 #include "chrome/browser/ui/find_bar/find_bar_controller.h" |
14 #include "chrome/browser/ui/location_bar/location_bar.h" | 17 #include "chrome/browser/ui/location_bar/location_bar.h" |
15 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
16 #include "chrome/test/base/in_process_browser_test.h" | 19 #include "chrome/test/base/in_process_browser_test.h" |
| 20 #include "chrome/test/base/ui_test_utils.h" |
17 #include "ui/base/test/ui_controls.h" | 21 #include "ui/base/test/ui_controls.h" |
18 #import "ui/events/test/cocoa_test_event_utils.h" | 22 #import "ui/events/test/cocoa_test_event_utils.h" |
19 | 23 |
| 24 // Expose private variables to make testing easier. |
| 25 @implementation FindBarCocoaController (Testing) |
| 26 - (NSButton*)nextButton { |
| 27 return nextButton_; |
| 28 } |
| 29 |
| 30 - (NSButton*)previousButton { |
| 31 return previousButton_; |
| 32 } |
| 33 @end |
| 34 |
| 35 using content::WebContents; |
| 36 using base::ASCIIToUTF16; |
| 37 |
20 namespace { | 38 namespace { |
21 | 39 |
| 40 const char kSimplePage[] = "simple.html"; |
| 41 |
22 bool FindBarHasFocus(Browser* browser) { | 42 bool FindBarHasFocus(Browser* browser) { |
23 NSWindow* window = browser->window()->GetNativeWindow(); | 43 NSWindow* window = browser->window()->GetNativeWindow(); |
24 NSText* text_view = base::mac::ObjCCast<NSText>([window firstResponder]); | 44 NSText* text_view = base::mac::ObjCCast<NSText>([window firstResponder]); |
25 return [[text_view delegate] isKindOfClass:[FindBarTextField class]]; | 45 return [[text_view delegate] isKindOfClass:[FindBarTextField class]]; |
26 } | 46 } |
27 | 47 |
| 48 GURL GetTestURL(const std::string& filename) { |
| 49 return ui_test_utils::GetTestUrl(base::FilePath().AppendASCII("find_in_page"), |
| 50 base::FilePath().AppendASCII(filename)); |
| 51 } |
| 52 |
| 53 FindBarCocoaController* GetFindBarCocoaController(Browser* browser) { |
| 54 FindBarBridge* bridge = |
| 55 static_cast<FindBarBridge*>(browser->GetFindBarController()->find_bar()); |
| 56 return bridge->find_bar_cocoa_controller(); |
| 57 } |
| 58 |
| 59 NSButton* GetFindBarControllerNextButton(Browser* browser) { |
| 60 FindBarCocoaController* cocoacontroller = GetFindBarCocoaController(browser); |
| 61 return [cocoacontroller nextButton]; |
| 62 } |
| 63 |
| 64 NSButton* GetFindBarControllerPreviousButton(Browser* browser) { |
| 65 FindBarCocoaController* cocoacontroller = GetFindBarCocoaController(browser); |
| 66 return [cocoacontroller previousButton]; |
| 67 } |
| 68 |
| 69 int GetFindInContentsMatchCount(WebContents* contents, |
| 70 const base::string16& search_string) { |
| 71 return ui_test_utils::FindInPage(contents, search_string, true, false, NULL, |
| 72 NULL); |
| 73 } |
| 74 |
| 75 void SimulateKeyPress(NSWindow* window, ui::KeyboardCode key) { |
| 76 base::RunLoop run_loop; |
| 77 ui_controls::EnableUIControls(); |
| 78 ui_controls::SendKeyPressNotifyWhenDone(window, key, false, false, false, |
| 79 false, run_loop.QuitClosure()); |
| 80 run_loop.Run(); |
| 81 } |
| 82 |
28 } // namespace | 83 } // namespace |
29 | 84 |
30 typedef InProcessBrowserTest FindBarBrowserTest; | 85 typedef InProcessBrowserTest FindBarBrowserTest; |
31 | 86 |
32 IN_PROC_BROWSER_TEST_F(FindBarBrowserTest, FocusOnTabSwitch) { | 87 IN_PROC_BROWSER_TEST_F(FindBarBrowserTest, FocusOnTabSwitch) { |
33 AddTabAtIndex(1, GURL("about:blank"), ui::PAGE_TRANSITION_LINK); | 88 AddTabAtIndex(1, GURL("about:blank"), ui::PAGE_TRANSITION_LINK); |
34 browser()->GetFindBarController()->Show(); | 89 browser()->GetFindBarController()->Show(); |
35 | 90 |
36 // Verify that if the find bar has focus then switching tabs and changing | 91 // Verify that if the find bar has focus then switching tabs and changing |
37 // back sets focus back to the find bar. | 92 // back sets focus back to the find bar. |
38 browser()->GetFindBarController()->find_bar()->SetFocusAndSelection(); | 93 browser()->GetFindBarController()->find_bar()->SetFocusAndSelection(); |
39 EXPECT_TRUE(FindBarHasFocus(browser())); | 94 EXPECT_TRUE(FindBarHasFocus(browser())); |
40 browser()->tab_strip_model()->ActivateTabAt(0, true); | 95 browser()->tab_strip_model()->ActivateTabAt(0, true); |
41 EXPECT_FALSE(FindBarHasFocus(browser())); | 96 EXPECT_FALSE(FindBarHasFocus(browser())); |
42 browser()->tab_strip_model()->ActivateTabAt(1, true); | 97 browser()->tab_strip_model()->ActivateTabAt(1, true); |
43 EXPECT_TRUE(FindBarHasFocus(browser())); | 98 EXPECT_TRUE(FindBarHasFocus(browser())); |
44 | 99 |
45 // Verify that if the find bar does not have focus then switching tabs and | 100 // Verify that if the find bar does not have focus then switching tabs and |
46 // changing does not set focus to the find bar. | 101 // changing does not set focus to the find bar. |
47 browser()->window()->GetLocationBar()->FocusLocation(true); | 102 browser()->window()->GetLocationBar()->FocusLocation(true); |
48 EXPECT_FALSE(FindBarHasFocus(browser())); | 103 EXPECT_FALSE(FindBarHasFocus(browser())); |
49 browser()->tab_strip_model()->ActivateTabAt(0, true); | 104 browser()->tab_strip_model()->ActivateTabAt(0, true); |
50 EXPECT_FALSE(FindBarHasFocus(browser())); | 105 EXPECT_FALSE(FindBarHasFocus(browser())); |
51 browser()->tab_strip_model()->ActivateTabAt(1, true); | 106 browser()->tab_strip_model()->ActivateTabAt(1, true); |
52 EXPECT_FALSE(FindBarHasFocus(browser())); | 107 EXPECT_FALSE(FindBarHasFocus(browser())); |
53 } | 108 } |
54 | 109 |
| 110 IN_PROC_BROWSER_TEST_F(FindBarBrowserTest, NextPreviousButtonsRemainDisabled) { |
| 111 ui_test_utils::NavigateToURL(browser(), GetTestURL(kSimplePage)); |
| 112 browser()->GetFindBarController()->Show(); |
| 113 browser()->GetFindBarController()->find_bar()->SetFocusAndSelection(); |
| 114 EXPECT_TRUE(FindBarHasFocus(browser())); |
| 115 |
| 116 // Simulate key press for character not in page contents. |
| 117 SimulateKeyPress(browser()->window()->GetNativeWindow(), ui::VKEY_Z); |
| 118 WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents(); |
| 119 EXPECT_EQ(GetFindInContentsMatchCount(contents, ASCIIToUTF16("Z")), 0); |
| 120 |
| 121 // Buttons should be disabled as there were no results. |
| 122 NSButton* nextButton = GetFindBarControllerNextButton(browser()); |
| 123 NSButton* previousButton = GetFindBarControllerPreviousButton(browser()); |
| 124 EXPECT_FALSE([nextButton isEnabled]); |
| 125 EXPECT_FALSE([previousButton isEnabled]); |
| 126 |
| 127 // Move focus to the location bar then back to the find bar. |
| 128 browser()->window()->GetLocationBar()->FocusLocation(true); |
| 129 EXPECT_FALSE(FindBarHasFocus(browser())); |
| 130 browser()->GetFindBarController()->find_bar()->SetFocusAndSelection(); |
| 131 EXPECT_TRUE(FindBarHasFocus(browser())); |
| 132 |
| 133 // Buttons should remain disabled. |
| 134 EXPECT_FALSE([nextButton isEnabled]); |
| 135 EXPECT_FALSE([previousButton isEnabled]); |
| 136 } |
| 137 |
| 138 IN_PROC_BROWSER_TEST_F(FindBarBrowserTest, NextPreviousButtonsRemainEnabled) { |
| 139 ui_test_utils::NavigateToURL(browser(), GetTestURL(kSimplePage)); |
| 140 browser()->GetFindBarController()->Show(); |
| 141 browser()->GetFindBarController()->find_bar()->SetFocusAndSelection(); |
| 142 EXPECT_TRUE(FindBarHasFocus(browser())); |
| 143 |
| 144 // Simulate key press for character in page contents. |
| 145 SimulateKeyPress(browser()->window()->GetNativeWindow(), ui::VKEY_A); |
| 146 WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents(); |
| 147 EXPECT_GT(GetFindInContentsMatchCount(contents, ASCIIToUTF16("A")), 0); |
| 148 |
| 149 // Buttons should be enabled because we had results. |
| 150 NSButton* nextButton = GetFindBarControllerNextButton(browser()); |
| 151 NSButton* previousButton = GetFindBarControllerPreviousButton(browser()); |
| 152 EXPECT_TRUE([nextButton isEnabled]); |
| 153 EXPECT_TRUE([previousButton isEnabled]); |
| 154 |
| 155 // Move focus to the location bar then back to the find bar. |
| 156 browser()->window()->GetLocationBar()->FocusLocation(true); |
| 157 EXPECT_FALSE(FindBarHasFocus(browser())); |
| 158 browser()->GetFindBarController()->find_bar()->SetFocusAndSelection(); |
| 159 EXPECT_TRUE(FindBarHasFocus(browser())); |
| 160 |
| 161 // Buttons should remain enabled. |
| 162 EXPECT_TRUE([nextButton isEnabled]); |
| 163 EXPECT_TRUE([previousButton isEnabled]); |
| 164 } |
| 165 |
55 IN_PROC_BROWSER_TEST_F(FindBarBrowserTest, EscapeKey) { | 166 IN_PROC_BROWSER_TEST_F(FindBarBrowserTest, EscapeKey) { |
56 // Enter fullscreen. | 167 // Enter fullscreen. |
57 std::unique_ptr<FullscreenNotificationObserver> waiter( | 168 std::unique_ptr<FullscreenNotificationObserver> waiter( |
58 new FullscreenNotificationObserver()); | 169 new FullscreenNotificationObserver()); |
59 browser() | 170 browser() |
60 ->exclusive_access_manager() | 171 ->exclusive_access_manager() |
61 ->fullscreen_controller() | 172 ->fullscreen_controller() |
62 ->ToggleBrowserFullscreenMode(); | 173 ->ToggleBrowserFullscreenMode(); |
63 waiter->Wait(); | 174 waiter->Wait(); |
64 | 175 |
65 NSWindow* window = browser()->window()->GetNativeWindow(); | 176 NSWindow* window = browser()->window()->GetNativeWindow(); |
66 BrowserWindowController* bwc = | 177 BrowserWindowController* bwc = |
67 [BrowserWindowController browserWindowControllerForWindow:window]; | 178 [BrowserWindowController browserWindowControllerForWindow:window]; |
68 EXPECT_TRUE([bwc isInAppKitFullscreen]); | 179 EXPECT_TRUE([bwc isInAppKitFullscreen]); |
69 | 180 |
70 // Show and focus on the find bar. | 181 // Show and focus on the find bar. |
71 browser()->GetFindBarController()->Show(); | 182 browser()->GetFindBarController()->Show(); |
72 browser()->GetFindBarController()->find_bar()->SetFocusAndSelection(); | 183 browser()->GetFindBarController()->find_bar()->SetFocusAndSelection(); |
73 EXPECT_TRUE(FindBarHasFocus(browser())); | 184 EXPECT_TRUE(FindBarHasFocus(browser())); |
74 | 185 |
75 // Simulate a key press with the ESC key. | 186 // Simulate a key press with the ESC key. |
76 base::RunLoop run_loop; | 187 SimulateKeyPress(window, ui::VKEY_ESCAPE); |
77 ui_controls::EnableUIControls(); | |
78 ui_controls::SendKeyPressNotifyWhenDone(window, ui::VKEY_ESCAPE, false, false, | |
79 false, false, run_loop.QuitClosure()); | |
80 run_loop.Run(); | |
81 | 188 |
82 // Check that the browser is still in fullscreen and that the find bar has | 189 // Check that the browser is still in fullscreen and that the find bar has |
83 // lost focus. | 190 // lost focus. |
84 EXPECT_FALSE(FindBarHasFocus(browser())); | 191 EXPECT_FALSE(FindBarHasFocus(browser())); |
85 EXPECT_TRUE([bwc isInAppKitFullscreen]); | 192 EXPECT_TRUE([bwc isInAppKitFullscreen]); |
86 } | 193 } |
OLD | NEW |