1// Copyright (c) 2010 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 "chrome/browser/ui/cocoa/cocoa_test_helper.h" 6#include "chrome/browser/ui/cocoa/image_utils.h" 7#include "testing/gtest/include/gtest/gtest.h" 8 9@interface ImageUtilsTestView : NSView { 10 @private 11 // Determine whether the view is flipped. 12 BOOL isFlipped_; 13 14 // Determines whether to draw using the new method with 15 // |neverFlipped:|. 16 BOOL useNeverFlipped_; 17 18 // Passed to |neverFlipped:| when drawing |image_|. 19 BOOL neverFlipped_; 20 21 scoped_nsobject<NSImage> image_; 22} 23@property(assign, nonatomic) BOOL isFlipped; 24@property(assign, nonatomic) BOOL useNeverFlipped; 25@property(assign, nonatomic) BOOL neverFlipped; 26@end 27 28@implementation ImageUtilsTestView 29@synthesize isFlipped = isFlipped_; 30@synthesize useNeverFlipped = useNeverFlipped_; 31@synthesize neverFlipped = neverFlipped_; 32 33- (id)initWithFrame:(NSRect)rect { 34 self = [super initWithFrame:rect]; 35 if (self) { 36 rect = NSInsetRect(rect, 5.0, 5.0); 37 rect.origin = NSZeroPoint; 38 const NSSize imageSize = NSInsetRect(rect, 5.0, 5.0).size; 39 image_.reset([[NSImage alloc] initWithSize:imageSize]); 40 41 NSBezierPath* path = [NSBezierPath bezierPath]; 42 [path moveToPoint:NSMakePoint(NSMinX(rect), NSMinY(rect))]; 43 [path lineToPoint:NSMakePoint(NSMinX(rect), NSMaxY(rect))]; 44 [path lineToPoint:NSMakePoint(NSMaxX(rect), NSMinY(rect))]; 45 [path closePath]; 46 47 [image_ lockFocus]; 48 [[NSColor blueColor] setFill]; 49 [path fill]; 50 [image_ unlockFocus]; 51 } 52 return self; 53} 54 55- (void)drawRect:(NSRect)rect { 56 NSBezierPath* path = [NSBezierPath bezierPath]; 57 [path moveToPoint:NSMakePoint(NSMinX(rect), NSMinY(rect))]; 58 [path lineToPoint:NSMakePoint(NSMinX(rect), NSMaxY(rect))]; 59 [path lineToPoint:NSMakePoint(NSMaxX(rect), NSMinY(rect))]; 60 [path closePath]; 61 62 [[NSColor redColor] setFill]; 63 [path fill]; 64 65 rect = NSInsetRect(rect, 5.0, 5.0); 66 rect = NSOffsetRect(rect, 2.0, 2.0); 67 68 if (useNeverFlipped_) { 69 [image_ drawInRect:rect 70 fromRect:NSZeroRect 71 operation:NSCompositeCopy 72 fraction:1.0 73 neverFlipped:neverFlipped_]; 74 } else { 75 [image_ drawInRect:rect 76 fromRect:NSZeroRect 77 operation:NSCompositeCopy 78 fraction:1.0]; 79 } 80} 81 82@end 83 84namespace { 85 86class ImageUtilTest : public CocoaTest { 87 public: 88 ImageUtilTest() { 89 const NSRect frame = NSMakeRect(0, 0, 300, 100); 90 scoped_nsobject<ImageUtilsTestView> view( 91 [[ImageUtilsTestView alloc] initWithFrame: frame]); 92 view_ = view.get(); 93 [[test_window() contentView] addSubview:view_]; 94 } 95 96 NSData* SnapshotView() { 97 [view_ display]; 98 99 const NSRect bounds = [view_ bounds]; 100 101 [view_ lockFocus]; 102 scoped_nsobject<NSBitmapImageRep> bitmap( 103 [[NSBitmapImageRep alloc] initWithFocusedViewRect:bounds]); 104 [view_ unlockFocus]; 105 106 return [bitmap TIFFRepresentation]; 107 } 108 109 NSData* SnapshotViewBase() { 110 [view_ setUseNeverFlipped:NO]; 111 return SnapshotView(); 112 } 113 114 NSData* SnapshotViewNeverFlipped(BOOL neverFlipped) { 115 [view_ setUseNeverFlipped:YES]; 116 [view_ setNeverFlipped:neverFlipped]; 117 return SnapshotView(); 118 } 119 120 ImageUtilsTestView* view_; 121}; 122 123TEST_F(ImageUtilTest, Test) { 124 // When not flipped, both drawing methods return the same data. 125 [view_ setIsFlipped:NO]; 126 NSData* baseSnapshotData = SnapshotViewBase(); 127 EXPECT_TRUE([baseSnapshotData isEqualToData:SnapshotViewNeverFlipped(YES)]); 128 EXPECT_TRUE([baseSnapshotData isEqualToData:SnapshotViewNeverFlipped(NO)]); 129 130 // When flipped, there's only a difference when the context flip is 131 // not being respected. 132 [view_ setIsFlipped:YES]; 133 baseSnapshotData = SnapshotViewBase(); 134 EXPECT_FALSE([baseSnapshotData isEqualToData:SnapshotViewNeverFlipped(YES)]); 135 EXPECT_TRUE([baseSnapshotData isEqualToData:SnapshotViewNeverFlipped(NO)]); 136} 137 138} // namespace 139