PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/ReactiveUI.Tests/PropertyBindingTest.cs

https://github.com/bsiegel/ReactiveUI
C# | 260 lines | 206 code | 54 blank | 0 comment | 0 complexity | 932a6a9610604c89f828e62d01ec4301 MD5 | raw file
Possible License(s): Apache-2.0, CC-BY-SA-3.0, LGPL-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Reactive;
  6. using System.Text;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using ReactiveUI.Xaml;
  10. using Xunit;
  11. namespace ReactiveUI.Tests
  12. {
  13. public class PropertyBindModel
  14. {
  15. public int AThing { get; set; }
  16. public string AnotherThing { get; set; }
  17. }
  18. public class PropertyBindViewModel : ReactiveObject
  19. {
  20. public string _Property1;
  21. public string Property1 {
  22. get { return _Property1; }
  23. set { this.RaiseAndSetIfChanged(x => x.Property1, value); }
  24. }
  25. public int _Property2;
  26. public int Property2 {
  27. get { return _Property2; }
  28. set { this.RaiseAndSetIfChanged(x => x.Property2, value); }
  29. }
  30. public double _JustADouble;
  31. public double JustADouble {
  32. get { return _JustADouble; }
  33. set { this.RaiseAndSetIfChanged(x => x.JustADouble, value); }
  34. }
  35. public double? _NullableDouble;
  36. public double? NullableDouble {
  37. get { return _NullableDouble; }
  38. set { this.RaiseAndSetIfChanged(x => x.NullableDouble, value); }
  39. }
  40. public ReactiveCollection<string> SomeCollectionOfStrings { get; protected set; }
  41. public PropertyBindModel Model { get; protected set; }
  42. public PropertyBindViewModel()
  43. {
  44. Model = new PropertyBindModel() {AThing = 42, AnotherThing = "Baz"};
  45. SomeCollectionOfStrings = new ReactiveCollection<string>(new[] { "Foo", "Bar" });
  46. }
  47. }
  48. public class PropertyBindView : Control, IViewFor<PropertyBindViewModel>
  49. {
  50. public PropertyBindViewModel ViewModel {
  51. get { return (PropertyBindViewModel)GetValue(ViewModelProperty); }
  52. set { SetValue(ViewModelProperty, value); }
  53. }
  54. public static readonly DependencyProperty ViewModelProperty =
  55. DependencyProperty.Register("ViewModel", typeof(PropertyBindViewModel), typeof(PropertyBindView), new PropertyMetadata(null));
  56. object IViewFor.ViewModel {
  57. get { return ViewModel; }
  58. set { ViewModel = (PropertyBindViewModel)value; }
  59. }
  60. public TextBox SomeTextBox;
  61. public ListBox SomeListBox;
  62. public TextBox Property2;
  63. public PropertyBindFakeControl FakeControl;
  64. public ItemsControl FakeItemsControl;
  65. public PropertyBindView()
  66. {
  67. SomeTextBox = new TextBox();
  68. SomeListBox = new ListBox();
  69. Property2 = new TextBox();
  70. FakeControl = new PropertyBindFakeControl();
  71. FakeItemsControl = new ItemsControl();
  72. }
  73. }
  74. public class PropertyBindFakeControl : Control
  75. {
  76. public double? NullableDouble {
  77. get { return (double?)GetValue(NullableDoubleProperty); }
  78. set { SetValue(NullableDoubleProperty, value); }
  79. }
  80. public static readonly DependencyProperty NullableDoubleProperty =
  81. DependencyProperty.Register("NullableDouble", typeof(double?), typeof(PropertyBindFakeControl), new PropertyMetadata(null));
  82. public double JustADouble {
  83. get { return (double)GetValue(JustADoubleProperty); }
  84. set { SetValue(JustADoubleProperty, value); }
  85. }
  86. public static readonly DependencyProperty JustADoubleProperty =
  87. DependencyProperty.Register("JustADouble", typeof(double), typeof(PropertyBindFakeControl), new PropertyMetadata(0.0));
  88. }
  89. public class PropertyBindingTest
  90. {
  91. [Fact]
  92. public void TwoWayBindSmokeTest()
  93. {
  94. var vm = new PropertyBindViewModel();
  95. var view = new PropertyBindView() {ViewModel = vm};
  96. var fixture = new PropertyBinderImplementation();
  97. vm.Property1 = "Foo";
  98. Assert.NotEqual(vm.Property1, view.SomeTextBox.Text);
  99. var disp = fixture.Bind(vm, view, x => x.Property1, x => x.SomeTextBox.Text, (IObservable<Unit>)null, null);
  100. Assert.Equal(vm.Property1, view.SomeTextBox.Text);
  101. Assert.Equal("Foo", vm.Property1);
  102. view.SomeTextBox.Text = "Bar";
  103. Assert.Equal(vm.Property1, "Bar");
  104. disp.Dispose();
  105. vm.Property1 = "Baz";
  106. Assert.Equal("Baz", vm.Property1);
  107. Assert.NotEqual(vm.Property1, view.SomeTextBox.Text);
  108. }
  109. [Fact]
  110. public void TypeConvertedTwoWayBindSmokeTest()
  111. {
  112. var vm = new PropertyBindViewModel();
  113. var view = new PropertyBindView() { ViewModel = vm };
  114. var fixture = new PropertyBinderImplementation();
  115. vm.Property2 = 17;
  116. Assert.NotEqual(vm.Property2.ToString(), view.SomeTextBox.Text);
  117. var disp = fixture.Bind(vm, view, x => x.Property2, x => x.SomeTextBox.Text, (IObservable<Unit>)null, null);
  118. Assert.Equal(vm.Property2.ToString(), view.SomeTextBox.Text);
  119. Assert.Equal(17, vm.Property2);
  120. view.SomeTextBox.Text = "42";
  121. Assert.Equal(42, vm.Property2);
  122. disp.Dispose();
  123. vm.Property2 = 0;
  124. Assert.Equal(0, vm.Property2);
  125. Assert.NotEqual("0", view.SomeTextBox.Text);
  126. }
  127. [Fact]
  128. public void BindingToItemsControl()
  129. {
  130. var vm = new PropertyBindViewModel();
  131. var view = new PropertyBindView() {ViewModel = vm};
  132. view.OneWayBind(view.ViewModel, x => x.SomeCollectionOfStrings, x => x.SomeListBox.ItemsSource);
  133. Assert.True(view.SomeListBox.ItemsSource.OfType<string>().Count() > 1);
  134. }
  135. [Fact]
  136. public void BindingIntoModelObjects()
  137. {
  138. var vm = new PropertyBindViewModel();
  139. var view = new PropertyBindView() {ViewModel = vm};
  140. view.OneWayBind(view.ViewModel, x => x.Model.AnotherThing, x => x.SomeTextBox.Text);
  141. Assert.Equal("Baz", view.SomeTextBox.Text);
  142. }
  143. [Fact]
  144. public void ImplicitBindPlusTypeConversion()
  145. {
  146. var vm = new PropertyBindViewModel();
  147. var view = new PropertyBindView() {ViewModel = vm};
  148. view.Bind(view.ViewModel, x => x.Property2);
  149. vm.Property2 = 42;
  150. Assert.Equal("42", view.Property2.Text);
  151. view.Property2.Text = "7";
  152. Assert.Equal(7, vm.Property2);
  153. }
  154. [Fact]
  155. public void ViewModelNullableToViewNonNullable()
  156. {
  157. var vm = new PropertyBindViewModel();
  158. var view = new PropertyBindView() {ViewModel = vm};
  159. view.Bind(view.ViewModel, x => x.NullableDouble, x => x.FakeControl.JustADouble);
  160. Assert.Equal(0.0, view.FakeControl.JustADouble);
  161. vm.NullableDouble = 4.0;
  162. Assert.Equal(4.0, view.FakeControl.JustADouble);
  163. vm.NullableDouble = null;
  164. Assert.Equal(4.0, view.FakeControl.JustADouble);
  165. vm.NullableDouble = 0.0;
  166. Assert.Equal(0.0, view.FakeControl.JustADouble);
  167. }
  168. [Fact]
  169. public void ViewModelNonNullableToViewNullable()
  170. {
  171. var vm = new PropertyBindViewModel();
  172. var view = new PropertyBindView() {ViewModel = vm};
  173. view.Bind(view.ViewModel, x => x.JustADouble, x => x.FakeControl.NullableDouble);
  174. Assert.Equal(0.0, vm.JustADouble);
  175. view.FakeControl.NullableDouble = 4.0;
  176. Assert.Equal(4.0, vm.JustADouble);
  177. view.FakeControl.NullableDouble = null;
  178. Assert.Equal(4.0, vm.JustADouble);
  179. view.FakeControl.NullableDouble = 0.0;
  180. Assert.Equal(0.0, vm.JustADouble);
  181. }
  182. [Fact]
  183. public void ViewModelNullableToViewNullable()
  184. {
  185. var vm = new PropertyBindViewModel();
  186. var view = new PropertyBindView() {ViewModel = vm};
  187. view.Bind(view.ViewModel, x => x.NullableDouble, x => x.FakeControl.NullableDouble);
  188. Assert.Equal(null, vm.NullableDouble);
  189. view.FakeControl.NullableDouble = 4.0;
  190. Assert.Equal(4.0, vm.NullableDouble);
  191. view.FakeControl.NullableDouble = null;
  192. Assert.Equal(null, vm.NullableDouble);
  193. view.FakeControl.NullableDouble = 0.0;
  194. Assert.Equal(0.0, vm.NullableDouble);
  195. }
  196. [Fact]
  197. public void ItemsControlShouldGetADataTemplate()
  198. {
  199. var vm = new PropertyBindViewModel();
  200. var view = new PropertyBindView() {ViewModel = vm};
  201. Assert.Null(view.FakeItemsControl.ItemTemplate);
  202. view.OneWayBind(vm, x => x.SomeCollectionOfStrings, x => x.FakeItemsControl.ItemsSource);
  203. Assert.NotNull(view.FakeItemsControl.ItemTemplate);
  204. }
  205. }
  206. }