@@ -1,10 +1,14 @@
using System.Collections.ObjectModel ;
using System.Diagnostics ;
using System.Globalization ;
using System.Windows ;
using System.Windows.Automation.Peers ;
using System.Windows.Controls ;
using System.Windows.Input ;
using System.Windows.Media ;
using System.Windows.Media.Imaging ;
using System.Windows.Threading ;
using Titano.Core.Localization ;
using Titano.Testing ;
using Titano.Ui.Controls ;
using Titano.Ui.Theming ;
@@ -127,6 +131,153 @@ public sealed class VisualsSmokeTests
Assert . Equal ( 412 , strip . CurrentIndex ) ;
} ) ;
[Test]
public void ListsKeepTheirFillWhenDisabled ( ) = > WpfHost . Run ( ( ) = >
{
var surface = ColorOf ( "Brush.Surface" ) ;
foreach ( var list in new [ ] { new ListBox ( ) , new ListBox { Style = ( Style ) Application . Current . FindResource ( "ListBox.Cards" ) } } )
{
list . Background = ( Brush ) Application . Current . FindResource ( "Brush.Surface" ) ;
list . ItemsSource = new [ ] { "Harbour sunset" } ;
list . IsEnabled = false ;
var bitmap = Render ( list , 200 , 100 ) ;
Assert . NotNull ( VisualTreeHelper . GetChild ( list , 0 ) as Border , "the theme's list frame" ) ;
Assert . True ( Near ( PixelAt ( bitmap , 100 , 95 ) , surface , 1 ) , "a disabled list keeps its fill instead of the system's white" ) ;
}
// A list view keeps the system template, which carries the column headers.
var view = new ListView { ItemsSource = new [ ] { "Harbour sunset" } , View = new GridView { Columns = { new GridViewColumn { Header = "Title" } } } } ;
Render ( view , 200 , 100 ) ;
Assert . NotNull ( FindChild < GridViewHeaderRowPresenter > ( view ) , "the column header row" ) ;
} ) ;
[Test]
public void ChartLabelsAreMutedAndMonthsWithoutDataTakeTheStrokeTone ( ) = > WpfHost . Run ( ( ) = >
{
foreach ( var theme in new [ ] { AppTheme . Dark , AppTheme . Light } )
{
ThemeManager . Apply ( theme ) ;
var ( muted , faint , stroke , card ) = ( ColorOf ( "Brush.TextMuted" ) , ColorOf ( "Brush.TextFaint" ) , ColorOf ( "Brush.Stroke" ) , ColorOf ( "Brush.Card" ) ) ;
// Zero values draw no bars: what remains is the labels.
var bars = Covered ( Render ( new BarChart { Items = [ new ChartItem ( "Canon EOS R6" , 0 ) , new ChartItem ( "Nikon Z 8" , 0 ) ] , ShowValues = false } , 300 , 60 ) ) ;
Assert . NotEmpty ( bars ) ;
Assert . True ( bars . All ( c = > Near ( c , muted ) ) , $"bar labels are TextMuted in {theme}, not TextFaint ({faint})" ) ;
var months = new [ ] { "J" , "F" , "M" , "A" , "M" , "J" , "J" , "A" , "S" , "O" , "N" , "D" } ;
var empty = new ChartRow ( "2027" , Enumerable . Repeat ( double . NaN , 12 ) . ToArray ( ) ) ;
var heatmap = Covered ( Render ( new HeatmapStrip { Rows = [ empty ] , ColumnLabels = months , ShowScale = false } , 400 , 60 ) ) ;
Assert . Contains ( heatmap , c = > Near ( c , stroke ) ) ;
Assert . False ( heatmap . Any ( c = > Near ( c , card ) & & ! Near ( c , stroke ) ) , $"months without data are not the card colour in {theme}" ) ;
Assert . True ( heatmap . All ( c = > Near ( c , stroke ) | | Near ( c , muted ) ) , $"heatmap labels are TextMuted in {theme}" ) ;
}
ThemeManager . Apply ( AppTheme . Dark ) ;
} ) ;
[Test]
public void ChartsTakeTheHandAndTheKeyboardOnlyWhileSomethingHandlesActivation ( ) = > WpfHost . Run ( ( ) = >
{
var items = new [ ] { new ChartItem ( "2025" , 2 ) , new ChartItem ( "2026" , 1 ) } ;
var bars = new BarChart { Items = items } ;
var stacked = new StackedBar { Items = items } ;
var heatmap = new HeatmapStrip { Rows = [ new ChartRow ( "2026" , [ 1 , 2 , 3 ] ) ] , ColumnLabels = [ "J" , "F" , "M" ] } ;
ChartItemEventHandler handler = ( _ , _ ) = > { } ;
var charts = new ( DrawingElement Chart , Action Attach , Action Detach ) [ ]
{
( bars , ( ) = > bars . ItemActivated + = handler , ( ) = > bars . ItemActivated - = handler ) ,
( stacked , ( ) = > stacked . ItemActivated + = handler , ( ) = > stacked . ItemActivated - = handler ) ,
( heatmap , ( ) = > heatmap . CellActivated + = handler , ( ) = > heatmap . CellActivated - = handler ) ,
} ;
foreach ( var ( chart , attach , detach ) in charts )
{
Render ( chart , 300 , 80 ) ;
var name = chart . GetType ( ) . Name ;
Assert . False ( ShowsHand ( chart ) , $"{name}: no hand cursor while nothing handles a click" ) ;
Assert . False ( chart . Focusable , $"{name}: no tab stop while nothing handles Enter" ) ;
attach ( ) ;
Assert . True ( ShowsHand ( chart ) , $"{name}: the hand over an item once a handler is attached" ) ;
Assert . True ( chart . Focusable , $"{name}: the keyboard reaches it once a handler is attached" ) ;
detach ( ) ;
Assert . False ( ShowsHand ( chart ) , $"{name}: no hand once the handler is gone" ) ;
Assert . False ( chart . Focusable , $"{name}: no tab stop once the handler is gone" ) ;
}
} ) ;
[Test]
public void TheKeyboardChoosesAndActivatesABar ( ) = > WpfHost . Run ( ( ) = >
{
var bars = new BarChart { Items = [ new ChartItem ( "2024" , 3 ) , new ChartItem ( "2025" , 2 ) , new ChartItem ( "2026" , 1 ) ] } ;
var host = new Border { Child = bars } ;
var window = new Window
{
Content = host ,
Width = 320 ,
Height = 160 ,
WindowStyle = WindowStyle . None ,
WindowStartupLocation = WindowStartupLocation . Manual ,
Left = - 20000 ,
Top = - 20000 ,
ShowActivated = false ,
ShowInTaskbar = false ,
} ;
window . Show ( ) ;
try
{
window . UpdateLayout ( ) ;
var bubbled = new List < int > ( ) ;
host . AddHandler ( BarChart . ItemActivatedEvent , new ChartItemEventHandler ( ( _ , e ) = > bubbled . Add ( e . Row ) ) ) ;
Press ( bars , Key . Enter ) ;
Assert . Empty ( bubbled , "Enter does nothing while no handler is attached to the chart" ) ;
var rows = new List < int > ( ) ;
bars . ItemActivated + = ( _ , e ) = > rows . Add ( e . Row ) ;
Press ( bars , Key . Down ) ;
Press ( bars , Key . Down ) ;
Press ( bars , Key . Down ) ;
Press ( bars , Key . Enter ) ;
Press ( bars , Key . Home ) ;
Press ( bars , Key . Space ) ;
Assert . SequenceEqual ( [ 2 , 0 ] , rows , "Down stops at the last bar; Home goes back to the first" ) ;
Assert . Contains ( "2024" , UIElementAutomationPeer . CreatePeerForElement ( bars ) . GetItemStatus ( ) ) ;
}
finally
{
window . Close ( ) ;
}
} ) ;
[Test]
public void CategoryBarsStartOnTheReadingSideWhileMonthsKeepTheirOrder ( ) = > WpfHost . Run ( ( ) = >
{
var accent = ColorOf ( "Brush.Accent" ) ;
var culture = Loc . Culture ;
try
{
foreach ( var ( language , rightToLeft ) in new [ ] { ( "en" , false ) , ( "ar" , true ) } )
{
Loc . SetCulture ( CultureInfo . GetCultureInfo ( language ) ) ;
// Rows 22 px apart: the short second bar (y 26– 40) grows from the labels' side.
var bars = Render ( new BarChart { Items = [ new ChartItem ( "A" , 4 ) , new ChartItem ( "B" , 1 ) ] , ShowValues = false } , 300 , 44 ) ;
var ( left , right ) = Extent ( bars , accent , 33 ) ;
Assert . True ( rightToLeft ? left > 150 : right < 150 & & right > 0 , $"{language}: the short bar spans {left}– {right}" ) ;
// The largest segment comes first, on the reading side.
var stacked = Render ( new StackedBar { Items = [ new ChartItem ( "A" , 3 ) , new ChartItem ( "B" , 1 ) ] , ShowLegend = false } , 300 , 12 ) ;
( left , right ) = Extent ( stacked , accent , 6 ) ;
Assert . True ( rightToLeft ? left > 60 & & right > = 296 : left is > = 0 and < = 3 & & right < 240 , $"{language}: the first segment spans {left}– {right}" ) ;
// The full month (January) stays on the left: months are a time axis.
var months = Render ( new HeatmapStrip { Rows = [ new ChartRow ( "2026" , [ 5 , 0 , 0 , 0 ] ) ] , ColumnLabels = [ "1" , "2" , "3" , "4" ] , ShowScale = false } , 300 , 60 ) ;
( left , right ) = Extent ( months , accent , 31 ) ;
Assert . True ( left > 0 & & right < 150 , $"{language}: January spans {left}– {right}" ) ;
}
}
finally
{
Loc . SetCulture ( culture ) ;
}
} ) ;
private static IEnumerable < ( string Name , FrameworkElement Element , double Width , double Height ) > Samples ( )
{
var n = 120_000 ;
@@ -235,6 +386,61 @@ public sealed class VisualsSmokeTests
return bitmap ;
}
private static Color ColorOf ( string key ) = > ( ( SolidColorBrush ) Application . Current . FindResource ( key ) ) . Color ;
private static Color PixelAt ( BitmapSource bitmap , int x , int y )
{
var pixel = new byte [ 4 ] ;
bitmap . CopyPixels ( new Int32Rect ( x , y , 1 , 1 ) , pixel , 4 , 0 ) ;
return Color . FromArgb ( pixel [ 3 ] , pixel [ 2 ] , pixel [ 1 ] , pixel [ 0 ] ) ;
}
/// <summary>The colours drawn at least two-thirds opaque, un-premultiplied: text edges keep the colour of their brush.</summary>
private static List < Color > Covered ( BitmapSource bitmap )
{
var stride = bitmap . PixelWidth * 4 ;
var pixels = new byte [ stride * bitmap . PixelHeight ] ;
bitmap . CopyPixels ( pixels , stride , 0 ) ;
var colours = new List < Color > ( ) ;
for ( var i = 0 ; i < pixels . Length ; i + = 4 )
{
var a = pixels [ i + 3 ] ;
if ( a < 170 ) continue ;
colours . Add ( Color . FromRgb ( ( byte ) ( pixels [ i + 2 ] * 255 / a ) , ( byte ) ( pixels [ i + 1 ] * 255 / a ) , ( byte ) ( pixels [ i ] * 255 / a ) ) ) ;
}
return colours ;
}
private static bool Near ( Color a , Color b , int tolerance = 12 ) = >
Math . Abs ( a . R - b . R ) < = tolerance & & Math . Abs ( a . G - b . G ) < = tolerance & & Math . Abs ( a . B - b . B ) < = tolerance ;
/// <summary>First and last x of row <paramref name="y"/> drawn in <paramref name="colour"/> at full opacity; (− 1, − 1) when none.</summary>
private static ( int Left , int Right ) Extent ( BitmapSource bitmap , Color colour , int y )
{
var ( left , right ) = ( - 1 , - 1 ) ;
for ( var x = 0 ; x < bitmap . PixelWidth ; x + + )
{
var pixel = PixelAt ( bitmap , x , y ) ;
if ( pixel . A ! = 255 | | ! Near ( pixel , colour , 2 ) ) continue ;
if ( left < 0 ) left = x ;
right = x ;
}
return ( left , right ) ;
}
private static bool ShowsHand ( DrawingElement chart )
{
for ( var y = 0.0 ; y < chart . ActualHeight ; y + = 2 )
{
for ( var x = 0.0 ; x < chart . ActualWidth ; x + = 2 )
if ( chart . CursorAt ( new Point ( x , y ) ) = = Cursors . Hand ) return true ;
}
return false ;
}
private static void Press ( UIElement target , Key key ) = >
target . RaiseEvent ( new KeyEventArgs ( Keyboard . PrimaryDevice , PresentationSource . FromVisual ( target ) ! , 0 , key ) { RoutedEvent = Keyboard . KeyDownEvent } ) ;
private static RenderTargetBitmap Render ( FrameworkElement element , double width , double height )
{
var host = Host ( element ) ;