Skip to content

Commit

Permalink
Merge pull request #1446 from cacaodev/CPArrayController-setSelection…
Browse files Browse the repository at this point in the history
…Indexes-fix

CPArrayController : setAvoidsEmptySelection:YES should not apply when setting an empty selection explicitely. With tests.
  • Loading branch information
Antoine Mercadal committed Feb 3, 2012
2 parents dd7d36d + 12201d3 commit f8cb4a1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
20 changes: 15 additions & 5 deletions AppKit/CPArrayController.j
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@
- (BOOL)setSelectionIndexes:(CPIndexSet)indexes
{
[self _selectionWillChange]
var r = [self __setSelectionIndexes:indexes];
var r = [self __setSelectionIndexes:indexes avoidEmpty:NO];
[self _selectionDidChange];
return r;
}
Expand All @@ -583,6 +583,11 @@
@ignore
*/
- (BOOL)__setSelectionIndexes:(CPIndexSet)indexes
{
[self __setSelectionIndexes:indexes avoidEmpty:_avoidsEmptySelection];
}

- (BOOL)__setSelectionIndexes:(CPIndexSet)indexes avoidEmpty:(BOOL)avoidEmpty
{
var newIndexes = indexes;

Expand All @@ -591,7 +596,7 @@

if (![newIndexes count])
{
if (_avoidsEmptySelection && [[self arrangedObjects] count])
if (avoidEmpty && [[self arrangedObjects] count])
newIndexes = [CPIndexSet indexSetWithIndex:0];
}
else
Expand All @@ -606,7 +611,7 @@
// Remove out of bounds indexes.
[newIndexes removeIndexesInRange:CPMakeRange(objectsCount, [newIndexes lastIndex] + 1)];
// When avoiding empty selection and the deleted selection was at the bottom, select the last item.
if (![newIndexes count] && _avoidsEmptySelection && objectsCount)
if (![newIndexes count] && avoidEmpty && objectsCount)
newIndexes = [CPIndexSet indexSetWithIndex:objectsCount - 1];
}

Expand Down Expand Up @@ -647,7 +652,7 @@
[self willChangeValueForKey:@"selectionIndexes"];
[self _selectionWillChange];

var r = [self __setSelectedObjects:objects];
var r = [self __setSelectedObjects:objects avoidEmpty:NO];

[self didChangeValueForKey:@"selectionIndexes"];
[self _selectionDidChange];
Expand All @@ -659,6 +664,11 @@
@ignore
*/
- (BOOL)__setSelectedObjects:(CPArray)objects
{
[self __setSelectedObjects:objects avoidEmpty:_avoidsEmptySelection];
}

- (BOOL)__setSelectedObjects:(CPArray)objects avoidEmpty:(BOOL)avoidEmpty
{
var set = [CPIndexSet indexSet],
count = [objects count],
Expand All @@ -672,7 +682,7 @@
[set addIndex:index];
}

[self __setSelectionIndexes:set];
[self __setSelectionIndexes:set avoidEmpty:avoidEmpty];
return YES;
}

Expand Down
42 changes: 41 additions & 1 deletion Tests/AppKit/CPArrayControllerTest.j
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,32 @@
[arrayController setPreservesSelection:NO];

[arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndexesInRange:CPMakeRange(1, 2)]];
[arrayController removeObjects:[arrayController selectedObjects]]
[arrayController removeObjects:[arrayController selectedObjects]];

[self assert:[CPIndexSet indexSet] equals:[arrayController selectionIndexes]
message:@"selection should be empty if arraycontroller doesn't avoid empty selection"];
}

- (void)testRemoveObjectWithAvoidingEmptySelection
{
var arrayController = [self arrayController];
[arrayController setAvoidsEmptySelection:YES];

[arrayController setSelectionIndex:2];
[arrayController removeObjectsAtArrangedObjectIndexes:[CPIndexSet indexSetWithIndex:2]];

[self assertTrue:([[arrayController selectionIndexes] count] > 0) message:@"Selection should not empty when arraycontroller avoids empty selection"];

[arrayController setContent:[self contentArray]];
[arrayController setSelectionIndex:2];
[arrayController removeObjects:[arrayController selectedObjects]];

[self assertTrue:([[arrayController selectionIndexes] count] > 0) message:@"Selection should not empty when arraycontroller avoids empty selection"];

// This will fail, currently we select the first index instead of an adjacent index.
// [self assertTrue:([arrayController selectionIndex] == 1) message:@"The selected index should be 1, was " + [arrayController selectionIndex]];
}

- (void)testRemoveObjectsWithPreservesSelection_SimpleArray
{
[self initControllerWithSimpleArray];
Expand Down Expand Up @@ -287,6 +307,26 @@
[self assert:[CPIndexSet indexSetWithIndexesInRange:CPMakeRange(0, 2)] equals:[arrayController selectionIndexes] message:@"last object cannot be selected"];
}

- (void)testSelectingEmptyIndexesExplicitlyWithAvoidsEmptySelection
{
var arrayController = [self arrayController];
[arrayController setAvoidsEmptySelection:YES];

[arrayController setSelectionIndex:0];
[arrayController setSelectionIndexes:[CPIndexSet indexSet]];
[self assertTrue:([[arrayController selectionIndexes] count] == 0) message:@"Selection should be empty when unselecting explicitly, even with avoidsEmptySelection"];
}

- (void)testSelectingEmptyObjectsExplicitlyWithAvoidsEmptySelection
{
var arrayController = [self arrayController];
[arrayController setAvoidsEmptySelection:YES];

[arrayController setSelectionIndex:0];
[arrayController setSelectedObjects:[CPArray array]];
[self assertTrue:([[arrayController selectionIndexes] count] == 0) message:@"Selection should be empty when unselecting explicitly, even with avoidsEmptySelection"];
}

- (void)testContentBinding
{
[[self arrayController] bind:@"contentArray" toObject:self withKeyPath:@"contentArray" options:0];
Expand Down

0 comments on commit f8cb4a1

Please sign in to comment.