PageRenderTime 24ms CodeModel.GetById 13ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

/Tests/AppKit/CPKeyValueBindingSimpleBindingsTest.j

http://github.com/cacaodev/cappuccino
Unknown | 196 lines | 148 code | 48 blank | 0 comment | 0 complexity | 30b5ddf868a6180326f41cb5cc887271 MD5 | raw file
  1
  2@import <AppKit/AppKit.j>
  3
  4@class Track
  5
  6/*!
  7    Bindings tests exercising the functionality seen in the Cocoa example "SimpleBindingsAdoption".
  8*/
  9@implementation CPKeyValueBindingSimpleBindingsTest : OJTestCase
 10{
 11    CPTextField         textField @accessors;
 12    CPSlider            slider @accessors;
 13    CPButton            button @accessors;
 14
 15    Track               track @accessors;
 16
 17    CPWindow            theWindow @accessors;
 18    CPObjectController  objectController @accessors;
 19}
 20
 21- (void)setUp
 22{
 23    // CPApp must be initialised or action sending will not work.
 24    [CPApplication sharedApplication];
 25
 26    track = [Track new];
 27    [track setVolume:5.0];
 28}
 29
 30- (void)createControls
 31{
 32    textField = [CPTextField textFieldWithStringValue:@"" placeholder:@"" width:100];
 33    slider = [[CPSlider alloc] initWithFrame:CGRectMakeZero()];
 34    button = [CPButton buttonWithTitle:@"Mute"];
 35}
 36
 37/*!
 38    Base case without bindings.
 39*/
 40- (void)testSimpleBindings01
 41{
 42    [self createControls];
 43
 44    [textField setTarget:self];
 45    [textField setAction:@selector(updateVolumeFrom:)];
 46
 47    [slider setTarget:self];
 48    [slider setAction:@selector(updateVolumeFrom:)];
 49
 50    [button setTarget:self];
 51    [button setAction:@selector(muteTrack01:)];
 52
 53    // Test the interaction.
 54    [textField setStringValue:@"0.7"];
 55    // Simulate user interaction to fire action.
 56    [textField performClick:self];
 57
 58    [self verifyVolume:0.7 method:"actions"];
 59
 60    [slider setFloatValue:9.0];
 61    [slider performClick:self];
 62
 63    [self verifyVolume:9 method:"actions"];
 64
 65    [button performClick:self];
 66
 67    [self verifyVolume:0 method:"actions"];
 68}
 69
 70/*!
 71    Using bindings and an object controller.
 72*/
 73- (void)testSimpleBindings02
 74{
 75    [self createControls];
 76
 77    objectController = [CPObjectController new];
 78    [objectController bind:@"contentObject" toObject:self withKeyPath:@"track" options:nil];
 79
 80    [textField bind:@"value" toObject:objectController withKeyPath:@"selection.volume" options:nil];
 81    [slider bind:@"value" toObject:objectController withKeyPath:@"selection.volume" options:nil];
 82
 83    [button setTarget:self];
 84    [button setAction:@selector(muteTrack:)];
 85
 86    // Test the interaction.
 87    [textField setStringValue:@"0.7"];
 88    // Simulate user interaction. By default bindings update on action only.
 89    [textField performClick:self];
 90
 91    [self verifyVolume:0.7 method:"bindings"];
 92
 93    [slider setFloatValue:9.0];
 94    [slider performClick:self];
 95
 96    [self verifyVolume:9 method:"bindings"];
 97
 98    [button performClick:self];
 99
100    [self verifyVolume:0 method:"bindings"];
101}
102
103/*!
104    Using bindings and a controller set up in a cib (e.g. using Interface Builder).
105*/
106- (void)testSimpleBindings03
107{
108    // Note: this cib will connect 'objectController'. This is only for debugging purposes,
109    // this code should run with or without that connection.
110    var cib = [CPBundle loadCibFile:[[CPBundle bundleForClass:CPKeyValueBindingSimpleBindingsTest] pathForResource:"SimpleBindingsAdoption_03.cib"] externalNameTable:@{ CPCibOwner: self }];
111
112    // Test the interaction.
113    [textField setStringValue:@"0.7"];
114    // Simulate user interaction. By default bindings update on action only.
115    [textField performClick:self];
116
117    [self verifyVolume:0.7 method:"bindings"];
118
119    [slider setFloatValue:9.0];
120    [slider performClick:self];
121
122    [self verifyVolume:9 method:"bindings"];
123
124    [button performClick:self];
125
126    [self verifyVolume:0 method:"bindings"];
127
128    // Test if CPNullPlaceholderBindingOption is correctly decoded
129    var bindingInfo = [textField infoForBinding:@"value"],
130        nullPlaceHolder = [[bindingInfo objectForKey:CPOptionsKey] objectForKey:CPNullPlaceholderBindingOption];
131    [self assert:@"Nothing" equals:nullPlaceHolder];
132}
133
134- (void)verifyVolume:(float)aVolume method:(CPString)aMethod
135{
136    [self assert:aVolume equals:[track volume] message:"volume should update through " + aMethod];
137    [self assert:[track volume] equals:[textField floatValue]];
138    [self assert:[track volume] equals:[slider floatValue]];
139}
140
141#pragma mark ----- actions used by implementation 01 -----
142
143- (void)updateVolumeFrom:(id)sender
144{
145    var newVolume = [sender floatValue];
146    [track setVolume:newVolume];
147    [self updateUserInterface];
148}
149
150- (void)updateUserInterface
151{
152    [slider setFloatValue:[track volume]];
153    [textField setFloatValue:[track volume]];
154}
155
156- (void)muteTrack01:(id)sender
157{
158    [track setVolume:0.0];
159    [self updateUserInterface];
160}
161
162#pragma mark ----- actions used by implementation 02 and 03 -----
163
164- (void)muteTrack:(id)sender
165{
166    [track setVolume:0.0];
167}
168
169@end
170
171@implementation Track : CPObject
172{
173    float       volume @accessors;
174    CPString    title @accessors;
175}
176
177- (void)setVolume:(float)aValue
178{
179    if (volume != aValue)
180    {
181        // The Cocoa version does not need this explicit conversion but we do,
182        // because otherwise bindings will turn this into a string when reading
183        // from the text field. A side effect of loose typing.
184        volume = parseFloat(aValue);
185    }
186}
187
188- (void)setTitle:(CPString)newTitle
189{
190    if (title != newTitle)
191    {
192        title = [newTitle copy];
193    }
194}
195
196@end