11using System . ComponentModel ;
2+ using System . Diagnostics ;
23using System . Linq ;
4+ using Microsoft . UI . Xaml ;
35using Microsoft . UI . Xaml . Media ;
46using NUnit . Framework ;
57using Windows . UI . Text ;
68
79namespace UnoPropertyGrid . Tests ;
810
911[ TestFixture ]
12+ #if WINDOWS_APP_SDK
13+ [ Apartment ( System . Threading . ApartmentState . STA ) ]
14+ #endif
1015public sealed class PropertyProviderTests
1116{
1217 [ Test ]
@@ -100,7 +105,24 @@ public void EventViewModel_ValidatesHandlerName()
100105 [ Test ]
101106 public void ViewModel_WritesFontFamilyValue ( )
102107 {
108+ #if WINDOWS_APP_SDK
109+ if ( Environment . GetEnvironmentVariable ( StandaloneWinUITestEnvironmentVariable ) != "1" )
110+ {
111+ RunFontFamilyTestInStandaloneProcess ( ) ;
112+ return ;
113+ }
114+ #endif
115+
116+ RunWithWinUIApplication ( Test ) ;
117+
118+ static void Test ( )
119+ {
103120 var target = new SampleComponent ( ) ;
121+ target . FontFamily = new FontFamily ( "Segoe UI" ) ;
122+ Assert . That ( target . FontFamily , Is . Not . Null ) ;
123+ Assert . That ( target . FontFamily ! . Source , Is . EqualTo ( "Segoe UI" ) ) ;
124+ target . FontFamily = null ;
125+
104126 var property = new TypeDescriptorPropertyProvider ( )
105127 . GetProperties ( target )
106128 . Single ( p => p . Name == nameof ( SampleComponent . FontFamily ) ) ;
@@ -110,7 +132,10 @@ public void ViewModel_WritesFontFamilyValue()
110132
111133 viewModel . FontFamilyValue = "Consolas" ;
112134
113- Assert . That ( target . FontFamily . Source , Is . EqualTo ( "Consolas" ) ) ;
135+ Assert . That ( viewModel . HasError , Is . False , viewModel . Error ) ;
136+ Assert . That ( target . FontFamily , Is . Not . Null ) ;
137+ Assert . That ( target . FontFamily ! . Source , Is . EqualTo ( "Consolas" ) ) ;
138+ }
114139 }
115140
116141 [ Test ]
@@ -157,7 +182,7 @@ sealed class SampleComponent
157182
158183 public string ReadOnlyName => "fixed" ;
159184
160- public FontFamily FontFamily { get ; set ; } = new ( "Segoe UI" ) ;
185+ public FontFamily ? FontFamily { get ; set ; }
161186
162187 public FontWeight FontWeight { get ; set ; } = new ( ) { Weight = 400 } ;
163188
@@ -179,4 +204,67 @@ enum SampleMode
179204 First ,
180205 Second
181206 }
207+
208+ #if WINDOWS_APP_SDK
209+ sealed class TestApplication : Application
210+ {
211+ }
212+ #endif
213+
214+ static void RunWithWinUIApplication ( Action action )
215+ {
216+ #if WINDOWS_APP_SDK
217+ Exception ? exception = null ;
218+ Application . Start ( _ =>
219+ {
220+ var app = new TestApplication ( ) ;
221+ try
222+ {
223+ action ( ) ;
224+ }
225+ catch ( Exception ex )
226+ {
227+ exception = ex ;
228+ }
229+ finally
230+ {
231+ Application . Current . Exit ( ) ;
232+ }
233+ } ) ;
234+
235+ if ( exception != null )
236+ throw exception ;
237+ #else
238+ action ( ) ;
239+ #endif
240+ }
241+
242+ #if WINDOWS_APP_SDK
243+ const string StandaloneWinUITestEnvironmentVariable = "UNOPROPERTYGRID_STANDALONE_WINUI_TEST" ;
244+
245+ static void RunFontFamilyTestInStandaloneProcess ( )
246+ {
247+ var assemblyPath = typeof ( PropertyProviderTests ) . Assembly . Location ;
248+ var executablePath = Path . ChangeExtension ( assemblyPath , ".exe" ) ;
249+ Assert . That ( File . Exists ( executablePath ) , Is . True , $ "Test executable was not found at '{ executablePath } '.") ;
250+
251+ using var process = new Process ( ) ;
252+ process . StartInfo = new ProcessStartInfo ( executablePath )
253+ {
254+ UseShellExecute = false ,
255+ RedirectStandardOutput = true ,
256+ RedirectStandardError = true ,
257+ WorkingDirectory = Path . GetDirectoryName ( executablePath ) !
258+ } ;
259+ process . StartInfo . ArgumentList . Add ( "--where" ) ;
260+ process . StartInfo . ArgumentList . Add ( $ "test == '{ typeof ( PropertyProviderTests ) . FullName } .{ nameof ( ViewModel_WritesFontFamilyValue ) } '") ;
261+ process . StartInfo . Environment [ StandaloneWinUITestEnvironmentVariable ] = "1" ;
262+
263+ process . Start ( ) ;
264+ var output = process . StandardOutput . ReadToEnd ( ) ;
265+ var error = process . StandardError . ReadToEnd ( ) ;
266+ Assert . That ( process . WaitForExit ( 60000 ) , Is . True , "Standalone WinUI test process timed out." ) ;
267+ Assert . That ( process . ExitCode , Is . EqualTo ( 0 ) , output + error ) ;
268+ }
269+ #endif
182270}
0 commit comments