Browse Source

Merge branch 'master' into alloc-boxing

pull/3066/head
Steven Kirk 7 years ago
committed by GitHub
parent
commit
c337a87e1c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 76
      CODE_OF_CONDUCT.md
  2. 1
      src/Avalonia.Animation/Properties/AssemblyInfo.cs
  3. 2
      src/Avalonia.Animation/TransitionInstance.cs
  4. 5
      src/Avalonia.Base/Utilities/MathUtilities.cs
  5. 12
      src/Avalonia.Base/ValueStore.cs
  6. 5
      src/Avalonia.Controls/AppBuilderBase.cs
  7. 9
      src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs
  8. 2
      src/Avalonia.Controls/AutoCompleteBox.cs
  9. 13
      src/Avalonia.Controls/ColumnDefinition.cs
  10. 54
      src/Avalonia.Controls/DefinitionBase.cs
  11. 1
      src/Avalonia.Controls/DesktopApplicationExtensions.cs
  12. 5
      src/Avalonia.Controls/Generators/TabItemContainerGenerator.cs
  13. 12
      src/Avalonia.Controls/Grid.cs
  14. 10
      src/Avalonia.Controls/ItemsControl.cs
  15. 7
      src/Avalonia.Controls/Presenters/IItemsPresenter.cs
  16. 15
      src/Avalonia.Controls/Presenters/ItemsPresenterBase.cs
  17. 2
      src/Avalonia.Controls/Primitives/PopupRoot.cs
  18. 29
      src/Avalonia.Controls/Primitives/SelectingItemsControl.cs
  19. 13
      src/Avalonia.Controls/RowDefinition.cs
  20. 6
      src/Avalonia.Controls/ToolTipService.cs
  21. 2
      src/Avalonia.Controls/WrapPanel.cs
  22. 13
      src/Avalonia.Input/MouseDevice.cs
  23. 2
      src/Avalonia.Input/Pointer.cs
  24. 21
      src/Avalonia.Input/TouchDevice.cs
  25. 1
      src/Avalonia.Native/AvaloniaNativePlatform.cs
  26. 5
      src/Avalonia.Native/WindowImplBase.cs
  27. 8
      src/Avalonia.Themes.Default/ContextMenu.xaml
  28. 29
      src/Avalonia.Themes.Default/MenuItem.xaml
  29. 2
      src/Avalonia.Visuals/Rendering/SceneGraph/SceneBuilder.cs
  30. 2
      src/Avalonia.X11/X11Platform.cs
  31. 10
      src/Avalonia.X11/X11Window.cs
  32. 12
      src/Avalonia.X11/XI2Manager.cs
  33. 8
      src/Windows/Avalonia.Win32/Input/WindowsMouseDevice.cs
  34. 27
      src/Windows/Avalonia.Win32/WindowImpl.cs
  35. 28
      tests/Avalonia.Animation.UnitTests/TransitionsTests.cs
  36. 12
      tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_Validation.cs
  37. 18
      tests/Avalonia.Controls.UnitTests/DesktopStyleApplicationLifetimeTests.cs
  38. 286
      tests/Avalonia.Controls.UnitTests/GridTests.cs
  39. 53
      tests/Avalonia.Controls.UnitTests/ItemsControlTests.cs
  40. 189
      tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs
  41. 20
      tests/Avalonia.Controls.UnitTests/TabControlTests.cs
  42. 109
      tests/Avalonia.Controls.UnitTests/ToolTipTests.cs
  43. 78
      tests/Avalonia.Controls.UnitTests/WrapPanelTests.cs
  44. 39
      tests/Avalonia.Input.UnitTests/PointerTests.cs
  45. 6
      tests/Avalonia.ReactiveUI.UnitTests/AutoSuspendHelperTest.cs
  46. 2
      tests/Avalonia.UnitTests/MouseTestHelper.cs
  47. 11
      tests/Avalonia.UnitTests/TestRoot.cs
  48. 75
      tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs

76
CODE_OF_CONDUCT.md

@ -0,0 +1,76 @@
# Contributor Covenant Code of Conduct
## Our Pledge
In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, sex characteristics, gender identity and expression,
level of experience, education, socio-economic status, nationality, personal
appearance, race, religion, or sexual identity and orientation.
## Our Standards
Examples of behavior that contributes to creating a positive environment
include:
* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
Examples of unacceptable behavior by participants include:
* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
## Our Responsibilities
Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.
Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.
## Scope
This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at steven@avaloniaui.net. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.
Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
[homepage]: https://www.contributor-covenant.org
For answers to common questions about this code of conduct, see
https://www.contributor-covenant.org/faq

1
src/Avalonia.Animation/Properties/AssemblyInfo.cs

@ -10,3 +10,4 @@ using System.Runtime.CompilerServices;
[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Avalonia.Animation.Animators")]
[assembly: InternalsVisibleTo("Avalonia.LeakTests")]
[assembly: InternalsVisibleTo("Avalonia.Animation.UnitTests")]

2
src/Avalonia.Animation/TransitionInstance.cs

@ -28,7 +28,7 @@ namespace Avalonia.Animation
private void TimerTick(TimeSpan t)
{
var interpVal = (double)t.Ticks / _duration.Ticks;
var interpVal = _duration.Ticks == 0 ? 1d : (double)t.Ticks / _duration.Ticks;
// Clamp interpolation value.
if (interpVal >= 1d | interpVal < 0d)

5
src/Avalonia.Base/Utilities/MathUtilities.cs

@ -159,6 +159,11 @@ namespace Avalonia.Utilities
/// <returns>The clamped value.</returns>
public static int Clamp(int val, int min, int max)
{
if (min > max)
{
throw new ArgumentException($"{min} cannot be greater than {max}.");
}
if (val < min)
{
return min;

12
src/Avalonia.Base/ValueStore.cs

@ -57,7 +57,8 @@ namespace Avalonia
{
if (priority == (int)BindingPriority.LocalValue)
{
_propertyValues.SetValue(property, Validate(property, value));
Validate(property, ref value);
_propertyValues.SetValue(property, value);
Changed(property, priority, v, value);
return;
}
@ -78,7 +79,8 @@ namespace Avalonia
if (priority == (int)BindingPriority.LocalValue)
{
_propertyValues.AddValue(property, Validate(property, value));
Validate(property, ref value);
_propertyValues.AddValue(property, value);
Changed(property, priority, AvaloniaProperty.UnsetValue, value);
return;
}
@ -166,16 +168,14 @@ namespace Avalonia
validate2);
}
private object Validate(AvaloniaProperty property, object value)
private void Validate(AvaloniaProperty property, ref object value)
{
var validate = ((IStyledPropertyAccessor)property).GetValidationFunc(_owner.GetType());
if (validate != null && value != AvaloniaProperty.UnsetValue)
{
return validate(_owner, value);
value = validate(_owner, value);
}
return value;
}
private DeferredSetter<T> GetDeferredSetter<T>(AvaloniaProperty property)

5
src/Avalonia.Controls/AppBuilderBase.cs

@ -125,9 +125,8 @@ namespace Avalonia.Controls
});
// Copy-pasted because we can't call extension methods due to generic constraints
var lifetime = new ClassicDesktopStyleApplicationLifetime(Instance) {ShutdownMode = ShutdownMode.OnMainWindowClose};
Instance.ApplicationLifetime = lifetime;
SetupWithoutStarting();
var lifetime = new ClassicDesktopStyleApplicationLifetime() {ShutdownMode = ShutdownMode.OnMainWindowClose};
SetupWithLifetime(lifetime);
lifetime.Start(Array.Empty<string>());
}

9
src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs

@ -5,12 +5,12 @@ using System.Threading;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Interactivity;
using Avalonia.Threading;
namespace Avalonia.Controls.ApplicationLifetimes
{
public class ClassicDesktopStyleApplicationLifetime : IClassicDesktopStyleApplicationLifetime, IDisposable
{
private readonly Application _app;
private int _exitCode;
private CancellationTokenSource _cts;
private bool _isShuttingDown;
@ -34,12 +34,11 @@ namespace Avalonia.Controls.ApplicationLifetimes
_activeLifetime?._windows.Add((Window)sender);
}
public ClassicDesktopStyleApplicationLifetime(Application app)
public ClassicDesktopStyleApplicationLifetime()
{
if (_activeLifetime != null)
throw new InvalidOperationException(
"Can not have multiple active ClassicDesktopStyleApplicationLifetime instances and the previously created one was not disposed");
_app = app;
_activeLifetime = this;
}
@ -103,7 +102,7 @@ namespace Avalonia.Controls.ApplicationLifetimes
Startup?.Invoke(this, new ControlledApplicationLifetimeStartupEventArgs(args));
_cts = new CancellationTokenSource();
MainWindow?.Show();
_app.Run(_cts.Token);
Dispatcher.UIThread.MainLoop(_cts.Token);
Environment.ExitCode = _exitCode;
return _exitCode;
}
@ -124,7 +123,7 @@ namespace Avalonia
this T builder, string[] args, ShutdownMode shutdownMode = ShutdownMode.OnLastWindowClose)
where T : AppBuilderBase<T>, new()
{
var lifetime = new ClassicDesktopStyleApplicationLifetime(builder.Instance) {ShutdownMode = shutdownMode};
var lifetime = new ClassicDesktopStyleApplicationLifetime() {ShutdownMode = shutdownMode};
builder.SetupWithLifetime(lifetime);
return lifetime.Start(args);
}

