PageRenderTime 24ms CodeModel.GetById 1ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

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