Files
cappuccino/Tests/AppKit/CPTextFieldTest.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

73 lines
2.4 KiB
Plaintext

@import <AppKit/CPTextField.j>
@implementation CPTextFieldTest : OJTestCase
{
}
+ (void)setUp
{
// This will init the global var CPApp which are used internally in the AppKit
[[CPApplication alloc] init];
}
/*!
Detect regressions with JS object theme attribute encoding in subclasses.
This test is not actually CPTextField specific at all. This just happens
to be the first place the keyed archiving bug was discovered, and there is
an additional, more specific, test in CPKeyedArchiverTest.
*/
- (void)testArchiveThemeAttributes
{
var view = [[CPTextFieldSubclass alloc] initWithFrame:CGRectMakeZero()],
decoded = [CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:view]];
[self assert:[view valueForThemeAttribute:@"content-inset"].top equals:2 message:@"content-inset should initialise correctly"];
[self assert:[view valueForThemeAttribute:@"content-inset"].top equals:[decoded valueForThemeAttribute:@"content-inset"].top message:@"content-inset should unarchive correctly"];
}
- (void)testFormatters
{
var control = [[CPTextField alloc] initWithFrame:CGRectMakeZero()],
numberFormatter = [[CPNumberFormatter alloc] init];
[numberFormatter setNumberStyle:CPNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:3];
[numberFormatter setGeneratesDecimalNumbers:NO];
[control setFormatter:numberFormatter];
[control setStringValue:@"12.3456"];
[self assertTrue:[[control objectValue] isKindOfClass:CPNumber] message:@"object should be a number"];
// Note that the control stores the value with a different precision than the maximum fraction
// digits of the number formatter. It's a little surprising but this makes the implementation easier
// and Cocoa does it too.
[self assert:[CPNumber numberWithFloat:12.3456] equals:[control objectValue]];
[self assert:"12.346" equals:[control stringValue]];
[control setFloatValue:45.3456];
[self assertTrue:"45.346" === [control stringValue]];
[control setFormatter:nil];
[control setStringValue:@"12"];
[self assert:@"12" equals:[control objectValue]];
}
@end
@implementation CPTextFieldSubclass : CPTextField
{
}
- (id)initWithFrame:(CGRect)aFrame
{
if (self = [super initWithFrame:aFrame])
{
[self setValue:CGInsetMake(2.0, 2.0, 2.0, 2.0) forThemeAttribute:"content-inset"];
}
return self;
}
@end