2
src/Avalonia.Controls/AutoCompleteBox.cs

@ -704,7 +704,7 @@ namespace Avalonia.Controls
added.Add(e.NewValue);
}
OnSelectionChanged(new SelectionChangedEventArgs(SelectionChangedEvent, removed, added));
OnSelectionChanged(new SelectionChangedEventArgs(SelectionChangedEvent, added, removed));
}
/// <summary>

13
src/Avalonia.Controls/ColumnDefinition.cs

@ -26,6 +26,16 @@ namespace Avalonia.Controls
public static readonly StyledProperty<GridLength> WidthProperty =
AvaloniaProperty.Register<ColumnDefinition, GridLength>(nameof(Width), new GridLength(1, GridUnitType.Star));
/// <summary>
/// Initializes static members of the <see cref="ColumnDefinition"/> class.
/// </summary>
static ColumnDefinition()
{
AffectsParentMeasure(MinWidthProperty, MaxWidthProperty);
WidthProperty.Changed.AddClassHandler<DefinitionBase>(OnUserSizePropertyChanged);
}
/// <summary>
/// Initializes a new instance of the <see cref="ColumnDefinition"/> class.
/// </summary>
@ -68,7 +78,6 @@ namespace Avalonia.Controls
}
set
{
Parent?.InvalidateMeasure();
SetValue(MaxWidthProperty, value);
}
}
@ -84,7 +93,6 @@ namespace Avalonia.Controls
}
set
{
Parent?.InvalidateMeasure();
SetValue(MinWidthProperty, value);
}
}
@ -100,7 +108,6 @@ namespace Avalonia.Controls
}
set
{
Parent?.InvalidateMeasure();
SetValue(WidthProperty, value);
}
}

54
src/Avalonia.Controls/DefinitionBase.cs

@ -7,9 +7,6 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using Avalonia;
using Avalonia.Collections;
using Avalonia.Utilities;
namespace Avalonia.Controls
@ -50,6 +47,8 @@ namespace Avalonia.Controls
}
}
}
Parent?.InvalidateMeasure();
}
/// <summary>
@ -63,6 +62,8 @@ namespace Avalonia.Controls
_sharedState.RemoveMember(this);
_sharedState = null;
}
Parent?.InvalidateMeasure();
}
/// <summary>
@ -114,6 +115,36 @@ namespace Avalonia.Controls
}
}
/// <remarks>
/// Notifies parent <see cref="Grid"/> or size scope that definition size has been changed.
/// </remarks>
internal static void OnUserSizePropertyChanged(DefinitionBase definition, AvaloniaPropertyChangedEventArgs e)
{
if (definition.Parent == null)
{
return;
}
if (definition._sharedState != null)
{
definition._sharedState.Invalidate();
}
else
{
GridUnitType oldUnitType = ((GridLength)e.OldValue).GridUnitType;
GridUnitType newUnitType = ((GridLength)e.NewValue).GridUnitType;
if (oldUnitType != newUnitType)
{
definition.Parent.Invalidate();
}
else
{
definition.Parent.InvalidateMeasure();
}
}
}
/// <summary>
/// Returns <c>true</c> if this definition is a part of shared group.
/// </summary>
@ -730,5 +761,22 @@ namespace Avalonia.Controls
SharedSizeGroupProperty.Changed.AddClassHandler<DefinitionBase>(OnSharedSizeGroupPropertyChanged);
PrivateSharedSizeScopeProperty.Changed.AddClassHandler<DefinitionBase>(OnPrivateSharedSizeScopePropertyChanged);
}
/// <summary>
/// Marks a property on a definition as affecting the parent grid's measurement.
/// </summary>
/// <param name="properties">The properties.</param>
protected static void AffectsParentMeasure(params AvaloniaProperty[] properties)
{
void Invalidate(AvaloniaPropertyChangedEventArgs e)
{
(e.Sender as DefinitionBase)?.Parent?.InvalidateMeasure();
}
foreach (var property in properties)
{
property.Changed.Subscribe(Invalidate);
}
}
}
}

1
src/Avalonia.Controls/DesktopApplicationExtensions.cs

@ -50,6 +50,7 @@ namespace Avalonia.Controls
/// On desktop-style platforms runs the application's main loop with custom CancellationToken
/// without setting a lifetime.
/// </summary>
/// <param name="app">The application.</param>
/// <param name="token">The token to track.</param>
public static void Run(this Application app, CancellationToken token)
{

5
src/Avalonia.Controls/Generators/TabItemContainerGenerator.cs

@ -48,11 +48,6 @@ namespace Avalonia.Controls.Generators
tabItem[~ContentControl.ContentTemplateProperty] = Owner[~TabControl.ContentTemplateProperty];
}
if (tabItem.Content == null)
{
tabItem[~ContentControl.ContentProperty] = tabItem[~StyledElement.DataContextProperty];
}
return tabItem;
}
}

12
src/Avalonia.Controls/Grid.cs

@ -6,6 +6,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Threading;
@ -178,6 +179,7 @@ namespace Avalonia.Controls
if (_data == null) { _data = new ExtendedData(); }
_data.ColumnDefinitions = value;
_data.ColumnDefinitions.Parent = this;
InvalidateMeasure();
}
}
@ -198,6 +200,7 @@ namespace Avalonia.Controls
if (_data == null) { _data = new ExtendedData(); }
_data.RowDefinitions = value;
_data.RowDefinitions.Parent = this;
InvalidateMeasure();
}
}
@ -569,6 +572,15 @@ namespace Avalonia.Controls
return (arrangeSize);
}
/// <summary>
/// <see cref="Panel.ChildrenChanged"/>
/// </summary>
protected override void ChildrenChanged(object sender, NotifyCollectionChangedEventArgs e)
{
CellsStructureDirty = true;
base.ChildrenChanged(sender, e);
}
/// <summary>
/// Invalidates grid caches and makes the grid dirty for measure.
/// </summary>

10
src/Avalonia.Controls/ItemsControl.cs

@ -359,6 +359,12 @@ namespace Avalonia.Controls
UpdateItemCount();
RemoveControlItemsFromLogicalChildren(oldValue);
AddControlItemsToLogicalChildren(newValue);
if (Presenter != null)
{
Presenter.Items = newValue;
}
SubscribeToItems(newValue);
}
@ -370,6 +376,8 @@ namespace Avalonia.Controls
/// <param name="e">The event args.</param>
protected virtual void ItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UpdateItemCount();
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
@ -381,7 +389,7 @@ namespace Avalonia.Controls
break;
}
UpdateItemCount();
Presenter?.ItemsChanged(e);
var collection = sender as ICollection;
PseudoClasses.Set(":empty", collection == null || collection.Count == 0);

7
src/Avalonia.Controls/Presenters/IItemsPresenter.cs

@ -1,12 +1,19 @@
// 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;
using System.Collections.Specialized;
namespace Avalonia.Controls.Presenters
{
public interface IItemsPresenter : IPresenter
{
IEnumerable Items { get; set; }
IPanel Panel { get; }
void ItemsChanged(NotifyCollectionChangedEventArgs e);
void ScrollIntoView(object item);
}
}

15
src/Avalonia.Controls/Presenters/ItemsPresenterBase.cs

@ -63,7 +63,7 @@ namespace Avalonia.Controls.Presenters
_itemsSubscription?.Dispose();
_itemsSubscription = null;
if (_createdPanel && value is INotifyCollectionChanged incc)
if (!IsHosted && _createdPanel && value is INotifyCollectionChanged incc)
{
_itemsSubscription = incc.WeakSubscribe(ItemsCollectionChanged);
}
@ -130,6 +130,8 @@ namespace Avalonia.Controls.Presenters
private set;
}
protected bool IsHosted => TemplatedParent is IItemsPresenterHost;
/// <inheritdoc/>
public override sealed void ApplyTemplate()
{
@ -144,6 +146,15 @@ namespace Avalonia.Controls.Presenters
{
}
/// <inheritdoc/>
void IItemsPresenter.ItemsChanged(NotifyCollectionChangedEventArgs e)
{
if (Panel != null)
{
ItemsChanged(e);
}
}
/// <summary>
/// Creates the <see cref="ItemContainerGenerator"/> for the control.
/// </summary>
@ -215,7 +226,7 @@ namespace Avalonia.Controls.Presenters
_createdPanel = true;
if (_itemsSubscription == null && Items is INotifyCollectionChanged incc)
if (!IsHosted && _itemsSubscription == null && Items is INotifyCollectionChanged incc)
{
_itemsSubscription = incc.WeakSubscribe(ItemsCollectionChanged);
}

2
src/Avalonia.Controls/Primitives/PopupRoot.cs

@ -41,6 +41,8 @@ namespace Avalonia.Controls.Primitives
/// <summary>
/// Initializes a new instance of the <see cref="PopupRoot"/> class.
/// </summary>
/// <param name="parent">The popup parent.</param>
/// <param name="impl">The popup implementation.</param>
/// <param name="dependencyResolver">
/// The dependency resolver to use. If null the default dependency resolver will be used.
/// </param>

29
src/Avalonia.Controls/Primitives/SelectingItemsControl.cs

@ -302,13 +302,24 @@ namespace Avalonia.Controls.Primitives
/// <inheritdoc/>
protected override void ItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
base.ItemsCollectionChanged(sender, e);
if (_updateCount > 0)
{
base.ItemsCollectionChanged(sender, e);
return;
}
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
_selection.ItemsInserted(e.NewStartingIndex, e.NewItems.Count);
break;
case NotifyCollectionChangedAction.Remove:
_selection.ItemsRemoved(e.OldStartingIndex, e.OldItems.Count);
break;
}
base.ItemsCollectionChanged(sender, e);
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
@ -318,14 +329,12 @@ namespace Avalonia.Controls.Primitives
}
else
{
_selection.ItemsInserted(e.NewStartingIndex, e.NewItems.Count);
UpdateSelectedItem(_selection.First(), false);
}
break;
case NotifyCollectionChangedAction.Remove:
_selection.ItemsRemoved(e.OldStartingIndex, e.OldItems.Count);
UpdateSelectedItem(_selection.First(), false);
ResetSelectedItems();
break;
@ -1088,9 +1097,15 @@ namespace Avalonia.Controls.Primitives
}
else
{
SelectedIndex = _updateSelectedIndex != int.MinValue ?
_updateSelectedIndex :
AlwaysSelected ? 0 : -1;
if (_updateSelectedIndex != int.MinValue)
{
SelectedIndex = _updateSelectedIndex;
}
if (AlwaysSelected && SelectedIndex == -1)
{
SelectedIndex = 0;
}
}
}
}

