|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Linq; |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 9 | +using Windows.UI.Xaml; |
| 10 | +using Windows.UI.Xaml.Controls; |
| 11 | +using Windows.UI.Xaml.Data; |
| 12 | + |
| 13 | +namespace Uno.UI.Tests.PasswordBoxTests |
| 14 | +{ |
| 15 | + [TestClass] |
| 16 | + public class Given_PasswordBox |
| 17 | + { |
| 18 | + [TestMethod] |
| 19 | + public void When_Binding_Set_Null() |
| 20 | + { |
| 21 | + var passwordBox = new PasswordBox(); |
| 22 | + var source = new MySource() { SourceText = "Spinach" }; |
| 23 | + |
| 24 | + passwordBox.DataContext = source; |
| 25 | + passwordBox.SetBinding(PasswordBox.PasswordProperty, new Binding() { Path = new PropertyPath("SourceText") }); |
| 26 | + |
| 27 | + Assert.AreEqual("Spinach", passwordBox.Password); |
| 28 | + |
| 29 | + source.SourceText = null; |
| 30 | + |
| 31 | + Assert.AreEqual("", passwordBox.Password); |
| 32 | + } |
| 33 | + |
| 34 | + [TestMethod] |
| 35 | + public void When_Set_DP_Null() |
| 36 | + { |
| 37 | + var passwordBox = new PasswordBox(); |
| 38 | + |
| 39 | + passwordBox.SetValue(PasswordBox.PasswordProperty, "Spinach"); |
| 40 | + Assert.AreEqual("Spinach", passwordBox.Password); |
| 41 | + passwordBox.SetValue(PasswordBox.PasswordProperty, null); |
| 42 | + Assert.AreEqual("", passwordBox.Password); |
| 43 | + } |
| 44 | + |
| 45 | + [TestMethod] |
| 46 | + public void When_Binding_Set_Non_String() |
| 47 | + { |
| 48 | + var passwordBox = new PasswordBox(); |
| 49 | + var source = new MySource() { SourceInt = 12 }; |
| 50 | + |
| 51 | + passwordBox.DataContext = source; |
| 52 | + passwordBox.SetBinding(PasswordBox.PasswordProperty, new Binding() { Path = new PropertyPath("SourceInt") }); |
| 53 | + |
| 54 | + Assert.AreEqual("12", passwordBox.Password); |
| 55 | + |
| 56 | + source.SourceInt = 19; |
| 57 | + |
| 58 | + Assert.AreEqual("19", passwordBox.Password); |
| 59 | + } |
| 60 | + |
| 61 | + public class MySource : System.ComponentModel.INotifyPropertyChanged |
| 62 | + { |
| 63 | + private string _sourceText; |
| 64 | + |
| 65 | + public string SourceText |
| 66 | + { |
| 67 | + get => _sourceText; |
| 68 | + set |
| 69 | + { |
| 70 | + if (_sourceText != value) |
| 71 | + { |
| 72 | + _sourceText = value; |
| 73 | + OnPropertyChanged(); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private int _sourceInt; |
| 79 | + |
| 80 | + public int SourceInt |
| 81 | + { |
| 82 | + get => _sourceInt; |
| 83 | + set |
| 84 | + { |
| 85 | + if (_sourceInt != value) |
| 86 | + { |
| 87 | + _sourceInt = value; |
| 88 | + OnPropertyChanged(); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; |
| 94 | + |
| 95 | + protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) |
| 96 | + => PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName)); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments