blob: d13ba68f6e16b18d74a0b2df792066f2bc24e881 [file] [log] [blame] [view]
andybons3322f762015-08-24 21:37:091# Introduction
2
3A collection of idioms that we use when writing the Cocoa views and controllers for Chromium.
4
5## NSWindowController Initialization
6
7To make sure that |window| and |delegate| are wired up correctly in your xib, it's useful to add this to your window controller:
8
9```
10- (void)awakeFromNib {
11 DCHECK([self window]);
12 DCHECK_EQ(self, [[self window] delegate]);
13}
14```
15
16## NSWindowController Cleanup
17
18"You want the window controller to release itself it |-windowDidClose:|, because else it could die while its views are still around. if it (auto)releases itself in the callback, the window and its views are already gone and they won't send messages to the released controller."
19- Nico Weber (thakis@)
20
21See [Window Closing Behavior, ADC Reference](http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Documents/Concepts/WindowClosingBehav.html#//apple_ref/doc/uid/20000027) for the full story.
22
23What this means in practice is:
24
25```
26@interface MyWindowController : NSWindowController<NSWindowDelegate> {
27 IBOutlet NSButton* closeButton_;
28}
29- (IBAction)closeButton:(id)sender;
30@end
31
32@implementation MyWindowController
33- (id)init {
34 if ((self = [super initWithWindowNibName:@"MyWindow" ofType:@"nib"])) {
35 }
36 return self;
37}
38
39- (void)awakeFromNib {
40 // Check that we set the window and its delegate in the XIB.
41 DCHECK([self window]);
42 DCHECK_EQ(self, [[self window] delegate]);
43}
44
45// NSWindowDelegate notification.
46- (void)windowWillClose:(NSNotification*)notif {
47 [self autorelease];
48}
49
50// Action for a button that lets the user close the window.
51- (IBAction)closeButton:(id)sender {
52 // We clean ourselves up after the window has closed.
53 [self close];
54}
55@end
56```
57
58## Unit Tests
59
60There are four Chromium-specific GTest macros for writing ObjC++ test cases. These macros are EXPECT\_NSEQ, EXPECT\_NSNE, and ASSERT variants by the same names. These test `-[id<NSObject> isEqual:]` and will print the object's `-description` in GTest-style if the assertion fails. These macros are defined in //testing/gtest\_mac.h. Just include that file and you can start using them.
61
62This allows you to write this:
63```
64 EXPECT_NSEQ(@"foo", aString);
65```
66Instead of this:
67```
68 EXPECT_TRUE([aString isEqualToString:@"foo"]);
69```