- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / find_bar / find_bar_text_field_unittest.mm
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 #import <Cocoa/Cocoa.h>
6
7 #include "base/mac/scoped_nsobject.h"
8 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
9 #import "chrome/browser/ui/cocoa/find_bar/find_bar_text_field.h"
10 #import "chrome/browser/ui/cocoa/find_bar/find_bar_text_field_cell.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "testing/platform_test.h"
13
14 namespace {
15
16 // Width of the field so that we don't have to ask |field_| for it all
17 // the time.
18 static const CGFloat kWidth(300.0);
19
20 class FindBarTextFieldTest : public CocoaTest {
21  public:
22   FindBarTextFieldTest() {
23     // Make sure this is wide enough to play games with the cell
24     // decorations.
25     NSRect frame = NSMakeRect(0, 0, kWidth, 30);
26     base::scoped_nsobject<FindBarTextField> field(
27         [[FindBarTextField alloc] initWithFrame:frame]);
28     field_ = field.get();
29
30     [field_ setStringValue:@"Test test"];
31     [[test_window() contentView] addSubview:field_];
32   }
33
34   FindBarTextField* field_;
35 };
36
37 // Basic view tests (AddRemove, Display).
38 TEST_VIEW(FindBarTextFieldTest, field_);
39
40 // Test that we have the right cell class.
41 TEST_F(FindBarTextFieldTest, CellClass) {
42   EXPECT_TRUE([[field_ cell] isKindOfClass:[FindBarTextFieldCell class]]);
43 }
44
45 // Test that we get the same cell from -cell and
46 // -findBarTextFieldCell.
47 TEST_F(FindBarTextFieldTest, Cell) {
48   FindBarTextFieldCell* cell = [field_ findBarTextFieldCell];
49   EXPECT_EQ(cell, [field_ cell]);
50   EXPECT_TRUE(cell != nil);
51 }
52
53 // Test that becoming first responder sets things up correctly.
54 TEST_F(FindBarTextFieldTest, FirstResponder) {
55   EXPECT_EQ(nil, [field_ currentEditor]);
56   EXPECT_EQ([[field_ subviews] count], 0U);
57   [test_window() makePretendKeyWindowAndSetFirstResponder:field_];
58   EXPECT_FALSE(nil == [field_ currentEditor]);
59   EXPECT_EQ([[field_ subviews] count], 1U);
60   EXPECT_TRUE([[field_ currentEditor] isDescendantOf:field_]);
61 }
62
63 // Test drawing, mostly to ensure nothing leaks or crashes.
64 TEST_F(FindBarTextFieldTest, Display) {
65   [field_ display];
66
67   // Test focussed drawing.
68   [test_window() makePretendKeyWindowAndSetFirstResponder:field_];
69   [field_ display];
70   [test_window() clearPretendKeyWindowAndFirstResponder];
71
72   // Test display of various cell configurations.
73   FindBarTextFieldCell* cell = [field_ findBarTextFieldCell];
74   [cell setActiveMatch:4 of:5];
75   [field_ display];
76
77   [cell clearResults];
78   [field_ display];
79 }
80
81 }  // namespace