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.
5 #import "chrome/browser/ui/cocoa/autofill/autofill_section_view.h"
7 #include "base/mac/scoped_nsobject.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/gtest_mac.h"
11 #include "testing/platform_test.h"
12 #include "ui/events/test/cocoa_test_event_utils.h"
13 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
14 #import "ui/gfx/test/ui_cocoa_test_helper.h"
18 class AutofillSectionViewTest : public ui::CocoaTest {
20 void SetUp() override {
23 [[AutofillSectionView alloc] initWithFrame:NSMakeRect(0, 0, 20, 20)]);
24 [[test_window() contentView] addSubview:view_];
27 NSEvent* fakeEvent(NSEventType type) {
28 return [NSEvent enterExitEventWithType:type
32 windowNumber:[[view_ window] windowNumber]
33 context:[NSGraphicsContext currentContext]
39 void ResetDrawingContext(NSColor* fillColor) {
40 NSRect bounds = [view_ bounds];
43 [[NSBitmapImageRep alloc]
44 initWithBitmapDataPlanes: NULL
45 pixelsWide: NSWidth(bounds)
46 pixelsHigh: NSHeight(bounds)
51 colorSpaceName: NSCalibratedRGBColorSpace
52 bytesPerRow: NSWidth(bounds) * 4
56 if (!saved_context_) {
57 saved_context_.reset(new gfx::ScopedNSGraphicsContextSaveGState);
58 context_ = [NSGraphicsContext graphicsContextWithBitmapImageRep:bitmap_];
59 [NSGraphicsContext setCurrentContext:context_];
66 void CheckImageIsSolidColor(NSColor* color) {
67 ASSERT_TRUE(saved_context_);
68 [context_ flushGraphics];
69 NSRect bounds = [view_ bounds];
70 for (NSInteger y = 0; y < NSHeight(bounds); ++y) {
71 for (NSInteger x = 0; x < NSWidth(bounds); ++x) {
72 ASSERT_NSEQ(color, [bitmap_ colorAtX:x y:y]);
78 base::scoped_nsobject<AutofillSectionView> view_;
79 base::scoped_nsobject<NSBitmapImageRep> bitmap_;
80 scoped_ptr<gfx::ScopedNSGraphicsContextSaveGState> saved_context_;
81 NSGraphicsContext* context_;
86 // A simple delegate to intercept and register performClick: invocation.
87 @interface AutofillClickTestDelegate : NSControl {
91 @property(readonly, nonatomic) BOOL didFire;
95 @implementation AutofillClickTestDelegate
97 @synthesize didFire = didFire_;
99 // Override performClick to record invocation.
100 - (void)performClick:(id)sender {
106 TEST_VIEW(AutofillSectionViewTest, view_)
108 TEST_F(AutofillSectionViewTest, HoverColorIsOpaque) {
109 EXPECT_EQ(1.0, [[view_ hoverColor] alphaComponent]);
112 TEST_F(AutofillSectionViewTest, EventsCauseHighlighting) {
113 EXPECT_FALSE([view_ isHighlighted]);
115 [view_ mouseEntered:fakeEvent(NSMouseEntered)];
116 EXPECT_TRUE([view_ isHighlighted]);
118 [view_ mouseExited:fakeEvent(NSMouseExited)];
119 EXPECT_FALSE([view_ isHighlighted]);
122 TEST_F(AutofillSectionViewTest, HoverHighlighting) {
123 NSRect bounds = [view_ bounds];
124 NSColor* fillColor = [NSColor blueColor];
125 EXPECT_NSNE(fillColor, [view_ hoverColor]);
127 [view_ setShouldHighlightOnHover:YES];
129 // Adjust hoverColor for render quantization effects.
130 ResetDrawingContext([view_ hoverColor]);
131 [context_ flushGraphics];
132 NSColor* quantizedHoverColor = [bitmap_ colorAtX:0 y:0];
134 // Test a highlighted view has the right color.
135 ResetDrawingContext(fillColor);
136 [view_ mouseEntered:fakeEvent(NSMouseEntered)];
137 [view_ drawRect:bounds];
138 CheckImageIsSolidColor(quantizedHoverColor);
140 // Test a non-highlighted view doesn't change existing background.
141 ResetDrawingContext(fillColor);
142 [view_ mouseEntered:fakeEvent(NSMouseExited)];
143 [view_ drawRect:bounds];
144 CheckImageIsSolidColor(fillColor);
146 // Test there is no higlight if highlight mode is off.
147 [view_ setShouldHighlightOnHover:NO];
148 ResetDrawingContext(fillColor);
149 [view_ mouseEntered:fakeEvent(NSMouseEntered)];
150 [view_ drawRect:bounds];
151 CheckImageIsSolidColor(fillColor);
154 TEST_F(AutofillSectionViewTest, MouseClicksAreForwarded) {
155 base::scoped_nsobject<AutofillClickTestDelegate> delegate(
156 [[AutofillClickTestDelegate alloc] init]);
157 [view_ setClickTarget:delegate];
159 NSEvent* down_event =
160 cocoa_test_event_utils::LeftMouseDownAtPoint(NSMakePoint(5, 5));
161 ASSERT_FALSE([delegate didFire]);
162 [test_window() sendEvent:down_event];
163 EXPECT_TRUE([delegate didFire]);