11// Licensed to the .NET Foundation under one or more agreements.
22// The .NET Foundation licenses this file to you under the MIT license.
33
4+ using System ;
45using System . IO ;
56using System . Linq ;
67using Xunit ;
@@ -18,22 +19,26 @@ public PreloadingTests(ITestOutputHelper output, SharedBuildPerTestClassFixture
1819 }
1920
2021 [ Theory ]
21- [ InlineData ( false , false ) ]
22- [ InlineData ( false , true ) ]
23- [ InlineData ( true , false ) ]
24- [ InlineData ( true , true ) ]
25- public void PreloadAssets ( bool isPublish , bool fingerprintDotnetJs )
22+ [ InlineData ( false , false , false ) ]
23+ [ InlineData ( false , false , true ) ]
24+ [ InlineData ( false , true , false ) ]
25+ [ InlineData ( false , true , true ) ]
26+ [ InlineData ( true , false , false ) ]
27+ [ InlineData ( true , false , true ) ]
28+ [ InlineData ( true , true , false ) ]
29+ [ InlineData ( true , true , true ) ]
30+ public void PreloadAssets ( bool isPublish , bool fingerprintDotnetJs , bool preloadAssets )
2631 {
2732 Configuration config = Configuration . Debug ;
2833 ProjectInfo info = CopyTestAsset ( config , aot : false , TestAsset . WasmBasicTestApp , "PreloadAssets" ) ;
2934
30- string extraMSBuildArgs = $ "-p:WasmFingerprintDotnetJs={ fingerprintDotnetJs } ";
35+ string extraMSBuildArgs = $ "-p:WasmFingerprintDotnetJs={ fingerprintDotnetJs . ToString ( ) . ToLower ( ) } -p:WasmPreloadAssets= { preloadAssets . ToString ( ) . ToLower ( ) } ";
3136 if ( isPublish )
3237 PublishProject ( info , config , new PublishOptions ( ExtraMSBuildArgs : extraMSBuildArgs ) , wasmFingerprintDotnetJs : fingerprintDotnetJs ) ;
3338 else
3439 BuildProject ( info , config , new BuildOptions ( ExtraMSBuildArgs : extraMSBuildArgs ) , wasmFingerprintDotnetJs : fingerprintDotnetJs ) ;
3540
36- string ? indexHtmlPath = null ;
41+ string ? indexHtmlPath ;
3742 if ( isPublish )
3843 {
3944 indexHtmlPath = Path . Combine (
@@ -51,16 +56,37 @@ public void PreloadAssets(bool isPublish, bool fingerprintDotnetJs)
5156 Assert . True ( File . Exists ( indexHtmlPath ) ) ;
5257 string indexHtmlContent = File . ReadAllText ( indexHtmlPath ) ;
5358
54- if ( fingerprintDotnetJs )
59+ Assert . Equal ( preloadAssets ? 1 : 0 , CountOccurrences ( indexHtmlContent , "rel=\" preload\" " ) ) ;
60+ if ( preloadAssets )
5561 {
56- // Expect to find fingerprinted preload
57- Assert . Contains ( "<link href=\" _framework/dotnet" , indexHtmlContent ) ;
58- Assert . DoesNotContain ( "<link href=\" _framework/dotnet.js\" " , indexHtmlContent ) ;
62+ if ( fingerprintDotnetJs )
63+ {
64+ // Expect to find fingerprinted preload
65+ Assert . Contains ( "<link href=\" _framework/dotnet" , indexHtmlContent ) ;
66+ Assert . DoesNotContain ( "<link href=\" _framework/dotnet.js\" " , indexHtmlContent ) ;
67+ }
68+ else
69+ {
70+ // Expect to find non-fingerprinted preload
71+ Assert . Contains ( "<link href=\" _framework/dotnet.js\" " , indexHtmlContent ) ;
72+ }
5973 }
60- else
74+ }
75+
76+ public static int CountOccurrences ( string source , string substring )
77+ {
78+ if ( string . IsNullOrEmpty ( source ) || string . IsNullOrEmpty ( substring ) )
79+ return 0 ;
80+
81+ int count = 0 ;
82+ int index = 0 ;
83+
84+ while ( ( index = source . IndexOf ( substring , index , StringComparison . Ordinal ) ) != - 1 )
6185 {
62- // Expect to find non-fingerprinted preload
63- Assert . Contains ( "<link href= \" _framework/dotnet.js \" " , indexHtmlContent ) ;
86+ count ++ ;
87+ index += substring . Length ;
6488 }
89+
90+ return count ;
6591 }
6692}
0 commit comments