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.
5 #include "base/mac/foundation_util.h"
6 #include "chrome/browser/ui/browser.h"
7 #include "chrome/browser/ui/browser_window.h"
8 #include "chrome/browser/ui/cocoa/find_bar/find_bar_text_field.h"
9 #include "chrome/browser/ui/find_bar/find_bar.h"
10 #include "chrome/browser/ui/find_bar/find_bar_controller.h"
11 #include "chrome/browser/ui/location_bar/location_bar.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/test/base/in_process_browser_test.h"
17 bool FindBarHasFocus(Browser* browser) {
18 NSWindow* window = browser->window()->GetNativeWindow();
19 NSText* text_view = base::mac::ObjCCast<NSText>([window firstResponder]);
20 return [[text_view delegate] isKindOfClass:[FindBarTextField class]];
25 typedef InProcessBrowserTest FindBarBrowserTest;
27 IN_PROC_BROWSER_TEST_F(FindBarBrowserTest, FocusOnTabSwitch) {
28 AddTabAtIndex(1, GURL("about:blank"), ui::PAGE_TRANSITION_LINK);
29 browser()->GetFindBarController()->Show();
31 // Verify that if the find bar has focus then switching tabs and changing
32 // back sets focus back to the find bar.
33 browser()->GetFindBarController()->find_bar()->SetFocusAndSelection();
34 EXPECT_TRUE(FindBarHasFocus(browser()));
35 browser()->tab_strip_model()->ActivateTabAt(0, true);
36 EXPECT_FALSE(FindBarHasFocus(browser()));
37 browser()->tab_strip_model()->ActivateTabAt(1, true);
38 EXPECT_TRUE(FindBarHasFocus(browser()));
40 // Verify that if the find bar does not have focus then switching tabs and
41 // changing does not set focus to the find bar.
42 browser()->window()->GetLocationBar()->FocusLocation(true);
43 EXPECT_FALSE(FindBarHasFocus(browser()));
44 browser()->tab_strip_model()->ActivateTabAt(0, true);
45 EXPECT_FALSE(FindBarHasFocus(browser()));
46 browser()->tab_strip_model()->ActivateTabAt(1, true);
47 EXPECT_FALSE(FindBarHasFocus(browser()));