/ src / modules / AdvancedPaste / AdvancedPaste.UnitTests / ConvertersTests / HexColorToColorConverterTests.cs
HexColorToColorConverterTests.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using AdvancedPaste.Converters; 6 using Microsoft.VisualStudio.TestTools.UnitTesting; 7 using Windows.UI; 8 9 namespace AdvancedPaste.UnitTests.ConvertersTests; 10 11 [TestClass] 12 public sealed class HexColorToColorConverterTests 13 { 14 [TestMethod] 15 public void TestConvert_ValidSixDigitHex_ReturnsColor() 16 { 17 Color? result = HexColorConverterHelper.ConvertHexColorToRgb("#FFBFAB"); 18 Assert.IsNotNull(result); 19 20 var color = (Windows.UI.Color)result; 21 Assert.AreEqual(255, color.R); 22 Assert.AreEqual(191, color.G); 23 Assert.AreEqual(171, color.B); 24 Assert.AreEqual(255, color.A); 25 } 26 27 [TestMethod] 28 public void TestConvert_ValidThreeDigitHex_ReturnsColor() 29 { 30 Color? result = HexColorConverterHelper.ConvertHexColorToRgb("#abc"); 31 Assert.IsNotNull(result); 32 33 var color = (Windows.UI.Color)result; 34 35 // #abc should expand to #aabbcc 36 Assert.AreEqual(170, color.R); // 0xaa 37 Assert.AreEqual(187, color.G); // 0xbb 38 Assert.AreEqual(204, color.B); // 0xcc 39 Assert.AreEqual(255, color.A); 40 } 41 42 [TestMethod] 43 public void TestConvert_NullOrEmpty_ReturnsNull() 44 { 45 Assert.IsNull(HexColorConverterHelper.ConvertHexColorToRgb(null)); 46 Assert.IsNull(HexColorConverterHelper.ConvertHexColorToRgb(string.Empty)); 47 Assert.IsNull(HexColorConverterHelper.ConvertHexColorToRgb(" ")); 48 } 49 50 [TestMethod] 51 public void TestConvert_InvalidHex_ReturnsNull() 52 { 53 Assert.IsNull(HexColorConverterHelper.ConvertHexColorToRgb("#GGGGGG")); 54 Assert.IsNull(HexColorConverterHelper.ConvertHexColorToRgb("#12345")); 55 } 56 }