CPSplitViewController: Reimplement controller and split view items

Previously, CPSplitViewController was a scaffold: collapsing an item
merely hid its view—leaving its space and divider intact—while all
views could collapse, thickness limits went unapplied, and stub tests
never executed.

CPSplitViewController and CPSplitViewItem now follow NSSplitViewController
and NSSplitViewItem. Collapsed items are hidden while retaining their
size, sibling views absorb space by holding priority, dividers adjacent
to collapsed outer views hide, thickness limits constrain divider drags,
and collapsing via the animator proxy animates. Sidebars collapse when
the split view becomes too narrow, items remain synchronized with child
view controllers, and both controllers and items support archiving.
This commit is contained in:
David Richardson committed 2026-11-24 11:21:45 -06:00
1 parent cc2766c2f0
commit 74c71d99ca
7 files changed
+1995 -470

No files matched your search

+2
View File
@@ -96,6 +96,8 @@
@import "CPSlider.j"
@import "CPSound.j"
@import "CPSplitView.j"
@import "CPSplitViewController.j"
@import "CPSplitViewItem.j"
@import "CPStackView.j"
@import "CPStepper.j"
@import "CPTableColumn.j"
+19 -4
View File
@@ -52,20 +52,35 @@
*/
- (void)_animateToPosition:(float)position ofDividerAtIndex:(int)dividerIndex
{
var views = [_arrangedSubviews[dividerIndex], _arrangedSubviews[dividerIndex + 1], _dividerSubviews[dividerIndex]],
[self _animateLayoutChange:function()
{
[self setPosition:position ofDividerAtIndex:dividerIndex];
}];
}
/*
Runs a change to the layout of the split view and animates each arranged subview and
divider that the change moves, resizes, hides or shows. The change itself runs at once and
gives the split view its final state; a split view controller uses this to animate the
collapse and expansion of its items.
*/
- (void)_animateLayoutChange:(Function)aChange
{
var views = [_arrangedSubviews arrayByAddingObjectsFromArray:_dividerSubviews],
startStates = [];
for (var i = 0, count = views.length; i < count; i++)
startStates.push({ frame: CGRectMakeCopy([views[i] frame]), hidden: !![views[i] isHidden] });
[self setPosition:position ofDividerAtIndex:dividerIndex];
aChange();
for (var i = 0, count = views.length; i < count; i++)
[self _animateView:views[i] fromState:startStates[i]];
if ([views[i] superview] === self)
[self _animateView:views[i] fromState:startStates[i]];
}
/*
Animates a view from the state it had before a divider move to the state the move gave it.
Animates a view from the state it had before a layout change to the state the change gave it.
A view that is hidden at the end animates to no size along the split, and one that was
hidden at the start animates from no size; the end state is applied when the animation
ends, or at once when there is no animation.
File diff suppressed because it is too large. Load diff
+598
View File
@@ -0,0 +1,598 @@
/*
* CPSplitViewItem.j
* AppKit
*
* Created by David Richardson on 2026-11-23.
* 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 <Foundation/CPObject.j>
@import "CPViewController.j"
@import "CPWindow_Constants.j"
@import "_CPObjectAnimator.j"
@class CPSplitViewController
@typedef CPSplitViewItemBehavior
CPSplitViewItemBehaviorDefault = 0;
CPSplitViewItemBehaviorSidebar = 1;
CPSplitViewItemBehaviorContentList = 2;
CPSplitViewItemBehaviorInspector = 3;
@typedef CPSplitViewItemCollapseBehavior
CPSplitViewItemCollapseBehaviorDefault = 0;
CPSplitViewItemCollapseBehaviorPreferResizingSplitViewWithFixedSiblings = 1;
CPSplitViewItemCollapseBehaviorPreferResizingSiblingsWithFixedSplitView = 2;
CPSplitViewItemCollapseBehaviorUseConstraints = 3;
/*!
The value of a thickness property of a split view item that sets no limit.
*/
CPSplitViewItemUnspecifiedDimension = -1;
var CPSplitViewItemDefaultHoldingPriority = 250,
CPSplitViewItemSidebarThicknessFraction = 0.15,
CPSplitViewItemContentListThicknessFraction = 0.28,
CPSplitViewItemInspectorThickness = 270;
/*!
@ingroup appkit
@class CPSplitViewItem
A split view item describes one child view controller of a CPSplitViewController: whether
it is collapsed, whether the user can collapse it, how it holds its size and the limits of
its thickness. The thickness is the width of the item in a vertical split view and its
height in a horizontal one.
The collapsed state can be set through the animator proxy, so that
@code
[[item animator] setCollapsed:YES];
@endcode
inside a CPAnimationContext group animates the collapse.
These properties are archived but have no effect yet: springLoaded, allowsFullHeightLayout,
titlebarSeparatorStyle, automaticMaximumThickness, and the collapse behaviors other than
the default and PreferResizingSiblingsWithFixedSplitView, which both resize the siblings.
*/
@implementation CPSplitViewItem : CPObject
{
CPViewController _viewController;
CPSplitViewController _splitViewController;
CPSplitViewItemBehavior _behavior;
CPSplitViewItemCollapseBehavior _collapseBehavior;
BOOL _isCollapsed;
BOOL _canCollapse;
BOOL _canCollapseFromWindowResize;
BOOL _isSpringLoaded;
BOOL _allowsFullHeightLayout;
float _holdingPriority;
float _minimumThickness;
float _maximumThickness;
float _preferredThicknessFraction;
float _automaticMaximumThickness;
CPTitlebarSeparatorStyle _titlebarSeparatorStyle;
CPDictionary _animations;
id _animator;
}
// MARK: - Creating items
/*!
Returns an item for a view controller, with the default values.
*/
+ (id)splitViewItemWithViewController:(CPViewController)viewController
{
return [[self alloc] _initWithViewController:viewController behavior:CPSplitViewItemBehaviorDefault];
}
/*!
Returns an item for a sidebar. A sidebar can collapse, also when the split view becomes
too small for it, prefers 15% of the split view, and is spring loaded.
*/
+ (id)sidebarWithViewController:(CPViewController)viewController
{
return [[self alloc] _initWithViewController:viewController behavior:CPSplitViewItemBehaviorSidebar];
}
/*!
Returns an item for a content list, such as the message list of a mail application. A
content list prefers 28% of the split view.
*/
+ (id)contentListWithViewController:(CPViewController)viewController
{
return [[self alloc] _initWithViewController:viewController behavior:CPSplitViewItemBehaviorContentList];
}
/*!
Returns an item for an inspector. An inspector can collapse and has a fixed thickness of
270 points.
*/
+ (id)inspectorWithViewController:(CPViewController)viewController
{
return [[self alloc] _initWithViewController:viewController behavior:CPSplitViewItemBehaviorInspector];
}
- (id)init
{
return [self _initWithViewController:nil behavior:CPSplitViewItemBehaviorDefault];
}
/*
The values for each behavior follow the AppKit headers. AppKit gives sidebars and content
lists standard minimum and maximum thicknesses that its headers do not state; here they are
unspecified. A content list prefers 28% when a sidebar beside it is visible and 33% when
not; here it prefers 28% in both cases.
*/
- (id)_initWithViewController:(CPViewController)viewController behavior:(CPSplitViewItemBehavior)aBehavior
{
if (self = [super init])
{
_viewController = viewController;
_behavior = aBehavior;
_collapseBehavior = CPSplitViewItemCollapseBehaviorDefault;
_isCollapsed = NO;
_canCollapse = NO;
_canCollapseFromWindowResize = NO;
_isSpringLoaded = NO;
_allowsFullHeightLayout = YES;
_holdingPriority = CPSplitViewItemDefaultHoldingPriority;
_minimumThickness = CPSplitViewItemUnspecifiedDimension;
_maximumThickness = CPSplitViewItemUnspecifiedDimension;
_preferredThicknessFraction = CPSplitViewItemUnspecifiedDimension;
_automaticMaximumThickness = CPSplitViewItemUnspecifiedDimension;
_titlebarSeparatorStyle = CPTitlebarSeparatorStyleAutomatic;
_animations = @{};
switch (aBehavior)
{
case CPSplitViewItemBehaviorSidebar:
_canCollapse = YES;
_canCollapseFromWindowResize = YES;
_isSpringLoaded = YES;
_preferredThicknessFraction = CPSplitViewItemSidebarThicknessFraction;
break;
case CPSplitViewItemBehaviorContentList:
_preferredThicknessFraction = CPSplitViewItemContentListThicknessFraction;
break;
case CPSplitViewItemBehaviorInspector:
_canCollapse = YES;
_minimumThickness = CPSplitViewItemInspectorThickness;
_maximumThickness = CPSplitViewItemInspectorThickness;
break;
}
}
return self;
}
// MARK: - The view controller
/*!
Returns the view controller that the item represents.
*/
- (CPViewController)viewController
{
return _viewController;
}
/*!
Sets the view controller that the item represents. It raises
CPInternalInconsistencyException when the item is in a split view controller.
*/
- (void)setViewController:(CPViewController)viewController
{
if (_splitViewController)
[CPException raise:CPInternalInconsistencyException reason:@"The view controller of a split view item cannot change while the item is in a split view controller"];
_viewController = viewController;
}
/*!
Returns the standard behavior of the item, from the method that created it.
*/
- (CPSplitViewItemBehavior)behavior
{
return _behavior;
}
// MARK: - Collapsing
/*!
Returns YES when the item is collapsed. The value follows the user's collapses and
expansions too.
*/
- (BOOL)isCollapsed
{
return _isCollapsed;
}
/*!
Collapses or expands the item. A collapsed item takes no space and keeps its thickness for
its expansion. An item that is collapsed when it is added to a split view controller does
not load its view until it expands. Send this to the animator proxy to animate it.
*/
- (void)setCollapsed:(BOOL)shouldCollapse
{
[self _setCollapsed:shouldCollapse animated:NO];
}
- (void)_setCollapsed:(BOOL)shouldCollapse animated:(BOOL)shouldAnimate
{
shouldCollapse = !!shouldCollapse;
if (_isCollapsed === shouldCollapse)
return;
[self willChangeValueForKey:@"collapsed"];
_isCollapsed = shouldCollapse;
[self didChangeValueForKey:@"collapsed"];
[_splitViewController _splitViewItemDidChangeCollapsed:self animated:shouldAnimate];
}
/*
The collapsed state changes through the animator proxy too, which does not call
-setCollapsed:, so the item posts its own change notifications.
*/
+ (BOOL)automaticallyNotifiesObserversOfCollapsed
{
return NO;
}
/*
Records a collapse or expansion that the split view made, after a drag or a double click,
so that observers of "collapsed" learn of it.
*/
- (void)_noteCollapsed:(BOOL)isCollapsed
{
isCollapsed = !!isCollapsed;
if (_isCollapsed === isCollapsed)
return;
[self willChangeValueForKey:@"collapsed"];
_isCollapsed = isCollapsed;
[self didChangeValueForKey:@"collapsed"];
}
/*!
Returns YES when the user can collapse the item, by dragging or double-clicking a divider.
The default is NO; sidebars and inspectors can collapse.
*/
- (BOOL)canCollapse
{
return _canCollapse;
}
/*!
Sets whether the user can collapse the item. For a sidebar, this also sets whether the item
collapses when the split view becomes too small for it.
*/
- (void)setCanCollapse:(BOOL)canCollapse
{
_canCollapse = !!canCollapse;
if (_behavior === CPSplitViewItemBehaviorSidebar)
_canCollapseFromWindowResize = _canCollapse;
}
/*!
Returns YES when the item collapses when the split view becomes smaller than
-minimumThicknessForInlineSidebars of its controller. Only sidebars do this; it is YES for
sidebars and NO otherwise.
*/
- (BOOL)canCollapseFromWindowResize
{
return _canCollapseFromWindowResize;
}
- (void)setCanCollapseFromWindowResize:(BOOL)canCollapse
{
_canCollapseFromWindowResize = !!canCollapse;
}
/*!
Returns the way the item changes the layout when its collapsed state is set in code.
*/
- (CPSplitViewItemCollapseBehavior)collapseBehavior
{
return _collapseBehavior;
}
- (void)setCollapseBehavior:(CPSplitViewItemCollapseBehavior)aBehavior
{
_collapseBehavior = aBehavior;
}
// MARK: - Thickness
/*!
Returns the priority with which the item holds its thickness when the split view resizes.
The item with the lowest priority takes a change first. The default is 250.
*/
- (float)holdingPriority
{
return _holdingPriority;
}
- (void)setHoldingPriority:(float)aPriority
{
_holdingPriority = aPriority;
[_splitViewController _splitViewItemDidChangeHoldingPriority:self];
}
/*!
Returns the smallest thickness to which the user can drag the item, or
CPSplitViewItemUnspecifiedDimension for no limit.
*/
- (float)minimumThickness
{
return _minimumThickness;
}
- (void)setMinimumThickness:(float)aThickness
{
_minimumThickness = aThickness;
}
/*!
Returns the largest thickness to which the user can drag the item, or
CPSplitViewItemUnspecifiedDimension for no limit.
*/
- (float)maximumThickness
{
return _maximumThickness;
}
- (void)setMaximumThickness:(float)aThickness
{
_maximumThickness = aThickness;
}
/*!
Returns the part of the split view that the item prefers, from 0 to 1, or
CPSplitViewItemUnspecifiedDimension. An item whose view has no thickness takes this part
when its view is added to the split view.
*/
- (float)preferredThicknessFraction
{
return _preferredThicknessFraction;
}
- (void)setPreferredThicknessFraction:(float)aFraction
{
_preferredThicknessFraction = aFraction;
}
/*!
Returns the largest thickness for automatic sizing, or CPSplitViewItemUnspecifiedDimension.
*/
- (float)automaticMaximumThickness
{
return _automaticMaximumThickness;
}
- (void)setAutomaticMaximumThickness:(float)aThickness
{
_automaticMaximumThickness = aThickness;
}
// MARK: - Other properties
/*!
Returns YES when the item expands for a while during a drag over its divider. The default
is NO; sidebars are spring loaded.
*/
- (BOOL)isSpringLoaded
{
return _isSpringLoaded;
}
- (void)setSpringLoaded:(BOOL)isSpringLoaded
{
_isSpringLoaded = !!isSpringLoaded;
}
/*!
Returns YES when a sidebar or inspector can take the full height of a window whose content
goes under its title bar. The default is YES.
*/
- (BOOL)allowsFullHeightLayout
{
return _allowsFullHeightLayout;
}
- (void)setAllowsFullHeightLayout:(BOOL)allowsFullHeightLayout
{
_allowsFullHeightLayout = !!allowsFullHeightLayout;
}
/*!
Returns the style of the separator between the title bar and the item.
*/
- (CPTitlebarSeparatorStyle)titlebarSeparatorStyle
{
return _titlebarSeparatorStyle;
}
- (void)setTitlebarSeparatorStyle:(CPTitlebarSeparatorStyle)aStyle
{
_titlebarSeparatorStyle = aStyle;
}
/*!
Returns the split view controller that has the item, or nil.
*/
- (CPSplitViewController)splitViewController
{
return _splitViewController;
}
- (void)_setSplitViewController:(CPSplitViewController)aController
{
_splitViewController = aController;
}
- (CPString)description
{
return [super description] + " " + [_viewController description] + (_isCollapsed ? " (collapsed)" : "");
}
@end
// MARK: - Animation
@implementation CPSplitViewItem (CPAnimatablePropertyContainer)
+ (id)defaultAnimationForKey:(CPString)aKey
{
return nil;
}
/*!
Returns the proxy through which a change of the collapsed state animates.
*/
- (id)animator
{
if (!_animator)
_animator = [[CPSplitViewItemAnimator alloc] initWithTarget:self];
return _animator;
}
/*!
Returns the animations of the item, by key. The collapse uses the duration of the current
CPAnimationContext group; an animation stored under "collapsed" is kept but not used yet.
*/
- (CPDictionary)animations
{
return _animations;
}
- (void)setAnimations:(CPDictionary)theAnimations
{
_animations = theAnimations || @{};
}
- (id)animationForKey:(CPString)aKey
{
return [_animations objectForKey:aKey] || [[self class] defaultAnimationForKey:aKey];
}
@end
/*
The animator proxy of a split view item. A collapse or expansion sent to it animates the
split view of the item's controller inside the current CPAnimationContext group. Other
messages go to the item.
*/
@implementation CPSplitViewItemAnimator : _CPObjectAnimator
- (void)setCollapsed:(BOOL)shouldCollapse
{
[_target _setCollapsed:shouldCollapse animated:YES];
}
@end
// MARK: - Archiving
var CPSplitViewItemViewControllerKey = @"CPSplitViewItemViewControllerKey",
CPSplitViewItemSplitViewControllerKey = @"CPSplitViewItemSplitViewControllerKey",
CPSplitViewItemBehaviorKey = @"CPSplitViewItemBehaviorKey",
CPSplitViewItemCollapseBehaviorKey = @"CPSplitViewItemCollapseBehaviorKey",
CPSplitViewItemCollapsedKey = @"CPSplitViewItemCollapsedKey",
CPSplitViewItemCanCollapseKey = @"CPSplitViewItemCanCollapseKey",
CPSplitViewItemCanCollapseFromWindowResizeKey = @"CPSplitViewItemCanCollapseFromWindowResizeKey",
CPSplitViewItemSpringLoadedKey = @"CPSplitViewItemSpringLoadedKey",
CPSplitViewItemAllowsFullHeightLayoutKey = @"CPSplitViewItemAllowsFullHeightLayoutKey",
CPSplitViewItemHoldingPriorityKey = @"CPSplitViewItemHoldingPriorityKey",
CPSplitViewItemMinimumThicknessKey = @"CPSplitViewItemMinimumThicknessKey",
CPSplitViewItemMaximumThicknessKey = @"CPSplitViewItemMaximumThicknessKey",
CPSplitViewItemPreferredThicknessFractionKey = @"CPSplitViewItemPreferredThicknessFractionKey",
CPSplitViewItemAutomaticMaximumThicknessKey = @"CPSplitViewItemAutomaticMaximumThicknessKey",
CPSplitViewItemTitlebarSeparatorStyleKey = @"CPSplitViewItemTitlebarSeparatorStyleKey";
@implementation CPSplitViewItem (CPCoding)
- (id)initWithCoder:(CPCoder)aCoder
{
self = [self _initWithViewController:[aCoder decodeObjectForKey:CPSplitViewItemViewControllerKey]
behavior:[aCoder decodeIntForKey:CPSplitViewItemBehaviorKey]];
if (self)
{
_splitViewController = [aCoder decodeObjectForKey:CPSplitViewItemSplitViewControllerKey];
_collapseBehavior = [aCoder decodeIntForKey:CPSplitViewItemCollapseBehaviorKey];
_isCollapsed = [aCoder decodeBoolForKey:CPSplitViewItemCollapsedKey];
_canCollapse = [aCoder decodeBoolForKey:CPSplitViewItemCanCollapseKey];
_canCollapseFromWindowResize = [aCoder decodeBoolForKey:CPSplitViewItemCanCollapseFromWindowResizeKey];
_isSpringLoaded = [aCoder decodeBoolForKey:CPSplitViewItemSpringLoadedKey];
if ([aCoder containsValueForKey:CPSplitViewItemAllowsFullHeightLayoutKey])
_allowsFullHeightLayout = [aCoder decodeBoolForKey:CPSplitViewItemAllowsFullHeightLayoutKey];
if ([aCoder containsValueForKey:CPSplitViewItemHoldingPriorityKey])
_holdingPriority = [aCoder decodeFloatForKey:CPSplitViewItemHoldingPriorityKey];
if ([aCoder containsValueForKey:CPSplitViewItemMinimumThicknessKey])
_minimumThickness = [aCoder decodeFloatForKey:CPSplitViewItemMinimumThicknessKey];
if ([aCoder containsValueForKey:CPSplitViewItemMaximumThicknessKey])
_maximumThickness = [aCoder decodeFloatForKey:CPSplitViewItemMaximumThicknessKey];
if ([aCoder containsValueForKey:CPSplitViewItemPreferredThicknessFractionKey])
_preferredThicknessFraction = [aCoder decodeFloatForKey:CPSplitViewItemPreferredThicknessFractionKey];
if ([aCoder containsValueForKey:CPSplitViewItemAutomaticMaximumThicknessKey])
_automaticMaximumThickness = [aCoder decodeFloatForKey:CPSplitViewItemAutomaticMaximumThicknessKey];
_titlebarSeparatorStyle = [aCoder decodeIntForKey:CPSplitViewItemTitlebarSeparatorStyleKey];
}
return self;
}
- (void)encodeWithCoder:(CPCoder)aCoder
{
[aCoder encodeObject:_viewController forKey:CPSplitViewItemViewControllerKey];
[aCoder encodeConditionalObject:_splitViewController forKey:CPSplitViewItemSplitViewControllerKey];
[aCoder encodeInt:_behavior forKey:CPSplitViewItemBehaviorKey];
[aCoder encodeInt:_collapseBehavior forKey:CPSplitViewItemCollapseBehaviorKey];
[aCoder encodeBool:_isCollapsed forKey:CPSplitViewItemCollapsedKey];
[aCoder encodeBool:_canCollapse forKey:CPSplitViewItemCanCollapseKey];
[aCoder encodeBool:_canCollapseFromWindowResize forKey:CPSplitViewItemCanCollapseFromWindowResizeKey];
[aCoder encodeBool:_isSpringLoaded forKey:CPSplitViewItemSpringLoadedKey];
[aCoder encodeBool:_allowsFullHeightLayout forKey:CPSplitViewItemAllowsFullHeightLayoutKey];
[aCoder encodeFloat:_holdingPriority forKey:CPSplitViewItemHoldingPriorityKey];
[aCoder encodeFloat:_minimumThickness forKey:CPSplitViewItemMinimumThicknessKey];
[aCoder encodeFloat:_maximumThickness forKey:CPSplitViewItemMaximumThicknessKey];
[aCoder encodeFloat:_preferredThicknessFraction forKey:CPSplitViewItemPreferredThicknessFractionKey];
[aCoder encodeFloat:_automaticMaximumThickness forKey:CPSplitViewItemAutomaticMaximumThicknessKey];
[aCoder encodeInt:_titlebarSeparatorStyle forKey:CPSplitViewItemTitlebarSeparatorStyleKey];
}
@end
+11
View File
@@ -200,3 +200,14 @@ CPPanelWindowShadowStyle = 2;
CPCustomWindowShadowStyle = 3;
CPWindowConstrainToScreen = YES;
/*
The styles of the separator between a title bar and the content below it.
@global
@class CPWindow
*/
@typedef CPTitlebarSeparatorStyle
CPTitlebarSeparatorStyleAutomatic = 0;
CPTitlebarSeparatorStyleNone = 1;
CPTitlebarSeparatorStyleLine = 2;
CPTitlebarSeparatorStyleShadow = 3;
+613
View File
@@ -0,0 +1,613 @@
/*
* CPSplitViewControllerTest.j
* AppKit
*
* Created by David Richardson on 2026-11-23.
* 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/CPApplication.j>
@import <AppKit/CPMenuItem.j>
@import <AppKit/CPSplitViewController.j>
@class _CPSplitViewControllerTestChild
@class _CPSplitViewControllerTestObserver
/*
These tests state how a split view controller and its items behave as AppKit's
NSSplitViewController and NSSplitViewItem do: the items and the child view controllers,
the split view and its arranged subviews, the values of each item behavior, collapsing and
expanding, holding priorities, thickness limits, sidebars and inspectors, validation and
archiving.
Most tests use a controller with a sidebar 100 points wide and a content item 199 points
wide, both 100 points high. When the view loads, the split view takes the sum of their
widths and the thin divider between them (1 point under ojtest, where no theme is loaded):
300 by 100. The sidebar is at x = 0, the divider at x = 100 and the content at x = 101.
*/
@implementation CPSplitViewControllerTest : OJTestCase
{
CPSplitViewController controller;
CPViewController sidebarChild;
CPViewController contentChild;
CPSplitViewItem sidebarItem;
CPSplitViewItem contentItem;
}
- (void)setUp
{
[[CPApplication alloc] init];
controller = [[CPSplitViewController alloc] init];
sidebarChild = [[_CPSplitViewControllerTestChild alloc] initWithWidth:100];
contentChild = [[_CPSplitViewControllerTestChild alloc] initWithWidth:199];
sidebarItem = [CPSplitViewItem sidebarWithViewController:sidebarChild];
contentItem = [CPSplitViewItem splitViewItemWithViewController:contentChild];
}
- (void)loadTwoItems
{
[controller addSplitViewItem:sidebarItem];
[controller addSplitViewItem:contentItem];
[controller view];
}
- (void)assertFrame:(CGRect)aFrame of:(CPView)aView named:(CPString)aName
{
[self assertTrue:CGRectEqualToRect(aFrame, [aView frame]) message:aName + @": expected " + CPStringFromRect(aFrame) + @", got " + CPStringFromRect([aView frame])];
}
// MARK: - The controller and its view
- (void)testANewControllerHasNoItemsAndNoSplitView
{
[self assert:0 equals:[[controller splitViewItems] count] message:@"no items"];
[self assertNull:[controller splitView] message:@"no split view before the view loads"];
}
- (void)testTheViewIsAVerticalSplitViewWithThinDividersAndTheControllerAsDelegate
{
var view = [controller view];
[self assertTrue:[view isKindOfClass:[CPSplitView class]] message:@"the view is a split view"];
[self assert:view equals:[controller splitView] message:@"the split view is the view"];
[self assertTrue:[view isVertical] message:@"the split view is vertical"];
[self assert:CPSplitViewDividerStyleThin equals:[view dividerStyle] message:@"the dividers are thin"];
[self assert:controller equals:[view delegate] message:@"the controller is the delegate"];
}
- (void)testAControllerUsesASplitViewSetBeforeTheViewLoads
{
var splitView = [[CPSplitView alloc] initWithFrame:CGRectMake(0, 0, 300, 100)];
[controller setSplitView:splitView];
[self assert:splitView equals:[controller view] message:@"the set split view is the view"];
[self assert:controller equals:[splitView delegate] message:@"the controller is its delegate"];
}
- (void)testTheSplitViewCannotChangeAfterTheViewLoads
{
[controller view];
var raised = nil;
try
{
[controller setSplitView:[[CPSplitView alloc] initWithFrame:CGRectMakeZero()]];
}
catch (anException)
{
raised = [anException name];
}
[self assert:CPInternalInconsistencyException equals:raised message:@"a new split view raises"];
}
// MARK: - Item values
- (void)testADefaultItemHasTheDefaultValues
{
[self assert:contentChild equals:[contentItem viewController] message:@"the view controller"];
[self assert:CPSplitViewItemBehaviorDefault equals:[contentItem behavior] message:@"the behavior"];
[self assertFalse:[contentItem isCollapsed] message:@"not collapsed"];
[self assertFalse:[contentItem canCollapse] message:@"cannot collapse"];
[self assertFalse:[contentItem canCollapseFromWindowResize] message:@"cannot collapse from a resize"];
[self assert:250 equals:[contentItem holdingPriority] message:@"the holding priority"];
[self assert:CPSplitViewItemUnspecifiedDimension equals:[contentItem minimumThickness] message:@"no minimum"];
[self assert:CPSplitViewItemUnspecifiedDimension equals:[contentItem maximumThickness] message:@"no maximum"];
[self assert:CPSplitViewItemUnspecifiedDimension equals:[contentItem preferredThicknessFraction] message:@"no preferred fraction"];
[self assertFalse:[contentItem isSpringLoaded] message:@"not spring loaded"];
[self assertTrue:[contentItem allowsFullHeightLayout] message:@"allows full height layout"];
[self assertNull:[contentItem splitViewController] message:@"no controller"];
}
- (void)testASidebarCanCollapseAndPrefersFifteenPercent
{
[self assert:CPSplitViewItemBehaviorSidebar equals:[sidebarItem behavior] message:@"the behavior"];
[self assertTrue:[sidebarItem canCollapse] message:@"can collapse"];
[self assertTrue:[sidebarItem canCollapseFromWindowResize] message:@"can collapse from a resize"];
[self assertTrue:[sidebarItem isSpringLoaded] message:@"spring loaded"];
[self assert:0.15 equals:[sidebarItem preferredThicknessFraction] message:@"the preferred fraction"];
[sidebarItem setCanCollapse:NO];
[self assertFalse:[sidebarItem canCollapseFromWindowResize] message:@"canCollapse also sets canCollapseFromWindowResize for a sidebar"];
}
- (void)testAContentListPrefersTwentyEightPercent
{
var item = [CPSplitViewItem contentListWithViewController:contentChild];
[self assert:CPSplitViewItemBehaviorContentList equals:[item behavior] message:@"the behavior"];
[self assertFalse:[item canCollapse] message:@"cannot collapse"];
[self assert:0.28 equals:[item preferredThicknessFraction] message:@"the preferred fraction"];
}
- (void)testAnInspectorCanCollapseAndHasAFixedThickness
{
var item = [CPSplitViewItem inspectorWithViewController:contentChild];
[self assert:CPSplitViewItemBehaviorInspector equals:[item behavior] message:@"the behavior"];
[self assertTrue:[item canCollapse] message:@"can collapse"];
[self assertFalse:[item canCollapseFromWindowResize] message:@"cannot collapse from a resize"];
[self assert:270 equals:[item minimumThickness] message:@"the minimum"];
[self assert:270 equals:[item maximumThickness] message:@"the maximum"];
}
- (void)testAnItemOutsideAControllerOnlyRecordsItsCollapsedState
{
[contentItem setCollapsed:YES];
[self assertTrue:[contentItem isCollapsed] message:@"the item is collapsed"];
[self assertFalse:[contentChild isViewLoaded] message:@"the view does not load"];
}
// MARK: - Items, children and arranged subviews
- (void)testItemsBecomeChildrenAndArrangedSubviews
{
[self loadTwoItems];
[self assert:[sidebarItem, contentItem] equals:[controller splitViewItems] message:@"the items in order"];
[self assert:[sidebarChild, contentChild] equals:[controller childViewControllers] message:@"the children in order"];
[self assert:controller equals:[sidebarItem splitViewController] message:@"the item knows its controller"];
[self assert:controller equals:[sidebarChild parentViewController] message:@"the child knows its parent"];
[self assert:[[sidebarChild view], [contentChild view]] equals:[[controller splitView] arrangedSubviews] message:@"the arranged subviews"];
[self assert:sidebarItem equals:[controller splitViewItemForViewController:sidebarChild] message:@"the item of a child"];
[self assertNull:[controller splitViewItemForViewController:[[CPViewController alloc] init]] message:@"no item for another view controller"];
}
- (void)testTheSplitViewTakesTheSizeOfTheItemViewsWhenTheViewLoads
{
[self loadTwoItems];
[self assertFrame:CGRectMake(0, 0, 300, 100) of:[controller splitView] named:@"the split view"];
[self assertFrame:CGRectMake(0, 0, 100, 100) of:[sidebarChild view] named:@"the sidebar"];
[self assertFrame:CGRectMake(101, 0, 199, 100) of:[contentChild view] named:@"the content"];
}
- (void)testItemsAddedBeforeTheViewLoadsAreChildrenAtOnce
{
[controller addSplitViewItem:sidebarItem];
[self assert:[sidebarChild] equals:[controller childViewControllers] message:@"the child is added at once"];
[self assertFalse:[sidebarChild isViewLoaded] message:@"its view does not load yet"];
}
- (void)testAnInsertedItemTakesItsPlaceInTheSplitView
{
[self loadTwoItems];
var otherChild = [[_CPSplitViewControllerTestChild alloc] initWithWidth:50],
otherItem = [CPSplitViewItem splitViewItemWithViewController:otherChild];
[controller insertSplitViewItem:otherItem atIndex:1];
[self assert:[sidebarItem, otherItem, contentItem] equals:[controller splitViewItems] message:@"the items in order"];
[self assert:[[sidebarChild view], [otherChild view], [contentChild view]] equals:[[controller splitView] arrangedSubviews] message:@"the arranged subviews in order"];
}
- (void)testARemovedItemLeavesTheSplitViewAndTheChildren
{
[self loadTwoItems];
[controller removeSplitViewItem:sidebarItem];
[self assert:[contentItem] equals:[controller splitViewItems] message:@"the items"];
[self assert:[contentChild] equals:[controller childViewControllers] message:@"the children"];
[self assertNull:[sidebarItem splitViewController] message:@"the item has no controller"];
[self assertNull:[sidebarChild parentViewController] message:@"the child has no parent"];
[self assertNull:[[sidebarChild view] superview] message:@"the view leaves the split view"];
[self assertFrame:CGRectMake(0, 0, 300, 100) of:[contentChild view] named:@"the content fills the split view"];
}
- (void)testTheChildMethodsAddAndRemoveDefaultItems
{
[controller view];
[controller addChildViewController:contentChild];
var item = [controller splitViewItemForViewController:contentChild];
[self assertNotNull:item message:@"an added child gets an item"];
[self assert:CPSplitViewItemBehaviorDefault equals:[item behavior] message:@"the item has the default behavior"];
[self assert:[[contentChild view]] equals:[[controller splitView] arrangedSubviews] message:@"its view is arranged"];
[contentChild removeFromParentViewController];
[self assert:0 equals:[[controller splitViewItems] count] message:@"a removed child loses its item"];
}
- (void)testSettingTheItemsReplacesThem
{
[self loadTwoItems];
var otherItem = [CPSplitViewItem splitViewItemWithViewController:[[_CPSplitViewControllerTestChild alloc] initWithWidth:50]];
[controller setSplitViewItems:[contentItem, otherItem]];
[self assert:[contentItem, otherItem] equals:[controller splitViewItems] message:@"the new items"];
[self assertNull:[sidebarItem splitViewController] message:@"the dropped item has no controller"];
}
- (void)testInvalidInsertionsRaise
{
var names = [],
attempts = [
function() { [controller addSplitViewItem:nil]; },
function() { [controller addSplitViewItem:[CPSplitViewItem splitViewItemWithViewController:nil]]; },
function() { [controller insertSplitViewItem:contentItem atIndex:1]; },
function() { [controller removeSplitViewItem:contentItem]; }
];
for (var i = 0; i < attempts.length; i++)
{
try
{
attempts[i]();
names.push("none");
}
catch (anException)
{
names.push([anException name]);
}
}
[self assert:[CPInvalidArgumentException, CPInvalidArgumentException, CPRangeException, CPInvalidArgumentException] equals:names message:@"each invalid call raises"];
}
- (void)testTheViewControllerOfAnAddedItemCannotChange
{
[controller addSplitViewItem:contentItem];
var raised = nil;
try
{
[contentItem setViewController:sidebarChild];
}
catch (anException)
{
raised = [anException name];
}
[self assert:CPInternalInconsistencyException equals:raised message:@"a new view controller raises"];
}
// MARK: - Collapsing
- (void)testACollapsedItemIsHiddenKeepsItsSizeAndHidesItsOuterDivider
{
[self loadTwoItems];
[sidebarItem setCollapsed:YES];
var splitView = [controller splitView];
[self assertTrue:[[sidebarChild view] isHidden] message:@"the sidebar is hidden"];
[self assertTrue:[splitView isSubviewCollapsed:[sidebarChild view]] message:@"the split view sees it collapsed"];
[self assert:100 equals:CGRectGetWidth([[sidebarChild view] frame]) message:@"the sidebar keeps its width"];
[self assert:0 equals:CGRectGetWidth([splitView rectOfDividerAtIndex:0]) message:@"the divider beside it is hidden"];
[self assertFrame:CGRectMake(0, 0, 300, 100) of:[contentChild view] named:@"the content fills the split view"];
}
- (void)testAnExpandedItemTakesBackItsSize
{
[self loadTwoItems];
[sidebarItem setCollapsed:YES];
[sidebarItem setCollapsed:NO];
[self assertFalse:[[sidebarChild view] isHidden] message:@"the sidebar is shown"];
[self assertFrame:CGRectMake(0, 0, 100, 100) of:[sidebarChild view] named:@"the sidebar"];
[self assertFrame:CGRectMake(101, 0, 199, 100) of:[contentChild view] named:@"the content"];
}
- (void)testAnItemCollapsedWhenAddedLoadsItsViewAtItsFirstExpansion
{
[sidebarItem setCollapsed:YES];
[self loadTwoItems];
[self assertFalse:[sidebarChild isViewLoaded] message:@"the view of the collapsed item does not load"];
[self assert:[[contentChild view]] equals:[[controller splitView] arrangedSubviews] message:@"only the content is arranged"];
[sidebarItem setCollapsed:NO];
[self assertTrue:[sidebarChild isViewLoaded] message:@"the expansion loads the view"];
[self assert:[[sidebarChild view], [contentChild view]] equals:[[controller splitView] arrangedSubviews] message:@"the sidebar is arranged first"];
[self assertFalse:[[sidebarChild view] isHidden] message:@"the sidebar is shown"];
}
- (void)testAUserCollapseUpdatesTheItemAndItsObservers
{
[self loadTwoItems];
var observer = [[_CPSplitViewControllerTestObserver alloc] init];
[sidebarItem addObserver:observer forKeyPath:@"collapsed" options:0 context:nil];
[[controller splitView] setPosition:0 ofDividerAtIndex:0];
[sidebarItem removeObserver:observer forKeyPath:@"collapsed"];
[self assertTrue:[sidebarItem isCollapsed] message:@"the item is collapsed"];
[self assert:1 equals:observer.count message:@"observers learn of the collapse"];
}
- (void)testOnlyAnItemThatCanCollapseCollapsesFromADrag
{
[self loadTwoItems];
[[controller splitView] setPosition:300 ofDividerAtIndex:0];
[self assertFalse:[contentItem isCollapsed] message:@"the content cannot collapse"];
[self assertFalse:[[contentChild view] isHidden] message:@"the content stays visible"];
}
- (void)testTheAnimatorCollapsesTheItem
{
[self loadTwoItems];
[CPAnimationContext runAnimationGroup:function(aContext)
{
[aContext setDuration:0.25];
[[sidebarItem animator] setCollapsed:YES];
}
completionHandler:nil];
[self assertTrue:[sidebarItem isCollapsed] message:@"the item is collapsed"];
[self assertTrue:[[sidebarChild view] isHidden] message:@"the sidebar is hidden"];
[self assertFrame:CGRectMake(0, 0, 300, 100) of:[contentChild view] named:@"the content fills the split view"];
}
// MARK: - Sizes
- (void)testTheHoldingPriorityDecidesWhichItemTakesAResize
{
[self loadTwoItems];
[sidebarItem setHoldingPriority:260];
[[controller splitView] setFrameSize:CGSizeMake(400, 100)];
[self assert:260 equals:[[controller splitView] holdingPriorityForSubviewAtIndex:0] message:@"the split view has the item's priority"];
[self assertFrame:CGRectMake(0, 0, 100, 100) of:[sidebarChild view] named:@"the sidebar keeps its width"];
[self assertFrame:CGRectMake(101, 0, 299, 100) of:[contentChild view] named:@"the content takes the change"];
}
- (void)testTheThicknessLimitsOfTheItemsLimitADrag
{
[sidebarItem setMinimumThickness:80];
[sidebarItem setMaximumThickness:150];
[contentItem setMinimumThickness:180];
[self loadTwoItems];
var splitView = [controller splitView];
[splitView setPosition:200 ofDividerAtIndex:0];
[self assert:119 equals:CGRectGetMinX([splitView rectOfDividerAtIndex:0]) message:@"the content keeps its minimum"];
[contentItem setMinimumThickness:CPSplitViewItemUnspecifiedDimension];
[splitView setPosition:200 ofDividerAtIndex:0];
[self assert:150 equals:CGRectGetMinX([splitView rectOfDividerAtIndex:0]) message:@"the sidebar keeps its maximum"];
[splitView setPosition:85 ofDividerAtIndex:0];
[self assert:85 equals:CGRectGetMinX([splitView rectOfDividerAtIndex:0]) message:@"a position inside the limits"];
[sidebarItem setCanCollapse:NO];
[splitView setPosition:20 ofDividerAtIndex:0];
[self assert:80 equals:CGRectGetMinX([splitView rectOfDividerAtIndex:0]) message:@"the sidebar keeps its minimum"];
}
- (void)testASidebarCollapsesWhenTheSplitViewBecomesThinnerThanTheMinimum
{
[self loadTwoItems];
[controller setMinimumThicknessForInlineSidebars:250];
var splitView = [controller splitView];
[splitView setFrameSize:CGSizeMake(240, 100)];
[self assertTrue:[sidebarItem isCollapsed] message:@"the sidebar collapses"];
[splitView setFrameSize:CGSizeMake(300, 100)];
[self assertFalse:[sidebarItem isCollapsed] message:@"the sidebar expands again"];
}
- (void)testAnAutomaticMinimumComesFromTheMinimumThicknesses
{
[sidebarItem setMinimumThickness:80];
[contentItem setMinimumThickness:150];
[self loadTwoItems];
[self assert:CPSplitViewControllerAutomaticDimension equals:[controller minimumThicknessForInlineSidebars] message:@"the default is automatic"];
[[controller splitView] setFrameSize:CGSizeMake(230, 100)];
[self assertTrue:[sidebarItem isCollapsed] message:@"below 80 + 1 + 150 the sidebar collapses"];
[[controller splitView] setFrameSize:CGSizeMake(231, 100)];
[self assertFalse:[sidebarItem isCollapsed] message:@"at 231 it expands again"];
}
// MARK: - Sidebars and inspectors
- (void)testToggleSidebarTogglesTheFirstSidebar
{
[controller addSplitViewItem:contentItem];
[controller addSplitViewItem:sidebarItem];
[controller view];
[controller toggleSidebar:nil];
[self assertTrue:[sidebarItem isCollapsed] message:@"the sidebar collapses"];
[self assertFalse:[contentItem isCollapsed] message:@"the first item is not a sidebar"];
[controller toggleSidebar:nil];
[self assertFalse:[sidebarItem isCollapsed] message:@"the sidebar expands"];
}
- (void)testToggleInspectorTogglesTheFirstInspector
{
var inspectorChild = [[_CPSplitViewControllerTestChild alloc] initWithWidth:270],
inspectorItem = [CPSplitViewItem inspectorWithViewController:inspectorChild];
[self loadTwoItems];
[controller addSplitViewItem:inspectorItem];
[controller toggleInspector:nil];
[self assertTrue:[inspectorItem isCollapsed] message:@"the inspector collapses"];
[self assertFalse:[sidebarItem isCollapsed] message:@"the sidebar does not"];
}
- (void)testTogglesWithoutAMatchingItemDoNothing
{
[controller addSplitViewItem:contentItem];
[controller view];
[controller toggleSidebar:nil];
[controller toggleInspector:nil];
[self assertFalse:[contentItem isCollapsed] message:@"the default item does not collapse"];
}
- (void)testValidationEnablesTheToggleAndGivesTheMenuItemItsTitle
{
var menuItem = [[CPMenuItem alloc] initWithTitle:@"Sidebar" action:@selector(toggleSidebar:) keyEquivalent:@""];
[self loadTwoItems];
[self assertTrue:[controller validateUserInterfaceItem:menuItem] message:@"enabled with a sidebar"];
[self assert:@"Hide Sidebar" equals:[menuItem title] message:@"the title when the sidebar is visible"];
[sidebarItem setCollapsed:YES];
[controller validateUserInterfaceItem:menuItem];
[self assert:@"Show Sidebar" equals:[menuItem title] message:@"the title when the sidebar is collapsed"];
var inspectorMenuItem = [[CPMenuItem alloc] initWithTitle:@"Inspector" action:@selector(toggleInspector:) keyEquivalent:@""];
[self assertFalse:[controller validateUserInterfaceItem:inspectorMenuItem] message:@"disabled without an inspector"];
}
// MARK: - Archiving
- (void)testAnItemIsArchivedWithItsValues
{
[sidebarItem setHoldingPriority:260];
[sidebarItem setMinimumThickness:80];
[sidebarItem setMaximumThickness:300];
[sidebarItem setCollapseBehavior:CPSplitViewItemCollapseBehaviorPreferResizingSiblingsWithFixedSplitView];
[sidebarItem setAllowsFullHeightLayout:NO];
[sidebarItem setTitlebarSeparatorStyle:CPTitlebarSeparatorStyleLine];
[sidebarItem setCollapsed:YES];
var copy = [CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:sidebarItem]];
[self assert:CPSplitViewItemBehaviorSidebar equals:[copy behavior] message:@"the behavior"];
[self assertTrue:[copy isCollapsed] message:@"collapsed"];
[self assertTrue:[copy canCollapse] message:@"can collapse"];
[self assertTrue:[copy isSpringLoaded] message:@"spring loaded"];
[self assert:260 equals:[copy holdingPriority] message:@"the holding priority"];
[self assert:80 equals:[copy minimumThickness] message:@"the minimum"];
[self assert:300 equals:[copy maximumThickness] message:@"the maximum"];
[self assert:0.15 equals:[copy preferredThicknessFraction] message:@"the preferred fraction"];
[self assert:CPSplitViewItemCollapseBehaviorPreferResizingSiblingsWithFixedSplitView equals:[copy collapseBehavior] message:@"the collapse behavior"];
[self assertFalse:[copy allowsFullHeightLayout] message:@"full height layout"];
[self assert:CPTitlebarSeparatorStyleLine equals:[copy titlebarSeparatorStyle] message:@"the separator style"];
[self assertNotNull:[copy viewController] message:@"the view controller"];
}
- (void)testAControllerIsArchivedWithItsItemsAndSplitView
{
[sidebarItem setHoldingPriority:260];
[controller setMinimumThicknessForInlineSidebars:200];
[self loadTwoItems];
var copy = [CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:controller]],
items = [copy splitViewItems],
splitView = [copy splitView];
[self assert:2 equals:[items count] message:@"both items"];
[self assert:CPSplitViewItemBehaviorSidebar equals:[items[0] behavior] message:@"the sidebar comes first"];
[self assert:copy equals:[items[0] splitViewController] message:@"a decoded item knows its controller"];
[self assert:[[items[0] viewController], [items[1] viewController]] equals:[copy childViewControllers] message:@"the children are the items' view controllers"];
[self assert:200 equals:[copy minimumThicknessForInlineSidebars] message:@"the minimum for inline sidebars"];
[self assertNotNull:splitView message:@"the split view"];
[self assert:copy equals:[splitView delegate] message:@"the controller is the delegate of the split view"];
[copy view];
[self assert:[[[items[0] viewController] view], [[items[1] viewController] view]] equals:[splitView arrangedSubviews] message:@"the item views are arranged once"];
[items[0] setCollapsed:YES];
[self assertTrue:[[[items[0] viewController] view] isHidden] message:@"a decoded item collapses its view"];
}
@end
@implementation _CPSplitViewControllerTestChild : CPViewController
{
float _width;
}
- (id)initWithWidth:(float)aWidth
{
if (self = [super initWithCibName:nil bundle:nil])
_width = aWidth;
return self;
}
- (void)loadView
{
[self setView:[[CPView alloc] initWithFrame:CGRectMake(0, 0, _width, 100)]];
}
@end
@implementation _CPSplitViewControllerTestObserver : CPObject
{
int count;
}
- (id)init
{
if (self = [super init])
count = 0;
return self;
}
- (void)observeValueForKeyPath:(CPString)aKeyPath ofObject:(id)anObject change:(CPDictionary)aChange context:(id)aContext
{
count++;
}
@end
-186
View File
@@ -1,186 +0,0 @@
@import <AppKit/CPSplitViewController.j>
@import <AppKit/CPViewController.j>
// MARK: - Test Implementation
@implementation CPSplitViewControllerTest : OJTestCase
{
// Instance variables can be declared here if needed for tests.
}
// MARK: - Initialization Tests
- (void)testInit
{
var splitViewController = [[CPSplitViewController alloc] init];
[self assertNotNil:splitViewController message:@"CPSplitViewController should initialize."];
[self assertNotNil:[splitViewController splitViewItems] message:@"splitViewItems should be an empty array on init."];
[self assert:[[splitViewController splitViewItems] count] equals:0 message:@"splitViewItems should have 0 items on init."];
}
- (void)testViewLoading
{
var splitViewController = [[CPSplitViewController alloc] init];
// Accessing the view property triggers loadView
var view = [splitViewController view];
[self assertNotNil:view message:@"The controller's view should be created."];
[self assertTrue:[view isKindOfClass:[CPSplitView class]] message:@"The controller's view should be a CPSplitView."];
[self assert:[splitViewController splitView] equals:view message:@"The splitView accessor should return the controller's view."];
}
- (void)testSplitViewItemInit
{
var childVC = [[CPViewController alloc] init];
var item = [CPSplitViewItem splitViewItemWithViewController:childVC];
[self assertNotNil:item message:@"CPSplitViewItem should be created."];
[self assert:[item viewController] equals:childVC message:@"The item should hold the correct view controller."];
[self assertFalse:[item isCollapsed] message:@"A new item should not be collapsed."];
}
// MARK: - Item Management Tests
- (void)testAddSplitViewItem
{
var splitViewController = [[CPSplitViewController alloc] init];
[splitViewController view]; // Ensure view is loaded
var childVC1 = [[CPViewController alloc] init];
var item1 = [CPSplitViewItem splitViewItemWithViewController:childVC1];
[splitViewController addSplitViewItem:item1];
[self assert:[[splitViewController splitViewItems] count] equals:1 message:@"Should have 1 split view item."];
[self assert:[splitViewController splitViewItemForViewController:childVC1] equals:item1 message:@"Should be able to retrieve item by its view controller."];
[self assert:[item1 splitViewController] equals:splitViewController message:@"Item should have a reference to its parent controller."];
[self assert:[[[splitViewController splitView] arrangedSubviews] count] equals:1 message:@"The splitView should have one arranged subview."];
[self assert:[[[childVC1 view] superview] equals:[splitViewController splitView]] message:@"Child's view should be a subview of the splitView."];
}
- (void)testInsertSplitViewItem
{
var splitViewController = [[CPSplitViewController alloc] init];
[splitViewController view];
var childVC1 = [[CPViewController alloc] init];
var childVC2 = [[CPViewController alloc] init];
var item1 = [CPSplitViewItem splitViewItemWithViewController:childVC1];
var item2 = [CPSplitViewItem splitViewItemWithViewController:childVC2];
[splitViewController addSplitViewItem:item1];
[splitViewController insertSplitViewItem:item2 atIndex:0];
[self assert:[[splitViewController splitViewItems] count] equals:2 message:@"Should have 2 split view items."];
[self assert:[[splitViewController splitViewItems] objectAtIndex:0] equals:item2 message:@"Item2 should be at index 0."];
var arrangedSubviews = [[splitViewController splitView] arrangedSubviews];
[self assert:[arrangedSubviews objectAtIndex:0] equals:[childVC2 view] message:@"The view of item2 should be the first arranged subview."];
}
- (void)testRemoveSplitViewItem
{
var splitViewController = [[CPSplitViewController alloc] init];
[splitViewController view];
var childVC1 = [[CPViewController alloc] init];
var item1 = [CPSplitViewItem splitViewItemWithViewController:childVC1];
[splitViewController addSplitViewItem:item1];
[self assert:[[splitViewController splitViewItems] count] equals:1 message:@"Pre-condition: Should have 1 item."];
[splitViewController removeSplitViewItem:item1];
[self assert:[[splitViewController splitViewItems] count] equals:0 message:@"Should have 0 split view items after removal."];
[self assertNil:[item1 splitViewController] message:@"Item's reference to parent should be nil after removal."];
[self assert:[[[splitViewController splitView] arrangedSubviews] count] equals:0 message:@"The splitView should have no arranged subviews after removal."];
[self assertNil:[[childVC1 view] superview] message:@"Child's view should be removed from the splitView."];
}
- (void)testAddingItemsBeforeViewLoads
{
var splitViewController = [[CPSplitViewController alloc] init];
var childVC1 = [[CPViewController alloc] init];
var childVC2 = [[CPViewController alloc] init];
var item1 = [CPSplitViewItem splitViewItemWithViewController:childVC1];
var item2 = [CPSplitViewItem splitViewItemWithViewController:childVC2];
[splitViewController addSplitViewItem:item1];
[splitViewController addSplitViewItem:item2];
[self assert:[[[splitViewController childViewControllers] objectAtIndex:0] equals:childVC1] message:@"Child view controllers should be added immediately."];
// View is not loaded yet, so it should have no arranged subviews
[self assertNil:[splitViewController _splitView] message:@"SplitView should not exist yet."];
// Trigger viewDidLoad
[splitViewController view];
// Now check if the views were added
var arrangedSubviews = [[splitViewController splitView] arrangedSubviews];
[self assert:[arrangedSubviews count] equals:2 message:@"SplitView should have 2 subviews after loading."];
[self assert:[arrangedSubviews objectAtIndex:0] equals:[childVC1 view]];
[self assert:[arrangedSubviews objectAtIndex:1] equals:[childVC2 view]];
}
// MARK: - Toggling and State Tests
- (void)testToggleSidebar
{
var splitViewController = [[CPSplitViewController alloc] init];
var childVC1 = [[CPViewController alloc] init];
var item1 = [CPSplitViewItem splitViewItemWithViewController:childVC1];
[splitViewController addSplitViewItem:item1];
[splitViewController toggleSidebar:self];
[self assertTrue:[item1 isCollapsed] message:@"Sidebar item should be collapsed."];
[self assertTrue:[[childVC1 view] isHidden] message:@"Sidebar view should be hidden."];
[splitViewController toggleSidebar:self];
[self assertFalse:[item1 isCollapsed] message:@"Sidebar item should be expanded again."];
[self assertFalse:[[childVC1 view] isHidden] message:@"Sidebar view should be visible again."];
}
- (void)testToggleInspector
{
var splitViewController = [[CPSplitViewController alloc] init];
var childVC1 = [[CPViewController alloc] init];
var childVC2 = [[CPViewController alloc] init];
var item1 = [CPSplitViewItem splitViewItemWithViewController:childVC1];
var item2 = [CPSplitViewItem splitViewItemWithViewController:childVC2];
[splitViewController addSplitViewItem:item1];
[splitViewController addSplitViewItem:item2];
[splitViewController toggleInspector:self];
[self assertTrue:[item2 isCollapsed] message:@"Inspector item should be collapsed."];
[self assertTrue:[[childVC2 view] isHidden] message:@"Inspector view should be hidden."];
[splitViewController toggleInspector:self];
[self assertFalse:[item2 isCollapsed] message:@"Inspector item should be expanded again."];
[self assertFalse:[[childVC2 view] isHidden] message:@"Inspector view should be visible again."];
}
- (void)testSetCollapsed
{
var childVC = [[CPViewController alloc] init];
var item = [CPSplitViewItem splitViewItemWithViewController:childVC];
var childView = [childVC view];
[item setCollapsed:YES];
[self assertTrue:[item isCollapsed] message:@"Item's collapsed property should be YES."];
[self assertTrue:[childView isHidden] message:@"The item's view should be hidden."];
[item setCollapsed:NO];
[self assertFalse:[item isCollapsed] message:@"Item's collapsed property should be NO."];
[self assertFalse:[childView isHidden] message:@"The item's view should be visible."];
// Test idempotency
[item setCollapsed:NO];
[self assertFalse:[item isCollapsed] message:@"Setting the same value should have no effect."];
}
@end