ToolTipTests.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Reactive.Disposables;
  5. using Avalonia.Platform;
  6. using Avalonia.Threading;
  7. using Avalonia.UnitTests;
  8. using Avalonia.VisualTree;
  9. using Moq;
  10. using Xunit;
  11. namespace Avalonia.Controls.UnitTests
  12. {
  13. public class TolTipTests
  14. {
  15. private MouseTestHelper _mouseHelper = new MouseTestHelper();
  16. [Fact]
  17. public void Should_Not_Open_On_Detached_Control()
  18. {
  19. //issue #3188
  20. var control = new Decorator()
  21. {
  22. [ToolTip.TipProperty] = "Tip",
  23. [ToolTip.ShowDelayProperty] = 0
  24. };
  25. Assert.False((control as IVisual).IsAttachedToVisualTree);
  26. //here in issue #3188 exception is raised
  27. _mouseHelper.Enter(control);
  28. Assert.False(ToolTip.GetIsOpen(control));
  29. }
  30. [Fact]
  31. public void Should_Open_On_Pointer_Enter()
  32. {
  33. using (UnitTestApplication.Start(TestServices.StyledWindow))
  34. {
  35. var window = new Window();
  36. var target = new Decorator()
  37. {
  38. [ToolTip.TipProperty] = "Tip",
  39. [ToolTip.ShowDelayProperty] = 0
  40. };
  41. window.Content = target;
  42. window.ApplyTemplate();
  43. window.Presenter.ApplyTemplate();
  44. Assert.True((target as IVisual).IsAttachedToVisualTree);
  45. _mouseHelper.Enter(target);
  46. Assert.True(ToolTip.GetIsOpen(target));
  47. }
  48. }
  49. [Fact]
  50. public void Should_Open_On_Pointer_Enter_With_Delay()
  51. {
  52. Action timercallback = null;
  53. var delay = TimeSpan.Zero;
  54. var pti = Mock.Of<IPlatformThreadingInterface>(x => x.CurrentThreadIsLoopThread == true);
  55. Mock.Get(pti)
  56. .Setup(v => v.StartTimer(It.IsAny<DispatcherPriority>(), It.IsAny<TimeSpan>(), It.IsAny<Action>()))
  57. .Callback<DispatcherPriority, TimeSpan, Action>((priority, interval, tick) =>
  58. {
  59. delay = interval;
  60. timercallback = tick;
  61. })
  62. .Returns(Disposable.Empty);
  63. using (UnitTestApplication.Start(TestServices.StyledWindow.With(threadingInterface: pti)))
  64. {
  65. var window = new Window();
  66. var target = new Decorator()
  67. {
  68. [ToolTip.TipProperty] = "Tip",
  69. [ToolTip.ShowDelayProperty] = 1
  70. };
  71. window.Content = target;
  72. window.ApplyTemplate();
  73. window.Presenter.ApplyTemplate();
  74. Assert.True((target as IVisual).IsAttachedToVisualTree);
  75. _mouseHelper.Enter(target);
  76. Assert.Equal(TimeSpan.FromMilliseconds(1), delay);
  77. Assert.NotNull(timercallback);
  78. Assert.False(ToolTip.GetIsOpen(target));
  79. timercallback();
  80. Assert.True(ToolTip.GetIsOpen(target));
  81. }
  82. }
  83. }
  84. }