ListBoxTests.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // -----------------------------------------------------------------------
  2. // <copyright file="ListBoxTests.cs" company="Steven Kirk">
  3. // Copyright 2014 MIT Licence. See licence.md for more information.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. namespace Perspex.Controls.UnitTests
  7. {
  8. using System;
  9. using System.Linq;
  10. using Perspex.Controls;
  11. using Perspex.Controls.Presenters;
  12. using Perspex.Controls.Templates;
  13. using Perspex.LogicalTree;
  14. using Perspex.Styling;
  15. using Xunit;
  16. public class ListBoxTests
  17. {
  18. [Fact]
  19. public void LogicalChildren_Should_Be_Set()
  20. {
  21. var target = new ListBox
  22. {
  23. Template = new ControlTemplate(this.CreateListBoxTemplate),
  24. Items = new[] { "Foo", "Bar", "Baz " },
  25. };
  26. target.ApplyTemplate();
  27. Assert.Equal(3, target.GetLogicalChildren().Count());
  28. foreach (var child in target.GetLogicalChildren())
  29. {
  30. Assert.IsType<ListBoxItem>(child);
  31. }
  32. }
  33. [Fact]
  34. public void Setting_Item_IsSelected_Sets_ListBox_Selection()
  35. {
  36. var target = new ListBox
  37. {
  38. Template = new ControlTemplate(this.CreateListBoxTemplate),
  39. Items = new[] { "Foo", "Bar", "Baz " },
  40. };
  41. target.ApplyTemplate();
  42. ((ListBoxItem)target.GetLogicalChildren().ElementAt(1)).IsSelected = true;
  43. Assert.Equal("Bar", target.SelectedItem);
  44. Assert.Equal(1, target.SelectedIndex);
  45. }
  46. private Control CreateListBoxTemplate(ITemplatedControl parent)
  47. {
  48. return new ScrollViewer
  49. {
  50. Template = new ControlTemplate(this.CreateScrollViewerTemplate),
  51. Content = new ItemsPresenter
  52. {
  53. Name = "itemsPresenter",
  54. [~ItemsPresenter.ItemsProperty] = parent.GetObservable(ListBox.ItemsProperty),
  55. }
  56. };
  57. }
  58. private Control CreateScrollViewerTemplate(ITemplatedControl parent)
  59. {
  60. return new ScrollContentPresenter
  61. {
  62. [~ScrollContentPresenter.ContentProperty] = parent.GetObservable(ScrollViewer.ContentProperty),
  63. };
  64. }
  65. }
  66. }