Skip to content

Commit

Permalink
fix: test
Browse files Browse the repository at this point in the history
  • Loading branch information
workgroupengineering committed Mar 17, 2024
1 parent f7ed6a4 commit 5bc5515
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 38 deletions.
101 changes: 77 additions & 24 deletions tests/Avalonia.Controls.UnitTests/TabControlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
using Avalonia.Controls.Utils;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Layout;
using Avalonia.LogicalTree;
using Avalonia.Markup.Xaml;
using Avalonia.Platform;
using Avalonia.Styling;
using Avalonia.UnitTests;
using Moq;
using Xunit;

namespace Avalonia.Controls.UnitTests
Expand Down Expand Up @@ -226,7 +229,7 @@ public void TabItem_Templates_Should_Be_Set_Before_TabItem_ApplyTemplate()
Child = (target = new TabControl
{
Template = TabControlTemplate(),
Items =
Items =
{
new TabItem
{
Expand Down Expand Up @@ -639,7 +642,7 @@ public void TabItem_TabStripPlacement_Should_Be_Correctly_Set()
x => Assert.Equal(Dock.Right, x.TabStripPlacement)
);
}

[Fact]
public void TabItem_TabStripPlacement_Should_Be_Correctly_Set_For_New_Items()
{
Expand All @@ -658,7 +661,7 @@ public void TabItem_TabStripPlacement_Should_Be_Correctly_Set_For_New_Items()
ApplyTemplate(target);

target.ItemsSource = items;

var result = target.GetLogicalChildren()
.OfType<TabItem>()
.ToList();
Expand All @@ -685,28 +688,64 @@ public void TabItem_TabStripPlacement_Should_Be_Correctly_Set_For_New_Items()
[InlineData(Key.D, 0)]
public void Should_TabControl_Recognizes_AccessKey(Key accessKey, int selectedTabIndex)
{

var root = new TestRoot();
var tabControl = new TabControl()
var ah = new AccessKeyHandler();
using (UnitTestApplication.Start(TestServices.StyledWindow.With(accessKeyHandler: ah)))
{
Template = TabControlTemplate(),
Items =
var impl = CreateMockTopLevelImpl();

var tabControl = new TabControl()
{
new TabItem { Header = "General" },
new TabItem { Header = "_Arch" },
new TabItem { Header = "_Leaf" },
new TabItem { Header = "_Disabled", IsEnabled = false },
}
};
Template = TabControlTemplate(),
Items =
{
new TabItem
{
Header = "General",
},
new TabItem { Header = "_Arch" },
new TabItem { Header = "_Leaf"},
new TabItem { Header = "_Disabled", IsEnabled = false },
}
};

root.Child = tabControl;
var root = new TestTopLevel(impl.Object)
{
Template = CreateTemplate(),
Content = tabControl,
};

KeyDown(root, Key.LeftAlt);
KeyDown(root, accessKey, KeyModifiers.Alt);
KeyUp(root, accessKey, KeyModifiers.Alt);
KeyUp(root, Key.LeftAlt);
root.ApplyTemplate();
root.Presenter.UpdateChild();
ApplyTemplate(tabControl);

Assert.Equal(selectedTabIndex, tabControl.SelectedIndex);
KeyDown(root, Key.LeftAlt);
KeyDown(root, accessKey, KeyModifiers.Alt);
KeyUp(root, accessKey, KeyModifiers.Alt);
KeyUp(root, Key.LeftAlt);

Assert.Equal(selectedTabIndex, tabControl.SelectedIndex);
}

static FuncControlTemplate<TestTopLevel> CreateTemplate()
{
return new FuncControlTemplate<TestTopLevel>((x, scope) =>
new ContentPresenter
{
Name = "PART_ContentPresenter",
[~ContentPresenter.ContentProperty] = new TemplateBinding(ContentControl.ContentProperty),
[~ContentPresenter.ContentTemplateProperty] = new TemplateBinding(ContentControl.ContentTemplateProperty)
}.RegisterInNameScope(scope));
}

static Mock<ITopLevelImpl> CreateMockTopLevelImpl(bool setupProperties = false)
{
var topLevel = new Mock<ITopLevelImpl>();
if (setupProperties)
topLevel.SetupAllProperties();
topLevel.Setup(x => x.RenderScaling).Returns(1);
topLevel.Setup(x => x.Compositor).Returns(RendererMocks.CreateDummyCompositor());
return topLevel;
}

static void KeyDown(IInputElement target, Key key, KeyModifiers modifiers = KeyModifiers.None)
{
Expand Down Expand Up @@ -743,8 +782,8 @@ private static IControlTemplate TabControlTemplate()
new ContentPresenter
{
Name = "PART_SelectedContentHost",
[!ContentPresenter.ContentProperty] = parent[!TabControl.SelectedContentProperty],
[!ContentPresenter.ContentTemplateProperty] = parent[!TabControl.SelectedContentTemplateProperty],
[~ContentPresenter.ContentProperty] = new TemplateBinding(TabControl.SelectedContentProperty),
[~ContentPresenter.ContentTemplateProperty] = new TemplateBinding(TabControl.SelectedContentTemplateProperty),
}.RegisterInNameScope(scope)
}
});
Expand All @@ -756,12 +795,26 @@ private static IControlTemplate TabItemTemplate()
new ContentPresenter
{
Name = "PART_ContentPresenter",
[~ContentPresenter.ContentProperty] = new TemplateBinding(TabItem.HeaderProperty),
[~ContentPresenter.ContentTemplateProperty] = new TemplateBinding(TabItem.HeaderTemplateProperty),
RecognizesAccessKey = true,
[!ContentPresenter.ContentProperty] = parent[!TabItem.HeaderProperty],
[!ContentPresenter.ContentTemplateProperty] = parent[!TabItem.HeaderTemplateProperty]
}.RegisterInNameScope(scope));
}

