Files
cappuccino/Tests/AppKit/CPTableViewInteractionTest.j
David Richardson bb65f7ca80 CPTableView: Pass nil for the table column when context-clicking outside all columns
Right-clicking outside the column bounds previously attempted to retrieve the CPTableColumn at index -1, raising an exception that prevented the contextual menu from displaying. The menu delegate now receives nil for the column parameter in this case, matching the documented delegate contract.

Includes two unit tests covering context clicks on a populated row and outside the column bounds (with the table hosted inside a CPScrollView so the view extends beyond its column widths).
2026-11-25 15:09:03 -06:00

1038 lines
29 KiB
Plaintext

/*
* CPTableViewInteractionTest.j
* AppKit
*
* Created by David Richardson on 31/10/2026.
* Copyright David Richardson 2026.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <AppKit/AppKit.j>
/*
A person selects, edits and drags rows with the mouse and the keyboard. These tests
send a table the same events, and check what its delegate and data source receive.
The drag session itself needs a display, so the table subclass records the call
that would start it. A stub stands in for the dragging information. These tests
cannot show that a drag image follows the pointer or that a drop indicator is
visible.
*/
var InteractionDragType = @"InteractionDragType";
@implementation _CPTableInteractionTable : CPTableView
{
CPArray dragCalls @accessors;
}
- (void)dragView:(CPView)aView at:(CGPoint)aLocation offset:(CGSize)mouseOffset event:(CPEvent)anEvent pasteboard:(CPPasteboard)aPasteboard source:(id)aSourceObject slideBack:(BOOL)slideBack
{
if (!dragCalls)
dragCalls = [];
dragCalls.push({ @"pasteboard": aPasteboard, @"source": aSourceObject });
}
@end
/*
A data source that supplies values but cannot store an edited value.
*/
@implementation _CPTableInteractionReadOnlyDataSource : CPObject
{
CPArray rows @accessors;
}
- (id)init
{
self = [super init];
if (self)
rows = [@"Zero", @"One", @"Two", @"Three", @"Four"];
return self;
}
- (int)numberOfRowsInTableView:(CPTableView)aTableView
{
return rows.length;
}
- (id)tableView:(CPTableView)aTableView objectValueForTableColumn:(CPTableColumn)aColumn row:(int)aRow
{
return rows[aRow];
}
@end
@implementation _CPTableInteractionDataSource : _CPTableInteractionReadOnlyDataSource
{
CPArray editedValues @accessors;
}
- (id)init
{
self = [super init];
if (self)
editedValues = [];
return self;
}
- (void)tableView:(CPTableView)aTableView setObjectValue:(id)aValue forTableColumn:(CPTableColumn)aColumn row:(int)aRow
{
editedValues.push({ @"row": aRow, @"value": aValue });
}
@end
@implementation _CPTableInteractionDragDataSource : _CPTableInteractionDataSource
{
BOOL allowsDrag @accessors;
CPIndexSet writtenRows @accessors;
CPDragOperation validation @accessors;
BOOL acceptsDrop @accessors;
CPArray validations @accessors;
CPArray acceptedDrops @accessors;
id retargetRow @accessors;
id retargetOperation @accessors;
}
- (id)init
{
self = [super init];
if (self)
{
allowsDrag = YES;
validation = CPDragOperationMove;
acceptsDrop = YES;
validations = [];
acceptedDrops = [];
}
return self;
}
- (BOOL)tableView:(CPTableView)aTableView writeRowsWithIndexes:(CPIndexSet)theIndexes toPasteboard:(CPPasteboard)aPasteboard
{
writtenRows = theIndexes;
if (allowsDrag)
{
[aPasteboard declareTypes:[InteractionDragType] owner:self];
[aPasteboard setData:@"rows" forType:InteractionDragType];
}
return allowsDrag;
}
- (CPDragOperation)tableView:(CPTableView)aTableView validateDrop:(id)anInfo proposedRow:(int)aRow proposedDropOperation:(CPTableViewDropOperation)anOperation
{
validations.push({ @"row": aRow, @"operation": anOperation });
if (retargetRow != nil)
[aTableView setDropRow:retargetRow dropOperation:retargetOperation];
return validation;
}
- (BOOL)tableView:(CPTableView)aTableView acceptDrop:(id)anInfo row:(int)aRow dropOperation:(CPTableViewDropOperation)anOperation
{
acceptedDrops.push({ @"row": aRow, @"operation": anOperation });
return acceptsDrop;
}
@end
@implementation _CPTableInteractionDelegate : CPObject
{
CPArray calls @accessors;
BOOL allowsSelectionChange @accessors;
BOOL allowsEditing @accessors;
CPArray refusedRows @accessors;
}
- (id)init
{
self = [super init];
if (self)
{
calls = [];
allowsSelectionChange = YES;
allowsEditing = YES;
refusedRows = [];
}
return self;
}
- (BOOL)selectionShouldChangeInTableView:(CPTableView)aTableView
{
calls.push(@"selectionShouldChange");
return allowsSelectionChange;
}
- (BOOL)tableView:(CPTableView)aTableView shouldSelectRow:(int)aRow
{
return refusedRows.indexOf(aRow) === -1;
}
- (BOOL)tableView:(CPTableView)aTableView shouldEditTableColumn:(CPTableColumn)aColumn row:(int)aRow
{
calls.push(@"shouldEdit");
return allowsEditing;
}
- (void)tableViewSelectionIsChanging:(CPNotification)aNotification
{
calls.push(@"selectionIsChanging");
}
- (void)tableViewSelectionDidChange:(CPNotification)aNotification
{
calls.push(@"selectionDidChange");
}
@end
@implementation _CPTableInteractionProposalDelegate : CPObject
{
}
- (CPIndexSet)tableView:(CPTableView)aTableView selectionIndexesForProposedSelection:(CPIndexSet)theIndexes
{
return [CPIndexSet indexSetWithIndex:3];
}
@end
@implementation _CPTableInteractionDraggingInfo : CPObject
{
CPPasteboard draggingPasteboard @accessors;
CGPoint draggingLocation @accessors;
}
@end
/*
Records the column and row of each contextual menu request, and returns a menu.
*/
@implementation _CPTableInteractionMenuDelegate : CPObject
{
CPArray requests @accessors;
CPMenu menu @accessors;
}
- (id)init
{
self = [super init];
if (self)
{
requests = [];
menu = [[CPMenu alloc] initWithTitle:@"Context"];
}
return self;
}
- (CPMenu)tableView:(CPTableView)aTableView menuForTableColumn:(CPTableColumn)aColumn row:(CPInteger)aRow
{
requests.push({ @"column": aColumn, @"row": aRow });
return menu;
}
@end
@implementation CPTableViewInteractionTest : OJTestCase
{
CPWindow theWindow;
_CPTableInteractionTable tableView;
CPTableColumn tableColumn;
_CPTableInteractionDataSource dataSource;
_CPTableInteractionDelegate delegate;
}
- (void)setUp
{
// This will init the global var CPApp which are used internally in the AppKit
[[CPApplication alloc] init];
theWindow = [[CPWindow alloc] initWithContentRect:CGRectMake(0.0, 0.0, 400.0, 400.0) styleMask:CPWindowNotSizable];
tableView = [[_CPTableInteractionTable alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 300.0)];
tableColumn = [[CPTableColumn alloc] initWithIdentifier:@"Column"];
[tableColumn setWidth:200.0];
[tableColumn setEditable:YES];
[tableView addTableColumn:tableColumn];
[tableView setAllowsMultipleSelection:YES];
[[theWindow contentView] addSubview:tableView];
delegate = [[_CPTableInteractionDelegate alloc] init];
[self useDataSource:[[_CPTableInteractionDataSource alloc] init]];
[tableView setDelegate:delegate];
}
- (void)useDataSource:(id)aDataSource
{
dataSource = aDataSource;
[tableView setDataSource:dataSource];
[tableView reloadData];
}
- (CGPoint)pointInRow:(int)aRow
{
return CGPointMake(20.0, CGRectGetMidY([tableView rectOfRow:aRow]));
}
- (CPEvent)mouseEvent:(CPEventType)aType atPoint:(CGPoint)aPoint flags:(unsigned)theFlags clicks:(int)aClickCount
{
return [CPEvent mouseEventWithType:aType
location:[tableView convertPoint:aPoint toView:nil]
modifierFlags:theFlags
timestamp:0
windowNumber:[theWindow windowNumber]
context:nil
eventNumber:0
clickCount:aClickCount
pressure:0];
}
/*
Sends an event the way the platform does: through the application. The window hit-tests
a mouse-down and sends it to the view under the point, and the table view's tracking then
asks the application for the drags and the mouse-up. This method also sent each event to
-trackMouse: directly. When the table view was the view under the point, it then tracked
each event twice: a command-click toggled the row twice, and a second shift-click took
the new end of the selection as its anchor.
*/
- (void)sendEvent:(CPEvent)anEvent
{
[CPApp sendEvent:anEvent];
}
- (void)clickPoint:(CGPoint)aPoint flags:(unsigned)theFlags clicks:(int)aClickCount
{
[self sendEvent:[self mouseEvent:CPLeftMouseDown atPoint:aPoint flags:theFlags clicks:aClickCount]];
[self sendEvent:[self mouseEvent:CPLeftMouseUp atPoint:aPoint flags:theFlags clicks:aClickCount]];
}
- (void)clickRow:(int)aRow
{
[self clickPoint:[self pointInRow:aRow] flags:0 clicks:1];
}
- (void)clickRow:(int)aRow flags:(unsigned)theFlags
{
[self clickPoint:[self pointInRow:aRow] flags:theFlags clicks:1];
}
/*
Puts the table view in a scroll view, as an application does. The table view then fills
its clip view, so the empty area below the last row belongs to the table view. The two
tests of a click outside the rows clicked (20, 290) in a table view without a scroll view.
That table view is only as tall as its rows, so the window sent the click to the content
view. The table view received the click only because the event helper also sent each
event to -trackMouse:.
*/
- (void)placeTableViewInScrollView
{
var scrollView = [[CPScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 300.0)];
[tableView removeFromSuperview];
[[theWindow contentView] addSubview:scrollView];
[scrollView setDocumentView:tableView];
}
/*
Returns a point in the table view below its last row.
*/
- (CGPoint)pointBelowTheRows
{
return CGPointMake(20.0, CGRectGetMaxY([tableView rectOfRow:[tableView numberOfRows] - 1]) + 20.0);
}
/*
Clicks below the last row, after it checks that the point is inside the table view.
*/
- (void)clickBelowTheRows
{
var point = [self pointBelowTheRows];
[self assertTrue:CGRectContainsPoint([tableView bounds], point) message:@"The point below the rows must be inside the table view"];
[self clickPoint:point flags:0 clicks:1];
}
- (void)pressKey:(CPString)aCharacter flags:(unsigned)theFlags
{
[tableView keyDown:[CPEvent keyEventWithType:CPKeyDown
location:CGPointMakeZero()
modifierFlags:theFlags
timestamp:0
windowNumber:[theWindow windowNumber]
context:nil
characters:aCharacter
charactersIgnoringModifiers:aCharacter
isARepeat:NO
keyCode:0]];
}
- (CPArray)selectedRows
{
var rows = [];
[[tableView selectedRowIndexes] getIndexes:rows maxCount:-1 inIndexRange:nil];
return rows;
}
- (id)draggingInfoAtPoint:(CGPoint)aPoint
{
var info = [[_CPTableInteractionDraggingInfo alloc] init],
pasteboard = [CPPasteboard pasteboardWithName:CPDragPboard];
[pasteboard declareTypes:[InteractionDragType] owner:self];
[info setDraggingPasteboard:pasteboard];
[info setDraggingLocation:[tableView convertPoint:aPoint toView:nil]];
return info;
}
- (void)useDragDataSource
{
[self useDataSource:[[_CPTableInteractionDragDataSource alloc] init]];
[tableView registerForDraggedTypes:[InteractionDragType]];
}
// MARK: Selection
- (void)testClickSelectsARow
{
[self clickRow:2];
[self assert:[2] equals:[self selectedRows]];
}
- (void)testClickReplacesTheSelection
{
[self clickRow:1];
[self clickRow:3];
[self assert:[3] equals:[self selectedRows]];
}
- (void)testClickOutsideTheRowsClearsTheSelection
{
[self placeTableViewInScrollView];
[self clickRow:1];
[self clickBelowTheRows];
[self assert:[] equals:[self selectedRows]];
}
- (void)testClickOutsideTheRowsKeepsTheSelectionWhenItCannotBeEmpty
{
[self placeTableViewInScrollView];
[tableView setAllowsEmptySelection:NO];
[self clickRow:1];
[self clickBelowTheRows];
[self assert:[1] equals:[self selectedRows]];
}
- (void)testCommandClickAddsAndRemovesARow
{
[self clickRow:1];
[self clickRow:3 flags:CPCommandKeyMask];
[self assert:[1, 3] equals:[self selectedRows]];
[self clickRow:1 flags:CPCommandKeyMask];
[self assert:[3] equals:[self selectedRows]];
}
- (void)testCommandClickReplacesTheSelectionWithoutMultipleSelection
{
[tableView setAllowsMultipleSelection:NO];
[self clickRow:1];
[self clickRow:3 flags:CPCommandKeyMask];
[self assert:[3] equals:[self selectedRows]];
}
- (void)testShiftClickExtendsFromTheAnchor
{
[self clickRow:1];
[self clickRow:3 flags:CPShiftKeyMask];
[self assert:[1, 2, 3] equals:[self selectedRows]];
}
- (void)testDraggingAcrossRowsExtendsTheSelection
{
[self sendEvent:[self mouseEvent:CPLeftMouseDown atPoint:[self pointInRow:1] flags:0 clicks:1]];
[self sendEvent:[self mouseEvent:CPLeftMouseDragged atPoint:[self pointInRow:2] flags:0 clicks:1]];
[self sendEvent:[self mouseEvent:CPLeftMouseDragged atPoint:[self pointInRow:3] flags:0 clicks:1]];
[self sendEvent:[self mouseEvent:CPLeftMouseUp atPoint:[self pointInRow:3] flags:0 clicks:1]];
[self assert:[1, 2, 3] equals:[self selectedRows]];
}
- (void)testDelegateCanRefuseASelectionChange
{
[delegate setAllowsSelectionChange:NO];
[self clickRow:2];
[self assert:[] equals:[self selectedRows]];
[self assertTrue:[delegate calls].indexOf(@"selectionShouldChange") !== -1];
}
- (void)testDelegateCanRefuseARow
{
[delegate setRefusedRows:[2]];
[self clickRow:2];
[self assert:[] equals:[self selectedRows]];
}
- (void)testDelegateCanReplaceTheProposedSelection
{
[tableView setDelegate:[[_CPTableInteractionProposalDelegate alloc] init]];
[self clickRow:1];
[self assert:[3] equals:[self selectedRows]];
}
- (void)testAClickReportsOneSelectionChange
{
[self clickRow:2];
var changes = [delegate calls].filter(function(aCall)
{
return aCall === @"selectionDidChange";
});
[self assert:1 equals:changes.length];
}
- (void)testClickingTheSelectedRowReportsNoChange
{
[self clickRow:2];
[delegate setCalls:[]];
[self clickRow:2];
[self assertTrue:[delegate calls].indexOf(@"selectionDidChange") === -1];
}
- (void)testSelectAllSelectsEveryRow
{
[tableView selectAll:nil];
[self assert:[0, 1, 2, 3, 4] equals:[self selectedRows]];
}
- (void)testDownArrowMovesTheSelection
{
[self clickRow:1];
[self pressKey:CPDownArrowFunctionKey flags:0];
[self assert:[2] equals:[self selectedRows]];
}
- (void)testUpArrowMovesTheSelection
{
[self clickRow:2];
[self pressKey:CPUpArrowFunctionKey flags:0];
[self assert:[1] equals:[self selectedRows]];
}
- (void)testArrowStopsAtTheLastRow
{
[self clickRow:4];
[self pressKey:CPDownArrowFunctionKey flags:0];
[self assert:[4] equals:[self selectedRows]];
}
- (void)testArrowSelectsAnEndRowWhenNothingIsSelected
{
[self pressKey:CPDownArrowFunctionKey flags:0];
[self assert:[0] equals:[self selectedRows]];
[tableView deselectAll:nil];
[self pressKey:CPUpArrowFunctionKey flags:0];
[self assert:[4] equals:[self selectedRows]];
}
- (void)testShiftArrowExtendsAndShrinksTheSelection
{
[self clickRow:1];
[self pressKey:CPDownArrowFunctionKey flags:CPShiftKeyMask];
[self assert:[1, 2] equals:[self selectedRows]];
[self pressKey:CPUpArrowFunctionKey flags:CPShiftKeyMask];
[self assert:[1] equals:[self selectedRows]];
}
- (void)testArrowSkipsARefusedRow
{
[delegate setRefusedRows:[2]];
[self clickRow:1];
[self pressKey:CPDownArrowFunctionKey flags:0];
[self assert:[3] equals:[self selectedRows]];
}
- (void)testArrowRespectsARefusedSelectionChange
{
[self clickRow:1];
[delegate setAllowsSelectionChange:NO];
[self pressKey:CPDownArrowFunctionKey flags:0];
[self assert:[1] equals:[self selectedRows]];
}
// MARK: Editing
- (void)testEditingAnUnselectedRowRaises
{
[self assertThrows:function()
{
[tableView editColumn:0 row:1 withEvent:nil select:YES];
}];
}
- (void)testEditingReportsTheEditedCell
{
[self clickRow:1];
[tableView editColumn:0 row:1 withEvent:nil select:YES];
[self assert:0 equals:[tableView editedColumn]];
[self assert:1 equals:[tableView editedRow]];
}
- (void)testEditingMakesTheCellTheFirstResponder
{
[self clickRow:1];
[tableView editColumn:0 row:1 withEvent:nil select:YES];
[self assertTrue:[[theWindow firstResponder] isKindOfClass:[CPTextField class]]];
[self assert:@"One" equals:[[theWindow firstResponder] stringValue]];
}
- (void)testEndingAnEditCommitsTheValue
{
[self clickRow:1];
[tableView editColumn:0 row:1 withEvent:nil select:YES];
var field = [theWindow firstResponder];
[field setStringValue:@"Edited"];
[[CPNotificationCenter defaultCenter] postNotificationName:CPControlTextDidEndEditingNotification object:field];
[self assert:1 equals:[dataSource editedValues].length];
[self assert:1 equals:[dataSource editedValues][0].row];
[self assert:@"Edited" equals:[dataSource editedValues][0].value];
[self assert:tableView equals:[theWindow firstResponder]];
}
- (void)testDoubleClickStartsEditing
{
[self clickRow:1];
[self clickPoint:[self pointInRow:1] flags:0 clicks:2];
[self assert:1 equals:[tableView editedRow]];
[self assertTrue:[delegate calls].indexOf(@"shouldEdit") !== -1];
}
- (void)testDelegateCanRefuseAnEdit
{
[delegate setAllowsEditing:NO];
[self clickRow:1];
[self clickPoint:[self pointInRow:1] flags:0 clicks:2];
[self assert:CPNotFound equals:[tableView editedRow]];
}
- (void)testAColumnThatIsNotEditableDoesNotStartAnEdit
{
[tableColumn setEditable:NO];
[self clickRow:1];
[self clickPoint:[self pointInRow:1] flags:0 clicks:2];
[self assert:CPNotFound equals:[tableView editedRow]];
}
/*
Gives the column a checkbox data view, and checks that a click in row 1 lands on that row's
checkbox. -reloadData only schedules the layout that loads the data views, and a click does
not force it, so the table is laid out here. Without the layout no checkbox is under the
point, and both checkbox tests would pass or fail without a click reaching a checkbox.
*/
- (void)useCheckBoxDataView
{
var checkBox = [CPCheckBox checkBoxWithTitle:@""];
[checkBox setImagePosition:CPImageOnly];
[tableColumn setDataView:checkBox];
[tableView reloadData];
[tableView layoutIfNeeded];
var rowCheckBox = [tableView viewAtColumn:0 row:1 makeIfNecessary:NO];
[self assertTrue:[rowCheckBox isKindOfClass:[CPCheckBox class]] message:@"Row 1 must show a checkbox"];
[self assertTrue:CGRectContainsPoint([rowCheckBox frame], [self pointInRow:1]) message:@"A click in row 1 must land on its checkbox"];
}
- (void)testAClickOnACheckBoxInAnEditableColumnStoresTheValue
{
[self useCheckBoxDataView];
[self clickRow:1];
[self assert:1 equals:[dataSource editedValues].length];
[self assert:1 equals:[dataSource editedValues][0].row];
}
- (void)testAClickOnACheckBoxInAColumnThatIsNotEditableDoesNotStoreAValue
{
[tableColumn setEditable:NO];
[self useCheckBoxDataView];
[self clickRow:1];
[self assert:0 equals:[dataSource editedValues].length];
[self assert:[1] equals:[self selectedRows]];
}
/*
Removes the data source and binds the column to an array controller. The table then
stores an edited value through its content binding.
*/
- (void)useBindings
{
var arrayController = [[CPArrayController alloc] initWithContent:[
@{ @"name": @"Zero" },
@{ @"name": @"One" },
@{ @"name": @"Two" },
@{ @"name": @"Three" },
@{ @"name": @"Four" }
]];
dataSource = nil;
[tableView setDataSource:nil];
[tableColumn bind:CPValueBinding toObject:arrayController withKeyPath:@"arrangedObjects.name" options:nil];
[tableView reloadData];
}
/*
Sends a double click on a row whose second mouse-down moves the given distance before
the mouse-up.
*/
- (void)doubleClickRow:(int)aRow movingBy:(float)aDistance
{
var start = [self pointInRow:aRow],
end = CGPointMake(start.x + aDistance, start.y);
[self sendEvent:[self mouseEvent:CPLeftMouseDown atPoint:start flags:0 clicks:2]];
[self sendEvent:[self mouseEvent:CPLeftMouseDragged atPoint:end flags:0 clicks:2]];
[self sendEvent:[self mouseEvent:CPLeftMouseUp atPoint:end flags:0 clicks:2]];
}
- (void)testDoubleClickDoesNotStartAnEditThatNothingCanStore
{
[self useDataSource:[[_CPTableInteractionReadOnlyDataSource alloc] init]];
[self clickRow:1];
[self clickPoint:[self pointInRow:1] flags:0 clicks:2];
[self assert:CPNotFound equals:[tableView editedRow]];
}
- (void)testDoubleClickStartsEditingInABoundTable
{
[self useBindings];
[self clickRow:1];
[self clickPoint:[self pointInRow:1] flags:0 clicks:2];
[self assert:1 equals:[tableView editedRow]];
}
- (void)testAMoveWithinTheClickSlopStillStartsAnEdit
{
[self clickRow:1];
[self doubleClickRow:1 movingBy:3.0];
[self assert:1 equals:[tableView editedRow]];
}
- (void)testAMoveBeyondTheClickSlopDoesNotStartAnEdit
{
[self clickRow:1];
[self doubleClickRow:1 movingBy:10.0];
[self assert:CPNotFound equals:[tableView editedRow]];
}
- (void)testAMoveBeyondTheClickSlopDoesNotStartAnEditInABoundTable
{
[self useBindings];
[self clickRow:1];
[self doubleClickRow:1 movingBy:10.0];
[self assert:CPNotFound equals:[tableView editedRow]];
}
// MARK: Starting a drag
- (void)dragFromRow:(int)aRow toPoint:(CGPoint)aPoint
{
[self sendEvent:[self mouseEvent:CPLeftMouseDown atPoint:[self pointInRow:aRow] flags:0 clicks:1]];
[self sendEvent:[self mouseEvent:CPLeftMouseDragged atPoint:aPoint flags:0 clicks:1]];
}
- (void)testADraggableTableSelectsOnMouseUp
{
[self useDragDataSource];
[self sendEvent:[self mouseEvent:CPLeftMouseDown atPoint:[self pointInRow:2] flags:0 clicks:1]];
[self assert:[] equals:[self selectedRows]];
[self sendEvent:[self mouseEvent:CPLeftMouseUp atPoint:[self pointInRow:2] flags:0 clicks:1]];
[self assert:[2] equals:[self selectedRows]];
}
- (void)testDraggingARowStartsADrag
{
[self useDragDataSource];
var start = [self pointInRow:2];
[self dragFromRow:2 toPoint:CGPointMake(start.x + 20.0, start.y)];
[self assert:1 equals:[[tableView dragCalls] count]];
[self assert:[CPIndexSet indexSetWithIndex:2] equals:[dataSource writtenRows]];
[self assert:[InteractionDragType] equals:[[tableView dragCalls][0].pasteboard types]];
[self assert:tableView equals:[tableView dragCalls][0].source];
}
- (void)testDraggingASelectedRowDragsTheWholeSelection
{
[self useDragDataSource];
[tableView selectRowIndexes:[CPIndexSet indexSetWithIndexesInRange:CPMakeRange(1, 2)] byExtendingSelection:NO];
var start = [self pointInRow:2];
[self dragFromRow:2 toPoint:CGPointMake(start.x + 20.0, start.y)];
[self assert:[CPIndexSet indexSetWithIndexesInRange:CPMakeRange(1, 2)] equals:[dataSource writtenRows]];
}
- (void)testAMoveWithinTheClickSlopDoesNotStartADrag
{
[self useDragDataSource];
var start = [self pointInRow:2];
[self dragFromRow:2 toPoint:CGPointMake(start.x + 2.0, start.y)];
[self assertNull:[tableView dragCalls]];
}
- (void)testDataSourceCanRefuseADrag
{
[self useDragDataSource];
[dataSource setAllowsDrag:NO];
var start = [self pointInRow:2];
[self dragFromRow:2 toPoint:CGPointMake(start.x + 20.0, start.y)];
[self assertNull:[tableView dragCalls]];
}
// MARK: Dropping
- (void)testAnUnregisteredTypeIsRefused
{
[self useDragDataSource];
[tableView unregisterDraggedTypes];
[self assert:CPDragOperationNone equals:[tableView draggingEntered:[self draggingInfoAtPoint:[self pointInRow:2]]]];
[self assert:0 equals:[dataSource validations].length];
}
- (void)testARegisteredTypeIsValidated
{
[self useDragDataSource];
[self assert:CPDragOperationMove equals:[tableView draggingEntered:[self draggingInfoAtPoint:[self pointInRow:2]]]];
[self assert:1 equals:[dataSource validations].length];
}
- (void)testValidationCanRefuseADrop
{
[self useDragDataSource];
[dataSource setValidation:CPDragOperationNone];
[self assert:CPDragOperationNone equals:[tableView draggingUpdated:[self draggingInfoAtPoint:[self pointInRow:2]]]];
}
- (void)testTheMiddleOfARowProposesADropOnThatRow
{
[self useDragDataSource];
[tableView draggingUpdated:[self draggingInfoAtPoint:[self pointInRow:2]]];
[self assert:2 equals:[dataSource validations][0].row];
[self assert:CPTableViewDropOn equals:[dataSource validations][0].operation];
}
- (void)testTheTopOfARowProposesADropAboveThatRow
{
[self useDragDataSource];
var rect = [tableView rectOfRow:2];
[tableView draggingUpdated:[self draggingInfoAtPoint:CGPointMake(20.0, CGRectGetMinY(rect) + 1.0)]];
[self assert:2 equals:[dataSource validations][0].row];
[self assert:CPTableViewDropAbove equals:[dataSource validations][0].operation];
}
- (void)testTheBottomOfARowProposesADropAboveTheNextRow
{
[self useDragDataSource];
var rect = [tableView rectOfRow:2];
[tableView draggingUpdated:[self draggingInfoAtPoint:CGPointMake(20.0, CGRectGetMaxY(rect) - 1.0)]];
[self assert:3 equals:[dataSource validations][0].row];
[self assert:CPTableViewDropAbove equals:[dataSource validations][0].operation];
}
- (void)testBelowTheLastRowProposesADropAtTheEnd
{
[self useDragDataSource];
[tableView draggingUpdated:[self draggingInfoAtPoint:CGPointMake(20.0, 290.0)]];
[self assert:5 equals:[dataSource validations][0].row];
[self assert:CPTableViewDropAbove equals:[dataSource validations][0].operation];
}
- (void)testAcceptingADropReceivesTheProposedRow
{
[self useDragDataSource];
var info = [self draggingInfoAtPoint:[self pointInRow:2]];
[tableView draggingUpdated:info];
[self assertTrue:[tableView prepareForDragOperation:info]];
[self assertTrue:[tableView performDragOperation:info]];
[self assert:2 equals:[dataSource acceptedDrops][0].row];
[self assert:CPTableViewDropOn equals:[dataSource acceptedDrops][0].operation];
}
- (void)testDataSourceCanRefuseADrop
{
[self useDragDataSource];
[dataSource setAcceptsDrop:NO];
var info = [self draggingInfoAtPoint:[self pointInRow:2]];
[tableView draggingUpdated:info];
[self assertFalse:[tableView performDragOperation:info]];
}
- (void)testDataSourceCanRetargetADrop
{
[self useDragDataSource];
[dataSource setRetargetRow:4];
[dataSource setRetargetOperation:CPTableViewDropAbove];
var info = [self draggingInfoAtPoint:[self pointInRow:2]];
[tableView draggingUpdated:info];
[tableView performDragOperation:info];
[self assert:4 equals:[dataSource acceptedDrops][0].row];
[self assert:CPTableViewDropAbove equals:[dataSource acceptedDrops][0].operation];
}
- (void)testPreparingADropNeedsValidation
{
var info = [self draggingInfoAtPoint:[self pointInRow:2]];
[self assertFalse:[tableView prepareForDragOperation:info]];
}
// MARK: Contextual menu
- (void)testARightClickOnARowAsksTheDelegateForTheMenuOfThatColumnAndRow
{
var menuDelegate = [[_CPTableInteractionMenuDelegate alloc] init];
[tableView setDelegate:menuDelegate];
var menu = [tableView menuForEvent:[self mouseEvent:CPRightMouseDown atPoint:[self pointInRow:2] flags:0 clicks:1]];
[self assert:[menuDelegate menu] same:menu message:@"the table shows the delegate's menu"];
[self assert:1 equals:[[menuDelegate requests] count]];
[self assert:tableColumn same:[menuDelegate requests][0].column];
[self assert:2 equals:[menuDelegate requests][0].row];
}
/*
The column is 200 points wide. A table outside a clip view takes the width of its columns,
so the table goes in a scroll view 300 points wide: the table then fills the clip view, and
x = 250 is inside the table but outside every column.
*/
- (void)testARightClickOutsideEveryColumnAsksTheDelegateWithNoColumn
{
var scrollView = [[CPScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 300.0)];
[tableView removeFromSuperview];
[scrollView setDocumentView:tableView];
[[theWindow contentView] addSubview:scrollView];
[tableView tile];
var menuDelegate = [[_CPTableInteractionMenuDelegate alloc] init];
[tableView setDelegate:menuDelegate];
[self assertTrue:CGRectGetWidth([tableView bounds]) > 250.0 message:@"the table is wider than its column"];
var point = CGPointMake(250.0, CGRectGetMidY([tableView rectOfRow:1])),
menu = [tableView menuForEvent:[self mouseEvent:CPRightMouseDown atPoint:point flags:0 clicks:1]];
[self assert:[menuDelegate menu] same:menu message:@"the table shows the delegate's menu"];
[self assert:1 equals:[[menuDelegate requests] count]];
[self assertNull:[menuDelegate requests][0].column message:@"no column is under the point"];
[self assert:1 equals:[menuDelegate requests][0].row];
}
@end