PageRenderTime 22ms CodeModel.GetById 13ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/google-toolbox-for-mac/UnitTesting/GTMUIKit+UnitTesting.h

http://macfuse.googlecode.com/
C++ Header | 128 lines | 29 code | 16 blank | 83 comment | 0 complexity | 911087152c24036e45c07c4af058c7b5 MD5 | raw file
  1//
  2//  GTMUIKit+UnitTesting.h
  3//
  4//  Code for making unit testing of graphics/UI easier. Generally you
  5//  will only want to look at the macros:
  6//    GTMAssertDrawingEqualToFile
  7//    GTMAssertViewRepEqualToFile
  8//  and the protocol GTMUnitTestViewDrawer. When using these routines
  9//  make sure you are using device colors and not calibrated/generic colors
 10//  or else your test graphics WILL NOT match across devices/graphics cards.
 11//
 12//  Copyright 2006-2008 Google Inc.
 13//
 14//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
 15//  use this file except in compliance with the License.  You may obtain a copy
 16//  of the License at
 17// 
 18//  http://www.apache.org/licenses/LICENSE-2.0
 19// 
 20//  Unless required by applicable law or agreed to in writing, software
 21//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 22//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 23//  License for the specific language governing permissions and limitations under
 24//  the License.
 25//
 26
 27#import <UIKit/UIKit.h>
 28#import "GTMNSObject+UnitTesting.h"
 29
 30@protocol GTMUnitTestViewDrawer;
 31
 32//  Fails when the |a1|'s drawing in an area |a2| does not equal the image file named |a3|.
 33//  See the description of the GTMAssertViewRepEqualToFile macro
 34//  to understand how |a3| is found and written out.
 35//  See the description of the GTMUnitTestView for a better idea
 36//  how the view works.
 37//  Implemented as a macro to match the rest of the SenTest macros.
 38//
 39//  Args:
 40//    a1: The object that implements the GTMUnitTestViewDrawer protocol
 41//        that is doing the drawing.
 42//    a2: The size of the drawing
 43//    a3: The name of the image file to check against.
 44//        Do not include the extension
 45//    a4: contextInfo to pass to drawer
 46//    description: A format string as in the printf() function. 
 47//        Can be nil or an empty string but must be present. 
 48//    ...: A variable number of arguments to the format string. Can be absent.
 49//
 50
 51#define GTMAssertDrawingEqualToFile(a1, a2, a3, a4, description, ...) \
 52  do { \
 53    id<GTMUnitTestViewDrawer> a1Drawer = (a1); \
 54    CGSize a2Size = (a2); \
 55    NSString* a3String = (a3); \
 56    void *a4ContextInfo = (a4); \
 57    CGRect frame = CGRectMake(0, 0, a2Size.width, a2Size.height); \
 58    GTMUnitTestView *view = [[[GTMUnitTestView alloc] initWithFrame:frame drawer:a1Drawer contextInfo:a4ContextInfo] autorelease]; \
 59    GTMAssertObjectImageEqualToImageNamed(view, a3String, @"%@", STComposeString(description, ##__VA_ARGS__)); \
 60  } while(0)
 61
 62//  Category for making unit testing of graphics/UI easier.
 63
 64//  Allows you to take a state of a view. Supports both image and state.
 65//  See GTMNSObject+UnitTesting.h for details.
 66@interface UIView (GTMUnitTestingAdditions) <GTMUnitTestingImaging>
 67
 68//  Encodes the state of an object in a manner suitable for comparing against a master state file
 69//  This enables us to determine whether the object is in a suitable state.
 70//
 71//  Arguments:
 72//    inCoder - the coder to encode our state into
 73- (void)gtm_unitTestEncodeState:(NSCoder*)inCoder;
 74
 75//  Returns whether gtm_unitTestEncodeState should recurse into subviews
 76//
 77//  Returns:
 78//    should gtm_unitTestEncodeState pick up subview state.
 79- (BOOL)gtm_shouldEncodeStateForSubviews;
 80@end
 81
 82// Category to help UIImage testing. UIImage can be tested using
 83// GTMAssertObjectImageEqualToImageNamed macro, which automatically creates
 84// result images and diff images in case test fails.
 85@interface UIImage (GTMUnitTestingAdditions) <GTMUnitTestingImaging>
 86@end
 87
 88//  A view that allows you to delegate out drawing using the formal 
 89//  GTMUnitTestViewDelegate protocol
 90//  This is useful when writing up unit tests for visual elements.
 91//  Your test will often end up looking like this:
 92//  - (void)testFoo {
 93//   GTMAssertDrawingEqualToFile(self, CGSizeMake(200, 200), @"Foo", nil, nil);
 94//  }
 95//  and your testSuite will also implement the unitTestViewDrawRect method to do
 96//  it's actual drawing. The above creates a view of size 200x200 that draws
 97//  it's content using |self|'s unitTestViewDrawRect method and compares it to
 98//  the contents of the file Foo.tif to make sure it's valid
 99@interface GTMUnitTestView : UIView {
100 @private
101  id<GTMUnitTestViewDrawer> drawer_; // delegate for doing drawing (STRONG) 
102  void* contextInfo_; // info passed in by user for them to use when drawing
103}
104
105//  Create a GTMUnitTestView.
106//
107//  Args:
108//    rect: the area to draw.
109//    drawer: the object that will do the drawing via the GTMUnitTestViewDrawer
110//            protocol
111//    contextInfo: 
112- (id)initWithFrame:(CGRect)frame drawer:(id<GTMUnitTestViewDrawer>)drawer contextInfo:(void*)contextInfo;
113
114@end
115
116/// \cond Protocols
117
118// Formal protocol for doing unit testing of views. See description of
119// GTMUnitTestView for details.
120@protocol GTMUnitTestViewDrawer <NSObject>
121
122//  Draw the view. Equivalent to drawRect on a standard UIView.
123//
124//  Args:
125//    rect: the area to draw.
126- (void)gtm_unitTestViewDrawRect:(CGRect)rect contextInfo:(void*)contextInfo;
127
128@end