Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / autofill / autofill_section_view_unittest.mm
blob13486b4125f1eed03624a8e3308aa5de580cd556
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"
16 namespace {
18 class AutofillSectionViewTest : public ui::CocoaTest {
19  public:
20   void SetUp() override {
21     CocoaTest::SetUp();
22     view_.reset(
23         [[AutofillSectionView alloc] initWithFrame:NSMakeRect(0, 0, 20, 20)]);
24     [[test_window() contentView] addSubview:view_];
25   }
27   NSEvent* fakeEvent(NSEventType type) {
28     return [NSEvent enterExitEventWithType:type
29                               location:NSZeroPoint
30                          modifierFlags:0
31                              timestamp:0
32                           windowNumber:[[view_ window] windowNumber]
33                                context:[NSGraphicsContext currentContext]
34                            eventNumber:0
35                         trackingNumber:0
36                               userData:nil];
37   }
39   void ResetDrawingContext(NSColor* fillColor) {
40     NSRect bounds = [view_ bounds];
41     if (!bitmap_) {
42       bitmap_.reset(
43           [[NSBitmapImageRep alloc]
44               initWithBitmapDataPlanes: NULL
45                             pixelsWide: NSWidth(bounds)
46                             pixelsHigh: NSHeight(bounds)
47                          bitsPerSample: 8
48                        samplesPerPixel: 4
49                               hasAlpha: YES
50                               isPlanar: NO
51                         colorSpaceName: NSCalibratedRGBColorSpace
52                            bytesPerRow: NSWidth(bounds) * 4
53                           bitsPerPixel: 32]);
54     }
56     if (!saved_context_) {
57       saved_context_.reset(new gfx::ScopedNSGraphicsContextSaveGState);
58       context_ = [NSGraphicsContext graphicsContextWithBitmapImageRep:bitmap_];
59       [NSGraphicsContext setCurrentContext:context_];
60     }
62     [fillColor set];
63     NSRectFill(bounds);
64   }
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]);
73       }
74     }
75   }
77  protected:
78   base::scoped_nsobject<AutofillSectionView> view_;
79   base::scoped_nsobject<NSBitmapImageRep> bitmap_;
80   scoped_ptr<gfx::ScopedNSGraphicsContextSaveGState> saved_context_;
81   NSGraphicsContext* context_;
84 }  // namespace
86 // A simple delegate to intercept and register performClick: invocation.
87 @interface AutofillClickTestDelegate : NSControl {
88  @private
89   BOOL didFire_;
91 @property(readonly, nonatomic) BOOL didFire;
92 @end
95 @implementation AutofillClickTestDelegate
97 @synthesize didFire = didFire_;
99 // Override performClick to record invocation.
100 - (void)performClick:(id)sender {
101   didFire_ = YES;
104 @end
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]);