Tests: Test switching between two CSS themes using CPThemeSwitchTest

CPThemeSwitchTest switched between Aristo2 and Aristo3. Because Aristo2
is unsupported until converted to CSS, the tests failed on its image
structure, masking theme values that views failed to re-read.

The tests now switch between Aristo3 and an in-memory variant with every colour and
inset changed, keeping sizes and fonts unchanged because frame changes
belong to the metric pass.
This commit is contained in:
David Richardson committed 2026-12-02 09:43:37 -06:00
1 parent 8944a1de8a
commit 8b41fdbdea
1 file changed
+138 -16
+138 -16
View File
@@ -45,9 +45,10 @@
stepper, keeps the old theme's size. Such a frame difference belongs to the metric
pass, not to a value that was not read again.
These tests use the real Aristo2 and Aristo3 themes from the build, so they need
the themes built first, as `npx jake build` does. Each test reports every
difference it finds.
These tests use the real Aristo3 theme from the build, so they need the themes
built first, as `npx jake build` does. The second theme is a variant of Aristo3
that the tests make while they run (see -variantOfTheme:). Each test reports
every difference it finds.
The application's theme switch is not built yet. -switchToTheme:inWindow: does
what it must do, in the same order: set the default theme, then change the theme
@@ -56,11 +57,18 @@
var fs = require("fs"),
path = require("path");
/*
The variant of Aristo3, made once for the whole run, so that its name never refers
to two theme objects; and the number of colours it holds so far.
*/
var ThemeSwitchVariant = nil,
ThemeSwitchVariantColorCount = 0;
@implementation CPThemeSwitchTest : OJTestCase
{
CPTheme defaultTheme;
CPTheme aristo2;
CPTheme aristo3;
CPTheme variant;
}
- (void)setUp
@@ -69,10 +77,11 @@ var fs = require("fs"),
[[CPApplication alloc] init];
defaultTheme = [CPTheme defaultTheme];
aristo2 = BuiltThemeNamed(@"Aristo2");
aristo3 = BuiltThemeNamed(@"Aristo3");
[self assertTrue:aristo2 !== nil && aristo3 !== nil message:@"Aristo2 and Aristo3 were not found in the built blends. Build the themes first."];
[self assertTrue:aristo3 !== nil message:@"Aristo3 was not found in the built blends. Build the themes first."];
variant = [self variantOfTheme:aristo3];
}
- (void)tearDown
@@ -80,6 +89,113 @@ var fs = require("fs"),
[CPTheme setDefaultTheme:defaultTheme];
}
/*
2026-12-01: the switch was tested between Aristo2 and Aristo3. Aristo2 is not
supported until it is converted to CSS, so those tests failed on its image
structure, and that output hid the values that views did not read again.
The second theme is now a copy of a CSS theme in which every colour and every
inset is different, so a value copied from the old theme shows as a difference.
It exists only in memory, while the tests run.
- A colour becomes a new colour of its own, of the same kind. Some views choose
their subviews by the kind of a colour (CPTextField lays out a bezel view
only for a colour without CSS), so a change of kind would change the
structure, as the switch from Aristo2 did. A plain colour gets new
components. A CSS colour gets copies of its dictionaries with one more
property, a custom property that no style rule reads. A colour that the
theme resolves by name keeps its name: the named value is itself a colour
of the theme, and changes there.
- An inset grows by one on each side.
- Sizes and fonts stay. They change frames, and a frame difference belongs to
the metric pass, not to a value that was not read again.
- Flags, numbers, strings and images stay. Flags and numbers select behaviour,
such as css-based or an alignment, and a changed one can select a behaviour
the view does not have.
*/
- (CPTheme)variantOfTheme:(CPTheme)aTheme
{
if (ThemeSwitchVariant)
return ThemeSwitchVariant;
var theme = [[CPTheme alloc] initWithName:[aTheme name] + @" variant"],
attributesByClass = aTheme._attributes,
classNames = [[attributesByClass allKeys] sortedArrayUsingSelector:@selector(compare:)];
for (var i = 0; i < classNames.length; i++)
{
var attributes = [attributesByClass objectForKey:classNames[i]],
names = [[attributes allKeys] sortedArrayUsingSelector:@selector(compare:)];
for (var j = 0; j < names.length; j++)
{
var attribute = [attributes objectForKey:names[j]],
values = [attribute values],
states = [[values allKeys] sortedArrayUsingSelector:@selector(compare:)],
variantValues = @{},
variantAttribute = [[_CPThemeAttribute alloc] initWithName:[attribute name]
defaultValue:[attribute defaultValue]
defaultAttribute:attribute._themeDefaultAttribute];
for (var k = 0; k < states.length; k++)
[variantValues setObject:[self variantOfValue:[values objectForKey:states[k]]] forKey:states[k]];
variantAttribute._values = variantValues;
[theme _recordAttribute:variantAttribute forClass:classNames[i]];
}
}
ThemeSwitchVariant = theme;
return ThemeSwitchVariant;
}
- (id)variantOfValue:(id)aValue
{
if (aValue === nil || aValue === undefined || typeof aValue !== "object")
return aValue;
if (aValue.isa)
{
if ([aValue isKindOfClass:[_CPDynamicColor class]])
return aValue;
if ([aValue isKindOfClass:[CPColor class]])
{
var n = ++ThemeSwitchVariantColorCount;
if ([aValue isCSSBased])
{
var cssDictionary = [aValue cssDictionary] ? [CPDictionary dictionaryWithDictionary:[aValue cssDictionary]] : @{},
beforeDictionary = [aValue cssBeforeDictionary] ? [CPDictionary dictionaryWithDictionary:[aValue cssBeforeDictionary]] : nil,
afterDictionary = [aValue cssAfterDictionary] ? [CPDictionary dictionaryWithDictionary:[aValue cssAfterDictionary]] : nil;
[cssDictionary setObject:String(n) forKey:@"--theme-switch-variant"];
return [CPColor colorWithCSSDictionary:cssDictionary beforeDictionary:beforeDictionary afterDictionary:afterDictionary];
}
return [CPColor colorWithRed:((n * 37) % 256) / 255.0 green:((n * 91) % 256) / 255.0 blue:((n * 53) % 256) / 255.0 alpha:1.0];
}
if ([aValue isKindOfClass:[CPArray class]])
{
var self_ = self;
return aValue.map(function(anElement) { return [self_ variantOfValue:anElement]; });
}
return aValue;
}
var keys = Object.keys(aValue).sort();
if (keys.join(",") === "bottom,left,right,top" && keys.every(function(aKey) { return typeof aValue[aKey] === "number"; }))
return CGInsetMake(aValue.top + 1.0, aValue.right + 1.0, aValue.bottom + 1.0, aValue.left + 1.0);
return aValue;
}
/*
The view classes of the drop-in baseline, so that a class cannot leave these tests
unnoticed. A class the archive tests cannot create is left out for the same reason.
@@ -118,10 +234,16 @@ var fs = require("fs"),
/*
Describes a theme value. Unlike the drop-in snapshot, an image is described by its
file, so that a different image counts as a difference.
file, and a CSS colour by its dictionaries, so that a different image or CSS colour
counts as a difference. A CSS colour's cssString does not hold its dictionaries.
*/
- (CPString)describeThemeValue:(id)aValue
{
if (aValue && aValue.isa && [aValue isKindOfClass:[CPColor class]] && ![aValue isKindOfClass:[_CPDynamicColor class]] && [aValue isCSSBased])
return "CPColor:css " + [self describeThemeValue:[aValue cssDictionary]] +
" before " + [self describeThemeValue:[aValue cssBeforeDictionary]] +
" after " + [self describeThemeValue:[aValue cssAfterDictionary]];
if (aValue && aValue.isa && [aValue isKindOfClass:[CPImage class]])
return "CPImage:" + ([aValue filename] || "") + " " + DropInDescribeValue([aValue size]);
@@ -242,16 +364,16 @@ var fs = require("fs"),
return differences;
}
- (void)testSwitchingFromAristo2ToAristo3MatchesAViewMadeUnderAristo3
- (void)testSwitchingFromAristo3ToAVariantMatchesAViewMadeUnderTheVariant
{
var differences = [self differencesSwitchingFromTheme:aristo2 toTheme:aristo3];
var differences = [self differencesSwitchingFromTheme:aristo3 toTheme:variant];
[self assertTrue:differences.length === 0 message:@"After the theme changes, a view must match one made under the new theme:\n" + differences.join("\n")];
}
- (void)testSwitchingFromAristo3ToAristo2MatchesAViewMadeUnderAristo2
- (void)testSwitchingFromAVariantToAristo3MatchesAViewMadeUnderAristo3
{
var differences = [self differencesSwitchingFromTheme:aristo3 toTheme:aristo2];
var differences = [self differencesSwitchingFromTheme:variant toTheme:aristo3];
[self assertTrue:differences.length === 0 message:@"After the theme changes, a view must match one made under the new theme:\n" + differences.join("\n")];
}
@@ -284,7 +406,7 @@ var fs = require("fs"),
{
try
{
[CPTheme setDefaultTheme:aristo2];
[CPTheme setDefaultTheme:variant];
var window = [[CPWindow alloc] initWithContentRect:CGRectMake(0.0, 0.0, 400.0, 300.0)
styleMask:CPTitledWindowMask | CPClosableWindowMask | CPMiniaturizableWindowMask | CPResizableWindowMask],
@@ -311,7 +433,7 @@ var fs = require("fs"),
- (void)testAViewOutsideAWindowTakesTheCurrentThemeWhenItEntersOne
{
[CPTheme setDefaultTheme:aristo2];
[CPTheme setDefaultTheme:variant];
var window = [[CPWindow alloc] initWithContentRect:CGRectMake(0.0, 0.0, 400.0, 300.0) styleMask:CPTitledWindowMask],
button = [self newViewOfClassNamed:@"CPButton"];
@@ -324,16 +446,16 @@ var fs = require("fs"),
- (void)testAViewOutsideAWindowKeepsAnExplicitThemeWhenItEntersOne
{
[CPTheme setDefaultTheme:aristo2];
[CPTheme setDefaultTheme:variant];
var window = [[CPWindow alloc] initWithContentRect:CGRectMake(0.0, 0.0, 400.0, 300.0) styleMask:CPTitledWindowMask],
button = [self newViewOfClassNamed:@"CPButton"];
[button setTheme:aristo2];
[button setTheme:variant];
[self switchToTheme:aristo3 inWindow:window];
[[window contentView] addSubview:button];
[self assertTrue:[button theme] === aristo2 message:@"A view whose theme was set explicitly must keep it when it enters a window, but it uses " + [[button theme] name]];
[self assertTrue:[button theme] === variant message:@"A view whose theme was set explicitly must keep it when it enters a window, but it uses " + [[button theme] name]];
}
@end