Преглед на файлове

ViewModelViewHost type resolution assertion

artyom преди 6 години
родител
ревизия
65f5f58a07
променени са 1 файла, в които са добавени 39 реда и са изтрити 5 реда
  1. 39 5
      tests/Avalonia.ReactiveUI.UnitTests/AutoDataTemplateBindingHookTest.cs

+ 39 - 5
tests/Avalonia.ReactiveUI.UnitTests/AutoDataTemplateBindingHookTest.cs

@@ -6,9 +6,12 @@ using ReactiveUI;
 using Avalonia.ReactiveUI;
 using Avalonia.UnitTests;
 using Avalonia.Controls;
+using Avalonia.Controls.Templates;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Linq;
+using Avalonia.VisualTree;
+using Avalonia.Controls.Presenters;
 using Splat;
 
 namespace Avalonia.ReactiveUI.UnitTests
@@ -26,7 +29,7 @@ namespace Avalonia.ReactiveUI.UnitTests
 
         public class ExampleView : ReactiveUserControl<ExampleViewModel>
         {
-            public ListBox List { get; } = new ListBox();
+            public ItemsControl List { get; } = new ItemsControl();
 
             public ExampleView()
             {
@@ -40,17 +43,48 @@ namespace Avalonia.ReactiveUI.UnitTests
         {
             Locator.CurrentMutable.RegisterConstant(new AutoDataTemplateBindingHook(), typeof(IPropertyBindingHook));
             Locator.CurrentMutable.Register(() => new ExampleView(), typeof(IViewFor<ExampleViewModel>));
+            Locator.CurrentMutable.RegisterConstant(new AvaloniaActivationForViewFetcher(), typeof(IActivationForViewFetcher));
         }
 
         [Fact]
         public void Should_Apply_Data_Template_Binding_When_No_Template_Is_Set()
         {
             var view = new ExampleView();
-            var root = new TestRoot 
-            { 
-                Child = view
-            };
             Assert.NotNull(view.List.ItemTemplate);
         }
+
+        [Fact]
+        public void Should_Use_View_Model_View_Host_As_Data_Template()
+        {
+            var view = new ExampleView();
+            view.ViewModel.Items.Add(new NestedViewModel());
+
+            view.List.Template = GetTemplate();
+            view.List.ApplyTemplate();
+            view.List.Presenter.ApplyTemplate();
+
+            var child = view.List.Presenter.Panel.Children[0];
+            var container = (ContentPresenter) child;
+            container.UpdateChild();
+
+            Assert.IsType<ViewModelViewHost>(container.Child);
+        }
+
+        private FuncControlTemplate GetTemplate()
+        {
+            return new FuncControlTemplate<ItemsControl>(parent =>
+            {
+                return new Border
+                {
+                    Background = new Media.SolidColorBrush(0xffffffff),
+                    Child = new ItemsPresenter
+                    {
+                        Name = "PART_ItemsPresenter",
+                        MemberSelector = parent.MemberSelector,
+                        [~ItemsPresenter.ItemsProperty] = parent[~ItemsControl.ItemsProperty],
+                    }
+                };
+            });
+        }
     }
 }