TreeViewTests.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (c) The Perspex 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 System.Linq;
  5. using Perspex.Controls;
  6. using Perspex.Controls.Presenters;
  7. using Perspex.Controls.Templates;
  8. using Perspex.LogicalTree;
  9. using Perspex.Styling;
  10. using Xunit;
  11. namespace Perspex.Controls.UnitTests
  12. {
  13. public class TreeViewTests
  14. {
  15. [Fact]
  16. public void LogicalChildren_Should_Be_Set()
  17. {
  18. var target = new TreeView
  19. {
  20. Template = new FuncControlTemplate(CreateTreeViewTemplate),
  21. Items = new[] { "Foo", "Bar", "Baz " },
  22. };
  23. target.ApplyTemplate();
  24. Assert.Equal(3, target.GetLogicalChildren().Count());
  25. foreach (var child in target.GetLogicalChildren())
  26. {
  27. Assert.IsType<TreeViewItem>(child);
  28. }
  29. }
  30. [Fact]
  31. public void DataContexts_Should_Be_Correctly_Set()
  32. {
  33. var items = new object[]
  34. {
  35. "Foo",
  36. new Item("Bar"),
  37. new TextBlock { Text = "Baz" },
  38. new TreeViewItem { Header = "Qux" },
  39. };
  40. var target = new TreeView
  41. {
  42. Template = new FuncControlTemplate(CreateTreeViewTemplate),
  43. DataContext = "Base",
  44. DataTemplates = new DataTemplates
  45. {
  46. new FuncDataTemplate<Item>(x => new Button { Content = x })
  47. },
  48. Items = items,
  49. };
  50. target.ApplyTemplate();
  51. var dataContexts = target.Presenter.Panel.Children
  52. .Cast<Control>()
  53. .Select(x => x.DataContext)
  54. .ToList();
  55. Assert.Equal(
  56. new object[] { items[0], items[1], "Base", "Base" },
  57. dataContexts);
  58. }
  59. private Control CreateTreeViewTemplate(ITemplatedControl parent)
  60. {
  61. return new ScrollViewer
  62. {
  63. Template = new FuncControlTemplate(CreateScrollViewerTemplate),
  64. Content = new ItemsPresenter
  65. {
  66. Name = "itemsPresenter",
  67. [~ItemsPresenter.ItemsProperty] = parent.GetObservable(ItemsControl.ItemsProperty),
  68. }
  69. };
  70. }
  71. private Control CreateScrollViewerTemplate(ITemplatedControl parent)
  72. {
  73. return new ScrollContentPresenter
  74. {
  75. [~ContentPresenter.ContentProperty] = parent.GetObservable(ContentControl.ContentProperty),
  76. };
  77. }
  78. private class Item
  79. {
  80. public Item(string value)
  81. {
  82. Value = value;
  83. }
  84. public string Value { get; }
  85. }
  86. }
  87. }