/MapView/GTM/GTMUIKit+UnitTesting.m

http://github.com/route-me/route-me · Objective C · 122 lines · 58 code · 13 blank · 51 comment · 6 complexity · 3c93df6b116d215f0462b43ef14304e2 MD5 · raw file

  1. //
  2. // GTMUIKit+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 "GTMUIKit+UnitTesting.h"
  23. #import "GTMCALayer+UnitTesting.h"
  24. #import "GTMDefines.h"
  25. #if !GTM_IPHONE_SDK
  26. #error This file is for iPhone use only
  27. #endif // GTM_IPHONE_SDK
  28. // A view that allows you to delegate out drawing using the formal
  29. // GTMUnitTestViewDelegate protocol above. This is useful when writing up unit
  30. // tests for visual elements.
  31. // Your test will often end up looking like this:
  32. // - (void)testFoo {
  33. // GTMAssertDrawingEqualToFile(self, CGSizeMake(200, 200), @"Foo", nil, nil);
  34. // }
  35. // and your testSuite will also implement the unitTestViewDrawRect method to do
  36. // it's actual drawing. The above creates a view of size 200x200 that draws
  37. // it's content using |self|'s unitTestViewDrawRect method and compares it to
  38. // the contents of the file Foo.tif to make sure it's valid
  39. @implementation GTMUnitTestView
  40. - (id)initWithFrame:(CGRect)frame
  41. drawer:(id<GTMUnitTestViewDrawer>)drawer
  42. contextInfo:(void*)contextInfo{
  43. self = [super initWithFrame:frame];
  44. if (self != nil) {
  45. drawer_ = [drawer retain];
  46. contextInfo_ = contextInfo;
  47. }
  48. return self;
  49. }
  50. - (void)dealloc {
  51. [drawer_ release];
  52. [super dealloc];
  53. }
  54. - (void)drawRect:(CGRect)rect {
  55. [drawer_ gtm_unitTestViewDrawRect:rect contextInfo:contextInfo_];
  56. }
  57. @end
  58. @implementation UIView (GTMUnitTestingAdditions)
  59. // Returns an image containing a representation of the object
  60. // suitable for use in comparing against a master image.
  61. // NB this means that all colors should be from "NSDevice" color space
  62. // Does all of it's drawing with smoothfonts and antialiasing off
  63. // to avoid issues with font smoothing settings and antialias differences
  64. // between ppc and x86.
  65. //
  66. // Returns:
  67. // an image of the object
  68. - (CGImageRef)gtm_unitTestImage {
  69. CALayer* layer = [self layer];
  70. return [layer gtm_unitTestImage];
  71. }
  72. // Encodes the state of an object in a manner suitable for comparing
  73. // against a master state file so we can determine whether the
  74. // object is in a suitable state.
  75. //
  76. // Arguments:
  77. // inCoder - the coder to encode our state into
  78. - (void)gtm_unitTestEncodeState:(NSCoder*)inCoder {
  79. [super gtm_unitTestEncodeState:inCoder];
  80. [inCoder encodeBool:[self isHidden] forKey:@"ViewIsHidden"];
  81. CALayer* layer = [self layer];
  82. if (layer) {
  83. [layer gtm_unitTestEncodeState:inCoder];
  84. }
  85. if ([self gtm_shouldEncodeStateForSubviews]) {
  86. int i = 0;
  87. for (UIView *subview in [self subviews]) {
  88. [inCoder encodeObject:subview
  89. forKey:[NSString stringWithFormat:@"ViewSubView %d", i]];
  90. i++;
  91. }
  92. }
  93. }
  94. // Returns whether gtm_unitTestEncodeState should recurse into subviews
  95. //
  96. // Returns:
  97. // should gtm_unitTestEncodeState pick up subview state.
  98. - (BOOL)gtm_shouldEncodeStateForSubviews {
  99. return YES;
  100. }
  101. - (BOOL)gtm_shouldEncodeStateForSublayersOfLayer:(CALayer*)layer {
  102. return NO;
  103. }
  104. @end
  105. @implementation UIImage (GTMUnitTestingAdditions)
  106. - (CGImageRef)gtm_unitTestImage {
  107. return [self CGImage];
  108. }
  109. @end