Files
cappuccino/Tests/AppKit/CPCheckBoxTest.j
T
Alexandre Wilhelm 6fe9dae3d6 Fixed: AppKit unit-tests are not standalone
Previously, every AppKit tests used the same sharedApplication. Due to this implementation, a could not pass because a previous test made failed the current test. For instance, a test could fail because the window of the previous test resigned (just imagine a new window is the key window in the current test), and this resign could raise an error. The error was displayed for the current test thought this test was perfect !

We now instead of using sharedApplication create a new CPApplication per unit-test file in the class method setUp.
2015-07-07 14:12:19 -07:00

103 lines
4.6 KiB
Plaintext

@import <AppKit/CPCheckBox.j>
@implementation CPCheckBoxTest : OJTestCase
{
}
+ (void)setUp
{
// This will init the global var CPApp which are used internally in the AppKit
[[CPApplication alloc] init];
}
/*!
Verify that CPCheckBox placeholders work, both explicit and default ones.
*/
- (void)testPlaceholders
{
var control = [[CPCheckBox alloc] initWithFrame:CGRectMakeZero()];
[control setAllowsMixedState:YES];
var content =
[
CPOffState,
CPOnState
],
arrayController = [[CPArrayController alloc] initWithContent:content];
// First test defaults.
[control bind:CPValueBinding toObject:arrayController withKeyPath:@"selection.self" options:nil];
[arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndexesInRange:CPMakeRange(0, 2)]];
[self assert:CPMixedState equals:[control objectValue] message:"default CPMultipleValuesPlaceholderBindingOption placeholder should be 'mixed'"];
[arrayController setSelectionIndexes:[CPIndexSet indexSet]];
[self assert:CPOffState equals:[control objectValue] message:"default CPNoSelectionMarker placeholder should be 'off'"];
// There seems to be no obvious way to trigger a CPNullMarker here. Setting an index outside of the allowed range
// results in a no selection marker. In Cocoa, adding a [CPNull null] to the content array just results in it being
// converted (oddly) to CPOnState when in mixed state and an exception when not. The placeholder doesn't play in.
//[arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndex:2]];
//[self assert:CPOffState equals:[control objectValue] message:"default CPNullMarker placeholder should be 'off'"];
// And just to verify things are making sense.
[arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndex:0]];
[self assert:CPOffState equals:[control objectValue] message:"content 0 is off"];
[arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndex:1]];
[self assert:CPOnState equals:[control objectValue] message:"content 1 is on"];
[control unbind:CPValueBinding];
// Set up a new binding with explicit placeholders, different from the default ones.
var options = @{
CPMultipleValuesPlaceholderBindingOption: CPOffState,
CPNoSelectionPlaceholderBindingOption: CPOnState,
CPNotApplicablePlaceholderBindingOption: CPOnState,
CPNullPlaceholderBindingOption: CPMixedState,
};
[control bind:CPValueBinding toObject:arrayController withKeyPath:@"selection.self" options:options];
[arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndexesInRange:CPMakeRange(0, 2)]];
[self assert:CPOffState equals:[control objectValue] message:"explicit CPMultipleValuesPlaceholderBindingOption placeholder should be 'off'"];
[arrayController setSelectionIndexes:[CPIndexSet indexSet]];
[self assert:CPOnState equals:[control objectValue] message:"explicit CPNoSelectionMarker placeholder should be 'on'"];
// See note above on how a CPNullPlaceholder is never really triggered for a checkbox.
//[arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndex:2]];
//[self assert:CPMixedState equals:[control objectValue] message:"explicit CPNullMarker placeholder should be 'mixed'"];
[arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndex:0]];
[self assert:CPOffState equals:[control objectValue] message:"content 0 is off"];
[arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndex:1]];
[self assert:CPOnState equals:[control objectValue] message:"content 1 is on"];
}
- (void)testTransformValueBinding
{
var control = [[CPCheckBox alloc] initWithFrame:CGRectMakeZero()];
var content =
[
@{ @"state": YES },
@{ @"state": NO }
],
arrayController = [[CPArrayController alloc] initWithContent:content];
// First test defaults.
[control bind:CPValueBinding toObject:arrayController withKeyPath:@"selection.state" options:@{ CPValueTransformerNameBindingOption: @"CPNegateBoolean" }];
[arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndex:0]];
[self assert:CPOffState equals:[control objectValue] message:"content[0] is negated"]
[arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndex:1]];
[self assert:CPOnState equals:[control objectValue] message:"content[1] is negated"]
[control performClick:nil];
[self assert:CPOffState equals:[control objectValue] message:"value was changed"]
[self assert:YES equals:[content[1] valueForKey:@"state"] message:"content[1] was negated after change"]
}
@end