aboutsummaryrefslogtreecommitdiff
path: root/AppKit/GTMNSBezierPath+RoundRectTest.m
blob: 61bd2dd8e6f54e41b53bec0f27e48cd29dc76999 (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
//
//  NSBezierPath+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 <SenTestingKit/SenTestingKit.h>
#import "GTMNSBezierPath+RoundRect.h"
#import "GTMNSView+UnitTesting.h"

@interface GTMNSBezierPath_RoundRectTest : SenTestCase<GTMUnitTestViewDrawer>
@end

@implementation GTMNSBezierPath_RoundRectTest

- (void)testRoundRects {
  GTMAssertDrawingEqualToFile(self, NSMakeSize(330, 430), @"GTMNSBezierPath+RoundRectTest", nil, nil);
}


// Draws all of our tests so that we can compare this to our stored TIFF file.
- (void)unitTestViewDrawRect:(NSRect)rect contextInfo:(void*)contextInfo{
  NSRect theRects[] = { 
    NSMakeRect(0.0f, 10.0f, 0.0f, 0.0f), //Empty Rect test
    NSMakeRect(50.0f, 10.0f, 30.0f, 30.0f), //Square Test
    NSMakeRect(100.0f, 10.0f, 1.0f, 2.0f), //Small Test
    NSMakeRect(120.0f, 10.0f, 15.0f, 20.0f), //Medium Test
    NSMakeRect(140.0f, 10.0f, 150.0f, 30.0f)  //Large Test
  };
  const unsigned int theRectCount = sizeof(theRects) / sizeof(NSRect);
  
  // Line Width Tests
  float theLineWidths[] = { 0.5f, 50.0f, 2.0f };
  const unsigned int theLineWidthCount = sizeof(theLineWidths) / sizeof(float);
  unsigned int i,j;
  
  for (i = 0; i < theLineWidthCount; ++i) {
    for (j = 0; j < theRectCount; ++j) {
      NSBezierPath *roundRect = [NSBezierPath gtm_bezierPathWithRoundRect:theRects[j] cornerRadius:20.0f];
      [roundRect setLineWidth: theLineWidths[i]];
      [roundRect stroke];
      float newWidth = 35.0f;
      if (i < theLineWidthCount - 1) newWidth += theLineWidths[i + 1] + theLineWidths[i];
      theRects[j].origin.y += newWidth;
    }
  }
  
  // Fill test
  NSColor *theColors[] = { 
    [NSColor colorWithDeviceRed:1.0f green:0.0f blue:0.0f alpha:1.0f], 
    [NSColor colorWithDeviceRed:0.2f green:0.4f blue:0.6f alpha:0.4f]
  };
  const unsigned int theColorCount = sizeof(theColors)/sizeof(NSColor);
  
  for (i = 0; i < theColorCount; ++i) {
    for (j = 0; j < theRectCount; ++j) {
      NSBezierPath *roundRect = [NSBezierPath gtm_bezierPathWithRoundRect:theRects[j] cornerRadius:10.0f];
      [theColors[i] setFill];
      [roundRect fill];
      theRects[j].origin.y += 35.0f;
    }
  }
  
  // Flatness test
  float theFlatness[] = {0.0f, 0.1f, 1.0f, 10.0f};
  const unsigned int theFlatnessCount = sizeof(theFlatness)/sizeof(float);
  
  for (i = 0; i < theFlatnessCount; i++) {
    for (j = 0; j < theRectCount; ++j) {
      NSBezierPath *roundRect = [NSBezierPath gtm_bezierPathWithRoundRect:theRects[j] cornerRadius:6.0f];
      [roundRect setFlatness:theFlatness[i]];
      [roundRect stroke];
      theRects[j].origin.y += 35.0f;
    }
  }  
}


@end