PageRenderTime 93ms CodeModel.GetById 8ms app.highlight 81ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/google-toolbox-for-mac/AppKit/GTMWindowSheetControllerTest.m

http://macfuse.googlecode.com/
Objective C | 295 lines | 222 code | 45 blank | 28 comment | 0 complexity | 98439df8aeb74e2488ce19d3dc29c805 MD5 | raw file
  1//
  2//  GTMWindowSheetControllerTest.m
  3//
  4//  Copyright 2009 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 "GTMWindowSheetController.h"
 21#import "GTMNSObject+UnitTesting.h"
 22
 23@interface GTMWindowSheetControllerTest : GTMTestCase
 24                                          <GTMWindowSheetControllerDelegate,
 25                                           NSTabViewDelegate> {
 26 @private
 27  GTMWindowSheetController *sheetController_;
 28  NSWindow* window_;
 29  BOOL didAlertClose_;
 30  BOOL didSheetClose_;
 31}
 32- (void)alertDidEnd:(NSAlert *)alert
 33         returnCode:(NSInteger)returnCode
 34            context:(void *)context;
 35- (void)openSecondSheet:(NSAlert *)alert
 36             returnCode:(NSInteger)returnCode
 37                context:(void *)context;
 38- (void)sheetDidEnd:(NSWindow *)sheet
 39         returnCode:(NSInteger)returnCode
 40            context:(void *)context;
 41@end
 42
 43@implementation GTMWindowSheetControllerTest
 44
 45- (void)testOpenTwoSheetsAndSwitch {
 46  // Set up window
 47  NSWindow *window =
 48      [[[NSWindow alloc] initWithContentRect:NSMakeRect(100, 100, 600, 600)
 49                                   styleMask:NSTitledWindowMask
 50                                     backing:NSBackingStoreBuffered
 51                                       defer:NO] autorelease];
 52  STAssertNotNil(window, @"Could not allocate window");
 53  NSTabView *tabView =
 54      [[[NSTabView alloc] initWithFrame:NSMakeRect(10, 10, 580, 580)]
 55      autorelease];
 56  STAssertNotNil(tabView, @"Could not allocate tab view");
 57  [[window contentView] addSubview:tabView];
 58  [tabView setDelegate:self];
 59
 60  NSTabViewItem *item1 =
 61      [[[NSTabViewItem alloc] initWithIdentifier:@"one"] autorelease];
 62  [item1 setLabel:@"One"];
 63  NSTabViewItem *item2 =
 64      [[[NSTabViewItem alloc] initWithIdentifier:@"two"] autorelease];
 65  [item2 setLabel:@"Two"];
 66  [tabView addTabViewItem:item1];
 67  [tabView addTabViewItem:item2];
 68
 69  sheetController_ =
 70      [[[GTMWindowSheetController alloc] initWithWindow:window
 71                                               delegate:self] autorelease];
 72
 73  STAssertFalse([sheetController_ isSheetAttachedToView:
 74                 [[tabView selectedTabViewItem] view]],
 75                @"Sheet should not be attached to current view");
 76  STAssertEquals([[sheetController_ viewsWithAttachedSheets] count],
 77                 (NSUInteger)0,
 78                 @"Should have no views with sheets");
 79
 80  // Pop alert on first tab
 81  NSAlert* alert = [[NSAlert alloc] init];
 82
 83  [alert setMessageText:@"Hell Has Broken Loose."];
 84  [alert setInformativeText:@"All hell has broken loose. You may want to run "
 85                            @"outside screaming and waving your arms around "
 86                            @"wildly."];
 87
 88  NSButton *alertButton = [alert addButtonWithTitle:@"OK"];
 89
 90  [sheetController_ beginSystemSheet:alert
 91                        modalForView:[item1 view]
 92                      withParameters:[NSArray arrayWithObjects:
 93                                      [NSNull null],
 94                                      self,
 95                                      [NSValue valueWithPointer:
 96                                        @selector(alertDidEnd:returnCode:context:)],
 97                                      [NSValue valueWithPointer:nil],
 98                                      nil]];
 99  didAlertClose_ = NO;
100
101  STAssertTrue([sheetController_ isSheetAttachedToView:
102                [[tabView selectedTabViewItem] view]],
103                @"Sheet should be attached to current view");
104  STAssertEquals([[sheetController_ viewsWithAttachedSheets] count],
105                 (NSUInteger)1,
106                 @"Should have one view with sheets");
107
108  [tabView selectTabViewItem:item2];
109
110  STAssertFalse([sheetController_ isSheetAttachedToView:
111                 [[tabView selectedTabViewItem] view]],
112                @"Sheet should not be attached to current view");
113  STAssertEquals([[sheetController_ viewsWithAttachedSheets] count],
114                 (NSUInteger)1,
115                 @"Should have one view with sheets");
116
117  // Pop sheet on second tab
118  NSPanel *sheet =
119      [[[NSPanel alloc] initWithContentRect:NSMakeRect(0, 0, 300, 200)
120                                  styleMask:NSTitledWindowMask
121                                    backing:NSBackingStoreBuffered
122                                      defer:NO] autorelease];
123
124  [sheetController_ beginSheet:sheet
125                  modalForView:[item2 view]
126                 modalDelegate:self
127                didEndSelector:@selector(sheetDidEnd:returnCode:context:)
128                   contextInfo:nil];
129  didSheetClose_ = NO;
130
131  STAssertTrue([sheetController_ isSheetAttachedToView:
132                [[tabView selectedTabViewItem] view]],
133               @"Sheet should be attached to current view");
134  STAssertEquals([[sheetController_ viewsWithAttachedSheets] count],
135                 (NSUInteger)2,
136                 @"Should have two views with sheets");
137
138  [tabView selectTabViewItem:item1];
139
140  STAssertTrue([sheetController_ isSheetAttachedToView:
141                [[tabView selectedTabViewItem] view]],
142               @"Sheet should be attached to current view");
143  STAssertEquals([[sheetController_ viewsWithAttachedSheets] count],
144                 (NSUInteger)2,
145                 @"Should have two views with sheets");
146
147  // Close alert
148  [alertButton performClick:self];
149
150  STAssertFalse([sheetController_ isSheetAttachedToView:
151                 [[tabView selectedTabViewItem] view]],
152                @"Sheet should not be attached to current view");
153  STAssertEquals([[sheetController_ viewsWithAttachedSheets] count],
154                 (NSUInteger)1,
155                 @"Should have one view with sheets");
156  STAssertTrue(didAlertClose_, @"Alert should have closed");
157
158  [tabView selectTabViewItem:item2];
159
160  STAssertTrue([sheetController_ isSheetAttachedToView:
161                [[tabView selectedTabViewItem] view]],
162               @"Sheet should be attached to current view");
163  STAssertEquals([[sheetController_ viewsWithAttachedSheets] count],
164                 (NSUInteger)1,
165                 @"Should have one view with sheets");
166
167  // Close sheet
168  [[NSApplication sharedApplication] endSheet:sheet returnCode:NSOKButton];
169
170  STAssertFalse([sheetController_ isSheetAttachedToView:
171                 [[tabView selectedTabViewItem] view]],
172                @"Sheet should not be attached to current view");
173  STAssertEquals([[sheetController_ viewsWithAttachedSheets] count],
174                 (NSUInteger)0,
175                 @"Should have no views with sheets");
176  STAssertTrue(didSheetClose_, @"Sheet should have closed");
177}
178
179- (void)testOpenSheetAfterFirst {
180  // Set up window
181  window_ =
182    [[[NSWindow alloc] initWithContentRect:NSMakeRect(100, 100, 600, 600)
183                                 styleMask:NSTitledWindowMask
184                                   backing:NSBackingStoreBuffered
185                                     defer:NO] autorelease];
186  STAssertNotNil(window_, @"Could not allocate window");
187
188  sheetController_ =
189      [[[GTMWindowSheetController alloc] initWithWindow:window_
190                                               delegate:self] autorelease];
191
192  STAssertFalse([sheetController_ isSheetAttachedToView:
193                 [window_ contentView]],
194                @"Sheet should not be attached to current view");
195  STAssertEquals([[sheetController_ viewsWithAttachedSheets] count],
196                 (NSUInteger)0,
197                 @"Should have no views with sheets");
198
199  // Pop alert on first tab
200  NSAlert* alert = [[NSAlert alloc] init];
201
202  [alert setMessageText:@"Hell Has Broken Loose."];
203  [alert setInformativeText:@"All hell has broken loose. You may want to run "
204                            @"outside screaming and waving your arms around "
205                            @"wildly."];
206
207  NSButton *alertButton = [alert addButtonWithTitle:@"OK"];
208
209  // Allocate a second sheet to be launched by the alert
210  NSPanel *sheet =
211      [[[NSPanel alloc] initWithContentRect:NSMakeRect(0, 0, 300, 200)
212                                  styleMask:NSTitledWindowMask
213                                    backing:NSBackingStoreBuffered
214                                      defer:NO] autorelease];
215
216  [sheetController_ beginSystemSheet:alert
217                        modalForView:[window_ contentView]
218                      withParameters:[NSArray arrayWithObjects:
219                                      [NSNull null],
220                                      self,
221                                      [NSValue valueWithPointer:
222                                        @selector(openSecondSheet:returnCode:context:)],
223                                      [NSValue valueWithPointer:sheet],
224                                      nil]];
225  didAlertClose_ = NO;
226  didSheetClose_ = NO;
227
228  STAssertTrue([sheetController_ isSheetAttachedToView:
229                [window_ contentView]],
230                @"Sheet should be attached to view");
231  STAssertEquals([[sheetController_ viewsWithAttachedSheets] count],
232                 (NSUInteger)1,
233                 @"Should have one view with sheets");
234
235  // Close alert
236  [alertButton performClick:self];
237
238  STAssertTrue([sheetController_ isSheetAttachedToView:
239                [window_ contentView]],
240                @"Second sheet should be attached to view");
241  STAssertEquals([[sheetController_ viewsWithAttachedSheets] count],
242                 (NSUInteger)1,
243                 @"Should have one view with sheets");
244  STAssertTrue(didAlertClose_, @"Alert should have closed");
245
246  // Close sheet
247  [[NSApplication sharedApplication] endSheet:sheet returnCode:NSOKButton];
248
249  STAssertFalse([sheetController_ isSheetAttachedToView:
250                 [window_ contentView]],
251                @"Sheet should not be attached to current view");
252  STAssertEquals([[sheetController_ viewsWithAttachedSheets] count],
253                 (NSUInteger)0,
254                 @"Should have no views with sheets");
255  STAssertTrue(didSheetClose_, @"Sheet should have closed");
256}
257
258- (void)alertDidEnd:(NSAlert *)alert
259         returnCode:(NSInteger)returnCode
260            context:(void *)context {
261  didAlertClose_ = YES;
262}
263
264- (void)openSecondSheet:(NSAlert *)alert
265             returnCode:(NSInteger)returnCode
266                context:(void *)context {
267  didAlertClose_ = YES;
268
269  NSPanel* sheet = context;
270  // Pop a second sheet
271  [sheetController_ beginSheet:sheet
272                  modalForView:[window_ contentView]
273                 modalDelegate:self
274                didEndSelector:@selector(sheetDidEnd:returnCode:context:)
275                   contextInfo:nil];
276}
277
278- (void)sheetDidEnd:(NSWindow *)sheet
279         returnCode:(NSInteger)returnCode
280            context:(void *)context {
281  didSheetClose_ = YES;
282  [sheet orderOut:self];
283}
284
285- (void)tabView:(NSTabView *)tabView
286didSelectTabViewItem:(NSTabViewItem *)tabViewItem {
287  NSView* view = [tabViewItem view];
288  [sheetController_ setActiveView:view];
289}
290
291- (void)gtm_systemRequestsVisibilityForView:(NSView*)view {
292  STAssertTrue(false, @"Shouldn't be called");
293}
294
295@end