Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
- unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen committed Apr 3, 2020
1 parent b4c8ab4 commit 16f7ec9
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 18 deletions.
63 changes: 62 additions & 1 deletion Xamarin.Forms.Platform.Android.UnitTests/ShellTests.cs
Expand Up @@ -13,10 +13,43 @@
using NUnit.Framework;
using Xamarin.Forms.CustomAttributes;

[assembly: ExportRenderer(typeof(TestShell), typeof(TestShellRenderer))]
namespace Xamarin.Forms.Platform.Android.UnitTests
{
public class ShellTests : PlatformTestFixture
{
[Test, Category("Shell")]
[Description("Flyout Header Changes When Updated")]
public async Task FlyoutHeaderReactsToChanges()
{
var shell = CreateShell();
var initialHeader = new Label() { Text = "Hello" };
var newHeader = new Label() { Text = "Hello part 2" };
shell.FlyoutHeader = initialHeader;
var addedView = await Device.InvokeOnMainThreadAsync(() =>
{
var r = GetRenderer(shell);
var window = (Context.GetActivity()).Window;
(window.DecorView as global::Android.Views.ViewGroup).AddView(r.View);
return r.View;
});

try
{
Assert.IsNotNull(addedView);
Assert.IsNull(newHeader.GetValue(Platform.RendererProperty));
Assert.IsNotNull(initialHeader.GetValue(Platform.RendererProperty));
await Device.InvokeOnMainThreadAsync(() => shell.FlyoutHeader = newHeader);

Assert.IsNotNull(newHeader.GetValue(Platform.RendererProperty), "New Header Not Set Up");
Assert.IsNull(initialHeader.GetValue(Platform.RendererProperty), "Old Header Still Set Up");
}
finally
{
await Device.InvokeOnMainThreadAsync(() => addedView?.RemoveFromParent());
}
}

[Test, Category("Shell")]
[Description("Ensure Default Colors are White for BottomNavigationView")]
public async Task ShellTabColorsDefaultToWhite()
Expand Down Expand Up @@ -47,6 +80,7 @@ public class ShellAppearanceTest : IShellAppearanceElement

public Color EffectiveTabBarUnselectedColor { get; set; }
}

Shell CreateShell()
{
return new Shell()
Expand All @@ -73,4 +107,31 @@ Shell CreateShell()
};
}
}
}

public class TestShell : Shell { }

public class ShellAppearanceTest : IShellAppearanceElement
{
public Color EffectiveTabBarBackgroundColor { get; set; }

public Color EffectiveTabBarDisabledColor { get; set; }

public Color EffectiveTabBarForegroundColor { get; set; }

public Color EffectiveTabBarTitleColor { get; set; }

public Color EffectiveTabBarUnselectedColor { get; set; }
}

public class TestShellRenderer : ShellRenderer
{
protected override IShellFlyoutContentRenderer CreateShellFlyoutContentRenderer()
{
return base.CreateShellFlyoutContentRenderer();
}

public TestShellRenderer(Context context) : base(context)
{
}
}
}
Expand Up @@ -77,21 +77,7 @@ protected virtual void LoadView(IShellContext shellContext)

Profile.FramePartition("Add HeaderView");
_actionBarHeight = (int)context.ToPixels(56);

_flyoutHeader = ((IShellController)shellContext.Shell).FlyoutHeader;
if (_flyoutHeader != null)
_flyoutHeader.MeasureInvalidated += OnFlyoutHeaderMeasureInvalidated;

_headerView = new HeaderContainer(context, _flyoutHeader)
{
MatchWidth = true
};
_headerView.SetMinimumHeight(_actionBarHeight);
_headerView.LayoutParameters = new AppBarLayout.LayoutParams(LP.MatchParent, LP.WrapContent)
{
ScrollFlags = AppBarLayout.LayoutParams.ScrollFlagScroll
};
_appBar.AddView(_headerView);
UpdateFlyoutHeader();

Profile.FramePartition("Recycler.SetAdapter");
_adapter = new ShellFlyoutRecyclerAdapter(shellContext, OnElementSelected);
Expand Down Expand Up @@ -155,6 +141,42 @@ protected virtual void OnShellPropertyChanged(object sender, PropertyChangedEven
UpdateFlyoutBackground();
else if (e.Is(Shell.FlyoutVerticalScrollModeProperty))
UpdateVerticalScrollMode();
else if (e.IsOneOf(
Shell.FlyoutHeaderProperty,
Shell.FlyoutHeaderTemplateProperty))
UpdateFlyoutHeader();
}

void UpdateFlyoutHeader()
{
if(_headerView != null)
{
var oldHeaderView = _headerView;
_headerView = null;
_appBar.RemoveView(oldHeaderView);
oldHeaderView.Dispose();
}

if (_flyoutHeader != null)
{
_flyoutHeader.MeasureInvalidated -= OnFlyoutHeaderMeasureInvalidated;
}

_flyoutHeader = ((IShellController)_shellContext.Shell).FlyoutHeader;
if (_flyoutHeader != null)
_flyoutHeader.MeasureInvalidated += OnFlyoutHeaderMeasureInvalidated;

_headerView = new HeaderContainer(_shellContext.AndroidContext, _flyoutHeader)
{
MatchWidth = true
};
_headerView.SetMinimumHeight(_actionBarHeight);
_headerView.LayoutParameters = new AppBarLayout.LayoutParams(LP.MatchParent, LP.WrapContent)
{
ScrollFlags = AppBarLayout.LayoutParams.ScrollFlagScroll
};
_appBar.AddView(_headerView);
UpdateFlyoutHeaderBehavior();
}

void UpdateVerticalScrollMode()
Expand Down
4 changes: 2 additions & 2 deletions Xamarin.Forms.Platform.Android/Renderers/ShellRenderer.cs
Expand Up @@ -275,7 +275,7 @@ protected virtual void SwitchFragment(FragmentManager manager, AView targetView,
var fragment = _currentRenderer.Fragment;

Profile.FramePartition("Transaction");
FragmentTransaction transaction = manager.BeginTransaction();
FragmentTransaction transaction = manager.BeginTransactionEx();

if (animate)
transaction.SetTransitionEx((int)global::Android.App.FragmentTransit.EnterMask);
Expand Down Expand Up @@ -428,7 +428,7 @@ protected virtual void Dispose(bool disposing)
{
if (_currentRenderer != null && _currentRenderer.Fragment.IsAlive())
{
FragmentTransaction transaction = FragmentManager.BeginTransaction();
FragmentTransaction transaction = FragmentManager.BeginTransactionEx();
transaction.RemoveEx(_currentRenderer.Fragment);
transaction.CommitAllowingStateLossEx();
FragmentManager.ExecutePendingTransactionsEx();
Expand Down

0 comments on commit 16f7ec9

Please sign in to comment.