BasicTests.cs 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using Avalonia.Collections;
  5. using Avalonia.Controls;
  6. using Avalonia.Controls.Presenters;
  7. using Avalonia.Data;
  8. using Avalonia.Data.Converters;
  9. using Avalonia.Markup.Data;
  10. using Avalonia.Markup.Xaml.Styling;
  11. using Avalonia.Markup.Xaml.Templates;
  12. using Avalonia.Media;
  13. using Avalonia.Media.Immutable;
  14. using Avalonia.Styling;
  15. using Avalonia.UnitTests;
  16. using Portable.Xaml;
  17. using System.Collections;
  18. using System.ComponentModel;
  19. using System.Linq;
  20. using System.Xml;
  21. using Xunit;
  22. namespace Avalonia.Markup.Xaml.UnitTests.Xaml
  23. {
  24. public class BasicTests
  25. {
  26. [Fact]
  27. public void Simple_Property_Is_Set()
  28. {
  29. var xaml = @"<ContentControl xmlns='https://github.com/avaloniaui' Content='Foo'/>";
  30. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  31. Assert.NotNull(target);
  32. Assert.Equal("Foo", target.Content);
  33. }
  34. [Fact]
  35. public void Default_Content_Property_Is_Set()
  36. {
  37. var xaml = @"<ContentControl xmlns='https://github.com/avaloniaui'>Foo</ContentControl>";
  38. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  39. Assert.NotNull(target);
  40. Assert.Equal("Foo", target.Content);
  41. }
  42. [Fact]
  43. public void AvaloniaProperty_Without_Getter_And_Setter_Is_Set()
  44. {
  45. var xaml =
  46. @"<local:NonControl xmlns='https://github.com/avaloniaui'
  47. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'
  48. Foo='55' />";
  49. var target = AvaloniaXamlLoader.Parse<NonControl>(xaml);
  50. Assert.Equal(55, target.GetValue(NonControl.FooProperty));
  51. }
  52. [Fact]
  53. public void AvaloniaProperty_With_Getter_And_No_Setter_Is_Set()
  54. {
  55. if(!AvaloniaXamlLoader.UseLegacyXamlLoader)
  56. return;
  57. var xaml =
  58. @"<local:NonControl xmlns='https://github.com/avaloniaui'
  59. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'
  60. Bar='bar' />";
  61. var target = AvaloniaXamlLoader.Parse<NonControl>(xaml);
  62. Assert.Equal("bar", target.Bar);
  63. }
  64. [Fact]
  65. public void Attached_Property_Is_Set()
  66. {
  67. var xaml =
  68. @"<ContentControl xmlns='https://github.com/avaloniaui' TextBlock.FontSize='21'/>";
  69. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  70. Assert.NotNull(target);
  71. Assert.Equal(21.0, TextBlock.GetFontSize(target));
  72. }
  73. [Fact]
  74. public void Attached_Property_Is_Set_On_Control_Outside_Avalonia_Namspace()
  75. {
  76. // Test for issue #1548
  77. var xaml =
  78. @"<UserControl xmlns='https://github.com/avaloniaui'
  79. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
  80. <local:TestControl Grid.Column='2' />
  81. </UserControl>";
  82. var target = AvaloniaXamlLoader.Parse<UserControl>(xaml);
  83. Assert.Equal(2, Grid.GetColumn((TestControl)target.Content));
  84. }
  85. [Fact]
  86. public void Attached_Property_With_Namespace_Is_Set()
  87. {
  88. var xaml =
  89. @"<ContentControl xmlns='https://github.com/avaloniaui'
  90. xmlns:test='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'
  91. test:BasicTestsAttachedPropertyHolder.Foo='Bar'/>";
  92. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  93. Assert.NotNull(target);
  94. Assert.Equal("Bar", BasicTestsAttachedPropertyHolder.GetFoo(target));
  95. }
  96. [Fact]
  97. public void Attached_Property_Supports_Binding()
  98. {
  99. using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
  100. {
  101. var xaml =
  102. @"<Window xmlns='https://github.com/avaloniaui' TextBlock.FontSize='{Binding}'/>";
  103. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  104. target.DataContext = 21.0;
  105. Assert.Equal(21.0, TextBlock.GetFontSize(target));
  106. }
  107. }
  108. [Fact]
  109. public void Attached_Property_In_Panel_Is_Set()
  110. {
  111. var xaml = @"
  112. <Panel xmlns='https://github.com/avaloniaui'>
  113. <ToolTip.Tip>Foo</ToolTip.Tip>
  114. </Panel>";
  115. var target = AvaloniaXamlLoader.Parse<Panel>(xaml);
  116. Assert.Empty(target.Children);
  117. Assert.Equal("Foo", ToolTip.GetTip(target));
  118. }
  119. [Fact]
  120. public void NonExistent_Property_Throws()
  121. {
  122. var xaml =
  123. @"<ContentControl xmlns='https://github.com/avaloniaui' DoesntExist='foo'/>";
  124. XamlTestHelpers.AssertThrowsXamlException(() => AvaloniaXamlLoader.Parse<ContentControl>(xaml));
  125. }
  126. [Fact]
  127. public void Non_Attached_Property_With_Attached_Property_Syntax_Throws()
  128. {
  129. var xaml =
  130. @"<ContentControl xmlns='https://github.com/avaloniaui' TextBlock.Text='foo'/>";
  131. XamlTestHelpers.AssertThrowsXamlException(() => AvaloniaXamlLoader.Parse<ContentControl>(xaml));
  132. }
  133. [Fact]
  134. public void ContentControl_ContentTemplate_Is_Functional()
  135. {
  136. var xaml =
  137. @"<ContentControl xmlns='https://github.com/avaloniaui'>
  138. <ContentControl.ContentTemplate>
  139. <DataTemplate>
  140. <TextBlock Text='Foo' />
  141. </DataTemplate>
  142. </ContentControl.ContentTemplate>
  143. </ContentControl>";
  144. var contentControl = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  145. var target = contentControl.ContentTemplate;
  146. Assert.NotNull(target);
  147. var txt = (TextBlock)target.Build(null);
  148. Assert.Equal("Foo", txt.Text);
  149. }
  150. [Fact]
  151. public void Named_Control_Is_Added_To_NameScope_Simple()
  152. {
  153. var xaml = @"
  154. <UserControl xmlns='https://github.com/avaloniaui'>
  155. <Button Name='button'>Foo</Button>
  156. </UserControl>";
  157. var control = AvaloniaXamlLoader.Parse<UserControl>(xaml);
  158. var button = control.FindControl<Button>("button");
  159. Assert.Equal("Foo", button.Content);
  160. }
  161. [Fact]
  162. public void Direct_Content_In_ItemsControl_Is_Operational()
  163. {
  164. using (UnitTestApplication.Start(TestServices.StyledWindow))
  165. {
  166. var xaml = @"
  167. <Window xmlns='https://github.com/avaloniaui'>
  168. <ItemsControl Name='items'>
  169. <ContentControl>Foo</ContentControl>
  170. <ContentControl>Bar</ContentControl>
  171. </ItemsControl>
  172. </Window>";
  173. var control = AvaloniaXamlLoader.Parse<Window>(xaml);
  174. var itemsControl = control.FindControl<ItemsControl>("items");
  175. Assert.NotNull(itemsControl);
  176. var items = itemsControl.Items.Cast<ContentControl>().ToArray();
  177. Assert.Equal("Foo", items[0].Content);
  178. Assert.Equal("Bar", items[1].Content);
  179. }
  180. }
  181. [Fact]
  182. public void Panel_Children_Are_Added()
  183. {
  184. var xaml = @"
  185. <UserControl xmlns='https://github.com/avaloniaui'>
  186. <Panel Name='panel'>
  187. <ContentControl Name='Foo' />
  188. <ContentControl Name='Bar' />
  189. </Panel>
  190. </UserControl>";
  191. var control = AvaloniaXamlLoader.Parse<UserControl>(xaml);
  192. var panel = control.FindControl<Panel>("panel");
  193. Assert.Equal(2, panel.Children.Count);
  194. var foo = control.FindControl<ContentControl>("Foo");
  195. var bar = control.FindControl<ContentControl>("Bar");
  196. Assert.Contains(foo, panel.Children);
  197. Assert.Contains(bar, panel.Children);
  198. }
  199. [Fact]
  200. public void Grid_Row_Col_Definitions_Are_Built()
  201. {
  202. var xaml = @"
  203. <Grid xmlns='https://github.com/avaloniaui'>
  204. <Grid.ColumnDefinitions>
  205. <ColumnDefinition Width='100' />
  206. <ColumnDefinition Width='Auto' />
  207. <ColumnDefinition Width='*' />
  208. <ColumnDefinition Width='100*' />
  209. </Grid.ColumnDefinitions>
  210. <Grid.RowDefinitions>
  211. <RowDefinition Height='100' />
  212. <RowDefinition Height='Auto' />
  213. <RowDefinition Height='*' />
  214. <RowDefinition Height='100*' />
  215. </Grid.RowDefinitions>
  216. </Grid>";
  217. var grid = AvaloniaXamlLoader.Parse<Grid>(xaml);
  218. Assert.Equal(4, grid.ColumnDefinitions.Count);
  219. Assert.Equal(4, grid.RowDefinitions.Count);
  220. var expected1 = new GridLength(100);
  221. var expected2 = GridLength.Auto;
  222. var expected3 = new GridLength(1, GridUnitType.Star);
  223. var expected4 = new GridLength(100, GridUnitType.Star);
  224. Assert.Equal(expected1, grid.ColumnDefinitions[0].Width);
  225. Assert.Equal(expected2, grid.ColumnDefinitions[1].Width);
  226. Assert.Equal(expected3, grid.ColumnDefinitions[2].Width);
  227. Assert.Equal(expected4, grid.ColumnDefinitions[3].Width);
  228. Assert.Equal(expected1, grid.RowDefinitions[0].Height);
  229. Assert.Equal(expected2, grid.RowDefinitions[1].Height);
  230. Assert.Equal(expected3, grid.RowDefinitions[2].Height);
  231. Assert.Equal(expected4, grid.RowDefinitions[3].Height);
  232. }
  233. [Fact]
  234. public void Grid_Row_Col_Definitions_Are_Parsed()
  235. {
  236. var xaml = @"
  237. <Grid xmlns='https://github.com/avaloniaui'
  238. ColumnDefinitions='100,Auto,*,100*'
  239. RowDefinitions='100,Auto,*,100*'>
  240. </Grid>";
  241. var grid = AvaloniaXamlLoader.Parse<Grid>(xaml);
  242. Assert.Equal(4, grid.ColumnDefinitions.Count);
  243. Assert.Equal(4, grid.RowDefinitions.Count);
  244. var expected1 = new GridLength(100);
  245. var expected2 = GridLength.Auto;
  246. var expected3 = new GridLength(1, GridUnitType.Star);
  247. var expected4 = new GridLength(100, GridUnitType.Star);
  248. Assert.Equal(expected1, grid.ColumnDefinitions[0].Width);
  249. Assert.Equal(expected2, grid.ColumnDefinitions[1].Width);
  250. Assert.Equal(expected3, grid.ColumnDefinitions[2].Width);
  251. Assert.Equal(expected4, grid.ColumnDefinitions[3].Width);
  252. Assert.Equal(expected1, grid.RowDefinitions[0].Height);
  253. Assert.Equal(expected2, grid.RowDefinitions[1].Height);
  254. Assert.Equal(expected3, grid.RowDefinitions[2].Height);
  255. Assert.Equal(expected4, grid.RowDefinitions[3].Height);
  256. }
  257. [Fact]
  258. public void ControlTemplate_With_Nested_Child_Is_Operational()
  259. {
  260. var xaml = @"
  261. <ControlTemplate xmlns='https://github.com/avaloniaui'>
  262. <ContentControl Name='parent'>
  263. <ContentControl Name='child' />
  264. </ContentControl>
  265. </ControlTemplate>
  266. ";
  267. var template = AvaloniaXamlLoader.Parse<ControlTemplate>(xaml);
  268. var parent = (ContentControl)template.Build(new ContentControl());
  269. Assert.Equal("parent", parent.Name);
  270. var child = parent.Content as ContentControl;
  271. Assert.NotNull(child);
  272. Assert.Equal("child", child.Name);
  273. }
  274. [Fact]
  275. public void ControlTemplate_With_Panel_Children_Are_Added()
  276. {
  277. var xaml = @"
  278. <ControlTemplate xmlns='https://github.com/avaloniaui'>
  279. <Panel Name='panel'>
  280. <ContentControl Name='Foo' />
  281. <ContentControl Name='Bar' />
  282. </Panel>
  283. </ControlTemplate>
  284. ";
  285. var template = AvaloniaXamlLoader.Parse<ControlTemplate>(xaml);
  286. var panel = (Panel)template.Build(new ContentControl());
  287. Assert.Equal(2, panel.Children.Count);
  288. var foo = panel.Children[0];
  289. var bar = panel.Children[1];
  290. Assert.Equal("Foo", foo.Name);
  291. Assert.Equal("Bar", bar.Name);
  292. }
  293. [Fact]
  294. public void Named_x_Control_Is_Added_To_NameScope_Simple()
  295. {
  296. var xaml = @"
  297. <UserControl xmlns='https://github.com/avaloniaui'
  298. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  299. <Button x:Name='button'>Foo</Button>
  300. </UserControl>";
  301. var control = AvaloniaXamlLoader.Parse<UserControl>(xaml);
  302. var button = control.FindControl<Button>("button");
  303. Assert.Equal("Foo", button.Content);
  304. }
  305. [Fact]
  306. public void Standart_TypeConverter_Is_Used()
  307. {
  308. var xaml = @"<UserControl xmlns='https://github.com/avaloniaui' Width='200.5' />";
  309. var control = AvaloniaXamlLoader.Parse<UserControl>(xaml);
  310. Assert.Equal(200.5, control.Width);
  311. }
  312. [Fact]
  313. public void Avalonia_TypeConverter_Is_Used()
  314. {
  315. var xaml = @"<UserControl xmlns='https://github.com/avaloniaui' Background='White' />";
  316. var control = AvaloniaXamlLoader.Parse<UserControl>(xaml);
  317. var bk = control.Background;
  318. Assert.IsType<ImmutableSolidColorBrush>(bk);
  319. Assert.Equal(Colors.White, (bk as ISolidColorBrush).Color);
  320. }
  321. [Fact]
  322. public void Simple_Style_Is_Parsed()
  323. {
  324. var xaml = @"
  325. <Styles xmlns='https://github.com/avaloniaui'
  326. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  327. <Style Selector='TextBlock'>
  328. <Setter Property='Background' Value='White'/>
  329. <Setter Property='Width' Value='100'/>
  330. </Style>
  331. </Styles>";
  332. var styles = AvaloniaXamlLoader.Parse<Styles>(xaml);
  333. Assert.Single(styles);
  334. var style = (Style)styles[0];
  335. var setters = style.Setters.Cast<Setter>().ToArray();
  336. Assert.Equal(2, setters.Length);
  337. Assert.Equal(TextBlock.BackgroundProperty, setters[0].Property);
  338. Assert.Equal(Brushes.White.Color, ((ISolidColorBrush)setters[0].Value).Color);
  339. Assert.Equal(TextBlock.WidthProperty, setters[1].Property);
  340. Assert.Equal(100.0, setters[1].Value);
  341. }
  342. [Fact]
  343. public void Style_Setter_With_AttachedProperty_Is_Parsed()
  344. {
  345. var xaml = @"
  346. <Styles xmlns='https://github.com/avaloniaui'
  347. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  348. <Style Selector='ContentControl'>
  349. <Setter Property='TextBlock.FontSize' Value='21'/>
  350. </Style>
  351. </Styles>";
  352. var styles = AvaloniaXamlLoader.Parse<Styles>(xaml);
  353. Assert.Single(styles);
  354. var style = (Style)styles[0];
  355. var setters = style.Setters.Cast<Setter>().ToArray();
  356. Assert.Single(setters);
  357. Assert.Equal(TextBlock.FontSizeProperty, setters[0].Property);
  358. Assert.Equal(21.0, setters[0].Value);
  359. }
  360. [Fact]
  361. public void Complex_Style_Is_Parsed()
  362. {
  363. using (UnitTestApplication.Start(TestServices.StyledWindow))
  364. {
  365. var xaml = @"
  366. <Styles xmlns='https://github.com/avaloniaui'>
  367. <Style Selector='CheckBox'>
  368. <Setter Property='BorderBrush' Value='{DynamicResource ThemeBorderMidBrush}'/>
  369. <Setter Property='BorderThickness' Value='{DynamicResource ThemeBorderThickness}'/>
  370. <Setter Property='Template'>
  371. <ControlTemplate>
  372. <Grid ColumnDefinitions='Auto,*'>
  373. <Border Name='border'
  374. BorderBrush='{TemplateBinding BorderBrush}'
  375. BorderThickness='{TemplateBinding BorderThickness}'
  376. Width='18'
  377. Height='18'
  378. VerticalAlignment='Center'>
  379. <Path Name='checkMark'
  380. Fill='{StaticResource HighlightBrush}'
  381. Width='11'
  382. Height='10'
  383. Stretch='Uniform'
  384. HorizontalAlignment='Center'
  385. VerticalAlignment='Center'
  386. Data='M 1145.607177734375,430 C1145.607177734375,430 1141.449951171875,435.0772705078125 1141.449951171875,435.0772705078125 1141.449951171875,435.0772705078125 1139.232177734375,433.0999755859375 1139.232177734375,433.0999755859375 1139.232177734375,433.0999755859375 1138,434.5538330078125 1138,434.5538330078125 1138,434.5538330078125 1141.482177734375,438 1141.482177734375,438 1141.482177734375,438 1141.96875,437.9375 1141.96875,437.9375 1141.96875,437.9375 1147,431.34619140625 1147,431.34619140625 1147,431.34619140625 1145.607177734375,430 1145.607177734375,430 z'/>
  387. </Border>
  388. <ContentPresenter Name='PART_ContentPresenter'
  389. Content='{TemplateBinding Content}'
  390. ContentTemplate='{TemplateBinding ContentTemplate}'
  391. Margin='4,0,0,0'
  392. VerticalAlignment='Center'
  393. Grid.Column='1'/>
  394. </Grid>
  395. </ControlTemplate>
  396. </Setter>
  397. </Style>
  398. </Styles>
  399. ";
  400. var styles = AvaloniaXamlLoader.Parse<Styles>(xaml);
  401. Assert.Single(styles);
  402. var style = (Style)styles[0];
  403. var setters = style.Setters.Cast<Setter>().ToArray();
  404. Assert.Equal(3, setters.Length);
  405. Assert.Equal(CheckBox.BorderBrushProperty, setters[0].Property);
  406. Assert.Equal(CheckBox.BorderThicknessProperty, setters[1].Property);
  407. Assert.Equal(CheckBox.TemplateProperty, setters[2].Property);
  408. Assert.IsType<ControlTemplate>(setters[2].Value);
  409. }
  410. }
  411. [Fact]
  412. public void Style_Resources_Are_Built()
  413. {
  414. var xaml = @"
  415. <Style xmlns='https://github.com/avaloniaui'
  416. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  417. xmlns:sys='clr-namespace:System;assembly=netstandard'>
  418. <Style.Resources>
  419. <SolidColorBrush x:Key='Brush'>White</SolidColorBrush>
  420. <sys:Double x:Key='Double'>10</sys:Double>
  421. </Style.Resources>
  422. </Style>";
  423. var style = AvaloniaXamlLoader.Parse<Style>(xaml);
  424. Assert.True(style.Resources.Count > 0);
  425. style.TryGetResource("Brush", out var brush);
  426. Assert.NotNull(brush);
  427. Assert.IsType<SolidColorBrush>(brush);
  428. Assert.Equal(Colors.White, ((ISolidColorBrush)brush).Color);
  429. style.TryGetResource("Double", out var d);
  430. Assert.Equal(10.0, d);
  431. }
  432. [Fact]
  433. public void StyleInclude_Is_Built()
  434. {
  435. using (UnitTestApplication.Start(TestServices.StyledWindow))
  436. {
  437. var xaml = @"
  438. <Styles xmlns='https://github.com/avaloniaui'
  439. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  440. <StyleInclude Source='resm:Avalonia.Themes.Default.ContextMenu.xaml?assembly=Avalonia.Themes.Default'/>
  441. </Styles>";
  442. var styles = AvaloniaXamlLoader.Parse<Styles>(xaml);
  443. Assert.True(styles.Count == 1);
  444. var styleInclude = styles.First() as StyleInclude;
  445. Assert.NotNull(styleInclude);
  446. var style = styleInclude.Loaded;
  447. Assert.NotNull(style);
  448. }
  449. }
  450. [Fact]
  451. public void Simple_Xaml_Binding_Is_Operational()
  452. {
  453. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper
  454. .With(windowingPlatform: new MockWindowingPlatform())))
  455. {
  456. var xaml =
  457. @"<Window xmlns='https://github.com/avaloniaui' Content='{Binding}'/>";
  458. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  459. Assert.Null(target.Content);
  460. target.DataContext = "Foo";
  461. Assert.Equal("Foo", target.Content);
  462. }
  463. }
  464. [Fact]
  465. public void Xaml_Binding_Is_Delayed()
  466. {
  467. if (!AvaloniaXamlLoader.UseLegacyXamlLoader)
  468. return;
  469. using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
  470. {
  471. var xaml =
  472. @"<ContentControl xmlns='https://github.com/avaloniaui' Content='{Binding}'/>";
  473. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  474. Assert.Null(target.Content);
  475. target.DataContext = "Foo";
  476. Assert.Null(target.Content);
  477. DelayedBinding.ApplyBindings(target);
  478. Assert.Equal("Foo", target.Content);
  479. }
  480. }
  481. [Fact]
  482. public void Double_Xaml_Binding_Is_Operational()
  483. {
  484. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper
  485. .With(windowingPlatform: new MockWindowingPlatform())))
  486. {
  487. var xaml =
  488. @"<Window xmlns='https://github.com/avaloniaui' Width='{Binding}'/>";
  489. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  490. Assert.Null(target.Content);
  491. target.DataContext = 55.0;
  492. Assert.Equal(55.0, target.Width);
  493. }
  494. }
  495. [Fact]
  496. public void Collection_Xaml_Binding_Is_Operational()
  497. {
  498. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper
  499. .With(windowingPlatform: new MockWindowingPlatform())))
  500. {
  501. var xaml = @"
  502. <Window xmlns='https://github.com/avaloniaui'>
  503. <ItemsControl Name='itemsControl' Items='{Binding}'>
  504. </ItemsControl>
  505. </Window>
  506. ";
  507. var target = AvaloniaXamlLoader.Parse<Window>(xaml);
  508. Assert.NotNull(target.Content);
  509. var itemsControl = target.FindControl<ItemsControl>("itemsControl");
  510. var items = new string[] { "Foo", "Bar" };
  511. //DelayedBinding.ApplyBindings(itemsControl);
  512. target.DataContext = items;
  513. Assert.Equal(items, itemsControl.Items);
  514. }
  515. }
  516. [Fact]
  517. public void Multi_Xaml_Binding_Is_Parsed()
  518. {
  519. var xaml =
  520. @"<MultiBinding xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  521. Converter ='{x:Static BoolConverters.And}'>
  522. <Binding Path='Foo' />
  523. <Binding Path='Bar' />
  524. </MultiBinding>";
  525. var target = AvaloniaXamlLoader.Parse<MultiBinding>(xaml);
  526. Assert.Equal(2, target.Bindings.Count);
  527. Assert.Equal(BoolConverters.And, target.Converter);
  528. var bindings = target.Bindings.Cast<Binding>().ToArray();
  529. Assert.Equal("Foo", bindings[0].Path);
  530. Assert.Equal("Bar", bindings[1].Path);
  531. }
  532. [Fact]
  533. public void Control_Template_Is_Operational()
  534. {
  535. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper
  536. .With(windowingPlatform: new MockWindowingPlatform())))
  537. {
  538. var xaml = @"
  539. <Window xmlns='https://github.com/avaloniaui'
  540. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  541. <Window.Template>
  542. <ControlTemplate TargetType='Window'>
  543. <ContentPresenter Name='PART_ContentPresenter'
  544. Content='{TemplateBinding Content}'/>
  545. </ControlTemplate>
  546. </Window.Template>
  547. </Window>";
  548. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  549. Assert.NotNull(target.Template);
  550. Assert.Null(target.Presenter);
  551. target.ApplyTemplate();
  552. Assert.NotNull(target.Presenter);
  553. target.Content = "Foo";
  554. Assert.Equal("Foo", target.Presenter.Content);
  555. }
  556. }
  557. [Fact]
  558. public void Style_ControlTemplate_Is_Built()
  559. {
  560. var xaml = @"
  561. <Style xmlns='https://github.com/avaloniaui' Selector='ContentControl'>
  562. <Setter Property='Template'>
  563. <ControlTemplate>
  564. <ContentPresenter Name='PART_ContentPresenter'
  565. Content='{TemplateBinding Content}'
  566. ContentTemplate='{TemplateBinding ContentTemplate}' />
  567. </ControlTemplate>
  568. </Setter>
  569. </Style> ";
  570. var style = AvaloniaXamlLoader.Parse<Style>(xaml);
  571. Assert.Single(style.Setters);
  572. var setter = (Setter)style.Setters.First();
  573. Assert.Equal(ContentControl.TemplateProperty, setter.Property);
  574. Assert.IsType<ControlTemplate>(setter.Value);
  575. var template = (ControlTemplate)setter.Value;
  576. var control = new ContentControl();
  577. var result = (ContentPresenter)template.Build(control);
  578. Assert.NotNull(result);
  579. }
  580. [Fact]
  581. public void Named_Control_Is_Added_To_NameScope()
  582. {
  583. using (UnitTestApplication.Start(TestServices.StyledWindow))
  584. {
  585. var xaml = @"
  586. <Window xmlns='https://github.com/avaloniaui'
  587. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  588. <Button Name='button'>Foo</Button>
  589. </Window>";
  590. var window = AvaloniaXamlLoader.Parse<Window>(xaml);
  591. var button = window.FindControl<Button>("button");
  592. Assert.Equal("Foo", button.Content);
  593. }
  594. }
  595. [Fact(Skip =
  596. @"Doesn't work with Portable.xaml, it's working in different creation order -
  597. Handled in test 'Control_Is_Added_To_Parent_Before_Final_EndInit'
  598. do we need it?")]
  599. public void Control_Is_Added_To_Parent_Before_Properties_Are_Set()
  600. {
  601. using (UnitTestApplication.Start(TestServices.StyledWindow))
  602. {
  603. var xaml = @"
  604. <Window xmlns='https://github.com/avaloniaui'
  605. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  606. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
  607. <local:InitializationOrderTracker Width='100'/>
  608. </Window>";
  609. var window = AvaloniaXamlLoader.Parse<Window>(xaml);
  610. var tracker = (InitializationOrderTracker)window.Content;
  611. var attached = tracker.Order.IndexOf("AttachedToLogicalTree");
  612. var widthChanged = tracker.Order.IndexOf("Property Width Changed");
  613. Assert.NotEqual(-1, attached);
  614. Assert.NotEqual(-1, widthChanged);
  615. Assert.True(attached < widthChanged);
  616. }
  617. }
  618. [Fact]
  619. public void Control_Is_Added_To_Parent_Before_Final_EndInit()
  620. {
  621. using (UnitTestApplication.Start(TestServices.StyledWindow))
  622. {
  623. var xaml = @"
  624. <Window xmlns='https://github.com/avaloniaui'
  625. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  626. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
  627. <local:InitializationOrderTracker Width='100'/>
  628. </Window>";
  629. var window = AvaloniaXamlLoader.Parse<Window>(xaml);
  630. var tracker = (InitializationOrderTracker)window.Content;
  631. var attached = tracker.Order.IndexOf("AttachedToLogicalTree");
  632. var endInit = tracker.Order.IndexOf("EndInit 0");
  633. Assert.NotEqual(-1, attached);
  634. Assert.NotEqual(-1, endInit);
  635. Assert.True(attached < endInit);
  636. }
  637. }
  638. [Fact]
  639. public void All_Properties_Are_Set_Before_Final_EndInit()
  640. {
  641. using (UnitTestApplication.Start(TestServices.StyledWindow))
  642. {
  643. var xaml = @"
  644. <Window xmlns='https://github.com/avaloniaui'
  645. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  646. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
  647. <local:InitializationOrderTracker Width='100' Height='100'
  648. Tag='{Binding Height, RelativeSource={RelativeSource Self}}' />
  649. </Window>";
  650. var window = AvaloniaXamlLoader.Parse<Window>(xaml);
  651. var tracker = (InitializationOrderTracker)window.Content;
  652. //ensure binding is set and operational first
  653. Assert.Equal(100.0, tracker.Tag);
  654. Assert.Equal("EndInit 0", tracker.Order.Last());
  655. }
  656. }
  657. [Fact]
  658. public void BeginInit_Matches_EndInit()
  659. {
  660. using (UnitTestApplication.Start(TestServices.StyledWindow))
  661. {
  662. var xaml = @"
  663. <Window xmlns='https://github.com/avaloniaui'
  664. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  665. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
  666. <local:InitializationOrderTracker />
  667. </Window>";
  668. var window = AvaloniaXamlLoader.Parse<Window>(xaml);
  669. var tracker = (InitializationOrderTracker)window.Content;
  670. Assert.Equal(0, tracker.InitState);
  671. }
  672. }
  673. [Fact]
  674. public void DeferedXamlLoader_Should_Preserve_NamespacesContext()
  675. {
  676. var xaml =
  677. @"<ContentControl xmlns='https://github.com/avaloniaui'
  678. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  679. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
  680. <ContentControl.ContentTemplate>
  681. <DataTemplate>
  682. <TextBlock Tag='{x:Static local:NonControl.StringProperty}'/>
  683. </DataTemplate>
  684. </ContentControl.ContentTemplate>
  685. </ContentControl>";
  686. var contentControl = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  687. var template = contentControl.ContentTemplate;
  688. Assert.NotNull(template);
  689. var txt = (TextBlock)template.Build(null);
  690. Assert.Equal((object)NonControl.StringProperty, txt.Tag);
  691. }
  692. [Fact]
  693. public void Binding_To_List_AvaloniaProperty_Is_Operational()
  694. {
  695. using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
  696. {
  697. var xaml = @"
  698. <Window xmlns='https://github.com/avaloniaui'>
  699. <ListBox Items='{Binding Items}' SelectedItems='{Binding SelectedItems}'/>
  700. </Window>";
  701. var window = AvaloniaXamlLoader.Parse<Window>(xaml);
  702. var listBox = (ListBox)window.Content;
  703. var vm = new SelectedItemsViewModel()
  704. {
  705. Items = new string[] { "foo", "bar", "baz" }
  706. };
  707. window.DataContext = vm;
  708. Assert.Equal(vm.Items, listBox.Items);
  709. Assert.Equal(vm.SelectedItems, listBox.SelectedItems);
  710. }
  711. }
  712. [Fact]
  713. public void Element_Whitespace_Should_Be_Trimmed()
  714. {
  715. using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
  716. {
  717. var xaml = @"
  718. <Window xmlns='https://github.com/avaloniaui'>
  719. <TextBlock>
  720. Hello World!
  721. </TextBlock>
  722. </Window>";
  723. var window = AvaloniaXamlLoader.Parse<Window>(xaml);
  724. var textBlock = (TextBlock)window.Content;
  725. Assert.Equal("Hello World!", textBlock.Text);
  726. }
  727. }
  728. [Fact]
  729. public void Design_Mode_Properties_Should_Be_Ignored_At_Runtime_And_Set_In_Design_Mode()
  730. {
  731. using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
  732. {
  733. var xaml = @"
  734. <Window xmlns='https://github.com/avaloniaui'
  735. xmlns:d='http://schemas.microsoft.com/expression/blend/2008'
  736. xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006'
  737. mc:Ignorable='d'
  738. d:DataContext='data-context'
  739. d:DesignWidth='123'
  740. d:DesignHeight='321'
  741. >
  742. </Window>";
  743. foreach (var designMode in new[] {true, false})
  744. {
  745. var loader = new AvaloniaXamlLoader {IsDesignMode = designMode};
  746. var obj = (Window)loader.Load(xaml);
  747. var context = Design.GetDataContext(obj);
  748. var width = Design.GetWidth(obj);
  749. var height = Design.GetHeight(obj);
  750. if (designMode)
  751. {
  752. Assert.Equal("data-context", context);
  753. Assert.Equal(123, width);
  754. Assert.Equal(321, height);
  755. }
  756. else
  757. {
  758. Assert.False(obj.IsSet(Design.DataContextProperty));
  759. Assert.False(obj.IsSet(Design.WidthProperty));
  760. Assert.False(obj.IsSet(Design.HeightProperty));
  761. }
  762. }
  763. }
  764. }
  765. private class SelectedItemsViewModel : INotifyPropertyChanged
  766. {
  767. public string[] Items { get; set; }
  768. public event PropertyChangedEventHandler PropertyChanged;
  769. private IList _selectedItems = new AvaloniaList<string>();
  770. public IList SelectedItems
  771. {
  772. get { return _selectedItems; }
  773. set
  774. {
  775. _selectedItems = value;
  776. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedItems)));
  777. }
  778. }
  779. }
  780. }
  781. public class BasicTestsAttachedPropertyHolder
  782. {
  783. public static AvaloniaProperty<string> FooProperty =
  784. AvaloniaProperty.RegisterAttached<BasicTestsAttachedPropertyHolder, AvaloniaObject, string>("Foo");
  785. public static void SetFoo(AvaloniaObject target, string value) => target.SetValue(FooProperty, value);
  786. public static string GetFoo(AvaloniaObject target) => (string)target.GetValue(FooProperty);
  787. }
  788. }