13
src/Avalonia.Controls/RowDefinition.cs

@ -26,6 +26,16 @@ namespace Avalonia.Controls
public static readonly StyledProperty<GridLength> HeightProperty =
AvaloniaProperty.Register<RowDefinition, GridLength>(nameof(Height), new GridLength(1, GridUnitType.Star));
/// <summary>
/// Initializes static members of the <see cref="RowDefinition"/> class.
/// </summary>
static RowDefinition()
{
AffectsParentMeasure(MaxHeightProperty, MinHeightProperty);
HeightProperty.Changed.AddClassHandler<DefinitionBase>(OnUserSizePropertyChanged);
}
/// <summary>
/// Initializes a new instance of the <see cref="RowDefinition"/> class.
/// </summary>
@ -68,7 +78,6 @@ namespace Avalonia.Controls
}
set
{
Parent?.InvalidateMeasure();
SetValue(MaxHeightProperty, value);
}
}
@ -84,7 +93,6 @@ namespace Avalonia.Controls
}
set
{
Parent?.InvalidateMeasure();
SetValue(MinHeightProperty, value);
}
}
@ -100,7 +108,6 @@ namespace Avalonia.Controls
}
set
{
Parent?.InvalidateMeasure();
SetValue(HeightProperty, value);
}
}

6
src/Avalonia.Controls/ToolTipService.cs

@ -1,6 +1,7 @@
using System;
using Avalonia.Input;
using Avalonia.Threading;
using Avalonia.VisualTree;
namespace Avalonia.Controls
{
@ -79,7 +80,10 @@ namespace Avalonia.Controls
{
StopTimer();
ToolTip.SetIsOpen(control, true);
if ((control as IVisual).IsAttachedToVisualTree)
{
ToolTip.SetIsOpen(control, true);
}
}
private void Close(Control control)

2
src/Avalonia.Controls/WrapPanel.cs

@ -42,7 +42,7 @@ namespace Avalonia.Controls
/// </summary>
static WrapPanel()
{
AffectsMeasure<WrapPanel>(OrientationProperty);
AffectsMeasure<WrapPanel>(OrientationProperty, ItemWidthProperty, ItemHeightProperty);
}
/// <summary>

13
src/Avalonia.Input/MouseDevice.cs

@ -14,13 +14,14 @@ namespace Avalonia.Input
/// <summary>
/// Represents a mouse device.
/// </summary>
public class MouseDevice : IMouseDevice
public class MouseDevice : IMouseDevice, IDisposable
{
private int _clickCount;
private Rect _lastClickRect;
private ulong _lastClickTime;
private readonly Pointer _pointer;
private bool _disposed;
public MouseDevice(Pointer pointer = null)
{
@ -126,7 +127,9 @@ namespace Avalonia.Input
{
Contract.Requires<ArgumentNullException>(e != null);
var mouse = (IMouseDevice)e.Device;
var mouse = (MouseDevice)e.Device;
if(mouse._disposed)
return;
Position = e.Root.PointToScreen(e.Position);
var props = CreateProperties(e);
@ -441,5 +444,11 @@ namespace Avalonia.Input
el = (IInputElement)el.VisualParent;
}
}
public void Dispose()
{
_disposed = true;
_pointer?.Dispose();
}
}
}

2
src/Avalonia.Input/Pointer.cs

@ -37,7 +37,7 @@ namespace Avalonia.Input
{
if (Captured != null)
Captured.DetachedFromVisualTree -= OnCaptureDetached;
var oldCapture = control;
var oldCapture = Captured;
Captured = control;
PlatformCapture(control);
if (oldCapture != null)

21
src/Avalonia.Input/TouchDevice.cs

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Input.Raw;
@ -11,10 +12,11 @@ namespace Avalonia.Input
/// This class is supposed to be used on per-toplevel basis, don't use a shared one
/// </remarks>
/// </summary>
public class TouchDevice : IInputDevice
public class TouchDevice : IInputDevice, IDisposable
{
Dictionary<long, Pointer> _pointers = new Dictionary<long, Pointer>();
private readonly Dictionary<long, Pointer> _pointers = new Dictionary<long, Pointer>();
private bool _disposed;
KeyModifiers GetKeyModifiers(RawInputModifiers modifiers) =>
(KeyModifiers)(modifiers & RawInputModifiers.KeyboardMask);
@ -28,6 +30,8 @@ namespace Avalonia.Input
public void ProcessRawEvent(RawInputEventArgs ev)
{
if(_disposed)
return;
var args = (RawTouchEventArgs)ev;
if (!_pointers.TryGetValue(args.TouchPointId, out var pointer))
{
@ -82,6 +86,17 @@ namespace Avalonia.Input
}
public void Dispose()
{
if(_disposed)
return;
var values = _pointers.Values.ToList();
_pointers.Clear();
_disposed = true;
foreach (var p in values)
p.Dispose();
}
}
}

1
src/Avalonia.Native/AvaloniaNativePlatform.cs

@ -95,7 +95,6 @@ namespace Avalonia.Native
.Bind<IStandardCursorFactory>().ToConstant(new CursorFactory(_factory.CreateCursorFactory()))
.Bind<IPlatformIconLoader>().ToSingleton<IconLoader>()
.Bind<IKeyboardDevice>().ToConstant(KeyboardDevice)
.Bind<IMouseDevice>().ToConstant(MouseDevice)
.Bind<IPlatformSettings>().ToConstant(this)
.Bind<IWindowingPlatform>().ToConstant(this)
.Bind<IClipboard>().ToConstant(new ClipboardImpl(_factory.CreateClipboard()))

5
src/Avalonia.Native/WindowImplBase.cs

@ -24,7 +24,7 @@ namespace Avalonia.Native
private object _syncRoot = new object();
private bool _deferredRendering = false;
private bool _gpu = false;
private readonly IMouseDevice _mouse;
private readonly MouseDevice _mouse;
private readonly IKeyboardDevice _keyboard;
private readonly IStandardCursorFactory _cursorFactory;
private Size _savedLogicalSize;
@ -38,7 +38,7 @@ namespace Avalonia.Native
_deferredRendering = opts.UseDeferredRendering;
_keyboard = AvaloniaLocator.Current.GetService<IKeyboardDevice>();
_mouse = AvaloniaLocator.Current.GetService<IMouseDevice>();
_mouse = new MouseDevice();
_cursorFactory = AvaloniaLocator.Current.GetService<IStandardCursorFactory>();
}
@ -142,6 +142,7 @@ namespace Avalonia.Native
{
n?.Dispose();
}
_parent._mouse.Dispose();
}
void IAvnWindowBaseEvents.Activated() => _parent.Activated?.Invoke();

8
src/Avalonia.Themes.Default/ContextMenu.xaml

@ -11,19 +11,11 @@
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}">
<ScrollViewer>
<Panel>
<ItemsPresenter Name="PART_ItemsPresenter"
Items="{TemplateBinding Items}"
ItemsPanel="{TemplateBinding ItemsPanel}"
ItemTemplate="{TemplateBinding ItemTemplate}"
KeyboardNavigation.TabNavigation="Continue"/>
<Rectangle Name="iconSeparator"
Fill="{DynamicResource ThemeControlMidBrush}"
HorizontalAlignment="Left"
IsHitTestVisible="False"
Margin="29,2,0,2"
Width="1"/>
</Panel>
</ScrollViewer>
</Border>
</ControlTemplate>

29
src/Avalonia.Themes.Default/MenuItem.xaml

