/core/externals/update-engine/externals/google-toolbox-for-mac/AppKit/GTMLinearRGBShadingTest.m

http://macfuse.googlecode.com/ · Objective C · 132 lines · 100 code · 8 blank · 24 comment · 3 complexity · 82e7027d55784b1db1d76b8680ea6a34 MD5 · raw file

  1. //
  2. // GTMLinearRGBShadingTest.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 <SenTestingKit/SenTestingKit.h>
  19. #import "GTMSenTestCase.h"
  20. #import "GTMLinearRGBShading.h"
  21. @interface GTMLinearRGBShadingTest : GTMTestCase
  22. @end
  23. @implementation GTMLinearRGBShadingTest
  24. - (void)testShadingFrom {
  25. // Create a shading from red to blue, and check if 50% is purple
  26. NSColor *red = [NSColor redColor];
  27. NSColor *blue = [NSColor blueColor];
  28. NSColor *purple = [NSColor purpleColor];
  29. GTMLinearRGBShading *theShading =
  30. [GTMLinearRGBShading shadingFromColor:red
  31. toColor:blue
  32. fromSpaceNamed:NSCalibratedRGBColorSpace];
  33. STAssertNotNil(theShading,nil);
  34. STAssertEquals([theShading stopCount], (NSUInteger)2, nil);
  35. CGFloat *theColor = (CGFloat*)[theShading valueAtPosition: 0.5];
  36. STAssertEqualsWithAccuracy(theColor[0], [purple redComponent], 0.001, nil);
  37. STAssertEqualsWithAccuracy(theColor[1], [purple greenComponent], 0.001, nil);
  38. STAssertEqualsWithAccuracy(theColor[2], [purple blueComponent], 0.001, nil);
  39. STAssertEqualsWithAccuracy(theColor[3], [purple alphaComponent], 0.001, nil);
  40. }
  41. - (void)testShadingWith {
  42. // Create a shading with kColorCount colors and make sure all the values are there.
  43. enum { kColorCount = 100 };
  44. NSColor *theColors[kColorCount];
  45. CGFloat thePositions[kColorCount];
  46. const CGFloat kColorIncrement = 1.0 / kColorCount;
  47. for (NSUInteger i = 0; i < kColorCount; i++) {
  48. CGFloat newValue = kColorIncrement * i;
  49. thePositions[i] = newValue;
  50. theColors[i] = [NSColor colorWithCalibratedRed:newValue
  51. green:newValue
  52. blue:newValue
  53. alpha:newValue];
  54. }
  55. GTMLinearRGBShading *theShading =
  56. [GTMLinearRGBShading shadingWithColors:theColors
  57. fromSpaceNamed:NSCalibratedRGBColorSpace
  58. atPositions:thePositions
  59. count:kColorCount];
  60. for (NSUInteger i = 0; i < kColorCount; i++) {
  61. CGFloat newValue = kColorIncrement * i;
  62. CGFloat *theColor = (CGFloat*)[theShading valueAtPosition:newValue];
  63. STAssertEqualsWithAccuracy(theColor[0], newValue, 0.001, nil);
  64. STAssertEqualsWithAccuracy(theColor[1], newValue, 0.001, nil);
  65. STAssertEqualsWithAccuracy(theColor[2], newValue, 0.001, nil);
  66. STAssertEqualsWithAccuracy(theColor[3], newValue, 0.001, nil);
  67. }
  68. // Create a shading with 1 color to test that special handling
  69. NSColor *purple = [NSColor purpleColor];
  70. NSColor *singleColor[1] = { purple };
  71. CGFloat singlePosition[1] = { 0.5 };
  72. theShading =
  73. [GTMLinearRGBShading shadingWithColors:singleColor
  74. fromSpaceNamed:NSCalibratedRGBColorSpace
  75. atPositions:singlePosition
  76. count:1];
  77. // test over a range to make sure we always get the same color
  78. for (NSUInteger i = 0; i < kColorCount; i++) {
  79. CGFloat newValue = kColorIncrement * i;
  80. CGFloat *theColor = (CGFloat*)[theShading valueAtPosition:newValue];
  81. STAssertEqualsWithAccuracy(theColor[0], [purple redComponent], 0.001, nil);
  82. STAssertEqualsWithAccuracy(theColor[1], [purple greenComponent], 0.001, nil);
  83. STAssertEqualsWithAccuracy(theColor[2], [purple blueComponent], 0.001, nil);
  84. STAssertEqualsWithAccuracy(theColor[3], [purple alphaComponent], 0.001, nil);
  85. }
  86. }
  87. - (void)testShadeFunction {
  88. GTMLinearRGBShading *theShading =
  89. [GTMLinearRGBShading shadingWithColors:nil
  90. fromSpaceNamed:NSCalibratedRGBColorSpace
  91. atPositions:nil
  92. count:0];
  93. CGFunctionRef theFunction = [theShading shadeFunction];
  94. STAssertNotNULL(theFunction, nil);
  95. STAssertEquals(CFGetTypeID(theFunction), CGFunctionGetTypeID(), nil);
  96. }
  97. - (void)testColorSpace {
  98. // Calibrated RGB
  99. GTMLinearRGBShading *theShading =
  100. [GTMLinearRGBShading shadingWithColors:nil
  101. fromSpaceNamed:NSCalibratedRGBColorSpace
  102. atPositions:nil
  103. count:0];
  104. CGColorSpaceRef theColorSpace = [theShading colorSpace];
  105. STAssertNotNULL(theColorSpace, nil);
  106. STAssertEquals(CFGetTypeID(theColorSpace), CGColorSpaceGetTypeID(), nil);
  107. // Device RGB
  108. theShading =
  109. [GTMLinearRGBShading shadingWithColors:nil
  110. fromSpaceNamed:NSDeviceRGBColorSpace
  111. atPositions:nil
  112. count:0];
  113. theColorSpace = [theShading colorSpace];
  114. STAssertNotNULL(theColorSpace, nil);
  115. STAssertEquals(CFGetTypeID(theColorSpace), CGColorSpaceGetTypeID(), nil);
  116. // Device CMYK (not supported)
  117. theShading =
  118. [GTMLinearRGBShading shadingWithColors:nil
  119. fromSpaceNamed:NSDeviceCMYKColorSpace
  120. atPositions:nil
  121. count:0];
  122. STAssertNULL(theShading, nil);
  123. }
  124. @end