/core/externals/update-engine/externals/google-toolbox-for-mac/AppKit/GTMNSAnimatablePropertyContainerTest.m

http://macfuse.googlecode.com/ · Objective C · 237 lines · 168 code · 39 blank · 30 comment · 1 complexity · e5685cec8b3c95ed63f53ef574e115d7 MD5 · raw file

  1. //
  2. // GTMNSAnimatablePropertyContainerTest.m
  3. //
  4. // Copyright (c) 2010 Google Inc. All rights reserved.
  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 "GTMNSAnimatablePropertyContainerTest.h"
  19. #import "GTMNSAnimatablePropertyContainer.h"
  20. #import "GTMTypeCasting.h"
  21. #import "GTMFoundationUnitTestingUtilities.h"
  22. #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
  23. @implementation GTMNSAnimatablePropertyContainerWindow
  24. #if 0
  25. // Some useful debugging code. Enabled to track animation keys.
  26. - (id)animationForKey:(NSString *)key {
  27. id value = [super animationForKey:key];
  28. NSLog(@"Value: %@ Key: %@", value, key);
  29. return value;
  30. }
  31. #endif
  32. @end
  33. @implementation GTMNSAnimatablePropertyContainerWindowBox
  34. #if 0
  35. // Some useful debugging code. Enabled to track animation keys.
  36. - (id)animationForKey:(NSString *)key {
  37. id value = [super animationForKey:key];
  38. NSLog(@"Value: %@ Key: %@", value, key);
  39. return value;
  40. }
  41. #endif
  42. - (void)set:(NSInteger)value {
  43. #pragma unused(value)
  44. }
  45. @end
  46. @implementation GTMNSAnimatablePropertyContainerWindowController
  47. @synthesize nonLayerBox = nonLayerBox_;
  48. @synthesize layerBox = layerBox_;
  49. - (id)init {
  50. return [super initWithWindowNibName:@"GTMNSAnimatablePropertyContainerTest"];
  51. }
  52. - (void)windowWillClose:(NSNotification *)notification {
  53. if (![[notification object] isEqual:[self window]]) {
  54. [[NSException exceptionWithName:SenTestFailureException
  55. reason:@"Bad window in windowWillClose"
  56. userInfo:nil] raise];
  57. }
  58. [self autorelease];
  59. }
  60. @end
  61. @implementation GTMNSAnimatablePropertyContainerTest
  62. - (void)setUp {
  63. windowController_
  64. = [[GTMNSAnimatablePropertyContainerWindowController alloc] init];
  65. STAssertNotNil(windowController_, nil);
  66. NSWindow *window = [windowController_ window];
  67. STAssertNotNil(window, nil);
  68. timerCalled_ = [[GTMUnitTestingBooleanRunLoopContext alloc] init];
  69. }
  70. - (void)tearDown {
  71. [windowController_ close];
  72. windowController_ = nil;
  73. [timerCalled_ release];
  74. timerCalled_ = nil;
  75. }
  76. - (void)windowAlphaValueStopper:(NSTimer *)timer {
  77. NSWindow *window = GTM_DYNAMIC_CAST(NSWindow, [timer userInfo]);
  78. [timerCalled_ setShouldStop:YES];
  79. [[window gtm_animatorStopper] setAlphaValue:0.25];
  80. STAssertEquals([window alphaValue], (CGFloat)0.25, nil);
  81. }
  82. - (void)windowFrameStopper:(NSTimer *)timer {
  83. NSWindow *window = GTM_DYNAMIC_CAST(NSWindow, [timer userInfo]);
  84. [timerCalled_ setShouldStop:YES];
  85. [[window gtm_animatorStopper] setFrame:NSMakeRect(300, 300, 150, 150)
  86. display:YES];
  87. STAssertEquals([window frame], NSMakeRect(300, 300, 150, 150), nil);
  88. }
  89. - (void)nonLayerFrameStopper:(NSTimer *)timer {
  90. NSView *view = GTM_DYNAMIC_CAST(NSView, [timer userInfo]);
  91. [timerCalled_ setShouldStop:YES];
  92. [[view gtm_animatorStopper] setFrame:NSMakeRect(200, 200, 200, 200)];
  93. STAssertEquals([view frame], NSMakeRect(200, 200, 200, 200), nil);
  94. }
  95. - (void)layerFrameStopper:(NSTimer *)timer {
  96. NSView *view = GTM_DYNAMIC_CAST(NSView, [timer userInfo]);
  97. [timerCalled_ setShouldStop:YES];
  98. [[view gtm_animatorStopper] setFrame:NSMakeRect(200, 200, 200, 200)];
  99. STAssertEquals([view frame], NSMakeRect(200, 200, 200, 200), nil);
  100. }
  101. - (void)testWindowAnimations {
  102. NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
  103. // Test Alpha
  104. NSWindow *window = [windowController_ window];
  105. [window setAlphaValue:1.0];
  106. [timerCalled_ setShouldStop:NO];
  107. [NSAnimationContext beginGrouping];
  108. NSAnimationContext *currentContext = [NSAnimationContext currentContext];
  109. [currentContext setDuration:2];
  110. [[window animator] setAlphaValue:0.5];
  111. [NSAnimationContext endGrouping];
  112. [NSTimer scheduledTimerWithTimeInterval:0.1
  113. target:self
  114. selector:@selector(windowAlphaValueStopper:)
  115. userInfo:window
  116. repeats:NO];
  117. STAssertTrue([runLoop gtm_runUpToSixtySecondsWithContext:timerCalled_], nil);
  118. STAssertEquals([window alphaValue], (CGFloat)0.25, nil);
  119. // Test Frame
  120. [window setFrame:NSMakeRect(100, 100, 100, 100) display:YES];
  121. [timerCalled_ setShouldStop:NO];
  122. [NSAnimationContext beginGrouping];
  123. currentContext = [NSAnimationContext currentContext];
  124. [currentContext setDuration:2];
  125. [[window animator] setFrame:NSMakeRect(200, 200, 200, 200) display:YES];
  126. [NSAnimationContext endGrouping];
  127. [NSTimer scheduledTimerWithTimeInterval:0.1
  128. target:self
  129. selector:@selector(windowFrameStopper:)
  130. userInfo:window
  131. repeats:NO];
  132. STAssertTrue([runLoop gtm_runUpToSixtySecondsWithContext:timerCalled_], nil);
  133. STAssertEquals([window frame], NSMakeRect(300, 300, 150, 150), nil);
  134. // Test non-animation value
  135. [window setTitle:@"Foo"];
  136. [[window gtm_animatorStopper] setTitle:@"Bar"];
  137. STAssertEquals([window title], @"Bar", nil);
  138. // Test bad selector
  139. STAssertThrows([[window gtm_animatorStopper] testWindowAnimations], nil);
  140. }
  141. - (void)testNonLayerViewAnimations {
  142. NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
  143. NSBox *nonLayerBox = [windowController_ nonLayerBox];
  144. STAssertNotNil(nonLayerBox, nil);
  145. // Test frame
  146. [nonLayerBox setFrame:NSMakeRect(50, 50, 50, 50)];
  147. [timerCalled_ setShouldStop:NO];
  148. [NSAnimationContext beginGrouping];
  149. NSAnimationContext *currentContext = [NSAnimationContext currentContext];
  150. [currentContext setDuration:2];
  151. [[nonLayerBox animator] setFrame:NSMakeRect(100, 100, 100, 100)];
  152. [NSAnimationContext endGrouping];
  153. [NSTimer scheduledTimerWithTimeInterval:0.1
  154. target:self
  155. selector:@selector(nonLayerFrameStopper:)
  156. userInfo:nonLayerBox
  157. repeats:NO];
  158. STAssertTrue([runLoop gtm_runUpToSixtySecondsWithContext:timerCalled_], nil);
  159. STAssertEquals([nonLayerBox frame], NSMakeRect(200, 200, 200, 200), nil);
  160. // Test non-animation value
  161. [nonLayerBox setToolTip:@"Foo"];
  162. [[nonLayerBox gtm_animatorStopper] setToolTip:@"Bar"];
  163. STAssertEquals([nonLayerBox toolTip], @"Bar", nil);
  164. // Test bad selector
  165. STAssertThrows([[nonLayerBox gtm_animatorStopper] testNonLayerViewAnimations],
  166. nil);
  167. }
  168. - (void)testLayerViewAnimations {
  169. NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
  170. NSBox *layerBox = [windowController_ layerBox];
  171. STAssertNotNil(layerBox, nil);
  172. // Test frame
  173. [layerBox setFrame:NSMakeRect(50, 50, 50, 50)];
  174. [timerCalled_ setShouldStop:NO];
  175. [NSAnimationContext beginGrouping];
  176. NSAnimationContext *currentContext = [NSAnimationContext currentContext];
  177. [currentContext setDuration:2];
  178. [[layerBox animator] setFrame:NSMakeRect(100, 100, 100, 100)];
  179. [NSAnimationContext endGrouping];
  180. [NSTimer scheduledTimerWithTimeInterval:0.1
  181. target:self
  182. selector:@selector(layerFrameStopper:)
  183. userInfo:layerBox
  184. repeats:NO];
  185. STAssertTrue([runLoop gtm_runUpToSixtySecondsWithContext:timerCalled_], nil);
  186. STAssertEquals([layerBox frame], NSMakeRect(200, 200, 200, 200), nil);
  187. // Test non-animation value
  188. [layerBox setToolTip:@"Foo"];
  189. [[layerBox gtm_animatorStopper] setToolTip:@"Bar"];
  190. STAssertEquals([layerBox toolTip], @"Bar", nil);
  191. // Test bad selector
  192. STAssertThrows([[layerBox gtm_animatorStopper] testLayerViewAnimations],
  193. nil);
  194. // Test Short Selector
  195. STAssertThrows([[layerBox gtm_animatorStopper] set:1], nil);
  196. }
  197. @end
  198. #endif // MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5