Files
sbox-public/engine/Tests/Sandbox.Test.Engine/UI/Styles/StyleSheetImport.cs
Garry Newman d95169d8fa Unit Test Cleanup (#5064)
* Move all unit tests to Engine/Tests
* Fix Margin.EdgeSubtract
* Fix Capsule.Contains
* EnvironmentVariables.Remove if it's null
* Fixed ray trace never returning startedsolid
* Fix HistoryList.Navigate on empty list
* Fix GameObject.WorldPosition accepting NaNs
* Fix flex: initial expanding to the wrong grow/shrink
* Translation.TryConvert shouldn't throw on invalid enum strings
* Skip sound file tests on machines with no audio device
2026-06-12 13:23:50 +01:00

124 lines
3.5 KiB
C#

using Sandbox.Engine;
using Sandbox.UI;
using System;
namespace UITests;
[TestClass]
[DoNotParallelize]
public class StyleSheetImportTest
{
[TestMethod]
public void Parsing()
{
GlobalContext.Current.FileMount = new AggregateFileSystem();
FileSystem.Mounted.Mount( new LocalFileSystem( Environment.CurrentDirectory ) );
var sheet = StyleSheet.FromFile( "unittest/styles/import-test.scss" );
Assert.IsNotNull( sheet );
// Test variables
Assert.AreEqual( sheet.GetVariable( "$primary" ), "red" );
Assert.AreEqual( sheet.GetVariable( "$white" ), "black" );
// A dropped import would silently skip its switch case below - assert each selector is present.
Assert.IsTrue( sheet.Nodes.Any( n => n.SelectorStrings.Contains( ".one" ) ) );
Assert.IsTrue( sheet.Nodes.Any( n => n.SelectorStrings.Contains( ".include" ) ) );
// Test styles
foreach ( var node in sheet.Nodes )
{
switch ( node.SelectorStrings.First() )
{
case ".one":
{
Assert.AreEqual( node.Styles.MarginTop.ToString(), "50px" );
Assert.AreEqual( node.Styles.BackgroundColor.Value.Hex, "#FF0000" );
break;
}
case ".include":
{
Assert.AreEqual( node.Styles.BackgroundColor.Value.Hex, "#FF0000" );
break;
}
}
}
}
[TestMethod]
public void NestedImports()
{
GlobalContext.Current.FileMount = new AggregateFileSystem();
FileSystem.Mounted.Mount( new LocalFileSystem( Environment.CurrentDirectory ) );
var sheet = StyleSheet.FromFile( "unittest/styles/nested/nested-import-test.scss" );
Assert.IsNotNull( sheet );
// Test variables
Assert.AreEqual( sheet.GetVariable( "$var1" ), "red" );
Assert.AreEqual( sheet.GetVariable( "$var2" ), "blue" );
Assert.AreEqual( sheet.GetVariable( "$var3" ), "pink" ); // Was green but overridden by the nested import
// A dropped import would silently skip its switch case below - assert each selector is present.
Assert.IsTrue( sheet.Nodes.Any( n => n.SelectorStrings.Contains( ".base" ) ) );
Assert.IsTrue( sheet.Nodes.Any( n => n.SelectorStrings.Contains( ".nested" ) ) );
// Test styles
foreach ( var node in sheet.Nodes )
{
switch ( node.SelectorStrings.First() )
{
case ".base":
{
Assert.AreEqual( node.Styles.FontSize.ToString(), "32px" );
Assert.AreEqual( node.Styles.FontColor?.Hex, "#0000FF" );
break;
}
case ".nested":
{
Assert.AreEqual( node.Styles.FontSize.ToString(), "64px" );
Assert.AreEqual( node.Styles.FontColor?.Hex, "#FF0000" );
break;
}
}
}
}
[TestMethod]
public void ReloadingStyles()
{
GlobalContext.Current.FileMount = new AggregateFileSystem();
FileSystem.Mounted.Mount( new LocalFileSystem( Environment.CurrentDirectory ) );
var sheet = StyleSheet.FromString( ".box { background-color: red; }" );
Assert.IsNotNull( sheet );
Assert.AreEqual( sheet.Nodes.First().Styles.BackgroundColor, Color.Red );
sheet.UpdateFromString( ".box { width: 100%; }" );
Assert.AreEqual( sheet.Nodes.First().Styles.BackgroundColor, null );
}
/// <summary>
/// Reset should only clear the current context's sheets.
/// </summary>
[TestMethod]
public void ResetStyleSheetsIsContextScoped()
{
StyleSheet menuSheet;
using ( GlobalContext.MenuScope() )
{
menuSheet = StyleSheet.FromFile( "unittest/a10-menu-only.scss", null, true );
}
var gameSheet = StyleSheet.FromFile( "unittest/a10-game-only.scss", null, true );
StyleSheet.ResetStyleSheets();
Assert.IsTrue( StyleSheet.Loaded.Contains( menuSheet ) );
Assert.IsFalse( StyleSheet.Loaded.Contains( gameSheet ) );
}
}