aboutsummaryrefslogtreecommitdiff
path: root/AppKit/GTMNSBezierPath+RoundRectTest.m
blob: 02cd87b0fd850191337cbbfcc1ef6c28faa65d52 (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
//
//  GTMNSBezierPath+RoundRectTest.m
//
//  Copyright 2006-2008 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations under
//  the License.
//

#import <Cocoa/Cocoa.h>

#import "GTMNSBezierPath+RoundRect.h"
#import "GTMSenTestCase.h"

@interface GTMNSBezierPath_RoundRectTest : GTMTestCase
@end

@implementation GTMNSBezierPath_RoundRectTest

- (void)testRoundRects {
  NSBitmapImageRep *offscreenRep = [[[NSBitmapImageRep alloc]
                                     initWithBitmapDataPlanes:NULL
                                                   pixelsWide:100
                                                   pixelsHigh:100
                                                bitsPerSample:8
                                              samplesPerPixel:4
                                                     hasAlpha:YES
                                                     isPlanar:NO
                                               colorSpaceName:NSDeviceRGBColorSpace
                                                 bitmapFormat:NSAlphaFirstBitmapFormat
                                                  bytesPerRow:0
                                                 bitsPerPixel:0] autorelease];

  // set offscreen context
  NSGraphicsContext *nsContext =
      [NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep];
  [NSGraphicsContext setCurrentContext:nsContext];
  NSRect theRects[] = {
    NSMakeRect(0.0, 10.0, 0.0, 0.0), //Empty Rect test
    NSMakeRect(50.0, 10.0, 30.0, 30.0), //Square Test
    NSMakeRect(100.0, 10.0, 1.0, 2.0), //Small Test
    NSMakeRect(120.0, 10.0, 15.0, 20.0), //Medium Test
    NSMakeRect(140.0, 10.0, 150.0, 30.0),  //Large Test
    NSMakeRect(300.0, 10.0, 150.0, 30.0)  //Large Test 2 (for different radius)
  };
  const NSUInteger theRectCount = sizeof(theRects) / sizeof(theRects[0]);

  // Line Width Tests
  CGFloat theLineWidths[] = { 0.5, 50.0, 2.0 };
  const NSUInteger theLineWidthCount =
      sizeof(theLineWidths) / sizeof(theLineWidths[0]);
  NSUInteger i, j;

  for (i = 0; i < theLineWidthCount; ++i) {
    for (j = 0; j < theRectCount; ++j) {
      CGFloat cornerRadius = ( (j < (theRectCount - 1)) ? 20.0 : 0.0 );
      NSBezierPath *roundRect = [NSBezierPath gtm_bezierPathWithRoundRect:theRects[j]
                                                             cornerRadius:cornerRadius];
      [roundRect setLineWidth: theLineWidths[i]];
      [roundRect stroke];
      CGFloat newWidth = 35.0;
      if (i < theLineWidthCount - 1) {
        newWidth += theLineWidths[i + 1] + theLineWidths[i];
      }
      theRects[j].origin.y += newWidth;
    }
  }

  // Fill test
  NSColor *theColors[] = {
    [NSColor colorWithCalibratedRed:1.0 green:0.0 blue:0.0 alpha:1.0],
    [NSColor colorWithCalibratedRed:0.2 green:0.4 blue:0.6 alpha:0.4]
  };
  const NSUInteger theColorCount = sizeof(theColors)/sizeof(theColors[0]);

  for (i = 0; i < theColorCount; ++i) {
    for (j = 0; j < theRectCount; ++j) {
      CGFloat cornerRadius = ( (j < (theRectCount - 1)) ? 10.0 : 0.0 );
      NSBezierPath *roundRect = [NSBezierPath gtm_bezierPathWithRoundRect:theRects[j]
                                                             cornerRadius:cornerRadius];
      [theColors[i] setFill];
      [roundRect fill];
      theRects[j].origin.y += 35.0;
    }
  }

  // Flatness test
  CGFloat theFlatness[] = {0.0, 0.1, 1.0, 10.0};
  const NSUInteger theFlatnessCount = sizeof(theFlatness)/sizeof(theFlatness[0]);

  for (i = 0; i < theFlatnessCount; i++) {
    for (j = 0; j < theRectCount; ++j) {
      CGFloat cornerRadius = ( (j < (theRectCount - 1)) ? 6.0 : 0.0 );
      NSBezierPath *roundRect = [NSBezierPath gtm_bezierPathWithRoundRect:theRects[j]
                                                             cornerRadius:cornerRadius];
      [roundRect setFlatness:theFlatness[i]];
      [roundRect stroke];
      theRects[j].origin.y += 35.0;
    }
  }

  // Different radii
  NSRect bigRect = NSMakeRect(50, 440, 200, 40);
  NSBezierPath *roundRect = [NSBezierPath gtm_bezierPathWithRoundRect:bigRect
                                                  topLeftCornerRadius:0.0
                                                 topRightCornerRadius:5.0
                                               bottomLeftCornerRadius:10.0
                                              bottomRightCornerRadius:20.0];
  [roundRect setLineWidth:5.0];
  [roundRect stroke];
}


@end