private class TestTopLevel : TopLevel
{
private readonly ILayoutManager _layoutManager;
public bool IsClosed { get; private set; }

public TestTopLevel(ITopLevelImpl impl, ILayoutManager layoutManager = null)
: base(impl)
{
_layoutManager = layoutManager ?? new LayoutManager(this);
}

private protected override ILayoutManager CreateLayoutManager() => _layoutManager;
}

private static void Prepare(TabControl target)
{
ApplyTemplate(target);
Expand Down
19 changes: 10 additions & 9 deletions tests/Avalonia.UnitTests/TestServices.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
using System;
using Moq;
using Avalonia.Input;
using Avalonia.Layout;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.Platform;
using Avalonia.Styling;
using Avalonia.Themes.Simple;
using Avalonia.Rendering;
using System.Reactive.Concurrency;
using System.Collections.Generic;
using Avalonia.Controls;
using System.Reflection;
using Avalonia.Animation;
using Avalonia.Headless;
using Avalonia.Threading;
Expand Down Expand Up @@ -129,18 +123,22 @@ internal TestServices(
IFontManagerImpl fontManagerImpl = null,
ITextShaperImpl textShaperImpl = null,
IWindowImpl windowImpl = null,
IWindowingPlatform windowingPlatform = null) : this(assetLoader, focusManager, inputManager, keyboardDevice,
IWindowingPlatform windowingPlatform = null,
IAccessKeyHandler accessKeyHandler = null
) : this(assetLoader, focusManager, inputManager, keyboardDevice,
keyboardNavigation,
mouseDevice, platform, renderInterface, renderLoop, standardCursorFactory, theme,
dispatcherImpl, fontManagerImpl, textShaperImpl, windowImpl, windowingPlatform)
{
GlobalClock = globalClock;
AccessKeyHandler = accessKeyHandler;
}

public IAssetLoader AssetLoader { get; }
public IInputManager InputManager { get; }
public IFocusManager FocusManager { get; }
internal IGlobalClock GlobalClock { get; set; }
internal IAccessKeyHandler AccessKeyHandler { get; }
public Func<IKeyboardDevice> KeyboardDevice { get; }
public Func<IKeyboardNavigationHandler> KeyboardNavigation { get; }
public Func<IMouseDevice> MouseDevice { get; }
Expand Down Expand Up @@ -172,7 +170,8 @@ internal TestServices With(
ITextShaperImpl textShaperImpl = null,
IWindowImpl windowImpl = null,
IWindowingPlatform windowingPlatform = null,
IGlobalClock globalClock = null)
IGlobalClock globalClock = null,
IAccessKeyHandler accessKeyHandler = null)
{
return new TestServices(
globalClock ?? GlobalClock,
Expand All @@ -190,7 +189,9 @@ internal TestServices With(
theme: theme ?? Theme,
dispatcherImpl: dispatcherImpl ?? DispatcherImpl,
windowingPlatform: windowingPlatform ?? WindowingPlatform,
windowImpl: windowImpl ?? WindowImpl);
windowImpl: windowImpl ?? WindowImpl,
accessKeyHandler: accessKeyHandler ?? AccessKeyHandler
);
}

private static IStyle CreateSimpleTheme()
Expand Down
8 changes: 3 additions & 5 deletions tests/Avalonia.UnitTests/UnitTestApplication.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using System;
using Avalonia.Input;
using Avalonia.Layout;
using Avalonia.Platform;
using Avalonia.Styling;
using Avalonia.Controls;
using Avalonia.Rendering;
using Avalonia.Threading;
using System.Reactive.Disposables;
using System.Reactive.Concurrency;
using System.Threading;
using Avalonia.Input.Platform;
using Avalonia.Animation;
Expand Down Expand Up @@ -78,7 +74,9 @@ public override void RegisterServices()
.Bind<ICursorFactory>().ToConstant(Services.StandardCursorFactory)
.Bind<IWindowingPlatform>().ToConstant(Services.WindowingPlatform)
.Bind<PlatformHotkeyConfiguration>().ToSingleton<PlatformHotkeyConfiguration>()
.Bind<IPlatformSettings>().ToSingleton<DefaultPlatformSettings>();
.Bind<IPlatformSettings>().ToSingleton<DefaultPlatformSettings>()
.Bind<IAccessKeyHandler>().ToConstant(Services.AccessKeyHandler)
;

// This is a hack to make tests work, we need to refactor the way font manager is registered
// See https://github.com/AvaloniaUI/Avalonia/issues/10081
Expand Down

0 comments on commit 5bc5515

Please sign in to comment.