Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(257)

Unified Diff: ui/base/cocoa/controls/hover_image_menu_button_unittest.mm

Issue 15955003: Menu for the OSX app launcher. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Refactor HoverImageMenuButton into /ui/base/cocoa/controls Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/base/cocoa/controls/hover_image_menu_button_unittest.mm
diff --git a/ui/base/cocoa/controls/hover_image_menu_button_unittest.mm b/ui/base/cocoa/controls/hover_image_menu_button_unittest.mm
new file mode 100644
index 0000000000000000000000000000000000000000..37ecde06353e6690d634f664dd8d53b187cdc5a8
--- /dev/null
+++ b/ui/base/cocoa/controls/hover_image_menu_button_unittest.mm
@@ -0,0 +1,145 @@
+// 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 "grit/ui_resources.h"
+#import "testing/gtest_mac.h"
+#include "ui/base/resource/resource_bundle.h"
+#import "ui/base/test/ui_cocoa_test_helper.h"
+
+namespace ui {
+
+namespace {
+
+const int kIdrNormal = IDR_CHECKBOX;
+const int kIdrPressed = IDR_CHECKBOX_PRESSED;
+const int kIdrHovered = IDR_CHECKBOX_HOVER;
+
+// 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() {
+ 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.
+ }
+
+ NSImage* ResetImage(int resource_id, scoped_nsobject<NSImage>* storage) {
+ storage->reset([ResourceBundle::GetSharedInstance().
+ GetNativeImageNamed(resource_id).AsNSImage() retain]);
+ return storage->get();
+ }
+
+ void SetImages() {
+ [[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.
+ [[menu_button_ cell] setAlternateImage:ResetImage(kIdrPressed, &pressed_)];
+ [[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.
+ }
+
+ virtual void SetUp() OVERRIDE {
+ menu_button_.reset(
+ [[HoverImageMenuButton alloc] initWithFrame:NSMakeRect(0, 0, 50, 30)
+ pullsDown:YES]);
+ CocoaTest::SetUp();
+ [[test_window() contentView] addSubview:menu_button_];
+ }
+
+ protected:
+ scoped_nsobject<HoverImageMenuButton> menu_button_;
+ 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.
+ scoped_nsobject<NSImage> pressed_;
+ scoped_nsobject<NSImage> hovered_;
+
+ DISALLOW_COPY_AND_ASSIGN(HoverImageMenuButtonTest);
+};
+
+} // namespace
+
+TEST_VIEW(HoverImageMenuButtonTest, menu_button_);
+
+// Tests that the correct image is chosen, depending on the cell's state flags.
+TEST_F(HoverImageMenuButtonTest, CheckImagesForState) {
+ SetImages();
+ 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.
+ EXPECT_NSNE(pressed_, hovered_);
+ EXPECT_NSNE(hovered_, normal_);
+
+ 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]);
+
+ // Note that regular setImage is overridden to ensure the image goes into the
+ // NSPopUpButtonCell's menuItem.
+ [[menu_button_ cell] setImage:ResetImage(kIdrNormal, &normal_)];
+ EXPECT_TRUE([menu_button_ needsDisplay]);
+ [menu_button_ display];
+ EXPECT_FALSE([menu_button_ needsDisplay]);
+
+ // setAlternateImage comes from NSButtonCell.
+ [[menu_button_ cell] setAlternateImage:ResetImage(kIdrPressed, &pressed_)];
+ EXPECT_TRUE([menu_button_ needsDisplay]);
+ [menu_button_ display];
+ EXPECT_FALSE([menu_button_ needsDisplay]);
+
+ // setHoverImage comes directly from storage in HoverImageMenuButtonCell.
+ [[menu_button_ cell] setHoverImage:ResetImage(kIdrHovered, &hovered_)];
+ EXPECT_TRUE([menu_button_ needsDisplay]);
+ [menu_button_ display];
+ EXPECT_FALSE([menu_button_ needsDisplay]);
+}
+
+// Test that the mouse enter and exit is properly handled, to set hover state.
+TEST_F(HoverImageMenuButtonTest, SimulateMouseEnterExit) {
+ SetImages();
+ EXPECT_NSNE(normal_, hovered_);
+ [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

Powered by Google App Engine
This is Rietveld 408576698