PageRenderTime 36ms CodeModel.GetById 16ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/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
23#import "GTMCALayer+UnitTesting.h"
24
25@implementation CALayer (GTMUnitTestingAdditions) 
26
27//  Returns an image containing a representation of the object
28//  suitable for use in comparing against a master image.
29//  NB this means that all colors should be from "NSDevice" color space
30//  Does all of it's drawing with smoothfonts and antialiasing off
31//  to avoid issues with font smoothing settings and antialias differences
32//  between ppc and x86.
33//
34//  Returns:
35//    an image of the object
36- (CGImageRef)gtm_unitTestImage {
37  CGRect bounds = [self bounds];
38  CGSize size = CGSizeMake(CGRectGetWidth(bounds), CGRectGetHeight(bounds));
39  CGContextRef context = GTMCreateUnitTestBitmapContextOfSizeWithData(size,
40                                                                      NULL);
41  _GTMDevAssert(context, @"Couldn't create context");
42  
43  // iPhone renders are flipped
44  CGAffineTransform transform = CGAffineTransformMakeTranslation(0, size.height);
45  transform = CGAffineTransformScale(transform, 1.0, -1.0);
46  CGContextConcatCTM(context, transform);
47  
48  [self renderInContext:context];
49  CGImageRef image = CGBitmapContextCreateImage(context);
50  CFRelease(context);
51  return (CGImageRef)GTMCFAutorelease(image);
52}
53
54//  Encodes the state of an object in a manner suitable for comparing
55//  against a master state file so we can determine whether the
56//  object is in a suitable state.
57//
58//  Arguments:
59//    inCoder - the coder to encode our state into
60- (void)gtm_unitTestEncodeState:(NSCoder*)inCoder {
61  [super gtm_unitTestEncodeState:inCoder];
62  [inCoder encodeBool:[self isHidden] forKey:@"LayerIsHidden"];
63  [inCoder encodeBool:[self isDoubleSided] forKey:@"LayerIsDoublesided"];
64  [inCoder encodeBool:[self isOpaque] forKey:@"LayerIsOpaque"];
65  [inCoder encodeFloat:[self opacity] forKey:@"LayerOpacity"];
66  // TODO: There is a ton more we can add here. What are we interested in?
67  if ([self gtm_shouldEncodeStateForSublayers]) {
68    int i = 0;
69    for (CALayer *subLayer in [self sublayers]) {
70      [inCoder encodeObject:subLayer 
71                     forKey:[NSString stringWithFormat:@"CALayerSubLayer %d", i]];
72      i = i + 1;
73    }
74  }
75}
76
77//  Returns whether gtm_unitTestEncodeState should recurse into sublayers
78//
79//  Returns:
80//    should gtm_unitTestEncodeState pick up sublayer state.
81- (BOOL)gtm_shouldEncodeStateForSublayers {
82  BOOL value = YES;
83  if([self.delegate respondsToSelector:@selector(gtm_shouldEncodeStateForSublayersOfLayer:)]) {
84    value = [self.delegate gtm_shouldEncodeStateForSublayersOfLayer:self];
85  }
86  return value;
87}
88
89@end