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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
|
//
// GTMAppKit+UnitTesting.m
//
// Categories for making unit testing of graphics/UI easier.
//
// Copyright 2006-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 "GTMDefines.h"
#import "GTMAppKit+UnitTesting.h"
#import "GTMGeometryUtils.h"
#import "GTMMethodCheck.h"
#import "GTMGarbageCollection.h"
#if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4
#define ENCODE_NSINTEGER(coder, i, key) [(coder) encodeInt:(i) forKey:(key)]
#else
#define ENCODE_NSINTEGER(coder, i, key) [(coder) encodeInteger:(i) forKey:(key)]
#endif
@implementation NSApplication (GTMUnitTestingAdditions)
GTM_METHOD_CHECK(NSObject, gtm_unitTestEncodeState:);
- (void)gtm_unitTestEncodeState:(NSCoder*)inCoder {
[super gtm_unitTestEncodeState:inCoder];
ENCODE_NSINTEGER(inCoder, [[self mainWindow] windowNumber], @"ApplicationMainWindow");
// Descend down into the windows allowing them to store their state
NSWindow *window = nil;
int i = 0;
GTM_FOREACH_OBJECT(window, [self windows]) {
if ([window isVisible]) {
// Only record visible windows because invisible windows may be closing on us
// This appears to happen differently in 64 bit vs 32 bit, and items
// in the window may hold an extra retain count for a while until the
// event loop is spun. To avoid all this, we just don't record non
// visible windows.
// See rdar://5851458 for details.
[inCoder encodeObject:window forKey:[NSString stringWithFormat:@"Window %d", i]];
i = i + 1;
}
}
// and encode the menu bar
NSMenu *mainMenu = [self mainMenu];
if (mainMenu) {
[inCoder encodeObject:mainMenu forKey:@"MenuBar"];
}
}
@end
@implementation NSWindow (GTMUnitTestingAdditions)
- (CGImageRef)gtm_unitTestImage {
return [[[self contentView] superview] gtm_unitTestImage];
}
- (void)gtm_unitTestEncodeState:(NSCoder*)inCoder {
[super gtm_unitTestEncodeState:inCoder];
[inCoder encodeObject:[self title] forKey:@"WindowTitle"];
[inCoder encodeBool:[self isVisible] forKey:@"WindowIsVisible"];
// Do not record if window is key, because users running unit tests
// and clicking around to other apps, could change this mid test causing
// issues.
// [inCoder encodeBool:[self isKeyWindow] forKey:@"WindowIsKey"];
[inCoder encodeBool:[self isMainWindow] forKey:@"WindowIsMain"];
[inCoder encodeObject:[self contentView] forKey:@"WindowContent"];
if ([self toolbar]) {
[inCoder encodeObject:[self toolbar] forKey:@"WindowToolbar"];
}
}
@end
@implementation NSControl (GTMUnitTestingAdditions)
// Encodes the state of an object in a manner suitable for comparing
// against a master state file so we can determine whether the
// object is in a suitable state.
//
// Arguments:
// inCoder - the coder to encode our state into
- (void)gtm_unitTestEncodeState:(NSCoder*)inCoder {
[super gtm_unitTestEncodeState:inCoder];
[inCoder encodeObject:[self class] forKey:@"ControlType"];
[inCoder encodeObject:[self objectValue] forKey:@"ControlValue"];
[inCoder encodeObject:[self selectedCell] forKey:@"ControlSelectedCell"];
ENCODE_NSINTEGER(inCoder, [self tag], @"ControlTag");
[inCoder encodeBool:[self isEnabled] forKey:@"ControlIsEnabled"];
}
@end
@implementation NSButton (GTMUnitTestingAdditions)
// Encodes the state of an object in a manner suitable for comparing
// against a master state file so we can determine whether the
// object is in a suitable state.
//
// Arguments:
// inCoder - the coder to encode our state into
- (void)gtm_unitTestEncodeState:(NSCoder*)inCoder {
[super gtm_unitTestEncodeState:inCoder];
NSString *alternateTitle = [self alternateTitle];
if (alternateTitle) {
[inCoder encodeObject:alternateTitle forKey:@"ButtonAlternateTitle"];
}
}
@end
@implementation NSTextField (GTMUnitTestingAdditions)
- (BOOL)gtm_shouldEncodeStateForSubviews {
return NO;
}
// Encodes the state of an object in a manner suitable for comparing
// against a master state file so we can determine whether the
// object is in a suitable state.
//
// Arguments:
// inCoder - the coder to encode our state into
- (void)gtm_unitTestEncodeState:(NSCoder*)inCoder {
[super gtm_unitTestEncodeState:inCoder];
id controlCell = [self cell];
if ([controlCell isKindOfClass:[NSTextFieldCell class]]) {
NSTextFieldCell *textFieldCell = controlCell;
[inCoder encodeObject:[textFieldCell placeholderString]
forKey:@"PlaceHolderString"];
}
}
@end
@implementation NSCell (GTMUnitTestingAdditions)
// Encodes the state of an object in a manner suitable for comparing
// against a master state file so we can determine whether the
// object is in a suitable state.
//
// Arguments:
// inCoder - the coder to encode our state into
- (void)gtm_unitTestEncodeState:(NSCoder*)inCoder {
[super gtm_unitTestEncodeState:inCoder];
BOOL isImageCell = NO;
if ([self hasValidObjectValue]) {
id val = [self objectValue];
[inCoder encodeObject:val forKey:@"CellValue"];
isImageCell = [val isKindOfClass:[NSImage class]];
}
if (!isImageCell) {
// Image cells have a title that includes addresses that aren't going
// to be constant, so we don't encode them. All the info we need
// is going to be in the CellValue encoding.
[inCoder encodeObject:[self title] forKey:@"CellTitle"];
}
ENCODE_NSINTEGER(inCoder, [self state], @"CellState");
ENCODE_NSINTEGER(inCoder, [self tag], @"CellTag");
}
@end
@implementation NSImage (GTMUnitTestingAdditions)
- (void)gtm_unitTestEncodeState:(NSCoder*)inCoder {
[super gtm_unitTestEncodeState:inCoder];
[inCoder encodeObject:NSStringFromSize([self size]) forKey:@"ImageSize"];
[inCoder encodeObject:[self name] forKey:@"ImageName"];
}
- (CGImageRef)gtm_unitTestImage {
// Create up a context
NSSize size = [self size];
NSRect rect = GTMNSRectOfSize(size);
CGSize cgSize = GTMNSSizeToCGSize(size);
CGContextRef contextRef = GTMCreateUnitTestBitmapContextOfSizeWithData(cgSize,
NULL);
NSGraphicsContext *bitmapContext
= [NSGraphicsContext graphicsContextWithGraphicsPort:contextRef flipped:NO];
_GTMDevAssert(bitmapContext, @"Couldn't create ns bitmap context");
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:bitmapContext];
[self drawInRect:rect fromRect:rect operation:NSCompositeCopy fraction:1.0];
CGImageRef image = CGBitmapContextCreateImage(contextRef);
CFRelease(contextRef);
[NSGraphicsContext restoreGraphicsState];
return (CGImageRef)GTMCFAutorelease(image);
}
@end
@implementation NSMenu (GTMUnitTestingAdditions)
// Encodes the state of an object in a manner suitable for comparing
// against a master state file so we can determine whether the
// object is in a suitable state.
//
// Arguments:
// inCoder - the coder to encode our state into
- (void)gtm_unitTestEncodeState:(NSCoder*)inCoder {
[super gtm_unitTestEncodeState:inCoder];
// Hack here to work around
// rdar://5881796 Application menu item title wrong when accessed programatically
// which causes us to have different results on x86_64 vs x386.
// Hack is braced intentionally. We don't record the title of the
// "application" menu or it's menu title because they are wrong on 32 bit.
// They appear to work right on 64bit.
{
NSMenu *mainMenu = [NSApp mainMenu];
NSMenu *appleMenu = [[mainMenu itemAtIndex:0] submenu];
if (![self isEqual:appleMenu]) {
[inCoder encodeObject:[self title] forKey:@"MenuTitle"];
}
}
// Descend down into the menuitems allowing them to store their state
NSMenuItem *menuItem = nil;
int i = 0;
GTM_FOREACH_OBJECT(menuItem, [self itemArray]) {
[inCoder encodeObject:menuItem
forKey:[NSString stringWithFormat:@"MenuItem %d", i]];
++i;
}
}
@end
@implementation NSMenuItem (GTMUnitTestingAdditions)
- (void)gtm_unitTestEncodeState:(NSCoder*)inCoder {
[super gtm_unitTestEncodeState:inCoder];
// Hack here to work around
// rdar://5881796 Application menu item title wrong when accessed programatically
// which causes us to have different results on x86_64 vs x386.
// See comment above.
{
NSMenu *mainMenu = [NSApp mainMenu];
NSMenuItem *appleMenuItem = [mainMenu itemAtIndex:0];
if (![self isEqual:appleMenuItem]) {
[inCoder encodeObject:[self title] forKey:@"MenuItemTitle"];
}
}
[inCoder encodeObject:[self keyEquivalent] forKey:@"MenuItemKeyEquivalent"];
[inCoder encodeBool:[self isSeparatorItem] forKey:@"MenuItemIsSeparator"];
ENCODE_NSINTEGER(inCoder, [self state], @"MenuItemState");
[inCoder encodeBool:[self isEnabled] forKey:@"MenuItemIsEnabled"];
[inCoder encodeBool:[self isAlternate] forKey:@"MenuItemIsAlternate"];
[inCoder encodeObject:[self toolTip] forKey:@"MenuItemTooltip"];
ENCODE_NSINTEGER(inCoder, [self tag], @"MenuItemTag");
ENCODE_NSINTEGER(inCoder, [self indentationLevel], @"MenuItemIndentationLevel");
// Do our submenu if neccessary
if ([self hasSubmenu]) {
[inCoder encodeObject:[self submenu] forKey:@"MenuItemSubmenu"];
}
}
@end
@implementation NSTabView (GTMUnitTestingAdditions)
// Encodes the state of an object in a manner suitable for comparing
// against a master state file so we can determine whether the
// object is in a suitable state.
//
// Arguments:
// inCoder - the coder to encode our state into
- (void)gtm_unitTestEncodeState:(NSCoder*)inCoder {
[super gtm_unitTestEncodeState:inCoder];
NSTabViewItem *tab = nil;
int i = 0;
GTM_FOREACH_OBJECT(tab, [self tabViewItems]) {
NSString *key = [NSString stringWithFormat:@"TabItem %d", i];
[inCoder encodeObject:tab forKey:key];
i = i + 1;
}
}
@end
@implementation NSTabViewItem (GTMUnitTestingAdditions)
// Encodes the state of an object in a manner suitable for comparing
// against a master state file so we can determine whether the
// object is in a suitable state.
//
// Arguments:
// inCoder - the coder to encode our state into
- (void)gtm_unitTestEncodeState:(NSCoder*)inCoder {
[super gtm_unitTestEncodeState:inCoder];
[inCoder encodeObject:[self label] forKey:@"TabLabel"];
[inCoder encodeObject:[self view] forKey:@"TabView"];
}
@end
@implementation NSToolbar (GTMUnitTestingAdditions)
// Encodes the state of an object in a manner suitable for comparing
// against a master state file so we can determine whether the
// object is in a suitable state.
//
// Arguments:
// inCoder - the coder to encode our state into
- (void)gtm_unitTestEncodeState:(NSCoder*)inCoder {
[super gtm_unitTestEncodeState:inCoder];
NSToolbarItem *item = nil;
NSUInteger i = 0;
GTM_FOREACH_OBJECT(item, [self items]) {
NSString *key = [NSString stringWithFormat:@"ToolbarItem %d", i];
[inCoder encodeObject:item forKey:key];
i = i + 1;
}
}
@end
@implementation NSToolbarItem (GTMUnitTestingAdditions)
// Encodes the state of an object in a manner suitable for comparing
// against a master state file so we can determine whether the
// object is in a suitable state.
//
// Arguments:
// inCoder - the coder to encode our state into
- (void)gtm_unitTestEncodeState:(NSCoder*)inCoder {
[super gtm_unitTestEncodeState:inCoder];
[inCoder encodeObject:[self label] forKey:@"Label"];
[inCoder encodeObject:[self paletteLabel] forKey:@"PaletteLabel"];
[inCoder encodeObject:[self toolTip] forKey:@"ToolTip"];
NSView *view = [self view];
if (view) {
[inCoder encodeObject:view forKey:@"View"];
}
}
@end
@implementation NSMatrix (GTMUnitTestingAdditions)
// Encodes the state of an object in a manner suitable for comparing
// against a master state file so we can determine whether the
// object is in a suitable state.
//
// Arguments:
// inCoder - the coder to encode our state into
- (void)gtm_unitTestEncodeState:(NSCoder*)inCoder {
[super gtm_unitTestEncodeState:inCoder];
ENCODE_NSINTEGER(inCoder, [self mode], @"MatrixMode");
ENCODE_NSINTEGER(inCoder, [self numberOfRows], @"MatrixRowCount");
ENCODE_NSINTEGER(inCoder, [self numberOfColumns], @"MatrixColumnCount");
[inCoder encodeBool:[self allowsEmptySelection]
forKey:@"MatrixAllowEmptySelection"];
[inCoder encodeBool:[self isSelectionByRect] forKey:@"MatrixSelectionByRect"];
[inCoder encodeBool:[self autosizesCells] forKey:@"MatrixAutosizesCells"];
[inCoder encodeSize:[self intercellSpacing] forKey:@"MatrixIntercellSpacing"];
[inCoder encodeObject:[self prototype] forKey:@"MatrixCellPrototype"];
// Dump the list of cells
NSCell *cell;
long i = 0;
GTM_FOREACH_OBJECT(cell, [self cells]) {
[inCoder encodeObject:cell
forKey:[NSString stringWithFormat:@"MatrixCell %ld", i]];
++i;
}
}
@end
@implementation NSBox (GTMUnitTestingAdditions)
// Encodes the state of an object in a manner suitable for comparing
// against a master state file so we can determine whether the
// object is in a suitable state.
//
// Arguments:
// inCoder - the coder to encode our state into
- (void)gtm_unitTestEncodeState:(NSCoder*)inCoder {
[super gtm_unitTestEncodeState:inCoder];
[inCoder encodeObject:[self title] forKey:@"BoxTitle"];
ENCODE_NSINTEGER(inCoder, [self titlePosition], @"BoxTitlePosition");
ENCODE_NSINTEGER(inCoder, [self boxType], @"BoxType");
ENCODE_NSINTEGER(inCoder, [self borderType], @"BoxBorderType");
// 10.5+ [inCoder encodeBool:[self isTransparent] forKey:@"BoxIsTransparent"];
}
@end
@implementation NSSegmentedControl (GTMUnitTestingAdditions)
// Encodes the state of an NSSegmentedControl and all its segments.
//
// Arguments:
// inCoder - the coder to encode state into
- (void)gtm_unitTestEncodeState:(NSCoder*)inCoder {
[super gtm_unitTestEncodeState:inCoder];
NSInteger segmentCount = [self segmentCount];
ENCODE_NSINTEGER(inCoder, segmentCount, @"SegmentCount");
for (NSInteger i = 0; i < segmentCount; ++i) {
NSString *key = [NSString stringWithFormat:@"Segment %d", i];
[inCoder encodeObject:[self labelForSegment:i] forKey:key];
}
}
@end
@implementation NSComboBox (GTMUnitTestingAdditions)
- (BOOL)gtm_shouldEncodeStateForSubviews {
// Subclass of NSTextView, don't want subviews for the same reason.
return NO;
}
// Encodes the state of an NSSegmentedControl and all its segments.
//
// Arguments:
// inCoder - the coder to encode state into
- (void)gtm_unitTestEncodeState:(NSCoder*)inCoder {
[super gtm_unitTestEncodeState:inCoder];
NSInteger aCount = [self numberOfItems];
ENCODE_NSINTEGER(inCoder, aCount, @"ComboBoxNumberOfItems");
aCount = [self numberOfVisibleItems];
ENCODE_NSINTEGER(inCoder, aCount, @"ComboBoxNumberOfVisibleItems");
// Include the objectValues if it doesn't use a data source.
if (![self usesDataSource]) {
NSArray *objectValues = [self objectValues];
for (NSUInteger i = 0; i < [objectValues count]; ++i) {
id value = [objectValues objectAtIndex:i];
if ([value isKindOfClass:[NSString class]]) {
NSString *key = [NSString stringWithFormat:@"ComboBoxObjectValue %u", i];
[inCoder encodeObject:value forKey:key];
}
}
}
}
@end
// A view that allows you to delegate out drawing using the formal
// GTMUnitTestViewDelegate protocol above. This is useful when writing up unit
// tests for visual elements.
// Your test will often end up looking like this:
// - (void)testFoo {
// GTMAssertDrawingEqualToFile(self, NSMakeSize(200, 200), @"Foo", nil, nil);
// }
// and your testSuite will also implement the unitTestViewDrawRect method to do
// it's actual drawing. The above creates a view of size 200x200 that draws
// it's content using |self|'s unitTestViewDrawRect method and compares it to
// the contents of the file Foo.tif to make sure it's valid
@implementation GTMUnitTestView
- (id)initWithFrame:(NSRect)frame
drawer:(id<GTMUnitTestViewDrawer>)drawer
contextInfo:(void*)contextInfo {
self = [super initWithFrame:frame];
if (self != nil) {
drawer_ = [drawer retain];
contextInfo_ = contextInfo;
}
return self;
}
- (void)dealloc {
[drawer_ release];
[super dealloc];
}
- (void)drawRect:(NSRect)rect {
[drawer_ gtm_unitTestViewDrawRect:rect contextInfo:contextInfo_];
}
@end
@implementation NSView (GTMUnitTestingAdditions)
// Returns an image containing a representation of the object
// suitable for use in comparing against a master image.
// Does all of it's drawing with smoothfonts and antialiasing off
// to avoid issues with font smoothing settings and antialias differences
// between ppc and x86.
//
// Returns:
// an image of the object
- (CGImageRef)gtm_unitTestImage {
// Create up a context
NSRect bounds = [self bounds];
CGSize cgSize = GTMNSSizeToCGSize(bounds.size);
CGContextRef contextRef = GTMCreateUnitTestBitmapContextOfSizeWithData(cgSize,
NULL);
NSGraphicsContext *bitmapContext
= [NSGraphicsContext graphicsContextWithGraphicsPort:contextRef flipped:NO];
_GTMDevAssert(bitmapContext, @"Couldn't create ns bitmap context");
// Save our state and turn off font smoothing and antialias.
CGContextSaveGState(contextRef);
CGContextSetShouldSmoothFonts(contextRef, false);
CGContextSetShouldAntialias(contextRef, false);
[self displayRectIgnoringOpacity:bounds inContext:bitmapContext];
CGImageRef image = CGBitmapContextCreateImage(contextRef);
CFRelease(contextRef);
return (CGImageRef)GTMCFAutorelease(image);
}
// Returns whether gtm_unitTestEncodeState should recurse into subviews
// of a particular view.
// If you have "Full keyboard access" in the
// Keyboard & Mouse > Keyboard Shortcuts preferences pane set to "Text boxes
// and Lists only" that Apple adds a set of subviews to NSTextFields. So in the
// case of NSTextFields we don't want to recurse into their subviews. There may
// be other cases like this, so instead of specializing gtm_unitTestEncodeState: to
// look for NSTextFields, NSTextFields will just not allow us to recurse into
// their subviews.
//
// Returns:
// should gtm_unitTestEncodeState pick up subview state.
- (BOOL)gtm_shouldEncodeStateForSubviews {
return YES;
}
// Encodes the state of an object in a manner suitable for comparing
// against a master state file so we can determine whether the
// object is in a suitable state.
//
// Arguments:
// inCoder - the coder to encode our state into
- (void)gtm_unitTestEncodeState:(NSCoder*)inCoder {
[super gtm_unitTestEncodeState:inCoder];
[inCoder encodeBool:[self isHidden] forKey:@"ViewIsHidden"];
[inCoder encodeObject:[self toolTip] forKey:@"ViewToolTip"];
NSArray *supportedAttrs = [self accessibilityAttributeNames];
if ([supportedAttrs containsObject:NSAccessibilityHelpAttribute]) {
NSString *help
= [self accessibilityAttributeValue:NSAccessibilityHelpAttribute];
[inCoder encodeObject:help forKey:@"ViewAccessibilityHelp"];
}
if ([supportedAttrs containsObject:NSAccessibilityDescriptionAttribute]) {
NSString *description
= [self accessibilityAttributeValue:NSAccessibilityDescriptionAttribute];
[inCoder encodeObject:description forKey:@"ViewAccessibilityDescription"];
}
NSMenu *menu = [self menu];
if (menu) {
[inCoder encodeObject:menu forKey:@"ViewMenu"];
}
if ([self gtm_shouldEncodeStateForSubviews]) {
NSView *subview = nil;
int i = 0;
GTM_FOREACH_OBJECT(subview, [self subviews]) {
[inCoder encodeObject:subview forKey:[NSString stringWithFormat:@"ViewSubView %d", i]];
i = i + 1;
}
}
}
@end
|