@ -4,14 +4,14 @@
<Style Selector="MenuItem">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Padding" Value="6,0"/>
<Setter Property="Padding" Value="6 0"/>
<Setter Property="Template">
<ControlTemplate>
<Border Name="root"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid ColumnDefinitions="22,13,*,20">
<Grid ColumnDefinitions="20,5,*,20">
<ContentPresenter Name="icon"
Content="{TemplateBinding Icon}"
Width="16"
@ -50,20 +50,12 @@
<Border Background="{TemplateBinding Background}"
BorderBrush="{DynamicResource ThemeBorderMidBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ScrollViewer>
<Panel>
<ScrollViewer>
<ItemsPresenter Name="PART_ItemsPresenter"
Items="{TemplateBinding Items}"
ItemsPanel="{TemplateBinding ItemsPanel}"
ItemTemplate="{TemplateBinding ItemTemplate}"
Margin="2"/>
<Rectangle Name="iconSeparator"
Fill="{DynamicResource ThemeControlMidBrush}"
HorizontalAlignment="Left"
IsHitTestVisible="False"
Margin="29,2,0,2"
Width="1"/>
</Panel>
Margin="4 2"/>
</ScrollViewer>
</Border>
</Popup>
@ -77,13 +69,14 @@
<Setter Property="Template">
<ControlTemplate>
<Separator Background="{DynamicResource ThemeControlMidBrush}"
Margin="29,1,0,1"
Margin="20,1,0,1"
Height="1"/>
</ControlTemplate>
</Setter>
</Style>
<Style Selector="Menu > MenuItem">
<Setter Property="Padding" Value="6 0"/>
<Setter Property="Template">
<ControlTemplate>
<Border Name="root"
@ -108,19 +101,11 @@
BorderBrush="{DynamicResource ThemeBorderMidBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ScrollViewer>
<Panel>
<ItemsPresenter Name="PART_ItemsPresenter"
<ItemsPresenter Name="PART_ItemsPresenter"
Items="{TemplateBinding Items}"
ItemsPanel="{TemplateBinding ItemsPanel}"
ItemTemplate="{TemplateBinding ItemTemplate}"
Margin="2"/>
<Rectangle Name="iconSeparator"
Fill="{DynamicResource ThemeControlMidBrush}"
HorizontalAlignment="Left"
IsHitTestVisible="False"
Margin="29,2,0,2"
Width="1"/>
</Panel>
</ScrollViewer>
</Border>
</Popup>

2
src/Avalonia.Visuals/Rendering/SceneGraph/SceneBuilder.cs

