From 8e7d2e5a814f13d4f7cb5258a47a483511aadb9a Mon Sep 17 00:00:00 2001 From: wojciech krysiak Date: Wed, 29 Aug 2018 21:39:51 +0200 Subject: [PATCH 1/6] Handle the case of multiple content presenters within a content control handled via ContentControlMixin --- src/Avalonia.Controls/Mixins/ContentControlMixin.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Avalonia.Controls/Mixins/ContentControlMixin.cs b/src/Avalonia.Controls/Mixins/ContentControlMixin.cs index 95193c0432..e4204bd27f 100644 --- a/src/Avalonia.Controls/Mixins/ContentControlMixin.cs +++ b/src/Avalonia.Controls/Mixins/ContentControlMixin.cs @@ -3,6 +3,7 @@ using System; using System.Linq; +using System.Reactive.Disposables; using System.Runtime.CompilerServices; using Avalonia.Collections; using Avalonia.Controls.Presenters; @@ -75,6 +76,12 @@ namespace Avalonia.Controls.Mixins null, presenter.GetValue(ContentPresenter.ChildProperty)); + if (subscriptions.Value.TryGetValue(sender, out IDisposable previousSubscription)) + { + subscription = new CompositeDisposable(previousSubscription, subscription); + subscriptions.Value.Remove(sender); + } + subscriptions.Value.Add(sender, subscription); } } From a14afe5c2c61ec80fb4689d535a0466bdb75120c Mon Sep 17 00:00:00 2001 From: wojciech krysiak Date: Thu, 30 Aug 2018 19:00:23 +0200 Subject: [PATCH 2/6] Unit tests for the mixin changes --- .../Mixins/ContentControlMixinTests.cs | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 tests/Avalonia.Controls.UnitTests/Mixins/ContentControlMixinTests.cs diff --git a/tests/Avalonia.Controls.UnitTests/Mixins/ContentControlMixinTests.cs b/tests/Avalonia.Controls.UnitTests/Mixins/ContentControlMixinTests.cs new file mode 100644 index 0000000000..a0487842a9 --- /dev/null +++ b/tests/Avalonia.Controls.UnitTests/Mixins/ContentControlMixinTests.cs @@ -0,0 +1,136 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System.Collections.Generic; +using Avalonia.Collections; +using Avalonia.Controls.Mixins; +using Avalonia.Controls.Presenters; +using Avalonia.Controls.Primitives; +using Avalonia.Controls.Templates; +using Avalonia.LogicalTree; +using Moq; +using Xunit; + +namespace Avalonia.Controls.UnitTests.Mixins +{ + public class ContentControlMixinTests + { + [Fact] + public void Multiple_Mixin_Usages_Should_Not_Throw() + { + var target = new TestControl() + { + Template = new FuncControlTemplate(_ => new Panel + { + Children = + { + new ContentPresenter { Name = "Content_1_Presenter" }, + new ContentPresenter { Name = "Content_2_Presenter" } + } + }) + }; + + var ex = Record.Exception(() => target.ApplyTemplate()); + + Assert.Null(ex); + } + + [Fact] + public void Replacing_Template_Releases_Events() + { + var p1 = new ContentPresenter { Name = "Content_1_Presenter" }; + var p2 = new ContentPresenter { Name = "Content_2_Presenter" }; + + var callIndex = -1; + var called = new bool[4]; + + void Callback() + { + if (callIndex >= 0) + called[callIndex] = true; + } + + var listMock = new Mock>(); + listMock.Setup(l => l.Contains(It.IsAny())).Returns(false).Callback(Callback); + var list = listMock.Object; + + var target = new TestControl(list) + { + Template = new FuncControlTemplate(_ => new Panel + { + Children = + { + p1, + p2 + } + }) + }; + target.ApplyTemplate(); + + callIndex = 0; + p1.Content = new Control(); + p1.UpdateChild(); + + callIndex = 1; + p2.Content = new Control(); + p2.UpdateChild(); + + target.Template = null; + + callIndex = 2; + p1.Content = new Control(); + p1.UpdateChild(); + + callIndex = 3; + p2.Content = new Control(); + p2.UpdateChild(); + + + Assert.Equal(new[] { true, true, false, false }, called); + } + + private class TestControl : TemplatedControl + { + public static readonly StyledProperty Content1Property = + AvaloniaProperty.Register(nameof(Content1)); + + public static readonly StyledProperty Content2Property = + AvaloniaProperty.Register(nameof(Content2)); + + + static TestControl() + { + ContentControlMixin.Attach(Content1Property, x => x.GetLogicalChildren(), "Content_1_Presenter"); + ContentControlMixin.Attach(Content2Property, x => x.GetLogicalChildren(), "Content_2_Presenter"); + } + + private IAvaloniaList _mock; + + public TestControl() + { + } + + public TestControl(IAvaloniaList mock) + { + _mock = mock; + } + + public IAvaloniaList GetLogicalChildren() + { + return _mock ?? LogicalChildren; + } + + public object Content1 + { + get { return GetValue(Content1Property); } + set { SetValue(Content1Property, value); } + } + + public object Content2 + { + get { return GetValue(Content2Property); } + set { SetValue(Content2Property, value); } + } + } + } +} From 49f36d4ac60d3ce2adf0e9a0f9a08f95c2eeb2cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Marki=C4=8D?= Date: Sat, 1 Sep 2018 12:56:03 +0200 Subject: [PATCH 3/6] Adds reference to nightly build feed --- readme.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/readme.md b/readme.md index 345ad7fe9b..f345cbd9df 100644 --- a/readme.md +++ b/readme.md @@ -35,6 +35,9 @@ Install-Package Avalonia.Desktop Try out the latest build of Avalonia available for download here: https://ci.appveyor.com/project/AvaloniaUI/Avalonia/branch/master/artifacts +or use nightly build feeds as described here: +https://github.com/AvaloniaUI/Avalonia/wiki/Using-nightly-build-feed + ## Documentation As mentioned above, Avalonia is still in beta and as such there's not much documentation yet. You can take a look at the [getting started page](http://avaloniaui.net/docs/quickstart/) for an overview of how to get started but probably the best thing to do for now is to already know a little bit about WPF/Silverlight/UWP/XAML and ask questions in our [Gitter room](https://gitter.im/AvaloniaUI/Avalonia). From f797c1d6c10832811632117e6a0d70fcfdf6e2a2 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Sat, 1 Sep 2018 09:17:54 -0700 Subject: [PATCH 4/6] Fix Avalonia.Android output path in packages.cake. VS 15.8 changed the output directory for Android projects. We need this change to match the new behavior. --- packages.cake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages.cake b/packages.cake index d633230189..9defa3004c 100644 --- a/packages.cake +++ b/packages.cake @@ -303,7 +303,7 @@ public class Packages { new NuSpecContent { Source = "Avalonia.Android.dll", Target = "lib/MonoAndroid10" } }, - BasePath = context.Directory("./src/Android/Avalonia.Android/bin/" + parameters.DirSuffix + "/monoandroid44/"), + BasePath = context.Directory("./src/Android/Avalonia.Android/bin/" + parameters.DirSuffix + "/monoandroid44/MonoAndroid44/"), OutputDirectory = parameters.NugetRoot }, /////////////////////////////////////////////////////////////////////////////// From 01e1835ad884b4c85cca5f31c167876715099032 Mon Sep 17 00:00:00 2001 From: wojciech krysiak Date: Sun, 2 Sep 2018 23:03:21 +0200 Subject: [PATCH 5/6] Corrected test implementation --- .../Mixins/ContentControlMixinTests.cs | 60 ++++++------------- 1 file changed, 18 insertions(+), 42 deletions(-) diff --git a/tests/Avalonia.Controls.UnitTests/Mixins/ContentControlMixinTests.cs b/tests/Avalonia.Controls.UnitTests/Mixins/ContentControlMixinTests.cs index a0487842a9..71c396b2c6 100644 --- a/tests/Avalonia.Controls.UnitTests/Mixins/ContentControlMixinTests.cs +++ b/tests/Avalonia.Controls.UnitTests/Mixins/ContentControlMixinTests.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. See licence.md file in the project root for full license information. using System.Collections.Generic; +using System.Linq; using Avalonia.Collections; using Avalonia.Controls.Mixins; using Avalonia.Controls.Presenters; @@ -41,20 +42,10 @@ namespace Avalonia.Controls.UnitTests.Mixins var p1 = new ContentPresenter { Name = "Content_1_Presenter" }; var p2 = new ContentPresenter { Name = "Content_2_Presenter" }; - var callIndex = -1; - var called = new bool[4]; + var itemsAddedThroughMixin = new List(); + var itemsNotAddedThroughMixin = new List(); - void Callback() - { - if (callIndex >= 0) - called[callIndex] = true; - } - - var listMock = new Mock>(); - listMock.Setup(l => l.Contains(It.IsAny())).Returns(false).Callback(Callback); - var list = listMock.Object; - - var target = new TestControl(list) + var target = new TestControl { Template = new FuncControlTemplate(_ => new Panel { @@ -67,26 +58,28 @@ namespace Avalonia.Controls.UnitTests.Mixins }; target.ApplyTemplate(); - callIndex = 0; - p1.Content = new Control(); + Control tc; + + p1.Content = tc = new Control(); p1.UpdateChild(); + itemsAddedThroughMixin.Add(target.GetLogicalChildren().Contains(tc)); - callIndex = 1; - p2.Content = new Control(); + p2.Content = tc = new Control(); p2.UpdateChild(); + itemsAddedThroughMixin.Add(target.GetLogicalChildren().Contains(tc)); target.Template = null; - callIndex = 2; - p1.Content = new Control(); + p1.Content = tc = new Control(); p1.UpdateChild(); + itemsNotAddedThroughMixin.Add(target.GetLogicalChildren().Contains(tc)); - callIndex = 3; - p2.Content = new Control(); + p2.Content = tc = new Control(); p2.UpdateChild(); + itemsNotAddedThroughMixin.Add(target.GetLogicalChildren().Contains(tc)); - - Assert.Equal(new[] { true, true, false, false }, called); + Assert.Equal(new[] { true, true }, itemsAddedThroughMixin); + Assert.Equal(new[] { false, false }, itemsNotAddedThroughMixin); } private class TestControl : TemplatedControl @@ -97,27 +90,10 @@ namespace Avalonia.Controls.UnitTests.Mixins public static readonly StyledProperty Content2Property = AvaloniaProperty.Register(nameof(Content2)); - static TestControl() { - ContentControlMixin.Attach(Content1Property, x => x.GetLogicalChildren(), "Content_1_Presenter"); - ContentControlMixin.Attach(Content2Property, x => x.GetLogicalChildren(), "Content_2_Presenter"); - } - - private IAvaloniaList _mock; - - public TestControl() - { - } - - public TestControl(IAvaloniaList mock) - { - _mock = mock; - } - - public IAvaloniaList GetLogicalChildren() - { - return _mock ?? LogicalChildren; + ContentControlMixin.Attach(Content1Property, x => x.LogicalChildren, "Content_1_Presenter"); + ContentControlMixin.Attach(Content2Property, x => x.LogicalChildren, "Content_2_Presenter"); } public object Content1 From 1b82998775a597e6896eba1b66a0ad6be5c35cfa Mon Sep 17 00:00:00 2001 From: wojciech krysiak Date: Sun, 2 Sep 2018 23:24:50 +0200 Subject: [PATCH 6/6] Made test more readable --- .../Mixins/ContentControlMixinTests.cs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/tests/Avalonia.Controls.UnitTests/Mixins/ContentControlMixinTests.cs b/tests/Avalonia.Controls.UnitTests/Mixins/ContentControlMixinTests.cs index 71c396b2c6..f06553411c 100644 --- a/tests/Avalonia.Controls.UnitTests/Mixins/ContentControlMixinTests.cs +++ b/tests/Avalonia.Controls.UnitTests/Mixins/ContentControlMixinTests.cs @@ -41,10 +41,6 @@ namespace Avalonia.Controls.UnitTests.Mixins { var p1 = new ContentPresenter { Name = "Content_1_Presenter" }; var p2 = new ContentPresenter { Name = "Content_2_Presenter" }; - - var itemsAddedThroughMixin = new List(); - var itemsNotAddedThroughMixin = new List(); - var target = new TestControl { Template = new FuncControlTemplate(_ => new Panel @@ -62,24 +58,22 @@ namespace Avalonia.Controls.UnitTests.Mixins p1.Content = tc = new Control(); p1.UpdateChild(); - itemsAddedThroughMixin.Add(target.GetLogicalChildren().Contains(tc)); + Assert.Contains(tc, target.GetLogicalChildren()); p2.Content = tc = new Control(); p2.UpdateChild(); - itemsAddedThroughMixin.Add(target.GetLogicalChildren().Contains(tc)); + Assert.Contains(tc, target.GetLogicalChildren()); target.Template = null; p1.Content = tc = new Control(); p1.UpdateChild(); - itemsNotAddedThroughMixin.Add(target.GetLogicalChildren().Contains(tc)); + Assert.DoesNotContain(tc, target.GetLogicalChildren()); p2.Content = tc = new Control(); p2.UpdateChild(); - itemsNotAddedThroughMixin.Add(target.GetLogicalChildren().Contains(tc)); + Assert.DoesNotContain(tc, target.GetLogicalChildren()); - Assert.Equal(new[] { true, true }, itemsAddedThroughMixin); - Assert.Equal(new[] { false, false }, itemsNotAddedThroughMixin); } private class TestControl : TemplatedControl