PageRenderTime 110ms CodeModel.GetById 43ms app.highlight 64ms RepoModel.GetById 1ms app.codeStats 1ms

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