|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
1 | 5 | using CommunityToolkit.Tests;
|
2 | 6 | using CommunityToolkit.Tooling.TestGen;
|
3 | 7 | using CommunityToolkit.WinUI.Controls;
|
| 8 | +using CommunityToolkit.WinUI.Converters; |
4 | 9 |
|
5 | 10 | namespace PrimitivesExperiment.Tests;
|
6 | 11 |
|
@@ -48,4 +53,90 @@ public async Task SwitchPresenterLayoutTest(SwitchPresenterLayoutSample page)
|
48 | 53 | Assert.IsNotNull(txtbox, "Couldn't find new textbox");
|
49 | 54 | Assert.AreEqual("Confirmation code", txtbox.Header, "Textbox header not expected value");
|
50 | 55 | }
|
| 56 | + |
| 57 | + [UIThreadTestMethod] |
| 58 | + public async Task SwitchConverterBrushTest(SwitchConverterBrushSample page) |
| 59 | + { |
| 60 | + var combobox = page.FindDescendant<ComboBox>(); |
| 61 | + var textblock = page.FindDescendant("ResultBlock") as TextBlock; |
| 62 | + |
| 63 | + Assert.IsNotNull(combobox, "Couldn't find ComboBox"); |
| 64 | + Assert.IsNotNull(textblock, "Couldn't find SwitchPresenter"); |
| 65 | + |
| 66 | + // Are we in our initial case? |
| 67 | + Assert.AreEqual(0, combobox.SelectedIndex, "ComboBox not initialized"); |
| 68 | + Assert.AreEqual("Success", combobox.SelectedItem, "Not expected starting value in ComboBox"); |
| 69 | + |
| 70 | + // Check the TextBlock's brush |
| 71 | + Assert.AreEqual(((SolidColorBrush)page.Resources["SystemFillColorSuccessBrush"]).Color, ((SolidColorBrush)textblock.Foreground).Color, "TextBlock not in initial success state"); |
| 72 | + |
| 73 | + // Update combobox |
| 74 | + combobox.SelectedIndex = 1; |
| 75 | + |
| 76 | + // Wait for update |
| 77 | + await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); |
| 78 | + |
| 79 | + // Are we in the new case? |
| 80 | + Assert.AreEqual("Warning", combobox.SelectedItem, "ComboBox didn't change"); |
| 81 | + |
| 82 | + // Check the TextBlock's brush |
| 83 | + Assert.AreEqual(((SolidColorBrush)page.Resources["SystemFillColorCautionBrush"]).Color, ((SolidColorBrush)textblock.Foreground).Color, "TextBlock not in new state"); |
| 84 | + |
| 85 | + // Update combobox |
| 86 | + combobox.SelectedIndex = 2; |
| 87 | + |
| 88 | + // Wait for update |
| 89 | + await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); |
| 90 | + |
| 91 | + // Are we in the new case? |
| 92 | + Assert.AreEqual("Error", combobox.SelectedItem, "ComboBox didn't change 2"); |
| 93 | + |
| 94 | + // Check the TextBlock's brush |
| 95 | + Assert.AreEqual(((SolidColorBrush)page.Resources["SystemFillColorCriticalBrush"]).Color, ((SolidColorBrush)textblock.Foreground).Color, "TextBlock not in final state"); |
| 96 | + } |
| 97 | + |
| 98 | + [UIThreadTestMethod] |
| 99 | + public void SwitchConverterDirectTest() |
| 100 | + { |
| 101 | + // Multiply by 10 |
| 102 | + SwitchConverter sconverter = new() |
| 103 | + { |
| 104 | + SwitchCases = new CaseCollection { |
| 105 | + new Case() |
| 106 | + { |
| 107 | + Content = 10, |
| 108 | + Value = 1, |
| 109 | + }, |
| 110 | + new Case() |
| 111 | + { |
| 112 | + Content = 50, |
| 113 | + Value = 5, |
| 114 | + IsDefault = true |
| 115 | + }, |
| 116 | + new Case() |
| 117 | + { |
| 118 | + Content = 30, |
| 119 | + Value = 3, |
| 120 | + }, |
| 121 | + new Case() |
| 122 | + { |
| 123 | + Content = 20, |
| 124 | + Value = 2, |
| 125 | + }, |
| 126 | + }, |
| 127 | + TargetType = typeof(int) |
| 128 | + }; |
| 129 | + |
| 130 | + Assert.IsNotNull(sconverter); |
| 131 | + Assert.AreEqual(4, sconverter.SwitchCases.Count, "Not 4 cases"); |
| 132 | + |
| 133 | + var @default = sconverter.Convert(100, typeof(int), string.Empty, string.Empty); |
| 134 | + |
| 135 | + Assert.AreEqual(50, @default, "Unexpected default return"); |
| 136 | + |
| 137 | + Assert.AreEqual(10, sconverter.Convert(1, typeof(int), string.Empty, string.Empty), "Unexpected result with 1"); |
| 138 | + Assert.AreEqual(20, sconverter.Convert(2, typeof(int), string.Empty, string.Empty), "Unexpected result with 2"); |
| 139 | + Assert.AreEqual(30, sconverter.Convert(3, typeof(int), string.Empty, string.Empty), "Unexpected result with 3"); |
| 140 | + Assert.AreEqual(50, sconverter.Convert(5, typeof(int), string.Empty, string.Empty), "Unexpected result with 5"); |
| 141 | + } |
51 | 142 | }
|
0 commit comments