1 // Copyright (c) 2011 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/strings/string_util.h"
6 #include "base/strings/sys_string_conversions.h"
7 #include "chrome/browser/ui/browser_window.h"
8 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
9 #import "chrome/browser/ui/cocoa/find_bar/find_bar_cocoa_controller.h"
10 #import "chrome/browser/ui/cocoa/find_bar/find_bar_text_field.h"
11 #include "chrome/browser/ui/find_bar/find_notification_details.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "testing/platform_test.h"
14 #import "ui/base/cocoa/find_pasteboard.h"
16 // Expose private variables to make testing easier.
17 @interface FindBarCocoaController(Testing)
18 - (FindBarTextField*)findTextField;
21 @implementation FindBarCocoaController(Testing)
22 - (FindBarTextField*)findTextField {
26 - (NSButton*)nextButton {
30 - (NSButton*)previousButton {
31 return previousButton_;
37 class FindBarCocoaControllerTest : public CocoaTest {
39 void SetUp() override {
41 controller_.reset([[FindBarCocoaController alloc] initWithBrowser:nil]);
42 [[test_window() contentView] addSubview:[controller_ view]];
45 void TearDown() override {
46 CocoaTest::TearDown();
47 [controller_ stopAnimation];
51 base::scoped_nsobject<FindBarCocoaController> controller_;
54 TEST_VIEW(FindBarCocoaControllerTest, [controller_ view])
56 TEST_F(FindBarCocoaControllerTest, ImagesLoadedProperly) {
57 EXPECT_TRUE([[[controller_ nextButton] image] isValid]);
58 EXPECT_TRUE([[[controller_ previousButton] image] isValid]);
61 TEST_F(FindBarCocoaControllerTest, ShowAndHide) {
62 NSView* findBarView = [controller_ findBarView];
64 ASSERT_GT([findBarView frame].origin.y, 0);
65 ASSERT_FALSE([controller_ isFindBarVisible]);
66 ASSERT_TRUE([[controller_ view] isHidden]);
68 [controller_ showFindBar:NO];
69 EXPECT_EQ([findBarView frame].origin.y, 0);
70 EXPECT_TRUE([controller_ isFindBarVisible]);
71 ASSERT_FALSE([[controller_ view] isHidden]);
73 [controller_ hideFindBar:NO];
74 EXPECT_GT([findBarView frame].origin.y, 0);
75 EXPECT_FALSE([controller_ isFindBarVisible]);
76 ASSERT_TRUE([[controller_ view] isHidden]);
79 TEST_F(FindBarCocoaControllerTest, SetFindText) {
80 NSTextField* findTextField = [controller_ findTextField];
82 // Start by making the find bar visible.
83 [controller_ showFindBar:NO];
84 EXPECT_TRUE([controller_ isFindBarVisible]);
87 NSString* const kFindText = @"Google";
88 [controller_ setFindText:kFindText selectedRange:NSMakeRange(NSNotFound, 0)];
91 [[findTextField stringValue] compare:kFindText]);
93 // Call clearResults, which doesn't actually clear the find text but
94 // simply sets it back to what it was before. This is silly, but
95 // matches the behavior on other platforms. |details| isn't used by
96 // our implementation of clearResults, so it's ok to pass in an
98 FindNotificationDetails details;
99 [controller_ clearResults:details];
102 [[findTextField stringValue] compare:kFindText]);
105 TEST_F(FindBarCocoaControllerTest, ResultLabelUpdatesCorrectly) {
106 // TODO(rohitrao): Test this. It may involve creating some dummy
107 // FindNotificationDetails objects.
110 TEST_F(FindBarCocoaControllerTest, FindTextIsGlobal) {
111 base::scoped_nsobject<FindBarCocoaController> otherController(
112 [[FindBarCocoaController alloc] initWithBrowser:nil]);
113 [[test_window() contentView] addSubview:[otherController view]];
115 // Setting the text in one controller should update the other controller's
117 NSString* const kFindText = @"Respect to the man in the ice cream van";
118 [controller_ setFindText:kFindText selectedRange:NSMakeRange(NSNotFound, 0)];
121 [[controller_ findText] compare:kFindText]);
124 [[otherController.get() findText] compare:kFindText]);
127 TEST_F(FindBarCocoaControllerTest, SettingFindTextUpdatesFindPboard) {
128 NSString* const kFindText =
129 @"It's not a bird, it's not a plane, it must be Dave who's on the train";
130 [controller_ setFindText:kFindText selectedRange:NSMakeRange(NSNotFound, 0)];
133 [[[FindPasteboard sharedInstance] findText] compare:kFindText]);