From 01e35efdbb3bab2ebe252ce9859f0d56bb40d228 Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Wed, 7 Sep 2011 16:04:35 +0100 Subject: [PATCH] Refs #1343. Recreate manual bindings test as an automatic test. --- Tests/AppKit/CPArrayControllerTest.j | 60 +++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/Tests/AppKit/CPArrayControllerTest.j b/Tests/AppKit/CPArrayControllerTest.j index 3513f1a20..afb2a9e3f 100644 --- a/Tests/AppKit/CPArrayControllerTest.j +++ b/Tests/AppKit/CPArrayControllerTest.j @@ -464,6 +464,42 @@ [self assert:1 equals:[observations count] message:@"exactly 1 notification for insertObject x 2 (clearsFilterPredicate YES)"]; } +/** + Replicate the situation found in the Bindings manual test where one array controller's + contents depend on the selection of another. +*/ +- (void)testObservationBetweenBoundControllers +{ + var companies = [Companies new], + companiesController = [CPArrayController new], + employeesController = [CPArrayController new]; + + [companiesController bind:@"contentArray" toObject:companies withKeyPath:@"items" options:nil]; + [employeesController bind:@"contentArray" toObject:companiesController withKeyPath:@"selection.employees" options:nil]; + + [companiesController addObserver:self forKeyPath:@"selection" options:0 context:@"companies.selection"]; + [companiesController addObserver:self forKeyPath:@"selectionIndexes" options:0 context:@"companies.selectionIndexes"]; + [companiesController addObserver:self forKeyPath:@"arrangedObjects" options:0 context:@"companies.arrangedObjects"]; + [employeesController addObserver:self forKeyPath:@"selectionIndexes" options:0 context:@"employees.selectionIndexes"]; + [employeesController addObserver:self forKeyPath:@"selection" options:0 context:@"employees.selection"]; + [employeesController addObserver:self forKeyPath:@"arrangedObjects" options:0 context:@"employees.arrangedObjects"]; + + observations = []; + + [companiesController setSelectionIndexes:[CPIndexSet indexSetWithIndex:1]]; + + // There should be exactly one observation of each kind, and they should be in the right + // order. + [self assert:5 equals:[observations count]]; + [self assert:@"companies.selectionIndexes" equals:observations[0].context]; + [self assert:@"employees.arrangedObjects" equals:observations[1].context]; + [self assert:@"employees.selectionIndexes" equals:observations[2].context]; + [self assert:@"employees.selection" equals:observations[3].context]; + // This observation registers after the employees update because we are later in the + // observation queue than the binding. + [self assert:@"companies.selection" equals:observations[4].context]; +} + - (void)testCompoundKeyPaths { var departmentNameField = [[CPTextField alloc] init]; @@ -534,6 +570,7 @@ [observations addObject:{ keyPath: keyPath, + context: context, oldValue: [change valueForKey:CPKeyValueChangeOldKey], newValue: [change valueForKey:CPKeyValueChangeNewKey] }]; @@ -620,4 +657,25 @@ return self; } -@end \ No newline at end of file +@end + +@implementation Companies : CPObject +{ + CPMutableArray items @accessors; +} + +- (id)init +{ + self = [super init]; + + if (self) + { + items = [CPMutableArray array]; + [items addObject:[CPDictionary dictionaryWithObjectsAndKeys:@"Spacely Sprockets", @"name", [CPArray arrayWithObjects:@"Tom", @"Dick", @"Harry"], @"employees"]]; + [items addObject:[CPDictionary dictionaryWithObjectsAndKeys:@"Cogswell Cogs", @"name", [CPArray arrayWithObjects:@"Jane", @"Mary", @"Vic"], @"employees"]]; + } + + return self; +} + +@end