CompiledBindingExtensionTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Reactive.Subjects;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Avalonia.Controls;
  8. using Avalonia.Controls.Presenters;
  9. using Avalonia.Data.Core;
  10. using Avalonia.Markup.Data;
  11. using Avalonia.UnitTests;
  12. using XamlIl;
  13. using Xunit;
  14. namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions
  15. {
  16. public class CompiledBindingExtensionTests
  17. {
  18. [Fact]
  19. public void ResolvesClrPropertyBasedOnDataContextType()
  20. {
  21. using (UnitTestApplication.Start(TestServices.StyledWindow))
  22. {
  23. var xaml = @"
  24. <Window xmlns='https://github.com/avaloniaui'
  25. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  26. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests'
  27. x:DataContextType='local:TestDataContext'>
  28. <TextBlock Text='{CompiledBinding StringProperty}' Name='textBlock' />
  29. </Window>";
  30. var loader = new AvaloniaXamlLoader();
  31. var window = (Window)loader.Load(xaml);
  32. var textBlock = window.FindControl<TextBlock>("textBlock");
  33. DelayedBinding.ApplyBindings(textBlock);
  34. var dataContext = new TestDataContext
  35. {
  36. StringProperty = "foobar"
  37. };
  38. window.DataContext = dataContext;
  39. Assert.Equal(dataContext.StringProperty, textBlock.Text);
  40. }
  41. }
  42. [Fact]
  43. public void ResolvesStreamTaskBindingCorrectly()
  44. {
  45. using (UnitTestApplication.Start(TestServices.StyledWindow))
  46. {
  47. var xaml = @"
  48. <Window xmlns='https://github.com/avaloniaui'
  49. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  50. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests'
  51. x:DataContextType='local:TestDataContext'>
  52. <TextBlock Text='{CompiledBinding TaskProperty^}' Name='textBlock' />
  53. </Window>";
  54. var loader = new AvaloniaXamlLoader();
  55. var window = (Window)loader.Load(xaml);
  56. var textBlock = window.FindControl<TextBlock>("textBlock");
  57. DelayedBinding.ApplyBindings(textBlock);
  58. var dataContext = new TestDataContext
  59. {
  60. TaskProperty = Task.FromResult("foobar")
  61. };
  62. window.DataContext = dataContext;
  63. Assert.Equal(dataContext.TaskProperty.Result, textBlock.Text);
  64. }
  65. }
  66. [Fact]
  67. public void ResolvesStreamObservableBindingCorrectly()
  68. {
  69. using (UnitTestApplication.Start(TestServices.StyledWindow))
  70. {
  71. var xaml = @"
  72. <Window xmlns='https://github.com/avaloniaui'
  73. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  74. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests'
  75. x:DataContextType='local:TestDataContext'>
  76. <TextBlock Text='{CompiledBinding ObservableProperty^}' Name='textBlock' />
  77. </Window>";
  78. var loader = new AvaloniaXamlLoader();
  79. var window = (Window)loader.Load(xaml);
  80. var textBlock = window.FindControl<TextBlock>("textBlock");
  81. DelayedBinding.ApplyBindings(textBlock);
  82. var subject = new Subject<string>();
  83. var dataContext = new TestDataContext
  84. {
  85. ObservableProperty = subject
  86. };
  87. window.DataContext = dataContext;
  88. subject.OnNext("foobar");
  89. Assert.Equal("foobar", textBlock.Text);
  90. }
  91. }
  92. [Fact]
  93. public void ResolvesIndexerBindingCorrectly()
  94. {
  95. using (UnitTestApplication.Start(TestServices.StyledWindow))
  96. {
  97. var xaml = @"
  98. <Window xmlns='https://github.com/avaloniaui'
  99. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  100. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests'
  101. x:DataContextType='local:TestDataContext'>
  102. <TextBlock Text='{CompiledBinding ListProperty[3]}' Name='textBlock' />
  103. </Window>";
  104. var loader = new AvaloniaXamlLoader();
  105. var window = (Window)loader.Load(xaml);
  106. var textBlock = window.FindControl<TextBlock>("textBlock");
  107. var dataContext = new TestDataContext
  108. {
  109. ListProperty = { "A", "B", "C", "D", "E" }
  110. };
  111. window.DataContext = dataContext;
  112. Assert.Equal(dataContext.ListProperty[3], textBlock.Text);
  113. }
  114. }
  115. [Fact]
  116. public void ResolvesArrayIndexerBindingCorrectly()
  117. {
  118. using (UnitTestApplication.Start(TestServices.StyledWindow))
  119. {
  120. var xaml = @"
  121. <Window xmlns='https://github.com/avaloniaui'
  122. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  123. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests'
  124. x:DataContextType='local:TestDataContext'>
  125. <TextBlock Text='{CompiledBinding ArrayProperty[3]}' Name='textBlock' />
  126. </Window>";
  127. var loader = new AvaloniaXamlLoader();
  128. var window = (Window)loader.Load(xaml);
  129. var textBlock = window.FindControl<TextBlock>("textBlock");
  130. var dataContext = new TestDataContext
  131. {
  132. ArrayProperty = new[] { "A", "B", "C", "D", "E" }
  133. };
  134. window.DataContext = dataContext;
  135. Assert.Equal(dataContext.ArrayProperty[3], textBlock.Text);
  136. }
  137. }
  138. [Fact]
  139. public void ResolvesObservableIndexerBindingCorrectly()
  140. {
  141. using (UnitTestApplication.Start(TestServices.StyledWindow))
  142. {
  143. var xaml = @"
  144. <Window xmlns='https://github.com/avaloniaui'
  145. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  146. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests'
  147. x:DataContextType='local:TestDataContext'>
  148. <TextBlock Text='{CompiledBinding ObservableCollectionProperty[3]}' Name='textBlock' />
  149. </Window>";
  150. var loader = new AvaloniaXamlLoader();
  151. var window = (Window)loader.Load(xaml);
  152. var textBlock = window.FindControl<TextBlock>("textBlock");
  153. var dataContext = new TestDataContext
  154. {
  155. ObservableCollectionProperty = { "A", "B", "C", "D", "E" }
  156. };
  157. window.DataContext = dataContext;
  158. Assert.Equal(dataContext.ObservableCollectionProperty[3], textBlock.Text);
  159. dataContext.ObservableCollectionProperty[3] = "New Value";
  160. Assert.Equal(dataContext.ObservableCollectionProperty[3], textBlock.Text);
  161. }
  162. }
  163. [Fact]
  164. public void InfersCompiledBindingDataContextFromDataContextBinding()
  165. {
  166. using (UnitTestApplication.Start(TestServices.StyledWindow))
  167. {
  168. var xaml = @"
  169. <Window xmlns='https://github.com/avaloniaui'
  170. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  171. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests'
  172. x:DataContextType='local:TestDataContext'>
  173. <TextBlock DataContext='{CompiledBinding StringProperty}' Text='{CompiledBinding}' Name='textBlock' />
  174. </Window>";
  175. var loader = new AvaloniaXamlLoader();
  176. var window = (Window)loader.Load(xaml);
  177. var textBlock = window.FindControl<TextBlock>("textBlock");
  178. window.ApplyTemplate();
  179. var dataContext = new TestDataContext
  180. {
  181. StringProperty = "A"
  182. };
  183. window.DataContext = dataContext;
  184. Assert.Equal(dataContext.StringProperty, textBlock.Text);
  185. }
  186. }
  187. [Fact]
  188. public void ResolvesNonIntegerIndexerBindingCorrectly()
  189. {
  190. using (UnitTestApplication.Start(TestServices.StyledWindow))
  191. {
  192. var xaml = @"
  193. <Window xmlns='https://github.com/avaloniaui'
  194. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  195. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests'
  196. x:DataContextType='local:TestDataContext'>
  197. <TextBlock Text='{CompiledBinding NonIntegerIndexerProperty[Test]}' Name='textBlock' />
  198. </Window>";
  199. var loader = new AvaloniaXamlLoader();
  200. var window = (Window)loader.Load(xaml);
  201. var textBlock = window.FindControl<TextBlock>("textBlock");
  202. var dataContext = new TestDataContext();
  203. dataContext.NonIntegerIndexerProperty["Test"] = "Initial Value";
  204. window.DataContext = dataContext;
  205. Assert.Equal(dataContext.NonIntegerIndexerProperty["Test"], textBlock.Text);
  206. dataContext.NonIntegerIndexerProperty["Test"] = "New Value";
  207. Assert.Equal(dataContext.NonIntegerIndexerProperty["Test"], textBlock.Text);
  208. }
  209. }
  210. [Fact]
  211. public void InfersDataTemplateTypeFromDataTypeProperty()
  212. {
  213. using (UnitTestApplication.Start(TestServices.StyledWindow))
  214. {
  215. var xaml = @"
  216. <Window xmlns='https://github.com/avaloniaui'
  217. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  218. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests'
  219. x:DataContextType='local:TestDataContext'>
  220. <Window.DataTemplates>
  221. <DataTemplate DataType='{x:Type x:String}'>
  222. <TextBlock Text='{CompiledBinding}' Name='textBlock' />
  223. </DataTemplate>
  224. </Window.DataTemplates>
  225. <ContentControl Name='target' Content='{CompiledBinding StringProperty}' />
  226. </Window>";
  227. var loader = new AvaloniaXamlLoader();
  228. var window = (Window)loader.Load(xaml);
  229. var target = window.FindControl<ContentControl>("target");
  230. var dataContext = new TestDataContext();
  231. dataContext.StringProperty = "Initial Value";
  232. window.DataContext = dataContext;
  233. window.ApplyTemplate();
  234. target.ApplyTemplate();
  235. ((ContentPresenter)target.Presenter).UpdateChild();
  236. Assert.Equal(dataContext.StringProperty, ((TextBlock)target.Presenter.Child).Text);
  237. }
  238. }
  239. [Fact]
  240. public void ThrowsOnUninferrableLooseDataTemplateNoDataTypeWithCompiledBindingPath()
  241. {
  242. using (UnitTestApplication.Start(TestServices.StyledWindow))
  243. {
  244. var xaml = @"
  245. <Window xmlns='https://github.com/avaloniaui'
  246. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  247. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests'
  248. x:DataContextType='local:TestDataContext'>
  249. <Window.DataTemplates>
  250. <DataTemplate>
  251. <TextBlock Text='{CompiledBinding StringProperty}' Name='textBlock' />
  252. </DataTemplate>
  253. </Window.DataTemplates>
  254. <ContentControl Name='target' Content='{CompiledBinding}' />
  255. </Window>";
  256. var loader = new AvaloniaXamlLoader();
  257. Assert.Throws<XamlIlTransformException>(() => loader.Load(xaml));
  258. }
  259. }
  260. [Fact]
  261. public void InfersDataTemplateTypeFromParentCollectionItemsType()
  262. {
  263. using (UnitTestApplication.Start(TestServices.StyledWindow))
  264. {
  265. var xaml = @"
  266. <Window xmlns='https://github.com/avaloniaui'
  267. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  268. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests'
  269. x:DataContextType='local:TestDataContext'>
  270. <ItemsControl Items='{CompiledBinding ListProperty}' Name='target'>
  271. <ItemsControl.DataTemplates>
  272. <DataTemplate>
  273. <TextBlock Text='{CompiledBinding}' Name='textBlock' />
  274. </DataTemplate>
  275. </ItemsControl.DataTemplates>
  276. </ItemsControl>
  277. </Window>";
  278. var loader = new AvaloniaXamlLoader();
  279. var window = (Window)loader.Load(xaml);
  280. var target = window.FindControl<ItemsControl>("target");
  281. var dataContext = new TestDataContext();
  282. dataContext.ListProperty.Add("Test");
  283. window.DataContext = dataContext;
  284. window.ApplyTemplate();
  285. target.ApplyTemplate();
  286. target.Presenter.ApplyTemplate();
  287. Assert.Equal(dataContext.ListProperty[0], (string)((ContentPresenter)target.Presenter.Panel.Children[0]).Content);
  288. }
  289. }
  290. [Fact]
  291. public void ThrowsOnUninferrableDataTemplateInItemsControlWithoutItemsBinding()
  292. {
  293. using (UnitTestApplication.Start(TestServices.StyledWindow))
  294. {
  295. var xaml = @"
  296. <Window xmlns='https://github.com/avaloniaui'
  297. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  298. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests'
  299. x:DataContextType='local:TestDataContext'>
  300. <ItemsControl Name='target'>
  301. <ItemsControl.DataTemplates>
  302. <DataTemplate>
  303. <TextBlock Text='{CompiledBinding Property}' Name='textBlock' />
  304. </DataTemplate>
  305. </ItemsControl.DataTemplates>
  306. </ItemsControl>
  307. </Window>";
  308. var loader = new AvaloniaXamlLoader();
  309. Assert.Throws<XamlIlTransformException>(() => loader.Load(xaml));
  310. }
  311. }
  312. }
  313. public class TestDataContext
  314. {
  315. public string StringProperty { get; set; }
  316. public Task<string> TaskProperty { get; set; }
  317. public IObservable<string> ObservableProperty { get; set; }
  318. public ObservableCollection<string> ObservableCollectionProperty { get; set; } = new ObservableCollection<string>();
  319. public string[] ArrayProperty { get; set; }
  320. public List<string> ListProperty { get; set; } = new List<string>();
  321. public NonIntegerIndexer NonIntegerIndexerProperty { get; set; } = new NonIntegerIndexer();
  322. public class NonIntegerIndexer : NotifyingBase
  323. {
  324. private readonly Dictionary<string, string> _storage = new Dictionary<string, string>();
  325. public string this[string key]
  326. {
  327. get
  328. {
  329. return _storage[key];
  330. }
  331. set
  332. {
  333. _storage[key] = value;
  334. RaisePropertyChanged(CommonPropertyNames.IndexerName);
  335. }
  336. }
  337. }
  338. }
  339. }