aboutsummaryrefslogtreecommitdiff
path: root/UnitTesting/GTMUnitTestingTest.m
blob: 0f87d28245fe6cbf1faf1a2220f7e493756ae814 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//
//  GTMUnitTestingTest.m
//
//  Copyright 2008 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations under
//  the License.
//

#import "GTMSenTestCase.h"
#import "GTMUnitTestingTest.h"
#import "GTMAppKit+UnitTesting.h"

NSString *const kGTMWindowNibName = @"GTMUnitTestingTest";
NSString *const kGTMWindowSaveFileName = @"GTMUnitTestingWindow";

@interface GTMUnitTestingTest : GTMTestCase {
  int expectedFailureCount_;
}
@end

// GTMUnitTestingTest support classes
@interface GTMUnitTestingView : NSObject <GTMUnitTestViewDrawer> {
  BOOL goodContext_;
}
- (BOOL)hadGoodContext;
@end

@interface GTMUnitTestingDelegate : NSObject <NSImageDelegate> {
  BOOL didEncode_;
}
- (BOOL)didEncode;
@end

@interface GTMUnitTestingProxyTest : NSProxy
- (id)init;
@end

@implementation GTMUnitTestingTest

// Brings up the window defined in the nib and takes a snapshot of it.
// We use the "empty" GTMUnitTestingTestController controller so that
// initWithWindowNibName can find the appropriate bundle to load our nib from.
// For some reason when running unit tests, with all the injecting going on
// the nib loader can get confused as to where it should load a nib from.
// Having a NSWindowController subclass in the same bundle as the nib seems
// to help the nib loader find the nib, and any custom classes that are attached
// to it.
- (void)testUnitTestingFramework {
  // set up our delegates so we can test delegate handling
  GTMUnitTestingDelegate *appDelegate = [[GTMUnitTestingDelegate alloc] init];
  [NSApp setDelegate:appDelegate];

  // Get our window
  GTMUnitTestingTestController *testWindowController
    = [[GTMUnitTestingTestController alloc] initWithWindowNibName:kGTMWindowNibName];
  NSWindow *window = [testWindowController window];
  // Test the app state. This will cover windows and menus
  GTMAssertObjectStateEqualToStateNamed(NSApp,
                                        @"GTMUnitTestingTestApp",
                                        @"Testing the app state");

  // Test the window image and state
  GTMAssertObjectEqualToStateAndImageNamed(window,
                                           kGTMWindowSaveFileName,
                                           @"Testing the window image and state");

  // Verify that all of our delegate encoders got called
  STAssertTrue([appDelegate didEncode], @"app delegate didn't get called?");

  // Clean up
  [NSApp setDelegate:nil];
  [appDelegate release];
  [testWindowController release];
}

- (void)testViewUnitTesting {
  GTMUnitTestingView *unitTestingView = [[GTMUnitTestingView alloc] init];
  GTMAssertDrawingEqualToImageNamed(unitTestingView,
                                    NSMakeSize(200,200),
                                    @"GTMUnitTestingView",
                                    NSApp,
                                    @"Testing view drawing");
  STAssertTrue([unitTestingView hadGoodContext], @"bad context?");
  [unitTestingView release];
}

- (void)testImageUnitTesting {
  NSImage *image = [NSImage imageNamed:@"NSApplicationIcon"];
  GTMUnitTestingDelegate *imgDelegate = [[GTMUnitTestingDelegate alloc] init];
  [image setDelegate:imgDelegate];
  GTMAssertObjectEqualToStateAndImageNamed(image,
                                           @"GTMUnitTestingImage",
                                           @"Testing NSImage image and state");
  STAssertTrue([imgDelegate didEncode], @"imgDelegate didn't get called?");
  [image setDelegate:nil];
  [imgDelegate release];
}

