summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/image_utils_unittest.mm
blob: 4d97ed181d4d95d200bf9dd533a0b89035334a8f (plain)
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
// Copyright (c) 2010 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/cocoa/cocoa_test_helper.h"
#include "chrome/browser/cocoa/image_utils.h"
#include "testing/gtest/include/gtest/gtest.h"

@interface ImageUtilsTestView : NSView {
 @private
  // Determine whether the view is flipped.
  BOOL isFlipped_;

  // Determines whether to draw using the new method with
  // |neverFlipped:|.
  BOOL useNeverFlipped_;

  // Passed to |neverFlipped:| when drawing |image_|.
  BOOL neverFlipped_;

  scoped_nsobject<NSImage> image_;
}
@property(assign, nonatomic) BOOL isFlipped;
@property(assign, nonatomic) BOOL useNeverFlipped;
@property(assign, nonatomic) BOOL neverFlipped;
@end

@implementation ImageUtilsTestView
@synthesize isFlipped = isFlipped_;
@synthesize useNeverFlipped = useNeverFlipped_;
@synthesize neverFlipped = neverFlipped_;

- (id)initWithFrame:(NSRect)rect {
  self = [super initWithFrame:rect];
  if (self) {
    rect = NSInsetRect(rect, 5.0, 5.0);
    rect.origin = NSZeroPoint;
    const NSSize imageSize = NSInsetRect(rect, 5.0, 5.0).size;
    image_.reset([[NSImage alloc] initWithSize:imageSize]);

    NSBezierPath* path = [NSBezierPath bezierPath];
    [path moveToPoint:NSMakePoint(NSMinX(rect), NSMinY(rect))];
    [path lineToPoint:NSMakePoint(NSMinX(rect), NSMaxY(rect))];
    [path lineToPoint:NSMakePoint(NSMaxX(rect), NSMinY(rect))];
    [path closePath];

    [image_ lockFocus];
    [[NSColor blueColor] setFill];
    [path fill];
    [image_ unlockFocus];
  }
  return self;
}

- (void)drawRect:(NSRect)rect {
  NSBezierPath* path = [NSBezierPath bezierPath];
  [path moveToPoint:NSMakePoint(NSMinX(rect), NSMinY(rect))];
  [path lineToPoint:NSMakePoint(NSMinX(rect), NSMaxY(rect))];
  [path lineToPoint:NSMakePoint(NSMaxX(rect), NSMinY(rect))];
  [path closePath];

  [[NSColor redColor] setFill];
  [path fill];

  rect = NSInsetRect(rect, 5.0, 5.0);
  rect = NSOffsetRect(rect, 2.0, 2.0);

  if (useNeverFlipped_) {
    [image_ drawInRect:rect
              fromRect:NSZeroRect
             operation:NSCompositeCopy
              fraction:1.0
          neverFlipped:neverFlipped_];
  } else {
    [image_ drawInRect:rect
              fromRect:NSZeroRect
             operation:NSCompositeCopy
              fraction:1.0];
  }
}

@end

namespace {

class ImageUtilTest : public CocoaTest {
 public:
  ImageUtilTest() {
    const NSRect frame = NSMakeRect(0, 0, 300, 100);
    scoped_nsobject<ImageUtilsTestView> view(
        [[ImageUtilsTestView alloc] initWithFrame: frame]);
    view_ = view.get();
    [[test_window() contentView] addSubview:view_];
  }

  NSData* SnapshotView() {
    [view_ display];

    const NSRect bounds = [view_ bounds];

    [view_ lockFocus];
    scoped_nsobject<NSBitmapImageRep> bitmap(
        [[NSBitmapImageRep alloc] initWithFocusedViewRect:bounds]);
    [view_ unlockFocus];

    return [bitmap TIFFRepresentation];
  }

  NSData* SnapshotViewBase() {
    [view_ setUseNeverFlipped:NO];
    return SnapshotView();
  }

  NSData* SnapshotViewNeverFlipped(BOOL neverFlipped) {
    [view_ setUseNeverFlipped:YES];
    [view_ setNeverFlipped:neverFlipped];
    return SnapshotView();
  }

  ImageUtilsTestView* view_;
};

TEST_F(ImageUtilTest, Test) {
  // When not flipped, both drawing methods return the same data.
  [view_ setIsFlipped:NO];
  NSData* baseSnapshotData = SnapshotViewBase();
  EXPECT_TRUE([baseSnapshotData isEqualToData:SnapshotViewNeverFlipped(YES)]);
  EXPECT_TRUE([baseSnapshotData isEqualToData:SnapshotViewNeverFlipped(NO)]);

  // When flipped, there's only a difference when the context flip is
  // not being respected.
  [view_ setIsFlipped:YES];
  baseSnapshotData = SnapshotViewBase();
  EXPECT_FALSE([baseSnapshotData isEqualToData:SnapshotViewNeverFlipped(YES)]);
  EXPECT_TRUE([baseSnapshotData isEqualToData:SnapshotViewNeverFlipped(NO)]);
}

}  // namespace