Browse Source

fix

pull/7243/head
Takoooooo 4 years ago
parent
commit
7fad72cca6
  1. 2
      src/Avalonia.Input/TouchDevice.cs
  2. 348
      tests/Avalonia.Input.UnitTests/TouchDeviceTests.cs

2
src/Avalonia.Input/TouchDevice.cs

@ -61,7 +61,7 @@ namespace Avalonia.Input
var settings = AvaloniaLocator.Current.GetService<IPlatformSettings>(); var settings = AvaloniaLocator.Current.GetService<IPlatformSettings>();
if (settings == null) if (settings == null)
{ {
throw new Exception("IPlatformSettings can not be null"); throw new Exception("IPlatformSettings can not be null.");
} }
if (!_lastClickRect.Contains(args.Position) if (!_lastClickRect.Contains(args.Position)
|| ev.Timestamp - _lastClickTime > settings.DoubleClickTime.TotalMilliseconds) || ev.Timestamp - _lastClickTime > settings.DoubleClickTime.TotalMilliseconds)

348
tests/Avalonia.Input.UnitTests/TouchDeviceTests.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Reactive.Disposables;
using Avalonia.Input.Raw; using Avalonia.Input.Raw;
using Avalonia.Platform; using Avalonia.Platform;
using Avalonia.UnitTests; using Avalonia.UnitTests;
@ -12,56 +13,47 @@ namespace Avalonia.Input.UnitTests
[Fact] [Fact]
public void Tapped_Event_Is_Fired_With_Touch() public void Tapped_Event_Is_Fired_With_Touch()
{ {
using (UnitTestApplication.Start( using var app = UnitTestApp(new TimeSpan(200));
new TestServices(inputManager: new InputManager()))) var root = new TestRoot();
var touchDevice = new TouchDevice();
var isTapped = false;
var executedTimes = 0;
root.Tapped += (a, e) =>
{ {
var root = new TestRoot(); isTapped = true;
var touchDevice = new TouchDevice(); executedTimes++;
};
TapOnce(InputManager.Instance, touchDevice, root);
Assert.True(isTapped);
Assert.Equal(1, executedTimes);
var isTapped = false;
var executedTimes = 0;
root.Tapped += (a, e) =>
{
isTapped = true;
executedTimes++;
};
TapOnce(InputManager.Instance, touchDevice, root);
Assert.True(isTapped);
Assert.Equal(1, executedTimes);
}
} }
[Fact] [Fact]
public void DoubleTapped_Event_Is_Fired_With_Touch() public void DoubleTapped_Event_Is_Fired_With_Touch()
{ {
var platformSettingsMock = new Mock<IPlatformSettings>(); using var app = UnitTestApp(new TimeSpan(200));
platformSettingsMock.Setup(x => x.DoubleClickTime).Returns(new TimeSpan(200)); var root = new TestRoot();
AvaloniaLocator.CurrentMutable.BindToSelf(this) var touchDevice = new TouchDevice();
.Bind<IPlatformSettings>().ToConstant(platformSettingsMock.Object);
using (UnitTestApplication.Start( var isDoubleTapped = false;
new TestServices(inputManager: new InputManager()))) var doubleTappedExecutedTimes = 0;
var tappedExecutedTimes = 0;
root.DoubleTapped += (a, e) =>
{ {
var root = new TestRoot(); isDoubleTapped = true;
var touchDevice = new TouchDevice(); doubleTappedExecutedTimes++;
};
var isDoubleTapped = false; root.Tapped += (a, e) =>
var doubleTappedExecutedTimes = 0; {
var tappedExecutedTimes = 0; tappedExecutedTimes++;
root.DoubleTapped += (a, e) => };
{ TapOnce(InputManager.Instance, touchDevice, root);
isDoubleTapped = true; TapOnce(InputManager.Instance, touchDevice, root, touchPointId: 1);
doubleTappedExecutedTimes++; Assert.Equal(1, tappedExecutedTimes);
}; Assert.True(isDoubleTapped);
root.Tapped += (a, e) => Assert.Equal(1, doubleTappedExecutedTimes);
{
tappedExecutedTimes++;
};
TapOnce(InputManager.Instance, touchDevice, root);
TapOnce(InputManager.Instance, touchDevice, root, touchPointId: 1);
Assert.Equal(1, tappedExecutedTimes);
Assert.True(isDoubleTapped);
Assert.Equal(1, doubleTappedExecutedTimes);
}
} }
[Theory] [Theory]
@ -72,172 +64,156 @@ namespace Avalonia.Input.UnitTests
[InlineData(5)] [InlineData(5)]
public void PointerPressed_Counts_Clicks_Correctly(int clickCount) public void PointerPressed_Counts_Clicks_Correctly(int clickCount)
{ {
var platformSettingsMock = new Mock<IPlatformSettings>(); using var app = UnitTestApp(new TimeSpan(200));
platformSettingsMock.Setup(x => x.DoubleClickTime).Returns(new TimeSpan(200)); var root = new TestRoot();
AvaloniaLocator.CurrentMutable.BindToSelf(this) var touchDevice = new TouchDevice();
.Bind<IPlatformSettings>().ToConstant(platformSettingsMock.Object);
using (UnitTestApplication.Start(
new TestServices(inputManager: new InputManager())))
{
var root = new TestRoot();
var touchDevice = new TouchDevice();
var pointerPressedExecutedTimes = 0; var pointerPressedExecutedTimes = 0;
var pointerPressedClicks = 0; var pointerPressedClicks = 0;
root.PointerPressed += (a, e) => root.PointerPressed += (a, e) =>
{ {
pointerPressedClicks = e.ClickCount; pointerPressedClicks = e.ClickCount;
pointerPressedExecutedTimes++; pointerPressedExecutedTimes++;
}; };
for (int i = 0; i < clickCount; i++) for (int i = 0; i < clickCount; i++)
{ {
TapOnce(InputManager.Instance, touchDevice, root, touchPointId: i); TapOnce(InputManager.Instance, touchDevice, root, touchPointId: i);
}
Assert.Equal(clickCount, pointerPressedExecutedTimes);
Assert.Equal(pointerPressedClicks, clickCount);
} }
Assert.Equal(clickCount, pointerPressedExecutedTimes);
Assert.Equal(pointerPressedClicks, clickCount);
} }
[Fact] [Fact]
public void DoubleTapped_Not_Fired_When_Click_Too_Late() public void DoubleTapped_Not_Fired_When_Click_Too_Late()
{ {
var platformSettingsMock = new Mock<IPlatformSettings>(); using var app = UnitTestApp(new TimeSpan(0, 0, 0, 0, 20));
platformSettingsMock.Setup(x => x.DoubleClickTime).Returns(new TimeSpan(0, 0, 0, 0, 20)); var root = new TestRoot();
AvaloniaLocator.CurrentMutable.BindToSelf(this) var touchDevice = new TouchDevice();
.Bind<IPlatformSettings>().ToConstant(platformSettingsMock.Object);
using (UnitTestApplication.Start( var isDoubleTapped = false;
new TestServices(inputManager: new InputManager()))) var doubleTappedExecutedTimes = 0;
var tappedExecutedTimes = 0;
root.DoubleTapped += (a, e) =>
{
isDoubleTapped = true;
doubleTappedExecutedTimes++;
};
root.Tapped += (a, e) =>
{ {
var root = new TestRoot(); tappedExecutedTimes++;
var touchDevice = new TouchDevice(); };
TapOnce(InputManager.Instance, touchDevice, root);
TapOnce(InputManager.Instance, touchDevice, root, 21, 1);
Assert.Equal(2, tappedExecutedTimes);
Assert.False(isDoubleTapped);
Assert.Equal(0, doubleTappedExecutedTimes);
var isDoubleTapped = false;
var doubleTappedExecutedTimes = 0;
var tappedExecutedTimes = 0;
root.DoubleTapped += (a, e) =>
{
isDoubleTapped = true;
doubleTappedExecutedTimes++;
};
root.Tapped += (a, e) =>
{
tappedExecutedTimes++;
};
TapOnce(InputManager.Instance, touchDevice, root);
TapOnce(InputManager.Instance, touchDevice, root, 21, 1);
Assert.Equal(2, tappedExecutedTimes);
Assert.False(isDoubleTapped);
Assert.Equal(0, doubleTappedExecutedTimes);
}
} }
[Fact] [Fact]
public void DoubleTapped_Not_Fired_When_Second_Click_Is_From_Different_Touch_Contact() public void DoubleTapped_Not_Fired_When_Second_Click_Is_From_Different_Touch_Contact()
{ {
var tmp = new Mock<IPlatformSettings>(); using var app = UnitTestApp(new TimeSpan(200));
tmp.Setup(x => x.DoubleClickTime).Returns(new TimeSpan(200)); var root = new TestRoot();
AvaloniaLocator.CurrentMutable.BindToSelf(this) var touchDevice = new TouchDevice();
.Bind<IPlatformSettings>().ToConstant(tmp.Object);
using (UnitTestApplication.Start( var isDoubleTapped = false;
new TestServices(inputManager: new InputManager()))) var doubleTappedExecutedTimes = 0;
var tappedExecutedTimes = 0;
root.DoubleTapped += (a, e) =>
{ {
var root = new TestRoot(); isDoubleTapped = true;
var touchDevice = new TouchDevice(); doubleTappedExecutedTimes++;
};
var isDoubleTapped = false; root.Tapped += (a, e) =>
var doubleTappedExecutedTimes = 0; {
var tappedExecutedTimes = 0; tappedExecutedTimes++;
root.DoubleTapped += (a, e) => };
{ SendXTouchContactsWithIds(InputManager.Instance, touchDevice, root, RawPointerEventType.TouchBegin, 0, 1);
isDoubleTapped = true; SendXTouchContactsWithIds(InputManager.Instance, touchDevice, root, RawPointerEventType.TouchEnd, 0, 1);
doubleTappedExecutedTimes++; Assert.Equal(2, tappedExecutedTimes);
}; Assert.False(isDoubleTapped);
root.Tapped += (a, e) => Assert.Equal(0, doubleTappedExecutedTimes);
{
tappedExecutedTimes++;
};
SendXTouchContactsWithIds(InputManager.Instance, touchDevice, root, RawPointerEventType.TouchBegin, 0, 1);
SendXTouchContactsWithIds(InputManager.Instance, touchDevice, root, RawPointerEventType.TouchEnd, 0, 1);
Assert.Equal(2, tappedExecutedTimes);
Assert.False(isDoubleTapped);
Assert.Equal(0, doubleTappedExecutedTimes);
}
} }
[Fact] [Fact]
public void Click_Counting_Should_Work_Correctly_With_Few_Touch_Contacts() public void Click_Counting_Should_Work_Correctly_With_Few_Touch_Contacts()
{ {
var tmp = new Mock<IPlatformSettings>(); using var app = UnitTestApp(new TimeSpan(200));
tmp.Setup(x => x.DoubleClickTime).Returns(new TimeSpan(200));
AvaloniaLocator.CurrentMutable.BindToSelf(this)
.Bind<IPlatformSettings>().ToConstant(tmp.Object);
using (UnitTestApplication.Start(
new TestServices(inputManager: new InputManager())))
{
var root = new TestRoot();
var touchDevice = new TouchDevice();
var pointerPressedExecutedTimes = 0; var root = new TestRoot();
var tappedExecutedTimes = 0; var touchDevice = new TouchDevice();
var isDoubleTapped = false;
var doubleTappedExecutedTimes = 0;
root.PointerPressed += (a, e) =>
{
pointerPressedExecutedTimes++;
switch (pointerPressedExecutedTimes)
{
case <= 2:
Assert.True(e.ClickCount == 1);
break;
case 3:
Assert.True(e.ClickCount == 2);
break;
case 4:
Assert.True(e.ClickCount == 3);
break;
case 5:
Assert.True(e.ClickCount == 4);
break;
case 6:
Assert.True(e.ClickCount == 5);
break;
case 7:
Assert.True(e.ClickCount == 1);
break;
case 8:
Assert.True(e.ClickCount == 1);
break;
case 9:
Assert.True(e.ClickCount == 2);
break;
default:
break;
}
};
root.DoubleTapped += (a, e) =>
{
isDoubleTapped = true;
doubleTappedExecutedTimes++;
};
root.Tapped += (a, e) =>
{
tappedExecutedTimes++;
};
SendXTouchContactsWithIds(InputManager.Instance, touchDevice, root, RawPointerEventType.TouchBegin, 0, 1);
SendXTouchContactsWithIds(InputManager.Instance, touchDevice, root, RawPointerEventType.TouchEnd, 0, 1);
TapOnce(InputManager.Instance, touchDevice, root, touchPointId: 2);
TapOnce(InputManager.Instance, touchDevice, root, touchPointId: 3);
TapOnce(InputManager.Instance, touchDevice, root, touchPointId: 4);
SendXTouchContactsWithIds(InputManager.Instance, touchDevice, root, RawPointerEventType.TouchBegin, 5, 6, 7);
SendXTouchContactsWithIds(InputManager.Instance, touchDevice, root, RawPointerEventType.TouchEnd, 5, 6, 7);
TapOnce(InputManager.Instance, touchDevice, root, touchPointId: 8);
Assert.Equal(6, tappedExecutedTimes);
Assert.Equal(9, pointerPressedExecutedTimes);
Assert.True(isDoubleTapped);
Assert.Equal(3, doubleTappedExecutedTimes);
} var pointerPressedExecutedTimes = 0;
var tappedExecutedTimes = 0;
var isDoubleTapped = false;
var doubleTappedExecutedTimes = 0;
root.PointerPressed += (a, e) =>
{
pointerPressedExecutedTimes++;
switch (pointerPressedExecutedTimes)
{
case <= 2:
Assert.True(e.ClickCount == 1);
break;
case 3:
Assert.True(e.ClickCount == 2);
break;
case 4:
Assert.True(e.ClickCount == 3);
break;
case 5:
Assert.True(e.ClickCount == 4);
break;
case 6:
Assert.True(e.ClickCount == 5);
break;
case 7:
Assert.True(e.ClickCount == 1);
break;
case 8:
Assert.True(e.ClickCount == 1);
break;
case 9:
Assert.True(e.ClickCount == 2);
break;
default:
break;
}
};
root.DoubleTapped += (a, e) =>
{
isDoubleTapped = true;
doubleTappedExecutedTimes++;
};
root.Tapped += (a, e) =>
{
tappedExecutedTimes++;
};
SendXTouchContactsWithIds(InputManager.Instance, touchDevice, root, RawPointerEventType.TouchBegin, 0, 1);
SendXTouchContactsWithIds(InputManager.Instance, touchDevice, root, RawPointerEventType.TouchEnd, 0, 1);
TapOnce(InputManager.Instance, touchDevice, root, touchPointId: 2);
TapOnce(InputManager.Instance, touchDevice, root, touchPointId: 3);
TapOnce(InputManager.Instance, touchDevice, root, touchPointId: 4);
SendXTouchContactsWithIds(InputManager.Instance, touchDevice, root, RawPointerEventType.TouchBegin, 5, 6, 7);
SendXTouchContactsWithIds(InputManager.Instance, touchDevice, root, RawPointerEventType.TouchEnd, 5, 6, 7);
TapOnce(InputManager.Instance, touchDevice, root, touchPointId: 8);
Assert.Equal(6, tappedExecutedTimes);
Assert.Equal(9, pointerPressedExecutedTimes);
Assert.True(isDoubleTapped);
Assert.Equal(3, doubleTappedExecutedTimes);
}
private IDisposable UnitTestApp(TimeSpan doubleClickTime = new TimeSpan())
{
var unitTestApp = UnitTestApplication.Start(
new TestServices(inputManager: new InputManager()));
var scope = AvaloniaLocator.EnterScope();
var iSettingsMock = new Mock<IPlatformSettings>();
iSettingsMock.Setup(x => x.DoubleClickTime).Returns(doubleClickTime);
AvaloniaLocator.CurrentMutable.BindToSelf(this)
.Bind<IPlatformSettings>().ToConstant(iSettingsMock.Object);
return new CompositeDisposable(unitTestApp, scope);
} }
private static void SendXTouchContactsWithIds(IInputManager inputManager, TouchDevice device, IInputRoot root, RawPointerEventType type, params long[] touchPointIds) private static void SendXTouchContactsWithIds(IInputManager inputManager, TouchDevice device, IInputRoot root, RawPointerEventType type, params long[] touchPointIds)
{ {

Loading…
Cancel
Save