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