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

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

http://macfuse.googlecode.com/
Objective C | 253 lines | 173 code | 37 blank | 43 comment | 2 complexity | ff672914d2bead8b0624a97852a2a9ea MD5 | raw file
  1//
  2//  GTMUnitTestingTest.m
  3//
  4//  Copyright 2008 Google Inc.
  5//
  6//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7//  use this file except in compliance with the License.  You may obtain a copy
  8//  of the License at
  9//
 10//  http://www.apache.org/licenses/LICENSE-2.0
 11//
 12//  Unless required by applicable law or agreed to in writing, software
 13//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 14//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 15//  License for the specific language governing permissions and limitations under
 16//  the License.
 17//
 18
 19#import "GTMSenTestCase.h"
 20#import "GTMUnitTestingTest.h"
 21#import "GTMAppKit+UnitTesting.h"
 22
 23NSString *const kGTMWindowNibName = @"GTMUnitTestingTest";
 24NSString *const kGTMWindowSaveFileName = @"GTMUnitTestingWindow";
 25
 26@interface GTMUnitTestingTest : GTMTestCase {
 27  int expectedFailureCount_;
 28}
 29@end
 30
 31// GTMUnitTestingTest support classes
 32@interface GTMUnitTestingView : NSObject <GTMUnitTestViewDrawer> {
 33  BOOL goodContext_;
 34}
 35- (BOOL)hadGoodContext;
 36@end
 37
 38@interface GTMUnitTestingDelegate : NSObject <NSImageDelegate> {
 39  BOOL didEncode_;
 40}
 41- (BOOL)didEncode;
 42@end
 43
 44@interface GTMUnitTestingProxyTest : NSProxy
 45- (id)init;
 46@end
 47
 48@implementation GTMUnitTestingTest
 49
 50// Brings up the window defined in the nib and takes a snapshot of it.
 51// We use the "empty" GTMUnitTestingTestController controller so that
 52// initWithWindowNibName can find the appropriate bundle to load our nib from.
 53// For some reason when running unit tests, with all the injecting going on
 54// the nib loader can get confused as to where it should load a nib from.
 55// Having a NSWindowController subclass in the same bundle as the nib seems
 56// to help the nib loader find the nib, and any custom classes that are attached
 57// to it.
 58- (void)testUnitTestingFramework {
 59  // set up our delegates so we can test delegate handling
 60  GTMUnitTestingDelegate *appDelegate = [[GTMUnitTestingDelegate alloc] init];
 61  [NSApp setDelegate:appDelegate];
 62
 63  // Get our window
 64  GTMUnitTestingTestController *testWindowController
 65    = [[GTMUnitTestingTestController alloc] initWithWindowNibName:kGTMWindowNibName];
 66  NSWindow *window = [testWindowController window];
 67  // Test the app state. This will cover windows and menus
 68  GTMAssertObjectStateEqualToStateNamed(NSApp,
 69                                        @"GTMUnitTestingTestApp",
 70                                        @"Testing the app state");
 71
 72  // Test the window image and state
 73  GTMAssertObjectEqualToStateAndImageNamed(window,
 74                                           kGTMWindowSaveFileName,
 75                                           @"Testing the window image and state");
 76
 77  // Verify that all of our delegate encoders got called
 78  STAssertTrue([appDelegate didEncode], @"app delegate didn't get called?");
 79
 80  // Clean up
 81  [NSApp setDelegate:nil];
 82  [appDelegate release];
 83  [testWindowController release];
 84}
 85
 86- (void)testViewUnitTesting {
 87  GTMUnitTestingView *unitTestingView = [[GTMUnitTestingView alloc] init];
 88  GTMAssertDrawingEqualToImageNamed(unitTestingView,
 89                                    NSMakeSize(200,200),
 90                                    @"GTMUnitTestingView",
 91                                    NSApp,
 92                                    @"Testing view drawing");
 93  STAssertTrue([unitTestingView hadGoodContext], @"bad context?");
 94  [unitTestingView release];
 95}
 96
 97- (void)testImageUnitTesting {
 98  NSImage *image = [NSImage imageNamed:@"NSApplicationIcon"];
 99  GTMUnitTestingDelegate *imgDelegate = [[GTMUnitTestingDelegate alloc] init];
100  [image setDelegate:imgDelegate];
101  GTMAssertObjectEqualToStateAndImageNamed(image,
102                                           @"GTMUnitTestingImage",
103                                           @"Testing NSImage image and state");
104  STAssertTrue([imgDelegate didEncode], @"imgDelegate didn't get called?");
105  [image setDelegate:nil];
106  [imgDelegate release];
107}
108
109- (void)testFailures {
110  NSString *const bogusTestName = @"GTMUnitTestTestingFailTest";
111  NSString *tempDir = NSTemporaryDirectory();
112  STAssertNotNil(tempDir, @"No Temp Dir?");
113  NSString *originalPath = [NSObject gtm_getUnitTestSaveToDirectory];
114  STAssertNotNil(originalPath, @"No save dir?");
115  [NSObject gtm_setUnitTestSaveToDirectory:tempDir];
116  STAssertEqualObjects(tempDir, [NSObject gtm_getUnitTestSaveToDirectory],
117                       @"Save to dir not set?");
118  NSString *statePath = [self gtm_saveToPathForStateNamed:bogusTestName];
119  STAssertNotNil(statePath, @"no state path?");
120  NSString *imagePath = [self gtm_saveToPathForImageNamed:bogusTestName];
121  STAssertNotNil(imagePath, @"no image path?");
122  GTMUnitTestingTestController *testWindowController
123    = [[GTMUnitTestingTestController alloc] initWithWindowNibName:kGTMWindowNibName];
124  NSWindow *window = [testWindowController window];
125
126  // Test against a golden master filename that doesn't exist
127  expectedFailureCount_ = 2;
128  GTMAssertObjectEqualToStateAndImageNamed(window,
129                                           bogusTestName,
130                                           @"Creating image and state files");
131  STAssertEquals(expectedFailureCount_, 0,
132                 @"Didn't get expected failures creating files");
133
134  // Change our image and state and verify failures
135  [[testWindowController textField] setStringValue:@"Foo"];
136  expectedFailureCount_ = 2;
137  GTMAssertObjectEqualToStateAndImageNamed(window,
138                                           kGTMWindowSaveFileName,
139                                           @"Testing the window image and state");
140  STAssertEquals(expectedFailureCount_, 0,
141                 @"Didn't get expected failures testing files");
142
143  // Now change the size of our image and verify failures
144  NSRect oldFrame = [window frame];
145  NSRect newFrame = oldFrame;
146  newFrame.size.width += 1;
147  [window setFrame:newFrame display:YES];
148  expectedFailureCount_ = 1;
149  GTMAssertObjectImageEqualToImageNamed(window,
150                                        kGTMWindowSaveFileName,
151                                        @"Testing the changed window size");
152  [window setFrame:oldFrame display:YES];
153
154  // Set our unit test save dir to a bogus directory and
155  // run the tests again.
156  [NSObject gtm_setUnitTestSaveToDirectory:@"/zim/blatz/foo/bob/bar"];
157  expectedFailureCount_ = 2;
158  GTMAssertObjectEqualToStateAndImageNamed(window,
159                                           kGTMWindowSaveFileName,
160                                           @"Testing the window image and state");
161  STAssertEquals(expectedFailureCount_, 0,
162                 @"Didn't get expected failures testing files");
163  expectedFailureCount_ = 2;
164  GTMAssertObjectEqualToStateAndImageNamed(window,
165                                           @"GTMUnitTestingWindowDoesntExist",
166                                           @"Testing the window image and state");
167  STAssertEquals(expectedFailureCount_, 0,
168                 @"Didn't get expected failures testing files");
169
170  // Reset our unit test save dir
171  [NSObject gtm_setUnitTestSaveToDirectory:nil];
172
173  // Test against something that doesn't have an image
174  expectedFailureCount_ = 1;
175  GTMAssertObjectImageEqualToImageNamed(@"a string",
176                                        @"GTMStringsDontHaveImages",
177                                        @"Testing that strings should fail");
178  STAssertEquals(expectedFailureCount_, 0, @"Didn't get expected failures testing files");
179
180  // Test against something that doesn't implement our support
181  expectedFailureCount_ = 1;
182  GTMUnitTestingProxyTest *proxy = [[GTMUnitTestingProxyTest alloc] init];
183  GTMAssertObjectStateEqualToStateNamed(proxy,
184                                        @"NSProxiesDontDoState",
185                                        @"Testing that NSProxy should fail");
186  STAssertEquals(expectedFailureCount_, 0, @"Didn't get expected failures testing proxy");
187  [proxy release];
188
189  [window close];
190}
191
192- (void)failWithException:(NSException *)anException {
193  if (expectedFailureCount_ > 0) {
194    expectedFailureCount_ -= 1;
195  } else {
196    [super failWithException:anException];  // COV_NF_LINE - not expecting exception
197  }
198}
199
200
201@end
202
203@implementation GTMUnitTestingTestController
204- (NSTextField *)textField {
205  return field_;
206}
207
208@end
209
210@implementation GTMUnitTestingDelegate
211
212- (void)gtm_unitTestEncoderWillEncode:(id)sender inCoder:(NSCoder*)inCoder {
213  // Test various encodings
214  [inCoder encodeBool:YES forKey:@"BoolTest"];
215  [inCoder encodeInt:1 forKey:@"IntTest"];
216  [inCoder encodeInt32:1 forKey:@"Int32Test"];
217  [inCoder encodeInt64:1 forKey:@"Int64Test"];
218  [inCoder encodeFloat:1.0f forKey:@"FloatTest"];
219  [inCoder encodeDouble:1.0 forKey:@"DoubleTest"];
220  [inCoder encodeBytes:(const uint8_t*)"BytesTest" length:9 forKey:@"BytesTest"];
221  didEncode_ = YES;
222}
223
224- (BOOL)didEncode {
225  return didEncode_;
226}
227@end
228
229@implementation GTMUnitTestingView
230
231- (void)gtm_unitTestViewDrawRect:(NSRect)rect contextInfo:(void*)contextInfo {
232  [[NSColor redColor] set];
233  NSRectFill(rect);
234  goodContext_ = [(id)contextInfo isEqualTo:NSApp];
235}
236
237- (BOOL)hadGoodContext {
238  return goodContext_;
239}
240@end
241
242// GTMUnitTestingProxyTest is for testing the case where we don't conform to
243// the GTMUnitTestingEncoding protocol.
244@implementation GTMUnitTestingProxyTest
245- (id)init {
246  return self;
247}
248
249- (BOOL)conformsToProtocol:(Protocol *)protocol {
250  return NO;
251}
252
253@end