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 #import <Cocoa/Cocoa.h> 6 7 #include "base/memory/scoped_nsobject.h" 8 #import "chrome/browser/ui/cocoa/gradient_button_cell.h" 9 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/platform_test.h" 12 13 @interface GradientButtonCell (HoverValueTesting) 14 - (void)performOnePulseStep; 15 @end 16 17 namespace { 18 19 class GradientButtonCellTest : public CocoaTest { 20 public: 21 GradientButtonCellTest() { 22 NSRect frame = NSMakeRect(0, 0, 50, 30); 23 scoped_nsobject<NSButton>view([[NSButton alloc] initWithFrame:frame]); 24 view_ = view.get(); 25 scoped_nsobject<GradientButtonCell> cell([[GradientButtonCell alloc] 26 initTextCell:@"Testing"]); 27 [view_ setCell:cell.get()]; 28 [[test_window() contentView] addSubview:view_]; 29 } 30 31 NSButton* view_; 32 }; 33 34 TEST_VIEW(GradientButtonCellTest, view_) 35 36 // Test drawing, mostly to ensure nothing leaks or crashes. 37 TEST_F(GradientButtonCellTest, DisplayWithHover) { 38 [[view_ cell] setHoverAlpha:0.0]; 39 [view_ display]; 40 [[view_ cell] setHoverAlpha:0.5]; 41 [view_ display]; 42 [[view_ cell] setHoverAlpha:1.0]; 43 [view_ display]; 44 } 45 46 // Test hover, mostly to ensure nothing leaks or crashes. 47 TEST_F(GradientButtonCellTest, Hover) { 48 GradientButtonCell* cell = [view_ cell]; 49 [cell setMouseInside:YES animate:NO]; 50 EXPECT_EQ([[view_ cell] hoverAlpha], 1.0); 51 52 [cell setMouseInside:NO animate:YES]; 53 CGFloat alpha1 = [cell hoverAlpha]; 54 [cell performOnePulseStep]; 55 CGFloat alpha2 = [cell hoverAlpha]; 56 EXPECT_TRUE(alpha2 < alpha1); 57 } 58 59 // Tracking rects 60 TEST_F(GradientButtonCellTest, TrackingRects) { 61 GradientButtonCell* cell = [view_ cell]; 62 EXPECT_FALSE([cell showsBorderOnlyWhileMouseInside]); 63 EXPECT_FALSE([cell isMouseInside]); 64 65 [cell setShowsBorderOnlyWhileMouseInside:YES]; 66 [cell mouseEntered:nil]; 67 EXPECT_TRUE([cell isMouseInside]); 68 [cell mouseExited:nil]; 69 EXPECT_FALSE([cell isMouseInside]); 70 71 [cell setShowsBorderOnlyWhileMouseInside:NO]; 72 EXPECT_FALSE([cell isMouseInside]); 73 74 [cell setShowsBorderOnlyWhileMouseInside:YES]; 75 [cell setShowsBorderOnlyWhileMouseInside:YES]; 76 [cell setShowsBorderOnlyWhileMouseInside:NO]; 77 [cell setShowsBorderOnlyWhileMouseInside:NO]; 78 } 79 80 TEST_F(GradientButtonCellTest, ContinuousPulseOnOff) { 81 GradientButtonCell* cell = [view_ cell]; 82 83 // On/off 84 EXPECT_FALSE([cell isContinuousPulsing]); 85 [cell setIsContinuousPulsing:YES]; 86 EXPECT_TRUE([cell isContinuousPulsing]); 87 EXPECT_TRUE([cell pulsing]); 88 [cell setIsContinuousPulsing:NO]; 89 EXPECT_FALSE([cell isContinuousPulsing]); 90 91 // On/safeOff 92 [cell setIsContinuousPulsing:YES]; 93 EXPECT_TRUE([cell isContinuousPulsing]); 94 [cell safelyStopPulsing]; 95 } 96 97 // More for valgrind; we don't confirm state change does anything useful. 98 TEST_F(GradientButtonCellTest, PulseState) { 99 GradientButtonCell* cell = [view_ cell]; 100 101 [cell setMouseInside:YES animate:YES]; 102 // Allow for immediate state changes to keep test unflaky 103 EXPECT_TRUE(([cell pulseState] == gradient_button_cell::kPulsingOn) || 104 ([cell pulseState] == gradient_button_cell::kPulsedOn)); 105 106 [cell setMouseInside:NO animate:YES]; 107 // Allow for immediate state changes to keep test unflaky 108 EXPECT_TRUE(([cell pulseState] == gradient_button_cell::kPulsingOff) || 109 ([cell pulseState] == gradient_button_cell::kPulsedOff)); 110 } 111 112 } // namespace 113