OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
6 | 6 |
7 #include "base/scoped_nsobject.h" | 7 #include "base/scoped_nsobject.h" |
8 #import "chrome/browser/cocoa/gradient_button_cell.h" | 8 #import "chrome/browser/cocoa/gradient_button_cell.h" |
9 #import "chrome/browser/cocoa/cocoa_test_helper.h" | 9 #import "chrome/browser/cocoa/cocoa_test_helper.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 @interface GradientButtonCell (HoverValueTesting) | |
13 - (void)adjustHoverValue; | |
14 @end | |
15 | |
16 namespace { | 12 namespace { |
17 | 13 |
18 class GradientButtonCellTest : public testing::Test { | 14 class GradientButtonCellTest : public testing::Test { |
19 public: | 15 public: |
20 GradientButtonCellTest() { | 16 GradientButtonCellTest() { |
21 NSRect frame = NSMakeRect(0, 0, 50, 30); | 17 NSRect frame = NSMakeRect(0, 0, 50, 30); |
22 view_.reset([[NSButton alloc] initWithFrame:frame]); | 18 view_.reset([[NSButton alloc] initWithFrame:frame]); |
23 scoped_nsobject<GradientButtonCell> cell([[GradientButtonCell alloc] | 19 scoped_nsobject<GradientButtonCell> cell([[GradientButtonCell alloc] |
24 initTextCell:@"Testing"]); | 20 initTextCell:@"Testing"]); |
25 [view_ setCell:cell.get()]; | 21 [view_ setCell:cell.get()]; |
26 [cocoa_helper_.contentView() addSubview:view_.get()]; | 22 [cocoa_helper_.contentView() addSubview:view_.get()]; |
27 } | 23 } |
28 | 24 |
29 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... | 25 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... |
30 scoped_nsobject<NSButton> view_; | 26 scoped_nsobject<NSButton> view_; |
31 }; | 27 }; |
32 | 28 |
33 // Test adding/removing from the view hierarchy, mostly to ensure nothing | 29 // Test adding/removing from the view hierarchy, mostly to ensure nothing |
34 // leaks or crashes. | 30 // leaks or crashes. |
35 TEST_F(GradientButtonCellTest, AddRemove) { | 31 TEST_F(GradientButtonCellTest, AddRemove) { |
36 EXPECT_EQ(cocoa_helper_.contentView(), [view_ superview]); | 32 EXPECT_EQ(cocoa_helper_.contentView(), [view_ superview]); |
37 [view_.get() removeFromSuperview]; | 33 [view_.get() removeFromSuperview]; |
38 EXPECT_FALSE([view_ superview]); | 34 EXPECT_FALSE([view_ superview]); |
39 } | 35 } |
40 | 36 |
41 // Test drawing, mostly to ensure nothing leaks or crashes. | 37 // Test drawing, mostly to ensure nothing leaks or crashes. |
42 TEST_F(GradientButtonCellTest, Display) { | 38 TEST_F(GradientButtonCellTest, Display) { |
43 [[view_ cell] setHoverAlpha:0.0]; | |
44 [view_ display]; | 39 [view_ display]; |
45 [[view_ cell] setHoverAlpha:0.5]; | |
46 [view_ display]; | |
47 [[view_ cell] setHoverAlpha:1.0]; | |
48 [view_ display]; | |
49 } | |
50 | |
51 // Test drawing, mostly to ensure nothing leaks or crashes. | |
52 TEST_F(GradientButtonCellTest, Hover) { | |
53 GradientButtonCell* cell = [view_ cell]; | |
54 [cell setMouseInside:YES animate:NO]; | |
55 EXPECT_EQ([[view_ cell] hoverAlpha], 1.0); | |
56 | |
57 [cell setMouseInside:NO animate:YES]; | |
58 CGFloat alpha1 = [cell hoverAlpha]; | |
59 [cell adjustHoverValue]; | |
60 CGFloat alpha2 = [cell hoverAlpha]; | |
61 EXPECT_TRUE(alpha2 < alpha1); | |
62 } | 40 } |
63 | 41 |
64 // Tracking rects | 42 // Tracking rects |
65 TEST_F(GradientButtonCellTest, TrackingRects) { | 43 TEST_F(GradientButtonCellTest, TrackingRects) { |
66 GradientButtonCell* cell = [view_ cell]; | 44 GradientButtonCell* cell = [view_ cell]; |
67 EXPECT_FALSE([cell showsBorderOnlyWhileMouseInside]); | 45 EXPECT_FALSE([cell showsBorderOnlyWhileMouseInside]); |
68 EXPECT_FALSE([cell isMouseInside]); | 46 EXPECT_FALSE([cell isMouseInside]); |
69 | 47 |
70 [cell setShowsBorderOnlyWhileMouseInside:YES]; | 48 [cell setShowsBorderOnlyWhileMouseInside:YES]; |
71 [cell mouseEntered:nil]; | 49 [cell mouseEntered:nil]; |
72 EXPECT_TRUE([cell isMouseInside]); | 50 EXPECT_TRUE([cell isMouseInside]); |
73 [cell mouseExited:nil]; | 51 [cell mouseExited:nil]; |
74 EXPECT_FALSE([cell isMouseInside]); | 52 EXPECT_FALSE([cell isMouseInside]); |
75 | 53 |
76 [cell setShowsBorderOnlyWhileMouseInside:NO]; | 54 [cell setShowsBorderOnlyWhileMouseInside:NO]; |
77 EXPECT_FALSE([cell isMouseInside]); | 55 EXPECT_FALSE([cell isMouseInside]); |
78 | 56 |
79 [cell setShowsBorderOnlyWhileMouseInside:YES]; | 57 [cell setShowsBorderOnlyWhileMouseInside:YES]; |
80 [cell setShowsBorderOnlyWhileMouseInside:YES]; | 58 [cell setShowsBorderOnlyWhileMouseInside:YES]; |
81 [cell setShowsBorderOnlyWhileMouseInside:NO]; | 59 [cell setShowsBorderOnlyWhileMouseInside:NO]; |
82 [cell setShowsBorderOnlyWhileMouseInside:NO]; | 60 [cell setShowsBorderOnlyWhileMouseInside:NO]; |
83 } | 61 } |
84 | 62 |
85 } // namespace | 63 } // namespace |
OLD | NEW |