/core/externals/update-engine/externals/google-toolbox-for-mac/UnitTesting/GTMUnitTestingTest.m

http://macfuse.googlecode.com/ · Objective C · 253 lines · 173 code · 37 blank · 43 comment · 2 complexity · ff672914d2bead8b0624a97852a2a9ea MD5 · raw file

  1. //
  2. // GTMUnitTestingTest.m
  3. //
  4. // Copyright 2008 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 "GTMUnitTestingTest.h"
  20. #import "GTMAppKit+UnitTesting.h"
  21. NSString *const kGTMWindowNibName = @"GTMUnitTestingTest";
  22. NSString *const kGTMWindowSaveFileName = @"GTMUnitTestingWindow";
  23. @interface GTMUnitTestingTest : GTMTestCase {
  24. int expectedFailureCount_;
  25. }
  26. @end
  27. // GTMUnitTestingTest support classes
  28. @interface GTMUnitTestingView : NSObject <GTMUnitTestViewDrawer> {
  29. BOOL goodContext_;
  30. }
  31. - (BOOL)hadGoodContext;
  32. @end
  33. @interface GTMUnitTestingDelegate : NSObject <NSImageDelegate> {
  34. BOOL didEncode_;
  35. }
  36. - (BOOL)didEncode;
  37. @end
  38. @interface GTMUnitTestingProxyTest : NSProxy
  39. - (id)init;
  40. @end
  41. @implementation GTMUnitTestingTest
  42. // Brings up the window defined in the nib and takes a snapshot of it.
  43. // We use the "empty" GTMUnitTestingTestController controller so that
  44. // initWithWindowNibName can find the appropriate bundle to load our nib from.
  45. // For some reason when running unit tests, with all the injecting going on
  46. // the nib loader can get confused as to where it should load a nib from.
  47. // Having a NSWindowController subclass in the same bundle as the nib seems
  48. // to help the nib loader find the nib, and any custom classes that are attached
  49. // to it.
  50. - (void)testUnitTestingFramework {
  51. // set up our delegates so we can test delegate handling
  52. GTMUnitTestingDelegate *appDelegate = [[GTMUnitTestingDelegate alloc] init];
  53. [NSApp setDelegate:appDelegate];
  54. // Get our window
  55. GTMUnitTestingTestController *testWindowController
  56. = [[GTMUnitTestingTestController alloc] initWithWindowNibName:kGTMWindowNibName];
  57. NSWindow *window = [testWindowController window];
  58. // Test the app state. This will cover windows and menus
  59. GTMAssertObjectStateEqualToStateNamed(NSApp,
  60. @"GTMUnitTestingTestApp",
  61. @"Testing the app state");
  62. // Test the window image and state
  63. GTMAssertObjectEqualToStateAndImageNamed(window,
  64. kGTMWindowSaveFileName,
  65. @"Testing the window image and state");
  66. // Verify that all of our delegate encoders got called
  67. STAssertTrue([appDelegate didEncode], @"app delegate didn't get called?");
  68. // Clean up
  69. [NSApp setDelegate:nil];
  70. [appDelegate release];
  71. [testWindowController release];
  72. }
  73. - (void)testViewUnitTesting {
  74. GTMUnitTestingView *unitTestingView = [[GTMUnitTestingView alloc] init];
  75. GTMAssertDrawingEqualToImageNamed(unitTestingView,
  76. NSMakeSize(200,200),
  77. @"GTMUnitTestingView",
  78. NSApp,
  79. @"Testing view drawing");
  80. STAssertTrue([unitTestingView hadGoodContext], @"bad context?");
  81. [unitTestingView release];
  82. }
  83. - (void)testImageUnitTesting {
  84. NSImage *image = [NSImage imageNamed:@"NSApplicationIcon"];
  85. GTMUnitTestingDelegate *imgDelegate = [[GTMUnitTestingDelegate alloc] init];
  86. [image setDelegate:imgDelegate];
  87. GTMAssertObjectEqualToStateAndImageNamed(image,
  88. @"GTMUnitTestingImage",
  89. @"Testing NSImage image and state");
  90. STAssertTrue([imgDelegate didEncode], @"imgDelegate didn't get called?");
  91. [image setDelegate:nil];
  92. [imgDelegate release];
  93. }
  94. - (void)testFailures {
  95. NSString *const bogusTestName = @"GTMUnitTestTestingFailTest";
  96. NSString *tempDir = NSTemporaryDirectory();
  97. STAssertNotNil(tempDir, @"No Temp Dir?");
  98. NSString *originalPath = [NSObject gtm_getUnitTestSaveToDirectory];
  99. STAssertNotNil(originalPath, @"No save dir?");
  100. [NSObject gtm_setUnitTestSaveToDirectory:tempDir];
  101. STAssertEqualObjects(tempDir, [NSObject gtm_getUnitTestSaveToDirectory],
  102. @"Save to dir not set?");
  103. NSString *statePath = [self gtm_saveToPathForStateNamed:bogusTestName];
  104. STAssertNotNil(statePath, @"no state path?");
  105. NSString *imagePath = [self gtm_saveToPathForImageNamed:bogusTestName];
  106. STAssertNotNil(imagePath, @"no image path?");
  107. GTMUnitTestingTestController *testWindowController
  108. = [[GTMUnitTestingTestController alloc] initWithWindowNibName:kGTMWindowNibName];
  109. NSWindow *window = [testWindowController window];
  110. // Test against a golden master filename that doesn't exist
  111. expectedFailureCount_ = 2;
  112. GTMAssertObjectEqualToStateAndImageNamed(window,
  113. bogusTestName,
  114. @"Creating image and state files");
  115. STAssertEquals(expectedFailureCount_, 0,
  116. @"Didn't get expected failures creating files");
  117. // Change our image and state and verify failures
  118. [[testWindowController textField] setStringValue:@"Foo"];
  119. expectedFailureCount_ = 2;
  120. GTMAssertObjectEqualToStateAndImageNamed(window,
  121. kGTMWindowSaveFileName,
  122. @"Testing the window image and state");
  123. STAssertEquals(expectedFailureCount_, 0,
  124. @"Didn't get expected failures testing files");
  125. // Now change the size of our image and verify failures
  126. NSRect oldFrame = [window frame];
  127. NSRect newFrame = oldFrame;
  128. newFrame.size.width += 1;
  129. [window setFrame:newFrame display:YES];
  130. expectedFailureCount_ = 1;
  131. GTMAssertObjectImageEqualToImageNamed(window,
  132. kGTMWindowSaveFileName,
  133. @"Testing the changed window size");
  134. [window setFrame:oldFrame display:YES];
  135. // Set our unit test save dir to a bogus directory and
  136. // run the tests again.
  137. [NSObject gtm_setUnitTestSaveToDirectory:@"/zim/blatz/foo/bob/bar"];
  138. expectedFailureCount_ = 2;
  139. GTMAssertObjectEqualToStateAndImageNamed(window,
  140. kGTMWindowSaveFileName,
  141. @"Testing the window image and state");
  142. STAssertEquals(expectedFailureCount_, 0,
  143. @"Didn't get expected failures testing files");
  144. expectedFailureCount_ = 2;
  145. GTMAssertObjectEqualToStateAndImageNamed(window,
  146. @"GTMUnitTestingWindowDoesntExist",
  147. @"Testing the window image and state");
  148. STAssertEquals(expectedFailureCount_, 0,
  149. @"Didn't get expected failures testing files");
  150. // Reset our unit test save dir
  151. [NSObject gtm_setUnitTestSaveToDirectory:nil];
  152. // Test against something that doesn't have an image
  153. expectedFailureCount_ = 1;
  154. GTMAssertObjectImageEqualToImageNamed(@"a string",
  155. @"GTMStringsDontHaveImages",
  156. @"Testing that strings should fail");
  157. STAssertEquals(expectedFailureCount_, 0, @"Didn't get expected failures testing files");
  158. // Test against something that doesn't implement our support
  159. expectedFailureCount_ = 1;
  160. GTMUnitTestingProxyTest *proxy = [[GTMUnitTestingProxyTest alloc] init];
  161. GTMAssertObjectStateEqualToStateNamed(proxy,
  162. @"NSProxiesDontDoState",
  163. @"Testing that NSProxy should fail");
  164. STAssertEquals(expectedFailureCount_, 0, @"Didn't get expected failures testing proxy");
  165. [proxy release];
  166. [window close];
  167. }
  168. - (void)failWithException:(NSException *)anException {
  169. if (expectedFailureCount_ > 0) {
  170. expectedFailureCount_ -= 1;
  171. } else {
  172. [super failWithException:anException]; // COV_NF_LINE - not expecting exception
  173. }
  174. }
  175. @end
  176. @implementation GTMUnitTestingTestController
  177. - (NSTextField *)textField {
  178. return field_;
  179. }
  180. @end
  181. @implementation GTMUnitTestingDelegate
  182. - (void)gtm_unitTestEncoderWillEncode:(id)sender inCoder:(NSCoder*)inCoder {
  183. // Test various encodings
  184. [inCoder encodeBool:YES forKey:@"BoolTest"];
  185. [inCoder encodeInt:1 forKey:@"IntTest"];
  186. [inCoder encodeInt32:1 forKey:@"Int32Test"];
  187. [inCoder encodeInt64:1 forKey:@"Int64Test"];
  188. [inCoder encodeFloat:1.0f forKey:@"FloatTest"];
  189. [inCoder encodeDouble:1.0 forKey:@"DoubleTest"];
  190. [inCoder encodeBytes:(const uint8_t*)"BytesTest" length:9 forKey:@"BytesTest"];
  191. didEncode_ = YES;
  192. }
  193. - (BOOL)didEncode {
  194. return didEncode_;
  195. }
  196. @end
  197. @implementation GTMUnitTestingView
  198. - (void)gtm_unitTestViewDrawRect:(NSRect)rect contextInfo:(void*)contextInfo {
  199. [[NSColor redColor] set];
  200. NSRectFill(rect);
  201. goodContext_ = [(id)contextInfo isEqualTo:NSApp];
  202. }
  203. - (BOOL)hadGoodContext {
  204. return goodContext_;
  205. }
  206. @end
  207. // GTMUnitTestingProxyTest is for testing the case where we don't conform to
  208. // the GTMUnitTestingEncoding protocol.
  209. @implementation GTMUnitTestingProxyTest
  210. - (id)init {
  211. return self;
  212. }
  213. - (BOOL)conformsToProtocol:(Protocol *)protocol {
  214. return NO;
  215. }
  216. @end