Files
cappuccino/Tests/AppKit/CPBoxTest.j
David Richardson 63187a1af1 CPBox: Default custom boxes to clear fill unless set
Previously, a custom box without a fill drew a black rectangle under
Aristo3 because its fill fell back to the theme's background-color—
which Aristo3 supplies as a CSS color that the custom box's drawing
canvas cannot paint.

Matching AppKit, a custom box now defaults to a clear fill while
retaining any fill set explicitly or decoded from an archive, using a
new private -_hasOwnValueForThemeAttribute: helper to distinguish
the two cases.
2026-11-25 09:18:51 -06:00

168 lines
4.8 KiB
Plaintext

/*
* CPBoxTest.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 box that is archived and read back must keep its content view, its title and
its internal views, and the content view must remain inside the box.
*/
@implementation CPBoxTest : OJTestCase
{
CPBox box;
CPView contentView;
Function logWarning;
CPArray warnings;
}
- (void)setUp
{
// This will init the global var CPApp which are used internally in the AppKit
[[CPApplication alloc] init];
warnings = [];
logWarning = CPLog.warn;
CPLog.warn = function(aMessage)
{
warnings.push(String(aMessage));
};
box = [[CPBox alloc] initWithFrame:CGRectMake(0.0, 0.0, 200.0, 100.0)];
contentView = [[CPView alloc] initWithFrame:CGRectMake(0.0, 0.0, 60.0, 40.0)];
[contentView addSubview:[[CPView alloc] initWithFrame:CGRectMake(3.0, 5.0, 20.0, 10.0)]];
[box setContentView:contentView];
}
- (void)tearDown
{
CPLog.warn = logWarning;
}
- (CPBox)archivedBox
{
return [CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:box]];
}
- (void)testArchivingKeepsTheContentView
{
var decodedContentView = [[self archivedBox] contentView];
[self assertNotNull:decodedContentView];
[self assert:1 equals:[[decodedContentView subviews] count]];
[self assertTrue:CGRectEqualToRect([contentView frame], [decodedContentView frame])];
}
- (void)testArchivingKeepsTheContentViewInsideTheBox
{
var decodedBox = [self archivedBox],
boxView = [[decodedBox contentView] superview];
[self assertNotNull:boxView];
[self assert:decodedBox equals:[boxView superview]];
[self assertTrue:[[decodedBox subviews] containsObject:boxView]];
}
- (void)testArchivingLeavesTheBoxUnchanged
{
var subviews = [box subviews];
[self archivedBox];
[self assert:subviews equals:[box subviews]];
[self assert:contentView equals:[box contentView]];
}
- (void)testArchivingKeepsTheTitle
{
[box setTitle:@"Title"];
[box setTitlePosition:CPAtTop];
var decodedBox = [self archivedBox];
[self assert:@"Title" equals:[decodedBox title]];
[self assert:CPAtTop equals:[decodedBox titlePosition]];
}
- (void)testUntitledBoxDecodesWithoutAWarning
{
[self archivedBox];
[self assert:[] equals:warnings];
}
// MARK: - Fill of a custom box
/*
As in AppKit, a custom box has no fill unless one was set: fillColor is clear by default for
NSBoxCustom. A primary box keeps the theme's fill.
*/
- (void)testACustomBoxWithoutAFillHasNoFill
{
[box setBoxType:CPBoxCustom];
[box setBorderType:CPNoBorder];
[self assertTrue:[[box fillColor] isEqual:[CPColor clearColor]] message:@"a custom box without a fill has the clear colour"];
}
- (void)testACustomBoxWithALineBorderAndNoFillHasNoFill
{
[box setBoxType:CPBoxCustom];
[box setBorderType:CPLineBorder];
[self assertTrue:[[box fillColor] isEqual:[CPColor clearColor]] message:@"a custom box with a line border and no fill has the clear colour"];
}
- (void)testACustomBoxKeepsAFillThatWasSet
{
[box setBoxType:CPBoxCustom];
[box setBorderType:CPLineBorder];
[box setFillColor:[CPColor redColor]];
[self assertTrue:[[box fillColor] isEqual:[CPColor redColor]] message:@"a custom box keeps its fill"];
}
- (void)testArchivingKeepsTheFillOfACustomBox
{
[box setBoxType:CPBoxCustom];
[box setBorderType:CPLineBorder];
[box setFillColor:[CPColor redColor]];
[self assertTrue:[[[self archivedBox] fillColor] isEqual:[CPColor redColor]] message:@"a decoded custom box keeps its fill"];
}
- (void)testArchivingKeepsACustomBoxWithoutAFill
{
[box setBoxType:CPBoxCustom];
[box setBorderType:CPNoBorder];
[self assertTrue:[[[self archivedBox] fillColor] isEqual:[CPColor clearColor]] message:@"a decoded custom box without a fill has the clear colour"];
}
- (void)testAPrimaryBoxKeepsTheThemeFill
{
[self assertTrue:[box fillColor] === [box valueForThemeAttribute:@"background-color"] message:@"a primary box takes the theme's fill"];
}
@end