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