- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / find_bar / find_bar_cocoa_controller_unittest.mm
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.
4
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"
15
16 // Expose private variables to make testing easier.
17 @interface FindBarCocoaController(Testing)
18 - (FindBarTextField*)findTextField;
19 @end
20
21 @implementation FindBarCocoaController(Testing)
22 - (FindBarTextField*)findTextField {
23   return findText_;
24 }
25
26 - (NSButton*)nextButton {
27   return nextButton_;
28 }
29
30 - (NSButton*)previousButton {
31   return previousButton_;
32 }
33 @end
34
35 namespace {
36
37 class FindBarCocoaControllerTest : public CocoaTest {
38  public:
39   virtual void SetUp() {
40     CocoaTest::SetUp();
41     controller_.reset([[FindBarCocoaController alloc] initWithBrowser:nil]);
42     [[test_window() contentView] addSubview:[controller_ view]];
43   }
44
45   virtual void TearDown() {
46     CocoaTest::TearDown();
47     [controller_ stopAnimation];
48   }
49
50  protected:
51   base::scoped_nsobject<FindBarCocoaController> controller_;
52 };
53
54 TEST_VIEW(FindBarCocoaControllerTest, [controller_ view])
55
56 TEST_F(FindBarCocoaControllerTest, ImagesLoadedProperly) {
57   EXPECT_TRUE([[[controller_ nextButton] image] isValid]);
58   EXPECT_TRUE([[[controller_ previousButton] image] isValid]);
59 }
60
61 TEST_F(FindBarCocoaControllerTest, ShowAndHide) {
62   NSView* findBarView = [controller_ findBarView];
63
64   ASSERT_GT([findBarView frame].origin.y, 0);
65   ASSERT_FALSE([controller_ isFindBarVisible]);
66
67   [controller_ showFindBar:NO];
68   EXPECT_EQ([findBarView frame].origin.y, 0);
69   EXPECT_TRUE([controller_ isFindBarVisible]);
70
71   [controller_ hideFindBar:NO];
72   EXPECT_GT([findBarView frame].origin.y, 0);
73   EXPECT_FALSE([controller_ isFindBarVisible]);
74 }
75
76 TEST_F(FindBarCocoaControllerTest, SetFindText) {
77   NSTextField* findTextField = [controller_ findTextField];
78
79   // Start by making the find bar visible.
80   [controller_ showFindBar:NO];
81   EXPECT_TRUE([controller_ isFindBarVisible]);
82
83   // Set the find text.
84   NSString* const kFindText = @"Google";
85   [controller_ setFindText:kFindText selectedRange:NSMakeRange(NSNotFound, 0)];
86   EXPECT_EQ(
87       NSOrderedSame,
88       [[findTextField stringValue] compare:kFindText]);
89
90   // Call clearResults, which doesn't actually clear the find text but
91   // simply sets it back to what it was before.  This is silly, but
92   // matches the behavior on other platforms.  |details| isn't used by
93   // our implementation of clearResults, so it's ok to pass in an
94   // empty |details|.
95   FindNotificationDetails details;
96   [controller_ clearResults:details];
97   EXPECT_EQ(
98       NSOrderedSame,
99       [[findTextField stringValue] compare:kFindText]);
100 }
101
102 TEST_F(FindBarCocoaControllerTest, ResultLabelUpdatesCorrectly) {
103   // TODO(rohitrao): Test this.  It may involve creating some dummy
104   // FindNotificationDetails objects.
105 }
106
107 TEST_F(FindBarCocoaControllerTest, FindTextIsGlobal) {
108   base::scoped_nsobject<FindBarCocoaController> otherController(
109       [[FindBarCocoaController alloc] initWithBrowser:nil]);
110   [[test_window() contentView] addSubview:[otherController view]];
111
112   // Setting the text in one controller should update the other controller's
113   // text as well.
114   NSString* const kFindText = @"Respect to the man in the ice cream van";
115   [controller_ setFindText:kFindText selectedRange:NSMakeRange(NSNotFound, 0)];
116   EXPECT_EQ(
117       NSOrderedSame,
118       [[controller_ findText] compare:kFindText]);
119   EXPECT_EQ(
120       NSOrderedSame,
121       [[otherController.get() findText] compare:kFindText]);
122 }
123
124 TEST_F(FindBarCocoaControllerTest, SettingFindTextUpdatesFindPboard) {
125   NSString* const kFindText =
126       @"It's not a bird, it's not a plane, it must be Dave who's on the train";
127   [controller_ setFindText:kFindText selectedRange:NSMakeRange(NSNotFound, 0)];
128   EXPECT_EQ(
129       NSOrderedSame,
130       [[[FindPasteboard sharedInstance] findText] compare:kFindText]);
131 }
132
133 }  // namespace