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
|
// 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 "chrome/browser/ui/cocoa/autofill/autofill_section_view.h"
#include "base/mac/scoped_nsobject.h"
#include "base/memory/scoped_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"
#include "testing/platform_test.h"
#include "ui/events/test/cocoa_test_event_utils.h"
#include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
#import "ui/gfx/test/ui_cocoa_test_helper.h"
namespace {
class AutofillSectionViewTest : public ui::CocoaTest {
public:
void SetUp() override {
CocoaTest::SetUp();
view_.reset(
[[AutofillSectionView alloc] initWithFrame:NSMakeRect(0, 0, 20, 20)]);
[[test_window() contentView] addSubview:view_];
}
NSEvent* fakeEvent(NSEventType type) {
return [NSEvent enterExitEventWithType:type
location:NSZeroPoint
modifierFlags:0
timestamp:0
windowNumber:[[view_ window] windowNumber]
context:[NSGraphicsContext currentContext]
eventNumber:0
trackingNumber:0
userData:nil];
}
void ResetDrawingContext(NSColor* fillColor) {
NSRect bounds = [view_ bounds];
if (!bitmap_) {
bitmap_.reset(
[[NSBitmapImageRep alloc]
initWithBitmapDataPlanes: NULL
pixelsWide: NSWidth(bounds)
pixelsHigh: NSHeight(bounds)
bitsPerSample: 8
samplesPerPixel: 4
hasAlpha: YES
isPlanar: NO
colorSpaceName: NSCalibratedRGBColorSpace
bytesPerRow: NSWidth(bounds) * 4
bitsPerPixel: 32]);
}
if (!saved_context_) {
saved_context_.reset(new gfx::ScopedNSGraphicsContextSaveGState);
context_ = [NSGraphicsContext graphicsContextWithBitmapImageRep:bitmap_];
[NSGraphicsContext setCurrentContext:context_];
}
[fillColor set];
NSRectFill(bounds);
}
void CheckImageIsSolidColor(NSColor* color) {
ASSERT_TRUE(saved_context_);
[context_ flushGraphics];
NSRect bounds = [view_ bounds];
for (NSInteger y = 0; y < NSHeight(bounds); ++y) {
for (NSInteger x = 0; x < NSWidth(bounds); ++x) {
ASSERT_NSEQ(color, [bitmap_ colorAtX:x y:y]);
}
}
}
protected:
base::scoped_nsobject<AutofillSectionView> view_;
base::scoped_nsobject<NSBitmapImageRep> bitmap_;
scoped_ptr<gfx::ScopedNSGraphicsContextSaveGState> saved_context_;
NSGraphicsContext* context_;
};
} // namespace
// A simple delegate to intercept and register performClick: invocation.
@interface AutofillClickTestDelegate : NSControl {
@private
BOOL didFire_;
}
@property(readonly, nonatomic) BOOL didFire;
@end
@implementation AutofillClickTestDelegate
@synthesize didFire = didFire_;
// Override performClick to record invocation.
- (void)performClick:(id)sender {
didFire_ = YES;
}
@end
TEST_VIEW(AutofillSectionViewTest, view_)
TEST_F(AutofillSectionViewTest, HoverColorIsOpaque) {
EXPECT_EQ(1.0, [[view_ hoverColor] alphaComponent]);
}
TEST_F(AutofillSectionViewTest, EventsCauseHighlighting) {
EXPECT_FALSE([view_ isHighlighted]);
[view_ mouseEntered:fakeEvent(NSMouseEntered)];
EXPECT_TRUE([view_ isHighlighted]);
[view_ mouseExited:fakeEvent(NSMouseExited)];
EXPECT_FALSE([view_ isHighlighted]);
}
TEST_F(AutofillSectionViewTest, HoverHighlighting) {
NSRect bounds = [view_ bounds];
NSColor* fillColor = [NSColor blueColor];
EXPECT_NSNE(fillColor, [view_ hoverColor]);
[view_ setShouldHighlightOnHover:YES];
// Adjust hoverColor for render quantization effects.
ResetDrawingContext([view_ hoverColor]);
[context_ flushGraphics];
NSColor* quantizedHoverColor = [bitmap_ colorAtX:0 y:0];
// Test a highlighted view has the right color.
ResetDrawingContext(fillColor);
[view_ mouseEntered:fakeEvent(NSMouseEntered)];
[view_ drawRect:bounds];
CheckImageIsSolidColor(quantizedHoverColor);
// Test a non-highlighted view doesn't change existing background.
ResetDrawingContext(fillColor);
[view_ mouseEntered:fakeEvent(NSMouseExited)];
[view_ drawRect:bounds];
CheckImageIsSolidColor(fillColor);
// Test there is no higlight if highlight mode is off.
[view_ setShouldHighlightOnHover:NO];
ResetDrawingContext(fillColor);
[view_ mouseEntered:fakeEvent(NSMouseEntered)];
[view_ drawRect:bounds];
CheckImageIsSolidColor(fillColor);
}
TEST_F(AutofillSectionViewTest, MouseClicksAreForwarded) {
base::scoped_nsobject<AutofillClickTestDelegate> delegate(
[[AutofillClickTestDelegate alloc] init]);
[view_ setClickTarget:delegate];
NSEvent* down_event =
cocoa_test_event_utils::LeftMouseDownAtPoint(NSMakePoint(5, 5));
ASSERT_FALSE([delegate didFire]);
[test_window() sendEvent:down_event];
EXPECT_TRUE([delegate didFire]);
}
|