- (void)testFailures {
  NSString *const bogusTestName = @"GTMUnitTestTestingFailTest";
  NSString *tempDir = NSTemporaryDirectory();
  STAssertNotNil(tempDir, @"No Temp Dir?");
  NSString *originalPath = [NSObject gtm_getUnitTestSaveToDirectory];
  STAssertNotNil(originalPath, @"No save dir?");
  [NSObject gtm_setUnitTestSaveToDirectory:tempDir];
  STAssertEqualObjects(tempDir, [NSObject gtm_getUnitTestSaveToDirectory],
                       @"Save to dir not set?");
  NSString *statePath = [self gtm_saveToPathForStateNamed:bogusTestName];
  STAssertNotNil(statePath, @"no state path?");
  NSString *imagePath = [self gtm_saveToPathForImageNamed:bogusTestName];
  STAssertNotNil(imagePath, @"no image path?");
  GTMUnitTestingTestController *testWindowController
    = [[GTMUnitTestingTestController alloc] initWithWindowNibName:kGTMWindowNibName];
  NSWindow *window = [testWindowController window];

  // Test against a golden master filename that doesn't exist
  expectedFailureCount_ = 2;
  GTMAssertObjectEqualToStateAndImageNamed(window,
                                           bogusTestName,
                                           @"Creating image and state files");
  STAssertEquals(expectedFailureCount_, 0,
                 @"Didn't get expected failures creating files");

  // Change our image and state and verify failures
  [[testWindowController textField] setStringValue:@"Foo"];
  expectedFailureCount_ = 2;
  GTMAssertObjectEqualToStateAndImageNamed(window,
                                           kGTMWindowSaveFileName,
                                           @"Testing the window image and state");
  STAssertEquals(expectedFailureCount_, 0,
                 @"Didn't get expected failures testing files");

  // Now change the size of our image and verify failures
  NSRect oldFrame = [window frame];
  NSRect newFrame = oldFrame;
  newFrame.size.width += 1;
  [window setFrame:newFrame display:YES];
  expectedFailureCount_ = 1;
  GTMAssertObjectImageEqualToImageNamed(window,
                                        kGTMWindowSaveFileName,
                                        @"Testing the changed window size");
  [window setFrame:oldFrame display:YES];

  // Set our unit test save dir to a bogus directory and
  // run the tests again.
  [NSObject gtm_setUnitTestSaveToDirectory:@"/zim/blatz/foo/bob/bar"];
  expectedFailureCount_ = 2;
  GTMAssertObjectEqualToStateAndImageNamed(window,
                                           kGTMWindowSaveFileName,
                                           @"Testing the window image and state");
  STAssertEquals(expectedFailureCount_, 0,
                 @"Didn't get expected failures testing files");
  expectedFailureCount_ = 2;
  GTMAssertObjectEqualToStateAndImageNamed(window,
                                           @"GTMUnitTestingWindowDoesntExist",
                                           @"Testing the window image and state");
  STAssertEquals(expectedFailureCount_, 0,
                 @"Didn't get expected failures testing files");

  // Reset our unit test save dir
  [NSObject gtm_setUnitTestSaveToDirectory:nil];

  // Test against something that doesn't have an image
  expectedFailureCount_ = 1;
  GTMAssertObjectImageEqualToImageNamed(@"a string",
                                        @"GTMStringsDontHaveImages",
                                        @"Testing that strings should fail");
  STAssertEquals(expectedFailureCount_, 0, @"Didn't get expected failures testing files");

  // Test against something that doesn't implement our support
  expectedFailureCount_ = 1;
  GTMUnitTestingProxyTest *proxy = [[GTMUnitTestingProxyTest alloc] init];
  GTMAssertObjectStateEqualToStateNamed(proxy,
                                        @"NSProxiesDontDoState",
                                        @"Testing that NSProxy should fail");
  STAssertEquals(expectedFailureCount_, 0, @"Didn't get expected failures testing proxy");
  [proxy release];

  [window close];
}

- (void)failWithException:(NSException *)anException {
  if (expectedFailureCount_ > 0) {
    expectedFailureCount_ -= 1;
  } else {
    [super failWithException:anException];  // COV_NF_LINE - not expecting exception
  }
}


@end

@implementation GTMUnitTestingTestController
- (NSTextField *)textField {
  return field_;
}

@end

@implementation GTMUnitTestingDelegate

- (void)gtm_unitTestEncoderWillEncode:(id)sender inCoder:(NSCoder*)inCoder {
  // Test various encodings
  [inCoder encodeBool:YES forKey:@"BoolTest"];
  [inCoder encodeInt:1 forKey:@"IntTest"];
  [inCoder encodeInt32:1 forKey:@"Int32Test"];
  [inCoder encodeInt64:1 forKey:@"Int64Test"];
  [inCoder encodeFloat:1.0f forKey:@"FloatTest"];
  [inCoder encodeDouble:1.0 forKey:@"DoubleTest"];
  [inCoder encodeBytes:(const uint8_t*)"BytesTest" length:9 forKey:@"BytesTest"];
  didEncode_ = YES;
}

- (BOOL)didEncode {
  return didEncode_;
}
@end

@implementation GTMUnitTestingView

- (void)gtm_unitTestViewDrawRect:(NSRect)rect contextInfo:(void*)contextInfo {
  [[NSColor redColor] set];
  NSRectFill(rect);
  goodContext_ = [(id)contextInfo isEqualTo:NSApp];
}

- (BOOL)hadGoodContext {
  return goodContext_;
}
@end

// GTMUnitTestingProxyTest is for testing the case where we don't conform to
// the GTMUnitTestingEncoding protocol.
@implementation GTMUnitTestingProxyTest
- (id)init {
  return self;
}

- (BOOL)conformsToProtocol:(Protocol *)protocol {
  return NO;
}

@end