/core/externals/update-engine/externals/google-toolbox-for-mac/UnitTesting/GTMCALayer+UnitTesting.m

http://macfuse.googlecode.com/ · Objective C · 89 lines · 39 code · 8 blank · 42 comment · 3 complexity · 3ceaea2f8e6de6f2094a902fb193762d MD5 · raw file

  1. //
  2. // GTMCALayer+UnitTesting.m
  3. //
  4. // Category for making unit testing of graphics/UI easier.
  5. // Allows you to save a view out to a image file, and compare a view
  6. // with a previously stored representation to make sure it hasn't changed.
  7. //
  8. // Copyright 2006-2008 Google Inc.
  9. //
  10. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  11. // use this file except in compliance with the License. You may obtain a copy
  12. // of the License at
  13. //
  14. // http://www.apache.org/licenses/LICENSE-2.0
  15. //
  16. // Unless required by applicable law or agreed to in writing, software
  17. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  19. // License for the specific language governing permissions and limitations under
  20. // the License.
  21. //
  22. #import "GTMCALayer+UnitTesting.h"
  23. @implementation CALayer (GTMUnitTestingAdditions)
  24. // Returns an image containing a representation of the object
  25. // suitable for use in comparing against a master image.
  26. // NB this means that all colors should be from "NSDevice" color space
  27. // Does all of it's drawing with smoothfonts and antialiasing off
  28. // to avoid issues with font smoothing settings and antialias differences
  29. // between ppc and x86.
  30. //
  31. // Returns:
  32. // an image of the object
  33. - (CGImageRef)gtm_unitTestImage {
  34. CGRect bounds = [self bounds];
  35. CGSize size = CGSizeMake(CGRectGetWidth(bounds), CGRectGetHeight(bounds));
  36. CGContextRef context = GTMCreateUnitTestBitmapContextOfSizeWithData(size,
  37. NULL);
  38. _GTMDevAssert(context, @"Couldn't create context");
  39. // iPhone renders are flipped
  40. CGAffineTransform transform = CGAffineTransformMakeTranslation(0, size.height);
  41. transform = CGAffineTransformScale(transform, 1.0, -1.0);
  42. CGContextConcatCTM(context, transform);
  43. [self renderInContext:context];
  44. CGImageRef image = CGBitmapContextCreateImage(context);
  45. CFRelease(context);
  46. return (CGImageRef)GTMCFAutorelease(image);
  47. }
  48. // Encodes the state of an object in a manner suitable for comparing
  49. // against a master state file so we can determine whether the
  50. // object is in a suitable state.
  51. //
  52. // Arguments:
  53. // inCoder - the coder to encode our state into
  54. - (void)gtm_unitTestEncodeState:(NSCoder*)inCoder {
  55. [super gtm_unitTestEncodeState:inCoder];
  56. [inCoder encodeBool:[self isHidden] forKey:@"LayerIsHidden"];
  57. [inCoder encodeBool:[self isDoubleSided] forKey:@"LayerIsDoublesided"];
  58. [inCoder encodeBool:[self isOpaque] forKey:@"LayerIsOpaque"];
  59. [inCoder encodeFloat:[self opacity] forKey:@"LayerOpacity"];
  60. // TODO: There is a ton more we can add here. What are we interested in?
  61. if ([self gtm_shouldEncodeStateForSublayers]) {
  62. int i = 0;
  63. for (CALayer *subLayer in [self sublayers]) {
  64. [inCoder encodeObject:subLayer
  65. forKey:[NSString stringWithFormat:@"CALayerSubLayer %d", i]];
  66. i = i + 1;
  67. }
  68. }
  69. }
  70. // Returns whether gtm_unitTestEncodeState should recurse into sublayers
  71. //
  72. // Returns:
  73. // should gtm_unitTestEncodeState pick up sublayer state.
  74. - (BOOL)gtm_shouldEncodeStateForSublayers {
  75. BOOL value = YES;
  76. if([self.delegate respondsToSelector:@selector(gtm_shouldEncodeStateForSublayersOfLayer:)]) {
  77. value = [self.delegate gtm_shouldEncodeStateForSublayersOfLayer:self];
  78. }
  79. return value;
  80. }
  81. @end