Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Applying best practice #871

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
325 changes: 227 additions & 98 deletions .editorconfig

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions src/Caliburn.Micro.Core.Tests/Caliburn.Micro.Core.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
<Project Sdk="MSBuild.Sdk.Extras">

<!--
CA1707 // Identifiers should not contain underscores
SA0001 // XML comment analysis is disabled due to project configuration
xUnit1004 // Test methods should not be skipped
-->

<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<IsPackable>false</IsPackable>
<NoWarn>$(NoWarn);CA1707;SA0001;xUnit1004</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="Moq" Version="4.15.2" />
<PackageReference Update="Nerdbank.GitVersioning" Version="3.3.37" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Moq" Version="4.15.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Caliburn.Micro.Core\Caliburn.Micro.Core.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="Nerdbank.GitVersioning" Version="3.3.37" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,27 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using Caliburn.Micro.Core;
using Xunit;

namespace Caliburn.Micro.Core.Tests
{
public class ConductWithTests
{
namespace Caliburn.Micro.Core.Tests {
public class ConductWithTests {
[Fact]
public async Task Screen_ConductWithTests()
{
public async Task Screen_ConductWithTests() {
var root = new Screen();

var child1 = new StateScreen
{
DisplayName = "screen1"
var child1 = new StateScreen {
DisplayName = "screen1",
};
// simulate a long deactivation process
var child2 = new StateScreen(TimeSpan.FromSeconds(3))
{
DisplayName = "screen2"

// simulate a long deactivation process
var child2 = new StateScreen(TimeSpan.FromSeconds(3)) {
DisplayName = "screen2",
};

var child3 = new StateScreen()
{
DisplayName = "screen3"
var child3 = new StateScreen() {
DisplayName = "screen3",
};

child1.ConductWith(root);
Expand All @@ -49,23 +45,19 @@ public async Task Screen_ConductWithTests()
}

[Fact]
public async Task Conductor_ConductWithTests()
{
public async Task Conductor_ConductWithTests() {
var root = new Conductor<StateScreen>.Collection.AllActive();

var child1 = new StateScreen
{
DisplayName = "screen1"
var child1 = new StateScreen {
DisplayName = "screen1",
};
var child2 = new StateScreen(TimeSpan.FromSeconds(3))
{
var child2 = new StateScreen(TimeSpan.FromSeconds(3)) {
DisplayName = "screen2",
IsClosable = false,
};

var child3 = new StateScreen()
{
DisplayName = "screen3"
var child3 = new StateScreen() {
DisplayName = "screen3",
};

root.Items.Add(child1);
Expand All @@ -85,30 +77,25 @@ public async Task Conductor_ConductWithTests()
Assert.True(child3.IsClosed, "child 3 should be closed");
}

class StateScreen : Screen
{
public StateScreen()
{
private sealed class StateScreen : Screen {
private readonly TimeSpan? deactivationDelay;

public StateScreen() {
}

public StateScreen(TimeSpan? deactivationDelay)
{
this.deactivationDelay = deactivationDelay;
}
=> this.deactivationDelay = deactivationDelay;

public bool WasActivated { get; private set; }

public bool IsClosed { get; private set; }

public bool IsClosable { get; set; }

public override Task<bool> CanCloseAsync(CancellationToken cancellationToken = default)
{
return Task.FromResult(IsClosable);
}
public override Task<bool> CanCloseAsync(CancellationToken cancellationToken = default) => Task.FromResult(IsClosable);

protected override async Task OnActivateAsync(CancellationToken cancellationToken)
{
if (deactivationDelay.HasValue)
{
protected override async Task OnActivateAsync(CancellationToken cancellationToken) {
if (deactivationDelay.HasValue) {
await Task.Delay(deactivationDelay.Value, cancellationToken).ConfigureAwait(false);
}

Expand All @@ -118,19 +105,15 @@ protected override async Task OnActivateAsync(CancellationToken cancellationToke
IsClosable = false;
}

protected override async Task OnDeactivateAsync(bool close, CancellationToken cancellationToken = default(CancellationToken))
{
protected override async Task OnDeactivateAsync(bool close, CancellationToken cancellationToken = default(CancellationToken)) {
await base.OnDeactivateAsync(close, cancellationToken).ConfigureAwait(false);

if (deactivationDelay.HasValue)
{
if (deactivationDelay.HasValue) {
await Task.Delay(deactivationDelay.Value, cancellationToken).ConfigureAwait(false);
}

IsClosed = close;
}

private readonly TimeSpan? deactivationDelay;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,77 +1,52 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Caliburn.Micro.Core.Tests
{
public class ConductorWithCollectionOneActiveTests
{
private class StateScreen : Screen
{
public bool IsClosed { get; private set; }
public bool IsClosable { get; set; }

public override Task<bool> CanCloseAsync(CancellationToken cancellationToken)
{
return Task.FromResult(IsClosable);
}

protected override async Task OnDeactivateAsync(bool close, CancellationToken cancellationToken)
{
await base.OnDeactivateAsync(close, cancellationToken);
IsClosed = close;
}
}
using Xunit;

namespace Caliburn.Micro.Core.Tests {
public class ConductorWithCollectionOneActiveTests {
[Fact]
public void AddedItemAppearsInChildren()
{
public void AddedItemAppearsInChildren() {
var conductor = new Conductor<IScreen>.Collection.OneActive();
var conducted = new Screen();
conductor.Items.Add(conducted);
Assert.Contains(conducted, conductor.GetChildren());
}

[Fact]
public async Task CanCloseIsTrueWhenItemsAreClosable()
{
public async Task CanCloseIsTrueWhenItemsAreClosable() {
var conductor = new Conductor<IScreen>.Collection.OneActive();
var conducted = new StateScreen
{
IsClosable = true
var conducted = new StateScreen {
IsClosable = true,
};
conductor.Items.Add(conducted);

await ((IActivate)conductor).ActivateAsync(CancellationToken.None);
var canClose =await conductor.CanCloseAsync(CancellationToken.None);
bool canClose = await conductor.CanCloseAsync(CancellationToken.None);

Assert.True(canClose);
Assert.False(conducted.IsClosed);
}

[Fact(Skip = "Investigating close issue. http://caliburnmicro.codeplex.com/discussions/275824")]
public async Task CanCloseIsTrueWhenItemsAreNotClosableAndCloseStrategyCloses()
{
var conductor = new Conductor<IScreen>.Collection.OneActive
{
CloseStrategy = new DefaultCloseStrategy<IScreen>(true)
public async Task CanCloseIsTrueWhenItemsAreNotClosableAndCloseStrategyCloses() {
var conductor = new Conductor<IScreen>.Collection.OneActive {
CloseStrategy = new DefaultCloseStrategy<IScreen>(true),
};
var conducted = new StateScreen
{
IsClosable = true
var conducted = new StateScreen {
IsClosable = true,
};
conductor.Items.Add(conducted);
await((IActivate)conductor).ActivateAsync(CancellationToken.None);
var canClose = await conductor.CanCloseAsync(CancellationToken.None);
await ((IActivate)conductor).ActivateAsync(CancellationToken.None);
bool canClose = await conductor.CanCloseAsync(CancellationToken.None);

Assert.True(canClose);
Assert.True(conducted.IsClosed);
}

[Fact]
public async Task ChildrenAreActivatedIfConductorIsActive()
{
public async Task ChildrenAreActivatedIfConductorIsActive() {
var conductor = new Conductor<IScreen>.Collection.OneActive();
var conducted = new Screen();
conductor.Items.Add(conducted);
Expand All @@ -82,8 +57,7 @@ public async Task ChildrenAreActivatedIfConductorIsActive()
}

[Fact(Skip = "ActiveItem currently set regardless of IsActive value. See http://caliburnmicro.codeplex.com/discussions/276375")]
public async Task ChildrenAreNotActivatedIfConductorIsNotActive()
{
public async Task ChildrenAreNotActivatedIfConductorIsNotActive() {
var conductor = new Conductor<IScreen>.Collection.OneActive();
var conducted = new Screen();
conductor.Items.Add(conducted);
Expand All @@ -93,24 +67,21 @@ public async Task ChildrenAreNotActivatedIfConductorIsNotActive()
}

[Fact(Skip = "Behavior currently allowed; under investigation. See http://caliburnmicro.codeplex.com/discussions/276373")]
public void ConductorCannotConductSelf()
{
public void ConductorCannotConductSelf() {
var conductor = new Conductor<IScreen>.Collection.OneActive();
Assert.Throws<InvalidOperationException>(() => conductor.Items.Add(conductor));
}

[Fact]
public void ParentItemIsSetOnAddedConductedItem()
{
public void ParentItemIsSetOnAddedConductedItem() {
var conductor = new Conductor<IScreen>.Collection.OneActive();
var conducted = new Screen();
conductor.Items.Add(conducted);
Assert.Equal(conductor, conducted.Parent);
}

[Fact]
public void ParentItemIsSetOnReplacedConductedItem()
{
public void ParentItemIsSetOnReplacedConductedItem() {
var conductor = new Conductor<IScreen>.Collection.OneActive();
var originalConducted = new Screen();
conductor.Items.Add(originalConducted);
Expand All @@ -120,8 +91,7 @@ public void ParentItemIsSetOnReplacedConductedItem()
}

[Fact(Skip = "This is not possible as we don't get the removed items in the event handler.")]
public void ParentItemIsUnsetOnClear()
{
public void ParentItemIsUnsetOnClear() {
var conductor = new Conductor<IScreen>.Collection.OneActive();
var conducted = new Screen();
conductor.Items.Add(conducted);
Expand All @@ -130,8 +100,7 @@ public void ParentItemIsUnsetOnClear()
}

[Fact]
public void ParentItemIsUnsetOnRemovedConductedItem()
{
public void ParentItemIsUnsetOnRemovedConductedItem() {
var conductor = new Conductor<IScreen>.Collection.OneActive();
var conducted = new Screen();
conductor.Items.Add(conducted);
Expand All @@ -140,8 +109,7 @@ public void ParentItemIsUnsetOnRemovedConductedItem()
}

[Fact]
public void ParentItemIsUnsetOnReplaceConductedItem()
{
public void ParentItemIsUnsetOnReplaceConductedItem() {
var conductor = new Conductor<IScreen>.Collection.OneActive();
var conducted = new Screen();
conductor.Items.Add(conducted);
Expand All @@ -150,5 +118,19 @@ public void ParentItemIsUnsetOnReplaceConductedItem()
Assert.NotEqual(conductor, conducted.Parent);
Assert.Equal(conductor, conducted2.Parent);
}

private sealed class StateScreen : Screen {
public bool IsClosed { get; private set; }

public bool IsClosable { get; set; }

public override Task<bool> CanCloseAsync(CancellationToken cancellationToken)
=> Task.FromResult(IsClosable);

protected override async Task OnDeactivateAsync(bool close, CancellationToken cancellationToken) {
await base.OnDeactivateAsync(close, cancellationToken);
IsClosed = close;
}
}
}
}
Loading
Loading