OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 "ui/base/cocoa/controls/hover_image_menu_button.h" | |
6 | |
7 #include "base/mac/foundation_util.h" | |
8 #import "testing/gtest_mac.h" | |
9 #import "ui/base/cocoa/controls/hover_image_menu_button_cell.h" | |
10 #import "ui/base/test/ui_cocoa_test_helper.h" | |
11 | |
12 namespace ui { | |
13 | |
14 namespace { | |
15 | |
16 // Test initialization and display of the NSPopUpButton that shows the drop- | |
17 // down menu. Don't try to show the menu, since it will block the thread. | |
18 class HoverImageMenuButtonTest : public CocoaTest { | |
19 public: | |
20 HoverImageMenuButtonTest() {} | |
21 | |
22 // CocoaTest override: | |
23 virtual void SetUp() OVERRIDE; | |
24 | |
25 protected: | |
26 scoped_nsobject<HoverImageMenuButton> menu_button_; | |
27 scoped_nsobject<NSImage> normal_; | |
28 scoped_nsobject<NSImage> pressed_; | |
29 scoped_nsobject<NSImage> hovered_; | |
30 | |
31 DISALLOW_COPY_AND_ASSIGN(HoverImageMenuButtonTest); | |
32 }; | |
33 | |
34 void HoverImageMenuButtonTest::SetUp() { | |
35 menu_button_.reset( | |
36 [[HoverImageMenuButton alloc] initWithFrame:NSMakeRect(0, 0, 50, 30) | |
37 pullsDown:YES]); | |
38 | |
39 normal_.reset([base::mac::ObjCCastStrict<NSImage>( | |
40 [NSImage imageNamed:NSImageNameStatusAvailable]) retain]); | |
41 pressed_.reset([base::mac::ObjCCastStrict<NSImage>( | |
42 [NSImage imageNamed:NSImageNameStatusUnavailable]) retain]); | |
43 hovered_.reset([base::mac::ObjCCastStrict<NSImage>( | |
44 [NSImage imageNamed:NSImageNameStatusPartiallyAvailable]) retain]); | |
45 [[menu_button_ hoverImageMenuButtonCell] setImage:normal_]; | |
46 [[menu_button_ hoverImageMenuButtonCell] setAlternateImage:pressed_]; | |
47 [[menu_button_ hoverImageMenuButtonCell] setHoverImage:hovered_]; | |
48 | |
49 CocoaTest::SetUp(); | |
50 [[test_window() contentView] addSubview:menu_button_]; | |
51 } | |
52 | |
53 } // namespace | |
54 | |
55 TEST_VIEW(HoverImageMenuButtonTest, menu_button_); | |
56 | |
57 // Tests that the correct image is chosen, depending on the cell's state flags. | |
58 TEST_F(HoverImageMenuButtonTest, CheckImagesForState) { | |
59 EXPECT_FALSE([[menu_button_ cell] isHovered]); | |
60 EXPECT_FALSE([[menu_button_ cell] isHighlighted]); | |
61 EXPECT_NSEQ(normal_, [[menu_button_ cell] imageToDraw]); | |
62 [menu_button_ display]; | |
63 | |
64 [[menu_button_ cell] setHovered:YES]; | |
65 EXPECT_TRUE([[menu_button_ cell] isHovered]); | |
66 EXPECT_FALSE([[menu_button_ cell] isHighlighted]); | |
67 EXPECT_NSEQ(hovered_, [[menu_button_ cell] imageToDraw]); | |
68 [menu_button_ display]; | |
69 | |
70 // Highlighted takes precendece over hover. | |
71 [[menu_button_ cell] setHighlighted:YES]; | |
72 EXPECT_TRUE([[menu_button_ cell] isHovered]); | |
73 EXPECT_TRUE([[menu_button_ cell] isHighlighted]); | |
74 EXPECT_NSEQ(pressed_, [[menu_button_ cell] imageToDraw]); | |
75 [menu_button_ display]; | |
76 | |
77 [[menu_button_ cell] setHovered:NO]; | |
78 EXPECT_FALSE([[menu_button_ cell] isHovered]); | |
79 EXPECT_TRUE([[menu_button_ cell] isHighlighted]); | |
80 EXPECT_NSEQ(pressed_, [[menu_button_ cell] imageToDraw]); | |
81 [menu_button_ display]; | |
82 | |
83 [[menu_button_ cell] setHighlighted:NO]; | |
84 EXPECT_FALSE([[menu_button_ cell] isHovered]); | |
85 EXPECT_FALSE([[menu_button_ cell] isHighlighted]); | |
86 EXPECT_NSEQ(normal_, [[menu_button_ cell] imageToDraw]); | |
87 [menu_button_ display]; | |
88 } | |
89 | |
90 // Tests that calling the various setXImage functions calls setNeedsDisplay. | |
91 TEST_F(HoverImageMenuButtonTest, NewImageCausesDisplay) { | |
92 [menu_button_ display]; | |
93 EXPECT_FALSE([menu_button_ needsDisplay]); | |
94 | |
95 // Note that regular setImage is overridden to ensure the image goes into the | |
96 // NSPopUpButtonCell's menuItem. | |
97 [[menu_button_ cell] setImage:pressed_]; | |
98 | |
99 // Highlighting the cell requires a redisplay. | |
100 [[menu_button_ cell] setHighlighted:YES]; | |
101 EXPECT_TRUE([menu_button_ needsDisplay]); | |
102 [menu_button_ display]; | |
103 EXPECT_FALSE([menu_button_ needsDisplay]); | |
104 | |
105 // setAlternateImage comes from NSButtonCell. Ensure the added setHover* | |
106 // behaviour matches. | |
107 [[menu_button_ cell] setAlternateImage:normal_]; | |
108 EXPECT_TRUE([menu_button_ needsDisplay]); | |
109 [menu_button_ display]; | |
110 EXPECT_FALSE([menu_button_ needsDisplay]); | |
111 | |
112 // Setting the same image should not cause a redisplay. | |
113 [[menu_button_ cell] setAlternateImage:normal_]; | |
114 EXPECT_FALSE([menu_button_ needsDisplay]); | |
115 | |
116 // Unhighlighting requires a redisplay. | |
117 [[menu_button_ cell] setHighlighted:NO]; | |
118 EXPECT_TRUE([menu_button_ needsDisplay]); | |
119 [menu_button_ display]; | |
120 EXPECT_FALSE([menu_button_ needsDisplay]); | |
121 | |
122 // Changing hover state requires a redisplay. | |
123 [[menu_button_ cell] setHovered:YES]; | |
124 EXPECT_TRUE([menu_button_ needsDisplay]); | |
125 [menu_button_ display]; | |
126 EXPECT_FALSE([menu_button_ needsDisplay]); | |
127 | |
128 // setHoverImage comes directly from storage in HoverImageMenuButtonCell. | |
129 [[menu_button_ cell] setHoverImage:normal_]; | |
130 EXPECT_TRUE([menu_button_ needsDisplay]); | |
131 [menu_button_ display]; | |
132 EXPECT_FALSE([menu_button_ needsDisplay]); | |
133 | |
134 // Setting the same image should not cause a redisplay. | |
135 [[menu_button_ cell] setHoverImage:normal_]; | |
136 EXPECT_FALSE([menu_button_ needsDisplay]); | |
137 | |
138 // Unhover requires a redisplay. | |
139 [[menu_button_ cell] setHovered:NO]; | |
140 EXPECT_TRUE([menu_button_ needsDisplay]); | |
141 [menu_button_ display]; | |
142 EXPECT_FALSE([menu_button_ needsDisplay]); | |
143 | |
144 // Changing the image while not hovered should not require a redisplay. | |
145 [[menu_button_ cell] setHoverImage:pressed_]; | |
146 EXPECT_FALSE([menu_button_ needsDisplay]); | |
147 } | |
148 | |
149 // Test that the mouse enter and exit is properly handled, to set hover state. | |
150 TEST_F(HoverImageMenuButtonTest, SimulateMouseEnterExit) { | |
151 [menu_button_ display]; | |
152 EXPECT_FALSE([menu_button_ needsDisplay]); | |
153 EXPECT_NSEQ(normal_, [[menu_button_ cell] imageToDraw]); | |
154 | |
155 [menu_button_ mouseEntered:nil]; | |
156 EXPECT_TRUE([menu_button_ needsDisplay]); | |
157 EXPECT_NSEQ(hovered_, [[menu_button_ cell] imageToDraw]); | |
158 [menu_button_ display]; | |
159 EXPECT_FALSE([menu_button_ needsDisplay]); | |
160 | |
161 [menu_button_ mouseExited:nil]; | |
162 EXPECT_TRUE([menu_button_ needsDisplay]); | |
163 EXPECT_NSEQ(normal_, [[menu_button_ cell] imageToDraw]); | |
164 [menu_button_ display]; | |
165 EXPECT_FALSE([menu_button_ needsDisplay]); | |
166 } | |
167 | |
168 } // namespace ui | |
OLD | NEW |