Chromium Code Reviews| 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 "grit/ui_resources.h" | |
| 8 #import "testing/gtest_mac.h" | |
| 9 #include "ui/base/resource/resource_bundle.h" | |
| 10 #import "ui/base/test/ui_cocoa_test_helper.h" | |
| 11 | |
| 12 namespace ui { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 const int kIdrNormal = IDR_CHECKBOX; | |
| 17 const int kIdrPressed = IDR_CHECKBOX_PRESSED; | |
| 18 const int kIdrHovered = IDR_CHECKBOX_HOVER; | |
| 19 | |
| 20 // Test initialization and display of the NSPopUpButton that shows the drop- | |
| 21 // down menu. Don't try to show the menu, since it will block the thread. | |
| 22 class HoverImageMenuButtonTest : public CocoaTest { | |
| 23 public: | |
| 24 HoverImageMenuButtonTest() { | |
| 25 Init(); | |
|
sail
2013/05/31 19:07:03
don't need this (base class does it already)
tapted
2013/06/03 12:49:18
Done.
| |
| 26 } | |
| 27 | |
| 28 NSImage* ResetImage(int resource_id, scoped_nsobject<NSImage>* storage) { | |
| 29 storage->reset([ResourceBundle::GetSharedInstance(). | |
| 30 GetNativeImageNamed(resource_id).AsNSImage() retain]); | |
| 31 return storage->get(); | |
| 32 } | |
| 33 | |
| 34 void SetImages() { | |
| 35 [[menu_button_ cell] setImage:ResetImage(kIdrNormal, &normal_)]; | |
|
sail
2013/05/31 19:07:03
How about using system provided images instead of
tapted
2013/06/03 12:49:18
Done.
| |
| 36 [[menu_button_ cell] setAlternateImage:ResetImage(kIdrPressed, &pressed_)]; | |
| 37 [[menu_button_ cell] setHoverImage:ResetImage(kIdrHovered, &hovered_)]; | |
|
sail
2013/05/31 19:07:03
use hoverImageMenuButtonCell instead of cell ?
tapted
2013/06/03 12:49:18
Done.
| |
| 38 } | |
| 39 | |
| 40 virtual void SetUp() OVERRIDE { | |
| 41 menu_button_.reset( | |
| 42 [[HoverImageMenuButton alloc] initWithFrame:NSMakeRect(0, 0, 50, 30) | |
| 43 pullsDown:YES]); | |
| 44 CocoaTest::SetUp(); | |
| 45 [[test_window() contentView] addSubview:menu_button_]; | |
| 46 } | |
| 47 | |
| 48 protected: | |
| 49 scoped_nsobject<HoverImageMenuButton> menu_button_; | |
| 50 scoped_nsobject<NSImage> normal_; | |
|
sail
2013/05/31 19:07:03
how about setting these in the constructor, then y
tapted
2013/06/03 12:49:18
Done.
| |
| 51 scoped_nsobject<NSImage> pressed_; | |
| 52 scoped_nsobject<NSImage> hovered_; | |
| 53 | |
| 54 DISALLOW_COPY_AND_ASSIGN(HoverImageMenuButtonTest); | |
| 55 }; | |
| 56 | |
| 57 } // namespace | |
| 58 | |
| 59 TEST_VIEW(HoverImageMenuButtonTest, menu_button_); | |
| 60 | |
| 61 // Tests that the correct image is chosen, depending on the cell's state flags. | |
| 62 TEST_F(HoverImageMenuButtonTest, CheckImagesForState) { | |
| 63 SetImages(); | |
| 64 EXPECT_NSNE(normal_, pressed_); | |
|
sail
2013/05/31 19:07:03
I don't this is necessary.
tapted
2013/06/03 12:49:18
Done.
| |
| 65 EXPECT_NSNE(pressed_, hovered_); | |
| 66 EXPECT_NSNE(hovered_, normal_); | |
| 67 | |
| 68 EXPECT_FALSE([[menu_button_ cell] isHovered]); | |
| 69 EXPECT_FALSE([[menu_button_ cell] isHighlighted]); | |
| 70 EXPECT_NSEQ(normal_, [[menu_button_ cell] imageToDraw]); | |
| 71 [menu_button_ display]; | |
| 72 | |
| 73 [[menu_button_ cell] setHovered:YES]; | |
| 74 EXPECT_TRUE([[menu_button_ cell] isHovered]); | |
| 75 EXPECT_FALSE([[menu_button_ cell] isHighlighted]); | |
| 76 EXPECT_NSEQ(hovered_, [[menu_button_ cell] imageToDraw]); | |
| 77 [menu_button_ display]; | |
| 78 | |
| 79 // Highlighted takes precendece over hover. | |
| 80 [[menu_button_ cell] setHighlighted:YES]; | |
| 81 EXPECT_TRUE([[menu_button_ cell] isHovered]); | |
| 82 EXPECT_TRUE([[menu_button_ cell] isHighlighted]); | |
| 83 EXPECT_NSEQ(pressed_, [[menu_button_ cell] imageToDraw]); | |
| 84 [menu_button_ display]; | |
| 85 | |
| 86 [[menu_button_ cell] setHovered:NO]; | |
| 87 EXPECT_FALSE([[menu_button_ cell] isHovered]); | |
| 88 EXPECT_TRUE([[menu_button_ cell] isHighlighted]); | |
| 89 EXPECT_NSEQ(pressed_, [[menu_button_ cell] imageToDraw]); | |
| 90 [menu_button_ display]; | |
| 91 | |
| 92 [[menu_button_ cell] setHighlighted:NO]; | |
| 93 EXPECT_FALSE([[menu_button_ cell] isHovered]); | |
| 94 EXPECT_FALSE([[menu_button_ cell] isHighlighted]); | |
| 95 EXPECT_NSEQ(normal_, [[menu_button_ cell] imageToDraw]); | |
| 96 [menu_button_ display]; | |
| 97 } | |
| 98 | |
| 99 // Tests that calling the various setXImage functions calls setNeedsDisplay. | |
| 100 TEST_F(HoverImageMenuButtonTest, NewImageCausesDisplay) { | |
| 101 [menu_button_ display]; | |
| 102 EXPECT_FALSE([menu_button_ needsDisplay]); | |
| 103 | |
| 104 // Note that regular setImage is overridden to ensure the image goes into the | |
| 105 // NSPopUpButtonCell's menuItem. | |
| 106 [[menu_button_ cell] setImage:ResetImage(kIdrNormal, &normal_)]; | |
| 107 EXPECT_TRUE([menu_button_ needsDisplay]); | |
| 108 [menu_button_ display]; | |
| 109 EXPECT_FALSE([menu_button_ needsDisplay]); | |
| 110 | |
| 111 // setAlternateImage comes from NSButtonCell. | |
| 112 [[menu_button_ cell] setAlternateImage:ResetImage(kIdrPressed, &pressed_)]; | |
| 113 EXPECT_TRUE([menu_button_ needsDisplay]); | |
| 114 [menu_button_ display]; | |
| 115 EXPECT_FALSE([menu_button_ needsDisplay]); | |
| 116 | |
| 117 // setHoverImage comes directly from storage in HoverImageMenuButtonCell. | |
| 118 [[menu_button_ cell] setHoverImage:ResetImage(kIdrHovered, &hovered_)]; | |
| 119 EXPECT_TRUE([menu_button_ needsDisplay]); | |
| 120 [menu_button_ display]; | |
| 121 EXPECT_FALSE([menu_button_ needsDisplay]); | |
| 122 } | |
| 123 | |
| 124 // Test that the mouse enter and exit is properly handled, to set hover state. | |
| 125 TEST_F(HoverImageMenuButtonTest, SimulateMouseEnterExit) { | |
| 126 SetImages(); | |
| 127 EXPECT_NSNE(normal_, hovered_); | |
| 128 [menu_button_ display]; | |
| 129 EXPECT_FALSE([menu_button_ needsDisplay]); | |
| 130 EXPECT_NSEQ(normal_, [[menu_button_ cell] imageToDraw]); | |
| 131 | |
| 132 [menu_button_ mouseEntered:nil]; | |
| 133 EXPECT_TRUE([menu_button_ needsDisplay]); | |
| 134 EXPECT_NSEQ(hovered_, [[menu_button_ cell] imageToDraw]); | |
| 135 [menu_button_ display]; | |
| 136 EXPECT_FALSE([menu_button_ needsDisplay]); | |
| 137 | |
| 138 [menu_button_ mouseExited:nil]; | |
| 139 EXPECT_TRUE([menu_button_ needsDisplay]); | |
| 140 EXPECT_NSEQ(normal_, [[menu_button_ cell] imageToDraw]); | |
| 141 [menu_button_ display]; | |
| 142 EXPECT_FALSE([menu_button_ needsDisplay]); | |
| 143 } | |
| 144 | |
| 145 } // namespace ui | |
| OLD | NEW |