From ab49c6384013144dd7d961ce84aec4a3fa684efa Mon Sep 17 00:00:00 2001 From: Andrew Hankinson Date: Sun, 10 Feb 2013 20:03:41 -0500 Subject: [PATCH 1/2] Fix for #1723: CPArrayController insert This fix addresses the following problems: - fixes a bug with add: where it would try to add the sender object, not the represented object - changes insert: to insert a new represented object after the currently selected object. If no object is currently selected it behaves like add: and adds a new object to the end. This patch includes tests. --- AppKit/CPArrayController.j | 13 +++++++--- Tests/AppKit/CPArrayControllerTest.j | 39 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/AppKit/CPArrayController.j b/AppKit/CPArrayController.j index f907ee032..a712a6f4e 100644 --- a/AppKit/CPArrayController.j +++ b/AppKit/CPArrayController.j @@ -897,7 +897,9 @@ if (![self canAdd]) return; - [self insert:sender]; + var newObject = [self automaticallyPreparesContent] ? [self newObject] : [self _defaultNewObject]; + + [self addObject:newObject]; } /*! @@ -909,9 +911,14 @@ if (![self canInsert]) return; - var newObject = [self automaticallyPreparesContent] ? [self newObject] : [self _defaultNewObject]; + var newObject = [self automaticallyPreparesContent] ? [self newObject] : [self _defaultNewObject], + lastSelectedIndex = [_selectionIndexes lastIndex]; + + if (lastSelectedIndex) + [self insertObject:newObject atArrangedObjectIndex:lastSelectedIndex + 1]; + else + [self addObject:newObject]; - [self addObject:newObject]; } /*! diff --git a/Tests/AppKit/CPArrayControllerTest.j b/Tests/AppKit/CPArrayControllerTest.j index 83687650f..0de6cbd93 100644 --- a/Tests/AppKit/CPArrayControllerTest.j +++ b/Tests/AppKit/CPArrayControllerTest.j @@ -184,7 +184,46 @@ [self assert:[CPNumber numberWithInt:2] equals:[_arrayController arrangedObjects][1]]; } +- (void)testAdd +{ + _contentArray = []; + _arrayController = [[CPArrayController alloc] initWithContent:_contentArray]; + [self _testAdd]; +} +- (void)_testAdd +{ + [_arrayController setObjectClass:[Employee class]]; + [self assert:[[_arrayController contentArray] count] equals:0]; + [_arrayController add:nil]; + [self assert:[[_arrayController contentArray] count] equals:1]; + [self assert:[[_arrayController arrangedObjects][0] class] equals:[Employee class]]; + + // switch it up so that we can tell where this one is. + [_arrayController setObjectClass:[Department class]]; + [_arrayController add:nil]; + [self assert:[[[_arrayController arrangedObjects] lastObject] class] equals:[Department class]]; +} + +- (void)testInsert +{ + _contentArray = []; + _arrayController = [[CPArrayController alloc] initWithContent:_contentArray]; + [self _testInsert]; +} + +- (void)_testInsert +{ + [_arrayController setObjectClass:[Employee class]]; + [_arrayController add:nil]; + [_arrayController add:nil]; + [_arrayController add:nil]; + + [_arrayController setObjectClass:[Department class]]; + [_arrayController setSelectionIndex:1]; + [_arrayController insert:nil]; + [self assert:[[[_arrayController arrangedObjects] objectAtIndex:2] class] equals:[Department class]]; +} - (void)_initTestRemoveObjects_SimpleArray { From 82605f4170b12a559878fdce51947e02add320d3 Mon Sep 17 00:00:00 2001 From: Andrew Hankinson Date: Sun, 10 Feb 2013 20:58:58 -0500 Subject: [PATCH 2/2] Cause insert: to behave like Cocoa This fix inserts a new object at the current index, rather then the next. This diverges from the Cocoa documentation, but follows the Cocoa behaviour. --- AppKit/CPArrayController.j | 2 +- Tests/AppKit/CPArrayControllerTest.j | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/AppKit/CPArrayController.j b/AppKit/CPArrayController.j index a712a6f4e..9f1d606f7 100644 --- a/AppKit/CPArrayController.j +++ b/AppKit/CPArrayController.j @@ -915,7 +915,7 @@ lastSelectedIndex = [_selectionIndexes lastIndex]; if (lastSelectedIndex) - [self insertObject:newObject atArrangedObjectIndex:lastSelectedIndex + 1]; + [self insertObject:newObject atArrangedObjectIndex:lastSelectedIndex]; else [self addObject:newObject]; diff --git a/Tests/AppKit/CPArrayControllerTest.j b/Tests/AppKit/CPArrayControllerTest.j index 0de6cbd93..a54bb5dad 100644 --- a/Tests/AppKit/CPArrayControllerTest.j +++ b/Tests/AppKit/CPArrayControllerTest.j @@ -222,7 +222,7 @@ [_arrayController setObjectClass:[Department class]]; [_arrayController setSelectionIndex:1]; [_arrayController insert:nil]; - [self assert:[[[_arrayController arrangedObjects] objectAtIndex:2] class] equals:[Department class]]; + [self assert:[[[_arrayController arrangedObjects] objectAtIndex:1] class] equals:[Department class]]; } - (void)_initTestRemoveObjects_SimpleArray