@ -51,7 +51,7 @@ namespace Avalonia.Rendering.SceneGraph
UpdateSize(scene);
}
if (visual.VisualRoot != null)
if (visual.VisualRoot == scene.Root.Visual)
{
if (node?.Parent != null &&
visual.VisualParent != null &&

2
src/Avalonia.X11/X11Platform.cs

@ -19,9 +19,7 @@ namespace Avalonia.X11
class AvaloniaX11Platform : IWindowingPlatform
{
private Lazy<KeyboardDevice> _keyboardDevice = new Lazy<KeyboardDevice>(() => new KeyboardDevice());
private Lazy<MouseDevice> _mouseDevice = new Lazy<MouseDevice>(() => new MouseDevice());
public KeyboardDevice KeyboardDevice => _keyboardDevice.Value;
public MouseDevice MouseDevice => _mouseDevice.Value;
public Dictionary<IntPtr, Action<XEvent>> Windows = new Dictionary<IntPtr, Action<XEvent>>();
public XI2Manager XI2;
public X11Info Info { get; private set; }

10
src/Avalonia.X11/X11Window.cs

@ -32,7 +32,8 @@ namespace Avalonia.X11
private PixelPoint? _configurePoint;
private bool _triggeredExpose;
private IInputRoot _inputRoot;
private readonly IMouseDevice _mouse;
private readonly MouseDevice _mouse;
private readonly TouchDevice _touch;
private readonly IKeyboardDevice _keyboard;
private PixelPoint? _position;
private PixelSize _realSize;
@ -57,7 +58,8 @@ namespace Avalonia.X11
_platform = platform;
_popup = popupParent != null;
_x11 = platform.Info;
_mouse = platform.MouseDevice;
_mouse = new MouseDevice();
_touch = new TouchDevice();
_keyboard = platform.KeyboardDevice;
var glfeature = AvaloniaLocator.Current.GetService<IWindowingPlatformGlFeature>();
@ -702,6 +704,8 @@ namespace Avalonia.X11
_platform.XI2?.OnWindowDestroyed(_handle);
_handle = IntPtr.Zero;
Closed?.Invoke();
_mouse.Dispose();
_touch.Dispose();
}
if (_useRenderWindow && _renderHandle != IntPtr.Zero)
@ -830,6 +834,8 @@ namespace Avalonia.X11
}
public IMouseDevice MouseDevice => _mouse;
public TouchDevice TouchDevice => _touch;
public IPopupImpl CreatePopup()
=> _platform.Options.OverlayPopups ? null : new X11Window(_platform, this);

12
src/Avalonia.X11/XI2Manager.cs

@ -92,8 +92,6 @@ namespace Avalonia.X11
private PointerDeviceInfo _pointerDevice;
private AvaloniaX11Platform _platform;
private readonly TouchDevice _touchDevice = new TouchDevice();
public bool Init(AvaloniaX11Platform platform)
{
@ -198,7 +196,7 @@ namespace Avalonia.X11
(ev.Type == XiEventType.XI_TouchUpdate ?
RawPointerEventType.TouchUpdate :
RawPointerEventType.TouchEnd);
client.ScheduleInput(new RawTouchEventArgs(_touchDevice,
client.ScheduleInput(new RawTouchEventArgs(client.TouchDevice,
ev.Timestamp, client.InputRoot, type, ev.Position, ev.Modifiers, ev.Detail));
return;
}
@ -232,10 +230,10 @@ namespace Avalonia.X11
}
if (scrollDelta != default)
client.ScheduleInput(new RawMouseWheelEventArgs(_platform.MouseDevice, ev.Timestamp,
client.ScheduleInput(new RawMouseWheelEventArgs(client.MouseDevice, ev.Timestamp,
client.InputRoot, ev.Position, scrollDelta, ev.Modifiers));
if (_pointerDevice.HasMotion(ev))
client.ScheduleInput(new RawPointerEventArgs(_platform.MouseDevice, ev.Timestamp, client.InputRoot,
client.ScheduleInput(new RawPointerEventArgs(client.MouseDevice, ev.Timestamp, client.InputRoot,
RawPointerEventType.Move, ev.Position, ev.Modifiers));
}
@ -248,7 +246,7 @@ namespace Avalonia.X11
: ev.Button == 3 ? (down ? RawPointerEventType.RightButtonDown : RawPointerEventType.RightButtonUp)
: (RawPointerEventType?)null;
if (type.HasValue)
client.ScheduleInput(new RawPointerEventArgs(_platform.MouseDevice, ev.Timestamp, client.InputRoot,
client.ScheduleInput(new RawPointerEventArgs(client.MouseDevice, ev.Timestamp, client.InputRoot,
type.Value, ev.Position, ev.Modifiers));
}
@ -310,5 +308,7 @@ namespace Avalonia.X11
{
IInputRoot InputRoot { get; }
void ScheduleInput(RawInputEventArgs args);
IMouseDevice MouseDevice { get; }
TouchDevice TouchDevice { get; }
}
}

8
src/Windows/Avalonia.Win32/Input/WindowsMouseDevice.cs

@ -11,19 +11,11 @@ namespace Avalonia.Win32.Input
{
class WindowsMouseDevice : MouseDevice
{
public static WindowsMouseDevice Instance { get; } = new WindowsMouseDevice();
public WindowsMouseDevice() : base(new WindowsMousePointer())
{
}
public WindowImpl CurrentWindow
{
get;
set;
}
class WindowsMousePointer : Pointer
{
public WindowsMousePointer() : base(Pointer.GetNextFreeId(),PointerType.Mouse, true)

27
src/Windows/Avalonia.Win32/WindowImpl.cs

@ -30,6 +30,7 @@ namespace Avalonia.Win32
private IntPtr _hwnd;
private bool _multitouch;
private TouchDevice _touchDevice = new TouchDevice();
private MouseDevice _mouseDevice = new WindowsMouseDevice();
private IInputRoot _owner;
private ManagedDeferredRendererLock _rendererLock = new ManagedDeferredRendererLock();
private bool _trackingMouse;
@ -205,7 +206,7 @@ namespace Avalonia.Win32
}
}
public IMouseDevice MouseDevice => WindowsMouseDevice.Instance;
public IMouseDevice MouseDevice => _mouseDevice;
public WindowState WindowState
{
@ -333,7 +334,7 @@ namespace Avalonia.Win32
public void BeginMoveDrag(PointerPressedEventArgs e)
{
WindowsMouseDevice.Instance.Capture(null);
_mouseDevice.Capture(null);
UnmanagedMethods.DefWindowProc(_hwnd, (int)UnmanagedMethods.WindowsMessage.WM_NCLBUTTONDOWN,
new IntPtr((int)UnmanagedMethods.HitTestValues.HTCAPTION), IntPtr.Zero);
e.Pointer.Capture(null);
@ -356,7 +357,7 @@ namespace Avalonia.Win32
#if USE_MANAGED_DRAG
_managedDrag.BeginResizeDrag(edge, ScreenToClient(MouseDevice.Position));
#else
WindowsMouseDevice.Instance.Capture(null);
_mouseDevice.Capture(null);
UnmanagedMethods.DefWindowProc(_hwnd, (int)UnmanagedMethods.WindowsMessage.WM_NCLBUTTONDOWN,
new IntPtr((int)EdgeDic[edge]), IntPtr.Zero);
#endif
@ -437,9 +438,7 @@ namespace Avalonia.Win32
uint timestamp = unchecked((uint)UnmanagedMethods.GetMessageTime());
RawInputEventArgs e = null;
WindowsMouseDevice.Instance.CurrentWindow = this;
switch ((UnmanagedMethods.WindowsMessage)msg)
{
case UnmanagedMethods.WindowsMessage.WM_ACTIVATE:
@ -485,6 +484,8 @@ namespace Avalonia.Win32
_parent._disabledBy.Remove(this);
_parent.UpdateEnabled();
}
_mouseDevice.Dispose();
_touchDevice?.Dispose();
//Free other resources
Dispose();
return IntPtr.Zero;
@ -542,7 +543,7 @@ namespace Avalonia.Win32
if(ShouldIgnoreTouchEmulatedMessage())
break;
e = new RawPointerEventArgs(
WindowsMouseDevice.Instance,
_mouseDevice,
timestamp,
_owner,
msg == (int)UnmanagedMethods.WindowsMessage.WM_LBUTTONDOWN
@ -559,7 +560,7 @@ namespace Avalonia.Win32
if(ShouldIgnoreTouchEmulatedMessage())
break;
e = new RawPointerEventArgs(
WindowsMouseDevice.Instance,
_mouseDevice,
timestamp,
_owner,
msg == (int)UnmanagedMethods.WindowsMessage.WM_LBUTTONUP
@ -587,7 +588,7 @@ namespace Avalonia.Win32
}
e = new RawPointerEventArgs(
WindowsMouseDevice.Instance,
_mouseDevice,
timestamp,
_owner,
RawPointerEventType.Move,
@ -597,7 +598,7 @@ namespace Avalonia.Win32
case UnmanagedMethods.WindowsMessage.WM_MOUSEWHEEL:
e = new RawMouseWheelEventArgs(
WindowsMouseDevice.Instance,
_mouseDevice,
timestamp,
_owner,
PointToClient(PointFromLParam(lParam)),
@ -606,7 +607,7 @@ namespace Avalonia.Win32
case UnmanagedMethods.WindowsMessage.WM_MOUSEHWHEEL:
e = new RawMouseWheelEventArgs(
WindowsMouseDevice.Instance,
_mouseDevice,
timestamp,
_owner,
PointToClient(PointFromLParam(lParam)),
@ -616,7 +617,7 @@ namespace Avalonia.Win32
case UnmanagedMethods.WindowsMessage.WM_MOUSELEAVE:
_trackingMouse = false;
e = new RawPointerEventArgs(
WindowsMouseDevice.Instance,
_mouseDevice,
timestamp,
_owner,
RawPointerEventType.LeaveWindow,
@ -627,7 +628,7 @@ namespace Avalonia.Win32
case UnmanagedMethods.WindowsMessage.WM_NCRBUTTONDOWN:
case UnmanagedMethods.WindowsMessage.WM_NCMBUTTONDOWN:
e = new RawPointerEventArgs(
WindowsMouseDevice.Instance,
_mouseDevice,
timestamp,
_owner,
msg == (int)UnmanagedMethods.WindowsMessage.WM_NCLBUTTONDOWN

28
tests/Avalonia.Animation.UnitTests/TransitionsTests.cs

@ -1,14 +1,7 @@
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Avalonia.Animation;
using Avalonia.Controls;
using Avalonia.Styling;
using Avalonia.UnitTests;
using Avalonia.Data;
using Xunit;
using Avalonia.Animation.Easings;
namespace Avalonia.Animation.UnitTests
{
@ -69,5 +62,26 @@ namespace Avalonia.Animation.UnitTests
Assert.Equal(0, border.Opacity);
}
}
[Fact]
public void TransitionInstance_With_Zero_Duration_Is_Completed_On_First_Tick()
{
var clock = new MockGlobalClock();
using (UnitTestApplication.Start(new TestServices(globalClock: clock)))
{
int i = 0;
var inst = new TransitionInstance(clock, TimeSpan.Zero).Subscribe(nextValue =>
{
switch (i++)
{
case 0: Assert.Equal(0, nextValue); break;
case 1: Assert.Equal(1d, nextValue); break;
}
});
clock.Pulse(TimeSpan.FromMilliseconds(10));
}
}
}
}

12
tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_Validation.cs

@ -78,6 +78,18 @@ namespace Avalonia.Base.UnitTests
Assert.Equal(10, target.GetValue(Class1.AttachedProperty));
}
[Fact]
public void PropertyChanged_Event_Uses_Coerced_Value()
{
var inst = new Class1();
inst.PropertyChanged += (sender, e) =>
{
Assert.Equal(10, e.NewValue);
};
inst.SetValue(Class1.QuxProperty, 15);
}
private class Class1 : AvaloniaObject
{
public static readonly StyledProperty<int> QuxProperty =

18
tests/Avalonia.Controls.UnitTests/DesktopStyleApplicationLifetimeTests.cs

@ -16,7 +16,7 @@ namespace Avalonia.Controls.UnitTests
public void Should_Set_ExitCode_After_Shutdown()
{
using (UnitTestApplication.Start(TestServices.MockThreadingInterface))
using(var lifetime = new ClassicDesktopStyleApplicationLifetime(Application.Current))
using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
{
lifetime.Shutdown(1337);
@ -31,7 +31,7 @@ namespace Avalonia.Controls.UnitTests
public void Should_Close_All_Remaining_Open_Windows_After_Explicit_Exit_Call()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
using(var lifetime = new ClassicDesktopStyleApplicationLifetime(Application.Current))
using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
{
var windows = new List<Window> { new Window(), new Window(), new Window(), new Window() };
@ -50,7 +50,7 @@ namespace Avalonia.Controls.UnitTests
public void Should_Only_Exit_On_Explicit_Exit()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
using(var lifetime = new ClassicDesktopStyleApplicationLifetime(Application.Current))
using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
{
lifetime.ShutdownMode = ShutdownMode.OnExplicitShutdown;
@ -84,7 +84,7 @@ namespace Avalonia.Controls.UnitTests
public void Should_Exit_After_MainWindow_Closed()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
using(var lifetime = new ClassicDesktopStyleApplicationLifetime(Application.Current))
using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
{
lifetime.ShutdownMode = ShutdownMode.OnMainWindowClose;
@ -112,7 +112,7 @@ namespace Avalonia.Controls.UnitTests
public void Should_Exit_After_Last_Window_Closed()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
using(var lifetime = new ClassicDesktopStyleApplicationLifetime(Application.Current))
using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
{
lifetime.ShutdownMode = ShutdownMode.OnLastWindowClose;
@ -142,7 +142,7 @@ namespace Avalonia.Controls.UnitTests
public void Show_Should_Add_Window_To_OpenWindows()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
using(var lifetime = new ClassicDesktopStyleApplicationLifetime(Application.Current))
using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
{
var window = new Window();
@ -156,7 +156,7 @@ namespace Avalonia.Controls.UnitTests
public void Window_Should_Be_Added_To_OpenWindows_Only_Once()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
using(var lifetime = new ClassicDesktopStyleApplicationLifetime(Application.Current))
using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
{
var window = new Window();
@ -174,7 +174,7 @@ namespace Avalonia.Controls.UnitTests
public void Close_Should_Remove_Window_From_OpenWindows()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
using(var lifetime = new ClassicDesktopStyleApplicationLifetime(Application.Current))
using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
{
var window = new Window();
@ -197,7 +197,7 @@ namespace Avalonia.Controls.UnitTests
windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object));
using (UnitTestApplication.Start(services))
using(var lifetime = new ClassicDesktopStyleApplicationLifetime(Application.Current))
using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
{
var window = new Window();

286
tests/Avalonia.Controls.UnitTests/GridTests.cs

@ -1,11 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Platform;
using Avalonia.UnitTests;
using Moq;
using Xunit;
using Xunit.Abstractions;
@ -34,7 +29,6 @@ namespace Avalonia.Controls.UnitTests
private Grid CreateGrid(params (string name, GridLength width, double minWidth, double maxWidth)[] columns)
{
var grid = new Grid();
foreach (var k in columns.Select(c => new ColumnDefinition
{
@ -1178,6 +1172,41 @@ namespace Avalonia.Controls.UnitTests
Assert.Equal(0, grids[0].ColumnDefinitions[0].ActualWidth);
}
[Fact]
public void Size_Group_Definition_Resizes_Are_Tracked()
{
var grids = new[] {
CreateGrid(("A", new GridLength(5, GridUnitType.Pixel)), (null, new GridLength())),
CreateGrid(("A", new GridLength(5, GridUnitType.Pixel)), (null, new GridLength())) };
var scope = new Grid();
foreach (var xgrids in grids)
scope.Children.Add(xgrids);
var root = new Grid();
root.UseLayoutRounding = false;
root.SetValue(Grid.IsSharedSizeScopeProperty, true);
root.Children.Add(scope);
root.Measure(new Size(50, 50));
root.Arrange(new Rect(new Point(), new Point(50, 50)));
PrintColumnDefinitions(grids[0]);
Assert.Equal(5, grids[0].ColumnDefinitions[0].ActualWidth);
Assert.Equal(5, grids[1].ColumnDefinitions[0].ActualWidth);
grids[0].ColumnDefinitions[0].Width = new GridLength(10, GridUnitType.Pixel);
foreach (Grid grid in grids)
{
grid.Measure(new Size(50, 50));
grid.Arrange(new Rect(new Point(), new Point(50, 50)));
}
PrintColumnDefinitions(grids[0]);
Assert.Equal(10, grids[0].ColumnDefinitions[0].ActualWidth);
Assert.Equal(10, grids[1].ColumnDefinitions[0].ActualWidth);
}
[Fact]
public void Collection_Changes_Are_Tracked()
{
@ -1270,11 +1299,11 @@ namespace Avalonia.Controls.UnitTests
// grid.Measure(new Size(100, 100));
// grid.Arrange(new Rect(new Point(), new Point(100, 100)));
// PrintColumnDefinitions(grid);
// NOTE: THIS IS BROKEN IN WPF
// all in group are equal to width (MinWidth) of the sizer in the second column
// Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(6 + 1 * 6, cd.ActualWidth));
// NOTE: THIS IS BROKEN IN WPF
// grid.ColumnDefinitions[2].SharedSizeGroup = null;
@ -1382,6 +1411,245 @@ namespace Avalonia.Controls.UnitTests
Assert.Equal(new Size(100, 100), grid.Bounds.Size);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void Changing_Column_Width_Should_Invalidate_Grid(bool setUsingAvaloniaProperty)
{
var grid = new Grid { ColumnDefinitions = ColumnDefinitions.Parse("1*,1*") };
Change_Property_And_Verify_Measure_Requested(grid, () =>
{
if (setUsingAvaloniaProperty)
grid.ColumnDefinitions[0][ColumnDefinition.WidthProperty] = new GridLength(5);
else
grid.ColumnDefinitions[0].Width = new GridLength(5);
});
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void Changing_Column_MinWidth_Should_Invalidate_Grid(bool setUsingAvaloniaProperty)
{
var grid = new Grid { ColumnDefinitions = ColumnDefinitions.Parse("1*,1*") };
Change_Property_And_Verify_Measure_Requested(grid, () =>
{
if (setUsingAvaloniaProperty)
grid.ColumnDefinitions[0][ColumnDefinition.MinWidthProperty] = 5;
else
grid.ColumnDefinitions[0].MinWidth = 5;
});
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void Changing_Column_MaxWidth_Should_Invalidate_Grid(bool setUsingAvaloniaProperty)
{
var grid = new Grid { ColumnDefinitions = ColumnDefinitions.Parse("1*,1*") };
Change_Property_And_Verify_Measure_Requested(grid, () =>
{
if (setUsingAvaloniaProperty)
grid.ColumnDefinitions[0][ColumnDefinition.MaxWidthProperty] = 5;
else
grid.ColumnDefinitions[0].MaxWidth = 5;
});
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void Changing_Row_Height_Should_Invalidate_Grid(bool setUsingAvaloniaProperty)
{
var grid = new Grid { RowDefinitions = RowDefinitions.Parse("1*,1*") };
Change_Property_And_Verify_Measure_Requested(grid, () =>
{
if (setUsingAvaloniaProperty)
grid.RowDefinitions[0][RowDefinition.HeightProperty] = new GridLength(5);
else
grid.RowDefinitions[0].Height = new GridLength(5);
});
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void Changing_Row_MinHeight_Should_Invalidate_Grid(bool setUsingAvaloniaProperty)
{
var grid = new Grid { RowDefinitions = RowDefinitions.Parse("1*,1*") };
Change_Property_And_Verify_Measure_Requested(grid, () =>
{
if (setUsingAvaloniaProperty)
grid.RowDefinitions[0][RowDefinition.MinHeightProperty] = 5;
else
grid.RowDefinitions[0].MinHeight = 5;
});
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void Changing_Row_MaxHeight_Should_Invalidate_Grid(bool setUsingAvaloniaProperty)
{
var grid = new Grid { RowDefinitions = RowDefinitions.Parse("1*,1*") };
Change_Property_And_Verify_Measure_Requested(grid, () =>
{
if (setUsingAvaloniaProperty)
grid.RowDefinitions[0][RowDefinition.MaxHeightProperty] = 5;
else
grid.RowDefinitions[0].MaxHeight = 5;
});
}
[Fact]
public void Adding_Column_Should_Invalidate_Grid()
{
var grid = new Grid { ColumnDefinitions = ColumnDefinitions.Parse("1*,1*") };
Change_Property_And_Verify_Measure_Requested(grid, () =>
{
grid.ColumnDefinitions.Add(new ColumnDefinition(new GridLength(5)));
});
}
[Fact]
public void Adding_Row_Should_Invalidate_Grid()
{
var grid = new Grid { RowDefinitions = RowDefinitions.Parse("1*,1*") };
Change_Property_And_Verify_Measure_Requested(grid, () =>
{
grid.RowDefinitions.Add(new RowDefinition(new GridLength(5)));
});
}
[Fact]
public void Replacing_Columns_Should_Invalidate_Grid()
{
var grid = new Grid { ColumnDefinitions = ColumnDefinitions.Parse("1*,1*") };
Change_Property_And_Verify_Measure_Requested(grid, () =>
{
grid.ColumnDefinitions = ColumnDefinitions.Parse("2*,1*");
});
}
[Fact]
public void Replacing_Rows_Should_Invalidate_Grid()
{
var grid = new Grid { RowDefinitions = RowDefinitions.Parse("1*,1*") };
Change_Property_And_Verify_Measure_Requested(grid, () =>
{
grid.RowDefinitions = RowDefinitions.Parse("2*,1*");
});
}
[Fact]
public void Removing_Column_Should_Invalidate_Grid()
{
var grid = new Grid { ColumnDefinitions = ColumnDefinitions.Parse("1*,1*") };
Change_Property_And_Verify_Measure_Requested(grid, () =>
{
grid.ColumnDefinitions.RemoveAt(0);
});
}
[Fact]
public void Removing_Row_Should_Invalidate_Grid()
{
var grid = new Grid { RowDefinitions = RowDefinitions.Parse("1*,1*") };
Change_Property_And_Verify_Measure_Requested(grid, () =>
{
grid.RowDefinitions.RemoveAt(0);
});
}
[Fact]
public void Removing_Child_Should_Invalidate_Grid_And_Be_Operational()
{
var grid = new Grid { ColumnDefinitions = ColumnDefinitions.Parse("*,Auto") };
grid.Children.Add(new Decorator() { [Grid.ColumnProperty] = 0 });
grid.Children.Add(new Decorator() { Width = 10, Height = 10, [Grid.ColumnProperty] = 1 });
var size = new Size(100, 100);
grid.Measure(size);
grid.Arrange(new Rect(size));
Assert.True(grid.IsMeasureValid);
Assert.True(grid.IsArrangeValid);
Assert.Equal(90, grid.Children[0].Bounds.Width);
Assert.Equal(10, grid.Children[1].Bounds.Width);
grid.Children.RemoveAt(1);
Assert.False(grid.IsMeasureValid);
Assert.False(grid.IsArrangeValid);
grid.Measure(size);
grid.Arrange(new Rect(size));
Assert.True(grid.IsMeasureValid);
Assert.True(grid.IsArrangeValid);
Assert.Equal(100, grid.Children[0].Bounds.Width);
}
[Fact]
public void Adding_Child_Should_Invalidate_Grid_And_Be_Operational()
{
var grid = new Grid { ColumnDefinitions = ColumnDefinitions.Parse("*,Auto") };
grid.Children.Add(new Decorator() { [Grid.ColumnProperty] = 0 });
var size = new Size(100, 100);
grid.Measure(size);
grid.Arrange(new Rect(size));
Assert.True(grid.IsMeasureValid);
Assert.True(grid.IsArrangeValid);
Assert.Equal(100, grid.Children[0].Bounds.Width);
grid.Children.Add(new Decorator() { Width = 10, Height = 10, [Grid.ColumnProperty] = 1 });
Assert.False(grid.IsMeasureValid);
Assert.False(grid.IsArrangeValid);
grid.Measure(size);
grid.Arrange(new Rect(size));
Assert.True(grid.IsMeasureValid);
Assert.True(grid.IsArrangeValid);
Assert.Equal(90, grid.Children[0].Bounds.Width);
Assert.Equal(10, grid.Children[1].Bounds.Width);
}
private static void Change_Property_And_Verify_Measure_Requested(Grid grid, Action change)
{
grid.Measure(new Size(100, 100));
grid.Arrange(new Rect(grid.DesiredSize));
Assert.True(grid.IsMeasureValid);
Assert.True(grid.IsArrangeValid);
change();
Assert.False(grid.IsMeasureValid);
Assert.False(grid.IsArrangeValid);
}
private class TestControl : Control
{
public Size MeasureSize { get; set; }

53
tests/Avalonia.Controls.UnitTests/ItemsControlTests.cs

@ -12,6 +12,7 @@ using Xunit;
using System.Collections.ObjectModel;
using Avalonia.UnitTests;
using Avalonia.Input;
using System.Collections.Generic;
namespace Avalonia.Controls.UnitTests
{
@ -104,6 +105,28 @@ namespace Avalonia.Controls.UnitTests
Assert.Equal(new[] { child }, target.GetLogicalChildren());
}
[Fact]
public void Added_Container_Should_Have_LogicalParent_Set_To_ItemsControl()
{
var item = new Border();
var items = new ObservableCollection<Border>();
var target = new ItemsControl
{
Template = GetTemplate(),
Items = items,
};
var root = new TestRoot(true, target);
root.Measure(new Size(100, 100));
root.Arrange(new Rect(0, 0, 100, 100));
items.Add(item);
Assert.Equal(target, item.Parent);
}
[Fact]
public void Control_Item_Should_Be_Removed_From_Logical_Children_Before_ApplyTemplate()
{
@ -522,6 +545,36 @@ namespace Avalonia.Controls.UnitTests
}
}
[Fact]
public void Presenter_Items_Should_Be_In_Sync()
{
var target = new ItemsControl
{
Template = GetTemplate(),
Items = new object[]
{
new Button(),
new Button(),
},
};
var root = new TestRoot { Child = target };
var otherPanel = new StackPanel();
target.ApplyTemplate();
target.Presenter.ApplyTemplate();
target.ItemContainerGenerator.Materialized += (s, e) =>
{
Assert.IsType<Canvas>(e.Containers[0].Item);
};
target.Items = new[]
{
new Canvas()
};
}
private class Item
{
public Item(string value)

189
tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs

@ -1,6 +1,7 @@
// 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;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
@ -14,6 +15,7 @@ using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Markup.Data;
using Avalonia.Styling;
using Avalonia.UnitTests;
using Moq;
using Xunit;
@ -23,7 +25,7 @@ namespace Avalonia.Controls.UnitTests.Primitives
public class SelectingItemsControlTests
{
private MouseTestHelper _helper = new MouseTestHelper();
[Fact]
public void SelectedIndex_Should_Initially_Be_Minus_1()
{
@ -168,6 +170,130 @@ namespace Avalonia.Controls.UnitTests.Primitives
Assert.Equal("B", listBox.SelectedItem);
}
[Fact]
public void Setting_SelectedIndex_Before_Initialize_Should_Retain()
{
var listBox = new ListBox
{
SelectionMode = SelectionMode.Single,
Items = new[] { "foo", "bar", "baz" },
SelectedIndex = 1
};
listBox.BeginInit();
listBox.EndInit();
Assert.Equal(1, listBox.SelectedIndex);
Assert.Equal("bar", listBox.SelectedItem);
}
[Fact]
public void Setting_SelectedIndex_During_Initialize_Should_Take_Priority_Over_Previous_Value()
{
var listBox = new ListBox
{
SelectionMode = SelectionMode.Single,
Items = new[] { "foo", "bar", "baz" },
SelectedIndex = 2
};
listBox.BeginInit();
listBox.SelectedIndex = 1;
listBox.EndInit();
Assert.Equal(1, listBox.SelectedIndex);
Assert.Equal("bar", listBox.SelectedItem);
}
[Fact]
public void Setting_SelectedItem_Before_Initialize_Should_Retain()
{
var listBox = new ListBox
{
SelectionMode = SelectionMode.Single,
Items = new[] { "foo", "bar", "baz" },
SelectedItem = "bar"
};
listBox.BeginInit();
listBox.EndInit();
Assert.Equal(1, listBox.SelectedIndex);
Assert.Equal("bar", listBox.SelectedItem);
}
[Fact]
public void Setting_SelectedItems_Before_Initialize_Should_Retain()
{
var listBox = new ListBox
{
SelectionMode = SelectionMode.Multiple,
Items = new[] { "foo", "bar", "baz" },
};
var selected = new[] { "foo", "bar" };
foreach (var v in selected)
{
listBox.SelectedItems.Add(v);
}
listBox.BeginInit();
listBox.EndInit();
Assert.Equal(selected, listBox.SelectedItems);
}
[Fact]
public void Setting_SelectedItems_During_Initialize_Should_Take_Priority_Over_Previous_Value()
{
var listBox = new ListBox
{
SelectionMode = SelectionMode.Multiple,
Items = new[] { "foo", "bar", "baz" },
};
var selected = new[] { "foo", "bar" };
foreach (var v in new[] { "bar", "baz" })
{
listBox.SelectedItems.Add(v);
}
listBox.BeginInit();
listBox.SelectedItems = new AvaloniaList<object>(selected);
listBox.EndInit();
Assert.Equal(selected, listBox.SelectedItems);
}
[Fact]
public void Setting_SelectedIndex_Before_Initialize_With_AlwaysSelected_Should_Retain()
{
var listBox = new ListBox
{
SelectionMode = SelectionMode.Single | SelectionMode.AlwaysSelected,
Items = new[] { "foo", "bar", "baz" },
SelectedIndex = 1
};
listBox.BeginInit();
listBox.EndInit();
Assert.Equal(1, listBox.SelectedIndex);
Assert.Equal("bar", listBox.SelectedItem);
}
[Fact]
public void Setting_SelectedIndex_Before_ApplyTemplate_Should_Set_Item_IsSelected_True()
{
@ -849,7 +975,7 @@ namespace Avalonia.Controls.UnitTests.Primitives
var target = new ListBox
{
Template = Template(),
Items = new[] { "Foo", "Bar", "Baz", "Foo", "Bar", "Baz"},
Items = new[] { "Foo", "Bar", "Baz", "Foo", "Bar", "Baz" },
};
target.ApplyTemplate();
@ -980,6 +1106,45 @@ namespace Avalonia.Controls.UnitTests.Primitives
Assert.True(raised);
}
[Fact]
public void AutoScrollToSelectedItem_On_Reset_Works()
{
// Issue #3148
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var items = new ResettingCollection(100);
var target = new ListBox
{
Items = items,
ItemTemplate = new FuncDataTemplate<string>((x, _) =>
new TextBlock
{
Text = x,
Width = 100,
Height = 10
}),
AutoScrollToSelectedItem = true,
VirtualizationMode = ItemVirtualizationMode.Simple,
};
var root = new TestRoot(true, target);
root.Measure(new Size(100, 100));
root.Arrange(new Rect(0, 0, 100, 100));
Assert.True(target.Presenter.Panel.Children.Count > 0);
Assert.True(target.Presenter.Panel.Children.Count < 100);
target.SelectedItem = "Item99";
// #3148 triggered here.
items.Reset(new[] { "Item99" });
Assert.Equal(0, target.SelectedIndex);
Assert.Equal(1, target.Presenter.Panel.Children.Count);
}
}
[Fact]
public void Can_Set_Both_SelectedItem_And_SelectedItems_During_Initialization()
{
@ -1028,6 +1193,7 @@ namespace Avalonia.Controls.UnitTests.Primitives
Name = "itemsPresenter",
[~ItemsPresenter.ItemsProperty] = control[~ItemsControl.ItemsProperty],
[~ItemsPresenter.ItemsPanelProperty] = control[~ItemsControl.ItemsPanelProperty],
[~ItemsPresenter.VirtualizationModeProperty] = control[~ListBox.VirtualizationModeProperty],
}.RegisterInNameScope(scope));
}
@ -1072,5 +1238,24 @@ namespace Avalonia.Controls.UnitTests.Primitives
return base.MoveSelection(direction, wrap);
}
}
private class ResettingCollection : List<string>, INotifyCollectionChanged
{
public ResettingCollection(int itemCount)
{
AddRange(Enumerable.Range(0, itemCount).Select(x => $"Item{x}"));
}
public void Reset(IEnumerable<string> items)
{
Clear();
AddRange(items);
CollectionChanged?.Invoke(
this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
public event NotifyCollectionChangedEventHandler CollectionChanged;
}
}
}

20
tests/Avalonia.Controls.UnitTests/TabControlTests.cs

@ -4,6 +4,7 @@
using System;
using System.Collections.ObjectModel;
using System.Linq;
using Avalonia.Collections;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
@ -287,6 +288,25 @@ namespace Avalonia.Controls.UnitTests
Assert.Single(target.GetLogicalChildren(), content);
}
[Fact]
public void Should_Not_Propagate_DataContext_To_TabItem_Content()
{
var dataContext = "DataContext";
var tabItem = new TabItem();
var target = new TabControl
{
Template = TabControlTemplate(),
DataContext = dataContext,
Items = new AvaloniaList<object> { tabItem }
};
ApplyTemplate(target);
Assert.NotEqual(dataContext, tabItem.Content);
}
private IControlTemplate TabControlTemplate()
{
return new FuncControlTemplate<TabControl>((parent, scope) =>

109
tests/Avalonia.Controls.UnitTests/ToolTipTests.cs

@ -0,0 +1,109 @@
// 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;
using System.Reactive.Disposables;
using Avalonia.Platform;
using Avalonia.Threading;
using Avalonia.UnitTests;
using Avalonia.VisualTree;
using Moq;
using Xunit;
namespace Avalonia.Controls.UnitTests
{
public class TolTipTests
{
private MouseTestHelper _mouseHelper = new MouseTestHelper();
[Fact]
public void Should_Not_Open_On_Detached_Control()
{
//issue #3188
var control = new Decorator()
{
[ToolTip.TipProperty] = "Tip",
[ToolTip.ShowDelayProperty] = 0
};
Assert.False((control as IVisual).IsAttachedToVisualTree);
//here in issue #3188 exception is raised
_mouseHelper.Enter(control);
Assert.False(ToolTip.GetIsOpen(control));
}
[Fact]
public void Should_Open_On_Pointer_Enter()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var window = new Window();
var target = new Decorator()
{
[ToolTip.TipProperty] = "Tip",
[ToolTip.ShowDelayProperty] = 0
};
window.Content = target;
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
Assert.True((target as IVisual).IsAttachedToVisualTree);
_mouseHelper.Enter(target);
Assert.True(ToolTip.GetIsOpen(target));
}
}
[Fact]
public void Should_Open_On_Pointer_Enter_With_Delay()
{
Action timercallback = null;
var delay = TimeSpan.Zero;
var pti = Mock.Of<IPlatformThreadingInterface>(x => x.CurrentThreadIsLoopThread == true);
Mock.Get(pti)
.Setup(v => v.StartTimer(It.IsAny<DispatcherPriority>(), It.IsAny<TimeSpan>(), It.IsAny<Action>()))
.Callback<DispatcherPriority, TimeSpan, Action>((priority, interval, tick) =>
{
delay = interval;
timercallback = tick;
})
.Returns(Disposable.Empty);
using (UnitTestApplication.Start(TestServices.StyledWindow.With(threadingInterface: pti)))
{
var window = new Window();
var target = new Decorator()
{
[ToolTip.TipProperty] = "Tip",
[ToolTip.ShowDelayProperty] = 1
};
window.Content = target;
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
Assert.True((target as IVisual).IsAttachedToVisualTree);
_mouseHelper.Enter(target);
Assert.Equal(TimeSpan.FromMilliseconds(1), delay);
Assert.NotNull(timercallback);
Assert.False(ToolTip.GetIsOpen(target));
timercallback();
Assert.True(ToolTip.GetIsOpen(target));
}
}
}
}

78
tests/Avalonia.Controls.UnitTests/WrapPanelTests.cs

@ -12,14 +12,14 @@ namespace Avalonia.Controls.UnitTests
public void Lays_Out_Horizontally_On_Separate_Lines()
{
var target = new WrapPanel()
{
Width = 100,
Children =
{
Width = 100,
Children =
{
new Border { Height = 50, Width = 100 },
new Border { Height = 50, Width = 100 },
}
};
};
target.Measure(Size.Infinity);
target.Arrange(new Rect(target.DesiredSize));
@ -33,14 +33,14 @@ namespace Avalonia.Controls.UnitTests
public void Lays_Out_Horizontally_On_A_Single_Line()
{
var target = new WrapPanel()
{
Width = 200,
Children =
{
Width = 200,
Children =
{
new Border { Height = 50, Width = 100 },
new Border { Height = 50, Width = 100 },
}
};
};
target.Measure(Size.Infinity);
target.Arrange(new Rect(target.DesiredSize));
@ -54,15 +54,15 @@ namespace Avalonia.Controls.UnitTests
public void Lays_Out_Vertically_Children_On_A_Single_Line()
{
var target = new WrapPanel()
{
Orientation = Orientation.Vertical,
Height = 120,
Children =
{
Orientation = Orientation.Vertical,
Height = 120,
Children =
{
new Border { Height = 50, Width = 100 },
new Border { Height = 50, Width = 100 },
}
};
};
target.Measure(Size.Infinity);
target.Arrange(new Rect(target.DesiredSize));
@ -76,15 +76,15 @@ namespace Avalonia.Controls.UnitTests
public void Lays_Out_Vertically_On_Separate_Lines()
{
var target = new WrapPanel()
{
Orientation = Orientation.Vertical,
Height = 60,
Children =
{
Orientation = Orientation.Vertical,
Height = 60,
Children =
{
new Border { Height = 50, Width = 100 },
new Border { Height = 50, Width = 100 },
}
};
};
target.Measure(Size.Infinity);
target.Arrange(new Rect(target.DesiredSize));
@ -98,17 +98,17 @@ namespace Avalonia.Controls.UnitTests
public void Applies_ItemWidth_And_ItemHeight_Properties()
{
var target = new WrapPanel()
{
Orientation = Orientation.Horizontal,
Width = 50,
ItemWidth = 20,
ItemHeight = 15,
Children =
{
Orientation = Orientation.Horizontal,
Width = 50,
ItemWidth = 20,
ItemHeight = 15,
Children =
{
new Border(),
new Border(),
}
};
};
target.Measure(Size.Infinity);
target.Arrange(new Rect(target.DesiredSize));
@ -117,5 +117,33 @@ namespace Avalonia.Controls.UnitTests
Assert.Equal(new Rect(0, 0, 20, 15), target.Children[0].Bounds);
Assert.Equal(new Rect(20, 0, 20, 15), target.Children[1].Bounds);
}
[Fact]
void ItemWidth_Trigger_InvalidateMeasure()
{
var target = new WrapPanel();
target.Measure(new Size(10, 10));
Assert.True(target.IsMeasureValid);
target.ItemWidth = 1;
Assert.False(target.IsMeasureValid);
}
[Fact]
void ItemHeight_Trigger_InvalidateMeasure()
{
var target = new WrapPanel();
target.Measure(new Size(10, 10));
Assert.True(target.IsMeasureValid);
target.ItemHeight = 1;
Assert.False(target.IsMeasureValid);
}
}
}

39
tests/Avalonia.Input.UnitTests/PointerTests.cs

@ -0,0 +1,39 @@
using System.Collections.Generic;
using System.Linq;
using Avalonia.Controls;
using Avalonia.UnitTests;
using Avalonia.VisualTree;
using Xunit;
namespace Avalonia.Input.UnitTests
{
public class PointerTests
{
[Fact]
public void On_Capture_Transfer_PointerCaptureLost_Should_Propagate_Up_To_The_Common_Parent()
{
Border initialParent, initialCapture, newParent, newCapture;
var el = new StackPanel
{
Children =
{
(initialParent = new Border { Child = initialCapture = new Border() }),
(newParent = new Border { Child = newCapture = new Border() })
}
};
var receivers = new List<object>();
var root = new TestRoot(el);
foreach (InputElement d in root.GetSelfAndVisualDescendants())
d.PointerCaptureLost += (s, e) => receivers.Add(s);
var pointer = new Pointer(Pointer.GetNextFreeId(), PointerType.Mouse, true);
pointer.Capture(initialCapture);
pointer.Capture(newCapture);
Assert.True(receivers.SequenceEqual(new[] { initialCapture, initialParent }));
receivers.Clear();
pointer.Capture(null);
Assert.True(receivers.SequenceEqual(new object[] { newCapture, newParent, el, root }));
}
}
}

6
tests/Avalonia.ReactiveUI.UnitTests/AutoSuspendHelperTest.cs

@ -45,7 +45,7 @@ namespace Avalonia.ReactiveUI.UnitTests
public void AutoSuspendHelper_Should_Immediately_Fire_IsLaunchingNew()
{
using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
using (var lifetime = new ClassicDesktopStyleApplicationLifetime(Application.Current))
using (var lifetime = new ClassicDesktopStyleApplicationLifetime())
{
var isLaunchingReceived = false;
var application = AvaloniaLocator.Current.GetService<Application>();
@ -86,7 +86,7 @@ namespace Avalonia.ReactiveUI.UnitTests
public void ShouldPersistState_Should_Fire_On_App_Exit_When_SuspensionDriver_Is_Initialized()
{
using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
using (var lifetime = new ClassicDesktopStyleApplicationLifetime(Application.Current))
using (var lifetime = new ClassicDesktopStyleApplicationLifetime())
{
var shouldPersistReceived = false;
var application = AvaloniaLocator.Current.GetService<Application>();
@ -105,4 +105,4 @@ namespace Avalonia.ReactiveUI.UnitTests
}
}
}
}
}

2
tests/Avalonia.UnitTests/MouseTestHelper.cs

@ -84,9 +84,9 @@ namespace Avalonia.UnitTests
);
if (ButtonCount(props) == 0)
{
_pointer.Capture(null);
target.RaiseEvent(new PointerReleasedEventArgs(source, _pointer, (IVisual)target, position,
Timestamp(), props, GetModifiers(modifiers), _pressedButton));
_pointer.Capture(null);
}
else
Move(target, source, position);

11
tests/Avalonia.UnitTests/TestRoot.cs

@ -24,8 +24,19 @@ namespace Avalonia.UnitTests
}
public TestRoot(IControl child)
: this(false, child)
{
Child = child;
}
public TestRoot(bool useGlobalStyles, IControl child)
: this()
{
if (useGlobalStyles)
{
StylingParent = UnitTestApplication.Current;
}
Child = child;
}

75
tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs

@ -369,6 +369,81 @@ namespace Avalonia.Visuals.UnitTests.Rendering
}
[Fact]
public void Should_Update_VisualNodes_When_Child_Moved_To_New_Parent_And_New_Root()
{
var dispatcher = new ImmediateDispatcher();
var loop = new Mock<IRenderLoop>();
Decorator moveFrom;
Decorator moveTo;
Canvas moveMe;
var root = new TestRoot
{
Child = new StackPanel
{
Children =
{
(moveFrom = new Decorator
{
Child = moveMe = new Canvas(),
})
}
}
};
var otherRoot = new TestRoot
{
Child = new StackPanel
{
Children =
{
(moveTo = new Decorator())
}
}
};
var sceneBuilder = new SceneBuilder();
var target = new DeferredRenderer(
root,
loop.Object,
sceneBuilder: sceneBuilder,
dispatcher: dispatcher);
var otherSceneBuilder = new SceneBuilder();
var otherTarget = new DeferredRenderer(
otherRoot,
loop.Object,
sceneBuilder: otherSceneBuilder,
dispatcher: dispatcher);
root.Renderer = target;
otherRoot.Renderer = otherTarget;
target.Start();
otherTarget.Start();
RunFrame(target);
RunFrame(otherTarget);
moveFrom.Child = null;
moveTo.Child = moveMe;
RunFrame(target);
RunFrame(otherTarget);
var scene = target.UnitTestScene();
var otherScene = otherTarget.UnitTestScene();
var moveFromNode = (VisualNode)scene.FindNode(moveFrom);
var moveToNode = (VisualNode)otherScene.FindNode(moveTo);
Assert.Empty(moveFromNode.Children);
Assert.Equal(1, moveToNode.Children.Count);
Assert.Same(moveMe, moveToNode.Children[0].Visual);
}
[Fact]
public void Should_Push_Opacity_For_Controls_With_Less_Than_1_Opacity()
{

Loading…
Cancel
Save