AutoDataTemplateBindingHookTest.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 Xunit;
  4. using ReactiveUI;
  5. using Avalonia.ReactiveUI;
  6. using Avalonia.UnitTests;
  7. using Avalonia.Controls;
  8. using Avalonia.Controls.Templates;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Linq;
  12. using Avalonia.VisualTree;
  13. using Avalonia.Controls.Presenters;
  14. using Splat;
  15. namespace Avalonia.ReactiveUI.UnitTests
  16. {
  17. public class AutoDataTemplateBindingHookTest
  18. {
  19. public class NestedViewModel : ReactiveObject { }
  20. public class NestedView : ReactiveUserControl<NestedViewModel> { }
  21. public class ExampleViewModel : ReactiveObject
  22. {
  23. public ObservableCollection<NestedViewModel> Items { get; } = new ObservableCollection<NestedViewModel>();
  24. }
  25. public class ExampleView : ReactiveUserControl<ExampleViewModel>
  26. {
  27. public ItemsControl List { get; } = new ItemsControl();
  28. public ExampleView()
  29. {
  30. Content = List;
  31. ViewModel = new ExampleViewModel();
  32. this.OneWayBind(ViewModel, x => x.Items, x => x.List.Items);
  33. }
  34. }
  35. public AutoDataTemplateBindingHookTest()
  36. {
  37. Locator.CurrentMutable.RegisterConstant(new AutoDataTemplateBindingHook(), typeof(IPropertyBindingHook));
  38. Locator.CurrentMutable.Register(() => new ExampleView(), typeof(IViewFor<ExampleViewModel>));
  39. Locator.CurrentMutable.RegisterConstant(new AvaloniaActivationForViewFetcher(), typeof(IActivationForViewFetcher));
  40. }
  41. [Fact]
  42. public void Should_Apply_Data_Template_Binding_When_No_Template_Is_Set()
  43. {
  44. var view = new ExampleView();
  45. Assert.NotNull(view.List.ItemTemplate);
  46. }
  47. [Fact]
  48. public void Should_Use_View_Model_View_Host_As_Data_Template()
  49. {
  50. var view = new ExampleView();
  51. view.ViewModel.Items.Add(new NestedViewModel());
  52. view.List.Template = GetTemplate();
  53. view.List.ApplyTemplate();
  54. view.List.Presenter.ApplyTemplate();
  55. var child = view.List.Presenter.Panel.Children[0];
  56. var container = (ContentPresenter) child;
  57. container.UpdateChild();
  58. Assert.IsType<ViewModelViewHost>(container.Child);
  59. }
  60. private FuncControlTemplate GetTemplate()
  61. {
  62. return new FuncControlTemplate<ItemsControl>(parent =>
  63. {
  64. return new Border
  65. {
  66. Background = new Media.SolidColorBrush(0xffffffff),
  67. Child = new ItemsPresenter
  68. {
  69. Name = "PART_ItemsPresenter",
  70. MemberSelector = parent.MemberSelector,
  71. [~ItemsPresenter.ItemsProperty] = parent[~ItemsControl.ItemsProperty],
  72. }
  73. };
  74. });
  75. }
  76. }
  77. }