/core/externals/update-engine/externals/google-toolbox-for-mac/AppKit/GTMNSBezierPath+RoundRectTest.m

http://macfuse.googlecode.com/ · Objective C · 112 lines · 75 code · 15 blank · 22 comment · 7 complexity · 274e5beae861428edbf2756d1d6eb7dd MD5 · raw file

  1. //
  2. // GTMNSBezierPath+RoundRectTest.m
  3. //
  4. // Copyright 2006-2008 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations under
  16. // the License.
  17. //
  18. #import <Cocoa/Cocoa.h>
  19. #import <SenTestingKit/SenTestingKit.h>
  20. #import "GTMNSBezierPath+RoundRect.h"
  21. #import "GTMAppKit+UnitTesting.h"
  22. @interface GTMNSBezierPath_RoundRectTest : GTMTestCase<GTMUnitTestViewDrawer>
  23. @end
  24. @implementation GTMNSBezierPath_RoundRectTest
  25. - (void)testRoundRects {
  26. GTMAssertDrawingEqualToImageNamed(self, NSMakeSize(490, 500),
  27. @"GTMNSBezierPath+RoundRectTest", nil, nil);
  28. }
  29. // Draws all of our tests so that we can compare this to our stored TIFF file.
  30. - (void)gtm_unitTestViewDrawRect:(NSRect)rect contextInfo:(void*)contextInfo{
  31. NSRect theRects[] = {
  32. NSMakeRect(0.0, 10.0, 0.0, 0.0), //Empty Rect test
  33. NSMakeRect(50.0, 10.0, 30.0, 30.0), //Square Test
  34. NSMakeRect(100.0, 10.0, 1.0, 2.0), //Small Test
  35. NSMakeRect(120.0, 10.0, 15.0, 20.0), //Medium Test
  36. NSMakeRect(140.0, 10.0, 150.0, 30.0), //Large Test
  37. NSMakeRect(300.0, 10.0, 150.0, 30.0) //Large Test 2 (for different radius)
  38. };
  39. const NSUInteger theRectCount = sizeof(theRects) / sizeof(theRects[0]);
  40. // Line Width Tests
  41. CGFloat theLineWidths[] = { 0.5, 50.0, 2.0 };
  42. const NSUInteger theLineWidthCount = sizeof(theLineWidths) / sizeof(theLineWidths[0]);
  43. NSUInteger i,j;
  44. for (i = 0; i < theLineWidthCount; ++i) {
  45. for (j = 0; j < theRectCount; ++j) {
  46. CGFloat cornerRadius = ( (j < (theRectCount - 1)) ? 20.0 : 0.0 );
  47. NSBezierPath *roundRect = [NSBezierPath gtm_bezierPathWithRoundRect:theRects[j]
  48. cornerRadius:cornerRadius];
  49. [roundRect setLineWidth: theLineWidths[i]];
  50. [roundRect stroke];
  51. CGFloat newWidth = 35.0;
  52. if (i < theLineWidthCount - 1) {
  53. newWidth += theLineWidths[i + 1] + theLineWidths[i];
  54. }
  55. theRects[j].origin.y += newWidth;
  56. }
  57. }
  58. // Fill test
  59. NSColor *theColors[] = {
  60. [NSColor colorWithCalibratedRed:1.0 green:0.0 blue:0.0 alpha:1.0],
  61. [NSColor colorWithCalibratedRed:0.2 green:0.4 blue:0.6 alpha:0.4]
  62. };
  63. const NSUInteger theColorCount = sizeof(theColors)/sizeof(theColors[0]);
  64. for (i = 0; i < theColorCount; ++i) {
  65. for (j = 0; j < theRectCount; ++j) {
  66. CGFloat cornerRadius = ( (j < (theRectCount - 1)) ? 10.0 : 0.0 );
  67. NSBezierPath *roundRect = [NSBezierPath gtm_bezierPathWithRoundRect:theRects[j]
  68. cornerRadius:cornerRadius];
  69. [theColors[i] setFill];
  70. [roundRect fill];
  71. theRects[j].origin.y += 35.0;
  72. }
  73. }
  74. // Flatness test
  75. CGFloat theFlatness[] = {0.0, 0.1, 1.0, 10.0};
  76. const NSUInteger theFlatnessCount = sizeof(theFlatness)/sizeof(theFlatness[0]);
  77. for (i = 0; i < theFlatnessCount; i++) {
  78. for (j = 0; j < theRectCount; ++j) {
  79. CGFloat cornerRadius = ( (j < (theRectCount - 1)) ? 6.0 : 0.0 );
  80. NSBezierPath *roundRect = [NSBezierPath gtm_bezierPathWithRoundRect:theRects[j]
  81. cornerRadius:cornerRadius];
  82. [roundRect setFlatness:theFlatness[i]];
  83. [roundRect stroke];
  84. theRects[j].origin.y += 35.0;
  85. }
  86. }
  87. // Different radii
  88. NSRect bigRect = NSMakeRect(50, 440, 200, 40);
  89. NSBezierPath *roundRect = [NSBezierPath gtm_bezierPathWithRoundRect:bigRect
  90. topLeftCornerRadius:0.0
  91. topRightCornerRadius:5.0
  92. bottomLeftCornerRadius:10.0
  93. bottomRightCornerRadius:20.0];
  94. [roundRect setLineWidth:5.0];
  95. [roundRect stroke];
  96. }
  97. @end