BasicTests.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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 Avalonia.Collections;
  4. using Avalonia.Controls;
  5. using Avalonia.Controls.Presenters;
  6. using Avalonia.Markup.Xaml.Data;
  7. using Avalonia.Markup.Xaml.Styling;
  8. using Avalonia.Markup.Xaml.Templates;
  9. using Avalonia.Media;
  10. using Avalonia.Styling;
  11. using Avalonia.UnitTests;
  12. using System.Collections;
  13. using System.ComponentModel;
  14. using System.Linq;
  15. using Xunit;
  16. namespace Avalonia.Markup.Xaml.UnitTests.Xaml
  17. {
  18. public class BasicTests
  19. {
  20. [Fact]
  21. public void Simple_Property_Is_Set()
  22. {
  23. var xaml = @"<ContentControl xmlns='https://github.com/avaloniaui' Content='Foo'/>";
  24. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  25. Assert.NotNull(target);
  26. Assert.Equal("Foo", target.Content);
  27. }
  28. [Fact]
  29. public void Default_Content_Property_Is_Set()
  30. {
  31. var xaml = @"<ContentControl xmlns='https://github.com/avaloniaui'>Foo</ContentControl>";
  32. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  33. Assert.NotNull(target);
  34. Assert.Equal("Foo", target.Content);
  35. }
  36. [Fact]
  37. public void Attached_Property_Is_Set()
  38. {
  39. var xaml =
  40. @"<ContentControl xmlns='https://github.com/avaloniaui' TextBlock.FontSize='21'/>";
  41. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  42. Assert.NotNull(target);
  43. Assert.Equal(21.0, TextBlock.GetFontSize(target));
  44. }
  45. [Fact]
  46. public void Attached_Property_Supports_Binding()
  47. {
  48. using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
  49. {
  50. var xaml =
  51. @"<Window xmlns='https://github.com/avaloniaui' TextBlock.FontSize='{Binding}'/>";
  52. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  53. target.DataContext = 21.0;
  54. Assert.Equal(21.0, TextBlock.GetFontSize(target));
  55. }
  56. }
  57. [Fact]
  58. public void Attached_Property_In_Panel_Is_Set()
  59. {
  60. var xaml = @"
  61. <Panel xmlns='https://github.com/avaloniaui'>
  62. <ToolTip.Tip>Foo</ToolTip.Tip>
  63. </Panel>";
  64. var target = AvaloniaXamlLoader.Parse<Panel>(xaml);
  65. Assert.Equal(0, target.Children.Count);
  66. Assert.Equal("Foo", ToolTip.GetTip(target));
  67. }
  68. [Fact]
  69. public void ContentControl_ContentTemplate_Is_Functional()
  70. {
  71. var xaml =
  72. @"<ContentControl xmlns='https://github.com/avaloniaui'>
  73. <ContentControl.ContentTemplate>
  74. <DataTemplate>
  75. <TextBlock Text='Foo' />
  76. </DataTemplate>
  77. </ContentControl.ContentTemplate>
  78. </ContentControl>";
  79. var contentControl = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  80. var target = contentControl.ContentTemplate;
  81. Assert.NotNull(target);
  82. var txt = (TextBlock)target.Build(null);
  83. Assert.Equal("Foo", txt.Text);
  84. }
  85. [Fact]
  86. public void Named_Control_Is_Added_To_NameScope_Simple()
  87. {
  88. var xaml = @"
  89. <UserControl xmlns='https://github.com/avaloniaui'>
  90. <Button Name='button'>Foo</Button>
  91. </UserControl>";
  92. var control = AvaloniaXamlLoader.Parse<UserControl>(xaml);
  93. var button = control.FindControl<Button>("button");
  94. Assert.Equal("Foo", button.Content);
  95. }
  96. [Fact]
  97. public void Direct_Content_In_ItemsControl_Is_Operational()
  98. {
  99. using (UnitTestApplication.Start(TestServices.StyledWindow))
  100. {
  101. var xaml = @"
  102. <Window xmlns='https://github.com/avaloniaui'>
  103. <ItemsControl Name='items'>
  104. <ContentControl>Foo</ContentControl>
  105. <ContentControl>Bar</ContentControl>
  106. </ItemsControl>
  107. </Window>";
  108. var control = AvaloniaXamlLoader.Parse<Window>(xaml);
  109. var itemsControl = control.FindControl<ItemsControl>("items");
  110. Assert.NotNull(itemsControl);
  111. var items = itemsControl.Items.Cast<ContentControl>().ToArray();
  112. Assert.Equal("Foo", items[0].Content);
  113. Assert.Equal("Bar", items[1].Content);
  114. }
  115. }
  116. [Fact]
  117. public void Panel_Children_Are_Added()
  118. {
  119. var xaml = @"
  120. <UserControl xmlns='https://github.com/avaloniaui'>
  121. <Panel Name='panel'>
  122. <ContentControl Name='Foo' />
  123. <ContentControl Name='Bar' />
  124. </Panel>
  125. </UserControl>";
  126. var control = AvaloniaXamlLoader.Parse<UserControl>(xaml);
  127. var panel = control.FindControl<Panel>("panel");
  128. Assert.Equal(2, panel.Children.Count);
  129. var foo = control.FindControl<ContentControl>("Foo");
  130. var bar = control.FindControl<ContentControl>("Bar");
  131. Assert.Contains(foo, panel.Children);
  132. Assert.Contains(bar, panel.Children);
  133. }
  134. [Fact]
  135. public void ControlTemplate_With_Nested_Child_Is_Operational()
  136. {
  137. var xaml = @"
  138. <ControlTemplate xmlns='https://github.com/avaloniaui'>
  139. <ContentControl Name='parent'>
  140. <ContentControl Name='child' />
  141. </ContentControl>
  142. </ControlTemplate>
  143. ";
  144. var template = AvaloniaXamlLoader.Parse<ControlTemplate>(xaml);
  145. var parent = (ContentControl)template.Build(new ContentControl());
  146. Assert.Equal("parent", parent.Name);
  147. var child = parent.Content as ContentControl;
  148. Assert.NotNull(child);
  149. Assert.Equal("child", child.Name);
  150. }
  151. [Fact]
  152. public void ControlTemplate_With_Panel_Children_Are_Added()
  153. {
  154. var xaml = @"
  155. <ControlTemplate xmlns='https://github.com/avaloniaui'>
  156. <Panel Name='panel'>
  157. <ContentControl Name='Foo' />
  158. <ContentControl Name='Bar' />
  159. </Panel>
  160. </ControlTemplate>
  161. ";
  162. var template = AvaloniaXamlLoader.Parse<ControlTemplate>(xaml);
  163. var panel = (Panel)template.Build(new ContentControl());
  164. Assert.Equal(2, panel.Children.Count);
  165. var foo = panel.Children[0];
  166. var bar = panel.Children[1];
  167. Assert.Equal("Foo", foo.Name);
  168. Assert.Equal("Bar", bar.Name);
  169. }
  170. [Fact]
  171. public void Named_x_Control_Is_Added_To_NameScope_Simple()
  172. {
  173. var xaml = @"
  174. <UserControl xmlns='https://github.com/avaloniaui'
  175. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  176. <Button x:Name='button'>Foo</Button>
  177. </UserControl>";
  178. var control = AvaloniaXamlLoader.Parse<UserControl>(xaml);
  179. var button = control.FindControl<Button>("button");
  180. Assert.Equal("Foo", button.Content);
  181. }
  182. [Fact]
  183. public void Standart_TypeConverter_Is_Used()
  184. {
  185. var xaml = @"<UserControl xmlns='https://github.com/avaloniaui' Width='200.5' />";
  186. var control = AvaloniaXamlLoader.Parse<UserControl>(xaml);
  187. Assert.Equal(200.5, control.Width);
  188. }
  189. [Fact]
  190. public void Avalonia_TypeConverter_Is_Used()
  191. {
  192. var xaml = @"<UserControl xmlns='https://github.com/avaloniaui' Background='White' />";
  193. var control = AvaloniaXamlLoader.Parse<UserControl>(xaml);
  194. var bk = control.Background;
  195. Assert.IsType<SolidColorBrush>(bk);
  196. Assert.Equal(Colors.White, (bk as SolidColorBrush).Color);
  197. }
  198. [Fact]
  199. public void Simple_Style_Is_Parsed()
  200. {
  201. var xaml = @"
  202. <Styles xmlns='https://github.com/avaloniaui'
  203. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  204. <Style Selector='TextBlock'>
  205. <Setter Property='Background' Value='White'/>
  206. <Setter Property='Width' Value='100'/>
  207. </Style>
  208. </Styles>";
  209. var styles = AvaloniaXamlLoader.Parse<Styles>(xaml);
  210. Assert.Equal(1, styles.Count);
  211. var style = (Style)styles[0];
  212. var setters = style.Setters.Cast<Setter>().ToArray();
  213. Assert.Equal(2, setters.Length);
  214. Assert.Equal(TextBlock.BackgroundProperty, setters[0].Property);
  215. Assert.Equal(Brushes.White, setters[0].Value);
  216. Assert.Equal(TextBlock.WidthProperty, setters[1].Property);
  217. Assert.Equal(100.0, setters[1].Value);
  218. }
  219. [Fact]
  220. public void Style_Setter_With_AttachedProperty_Is_Parsed()
  221. {
  222. var xaml = @"
  223. <Styles xmlns='https://github.com/avaloniaui'
  224. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  225. <Style Selector='ContentControl'>
  226. <Setter Property='TextBlock.FontSize' Value='21'/>
  227. </Style>
  228. </Styles>";
  229. var styles = AvaloniaXamlLoader.Parse<Styles>(xaml);
  230. Assert.Equal(1, styles.Count);
  231. var style = (Style)styles[0];
  232. var setters = style.Setters.Cast<Setter>().ToArray();
  233. Assert.Equal(1, setters.Length);
  234. Assert.Equal(TextBlock.FontSizeProperty, setters[0].Property);
  235. Assert.Equal(21.0, setters[0].Value);
  236. }
  237. [Fact]
  238. public void Complex_Style_Is_Parsed()
  239. {
  240. using (UnitTestApplication.Start(TestServices.StyledWindow))
  241. {
  242. var xaml = @"
  243. <Styles xmlns='https://github.com/avaloniaui'>
  244. <Style Selector='CheckBox'>
  245. <Setter Property='BorderBrush' Value='{StyleResource ThemeBorderMidBrush}'/>
  246. <Setter Property='BorderThickness' Value='{StyleResource ThemeBorderThickness}'/>
  247. <Setter Property='Template'>
  248. <ControlTemplate>
  249. <Grid ColumnDefinitions='Auto,*'>
  250. <Border Name='border'
  251. BorderBrush='{TemplateBinding BorderBrush}'
  252. BorderThickness='{TemplateBinding BorderThickness}'
  253. Width='18'
  254. Height='18'
  255. VerticalAlignment='Center'>
  256. <Path Name='checkMark'
  257. Fill='{StyleResource HighlightBrush}'
  258. Width='11'
  259. Height='10'
  260. Stretch='Uniform'
  261. HorizontalAlignment='Center'
  262. VerticalAlignment='Center'
  263. 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'/>
  264. </Border>
  265. <ContentPresenter Name='PART_ContentPresenter'
  266. Content='{TemplateBinding Content}'
  267. ContentTemplate='{TemplateBinding ContentTemplate}'
  268. Margin='4,0,0,0'
  269. VerticalAlignment='Center'
  270. Grid.Column='1'/>
  271. </Grid>
  272. </ControlTemplate>
  273. </Setter>
  274. </Style>
  275. </Styles>
  276. ";
  277. var styles = AvaloniaXamlLoader.Parse<Styles>(xaml);
  278. Assert.Equal(1, styles.Count);
  279. var style = (Style)styles[0];
  280. var setters = style.Setters.Cast<Setter>().ToArray();
  281. Assert.Equal(3, setters.Length);
  282. Assert.Equal(CheckBox.BorderBrushProperty, setters[0].Property);
  283. Assert.Equal(CheckBox.BorderThicknessProperty, setters[1].Property);
  284. Assert.Equal(CheckBox.TemplateProperty, setters[2].Property);
  285. Assert.IsType<StyleResourceBinding>(setters[0].Value);
  286. Assert.IsType<StyleResourceBinding>(setters[1].Value);
  287. Assert.IsType<ControlTemplate>(setters[2].Value);
  288. }
  289. }
  290. [Fact]
  291. public void Style_Resources_Are_Build()
  292. {
  293. var xaml = @"
  294. <Style xmlns='https://github.com/avaloniaui'
  295. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  296. xmlns:sys='clr-namespace:System;assembly=mscorlib'>
  297. <Style.Resources>
  298. <SolidColorBrush x:Key='Brush'>White</SolidColorBrush>
  299. <sys:Double x:Key='Double'>10</sys:Double>
  300. </Style.Resources>
  301. </Style>";
  302. var style = AvaloniaXamlLoader.Parse<Style>(xaml);
  303. Assert.True(style.Resources.Count > 0);
  304. var brush = style.FindResource("Brush") as SolidColorBrush;
  305. Assert.NotNull(brush);
  306. Assert.Equal(Colors.White, brush.Color);
  307. var d = (double)style.FindResource("Double");
  308. Assert.Equal(10.0, d);
  309. }
  310. [Fact]
  311. public void StyleInclude_Is_Build()
  312. {
  313. using (UnitTestApplication.Start(TestServices.StyledWindow))
  314. {
  315. var xaml = @"
  316. <Styles xmlns='https://github.com/avaloniaui'
  317. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  318. <StyleInclude Source='resm:Avalonia.Themes.Default.ContextMenu.xaml?assembly=Avalonia.Themes.Default'/>
  319. </Styles>";
  320. var styles = AvaloniaXamlLoader.Parse<Styles>(xaml);
  321. Assert.True(styles.Count == 1);
  322. var styleInclude = styles.First() as StyleInclude;
  323. Assert.NotNull(styleInclude);
  324. var style = styleInclude.Loaded;
  325. Assert.NotNull(style);
  326. }
  327. }
  328. [Fact]
  329. public void Simple_Xaml_Binding_Is_Operational()
  330. {
  331. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper
  332. .With(windowingPlatform: new MockWindowingPlatform())))
  333. {
  334. var xaml =
  335. @"<Window xmlns='https://github.com/avaloniaui' Content='{Binding}'/>";
  336. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  337. Assert.Null(target.Content);
  338. target.DataContext = "Foo";
  339. Assert.Equal("Foo", target.Content);
  340. }
  341. }
  342. [Fact]
  343. public void Xaml_Binding_Is_Delayed()
  344. {
  345. using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
  346. {
  347. var xaml =
  348. @"<ContentControl xmlns='https://github.com/avaloniaui' Content='{Binding}'/>";
  349. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  350. Assert.Null(target.Content);
  351. target.DataContext = "Foo";
  352. Assert.Null(target.Content);
  353. DelayedBinding.ApplyBindings(target);
  354. Assert.Equal("Foo", target.Content);
  355. }
  356. }
  357. [Fact]
  358. public void Double_Xaml_Binding_Is_Operational()
  359. {
  360. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper
  361. .With(windowingPlatform: new MockWindowingPlatform())))
  362. {
  363. var xaml =
  364. @"<Window xmlns='https://github.com/avaloniaui' Width='{Binding}'/>";
  365. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  366. Assert.Null(target.Content);
  367. target.DataContext = 55.0;
  368. Assert.Equal(55.0, target.Width);
  369. }
  370. }
  371. [Fact]
  372. public void Collection_Xaml_Binding_Is_Operational()
  373. {
  374. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper
  375. .With(windowingPlatform: new MockWindowingPlatform())))
  376. {
  377. var xaml = @"
  378. <Window xmlns='https://github.com/avaloniaui'>
  379. <ItemsControl Name='itemsControl' Items='{Binding}'>
  380. </ItemsControl>
  381. </Window>
  382. ";
  383. var target = AvaloniaXamlLoader.Parse<Window>(xaml);
  384. Assert.NotNull(target.Content);
  385. var itemsControl = target.FindControl<ItemsControl>("itemsControl");
  386. var items = new string[] { "Foo", "Bar" };
  387. //DelayedBinding.ApplyBindings(itemsControl);
  388. target.DataContext = items;
  389. Assert.Equal(items, itemsControl.Items);
  390. }
  391. }
  392. [Fact]
  393. public void Multi_Xaml_Binding_Is_Parsed()
  394. {
  395. var xaml =
  396. @"<MultiBinding xmlns='https://github.com/avaloniaui'
  397. Converter='{Static BoolConverters.And}'>
  398. <Binding Path='Foo' />
  399. <Binding Path='Bar' />
  400. </MultiBinding>";
  401. var target = AvaloniaXamlLoader.Parse<MultiBinding>(xaml);
  402. Assert.Equal(2, target.Bindings.Count);
  403. Assert.Equal(BoolConverters.And, target.Converter);
  404. var bindings = target.Bindings.Cast<Binding>().ToArray();
  405. Assert.Equal("Foo", bindings[0].Path);
  406. Assert.Equal("Bar", bindings[1].Path);
  407. }
  408. [Fact]
  409. public void Control_Template_Is_Operational()
  410. {
  411. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper
  412. .With(windowingPlatform: new MockWindowingPlatform())))
  413. {
  414. var xaml = @"
  415. <Window xmlns='https://github.com/avaloniaui'
  416. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  417. <Window.Template>
  418. <ControlTemplate>
  419. <ContentPresenter Name='PART_ContentPresenter'
  420. Content='{TemplateBinding Content}'/>
  421. </ControlTemplate>
  422. </Window.Template>
  423. </Window>";
  424. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  425. Assert.NotNull(target.Template);
  426. Assert.Null(target.Presenter);
  427. target.ApplyTemplate();
  428. Assert.NotNull(target.Presenter);
  429. target.Content = "Foo";
  430. Assert.Equal("Foo", target.Presenter.Content);
  431. }
  432. }
  433. [Fact]
  434. public void Style_ControlTemplate_Is_Build()
  435. {
  436. var xaml = @"
  437. <Style xmlns='https://github.com/avaloniaui' Selector='ContentControl'>
  438. <Setter Property='Template'>
  439. <ControlTemplate>
  440. <ContentPresenter Name='PART_ContentPresenter'
  441. Content='{TemplateBinding Content}'
  442. ContentTemplate='{TemplateBinding ContentTemplate}' />
  443. </ControlTemplate>
  444. </Setter>
  445. </Style> ";
  446. var style = AvaloniaXamlLoader.Parse<Style>(xaml);
  447. Assert.Equal(1, style.Setters.Count());
  448. var setter = (Setter)style.Setters.First();
  449. Assert.Equal(ContentControl.TemplateProperty, setter.Property);
  450. Assert.IsType<ControlTemplate>(setter.Value);
  451. var template = (ControlTemplate)setter.Value;
  452. var control = new ContentControl();
  453. var result = (ContentPresenter)template.Build(control);
  454. Assert.NotNull(result);
  455. }
  456. [Fact]
  457. public void Named_Control_Is_Added_To_NameScope()
  458. {
  459. using (UnitTestApplication.Start(TestServices.StyledWindow))
  460. {
  461. var xaml = @"
  462. <Window xmlns='https://github.com/avaloniaui'
  463. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  464. <Button Name='button'>Foo</Button>
  465. </Window>";
  466. var window = AvaloniaXamlLoader.Parse<Window>(xaml);
  467. var button = window.FindControl<Button>("button");
  468. Assert.Equal("Foo", button.Content);
  469. }
  470. }
  471. #if OMNIXAML
  472. [Fact]
  473. #else
  474. [Fact(Skip = "Doesn't work with Portable.xaml, it's working in different order, do we need it?")]
  475. #endif
  476. public void Control_Is_Added_To_Parent_Before_Properties_Are_Set()
  477. {
  478. using (UnitTestApplication.Start(TestServices.StyledWindow))
  479. {
  480. var xaml = @"
  481. <Window xmlns='https://github.com/avaloniaui'
  482. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  483. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
  484. <local:InitializationOrderTracker Width='100'/>
  485. </Window>";
  486. var window = AvaloniaXamlLoader.Parse<Window>(xaml);
  487. var tracker = (InitializationOrderTracker)window.Content;
  488. var attached = tracker.Order.IndexOf("AttachedToLogicalTree");
  489. var widthChanged = tracker.Order.IndexOf("Property Width Changed");
  490. Assert.NotEqual(-1, attached);
  491. Assert.NotEqual(-1, widthChanged);
  492. Assert.True(attached < widthChanged);
  493. }
  494. }
  495. [Fact]
  496. public void BeginInit_Matches_EndInit()
  497. {
  498. using (UnitTestApplication.Start(TestServices.StyledWindow))
  499. {
  500. var xaml = @"
  501. <Window xmlns='https://github.com/avaloniaui'
  502. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  503. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
  504. <local:InitializationOrderTracker />
  505. </Window>";
  506. var window = AvaloniaXamlLoader.Parse<Window>(xaml);
  507. var tracker = (InitializationOrderTracker)window.Content;
  508. Assert.Equal(0, tracker.InitState);
  509. }
  510. }
  511. [Fact]
  512. public void DeferedXamlLoader_Should_Preserve_NamespacesContext()
  513. {
  514. var xaml =
  515. @"<ContentControl xmlns='https://github.com/avaloniaui'
  516. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
  517. <ContentControl.ContentTemplate>
  518. <DataTemplate>
  519. <TextBlock Tag='{Static local:NonControl.StringProperty}'/>
  520. </DataTemplate>
  521. </ContentControl.ContentTemplate>
  522. </ContentControl>";
  523. var contentControl = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  524. var template = contentControl.ContentTemplate;
  525. Assert.NotNull(template);
  526. var txt = (TextBlock)template.Build(null);
  527. Assert.Equal((object)NonControl.StringProperty, txt.Tag);
  528. }
  529. [Fact]
  530. public void Binding_To_List_AvaloniaProperty_Is_Operational()
  531. {
  532. using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
  533. {
  534. var xaml = @"
  535. <Window xmlns='https://github.com/avaloniaui'>
  536. <ListBox Items='{Binding Items}' SelectedItems='{Binding SelectedItems}'/>
  537. </Window>";
  538. var window = AvaloniaXamlLoader.Parse<Window>(xaml);
  539. var listBox = (ListBox)window.Content;
  540. var vm = new SelectedItemsViewModel()
  541. {
  542. Items = new string[] { "foo", "bar", "baz" }
  543. };
  544. window.DataContext = vm;
  545. Assert.Equal(vm.Items, listBox.Items);
  546. Assert.Equal(vm.SelectedItems, listBox.SelectedItems);
  547. }
  548. }
  549. private class SelectedItemsViewModel : INotifyPropertyChanged
  550. {
  551. public string[] Items { get; set; }
  552. public event PropertyChangedEventHandler PropertyChanged;
  553. private IList _selectedItems = new AvaloniaList<string>();
  554. public IList SelectedItems
  555. {
  556. get { return _selectedItems; }
  557. set
  558. {
  559. _selectedItems = value;
  560. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedItems)));
  561. }
  562. }
  563. }
  564. }
  565. }