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

+semver:major Added properties to ArchivingDlgViewModel to facilitate customizing the initial summary #1349

Merged
merged 7 commits into from
Oct 8, 2024
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [SIL.Windows.Forms.Archiving] Added public extensions class LinkLabelExtensions with some methods that were formerly in Extensions class (now in SIL.Archiving).
- [SIL.Archiving] Added public property isValid to IMDIPackage.
- [SIL.Archiving] Added public event InitializationFailed to IMDIArchivingDlgViewModel.
- [SIL.Archiving] Added the following properties to ArchivingDlgViewModel as an alternative way to customize the initial summary displayed: GetOverriddenPreArchivingMessages, InitialFileGroupDisplayMessageType, OverrideGetFileGroupDisplayMessage

### Changed

Expand Down Expand Up @@ -71,6 +72,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [SIL.Archiving] Changed IArchivingSession.Files (and Session.Files) into an IReadonlyList.
- [SIL.Archiving] Made IMDIPackage.CreateIMDIPackage asynchronous, changing its signature to take a CancellationToken parameter and return Task<bool>.
- [SIL.Archiving] Made MetaTranscript.WriteCorpusImdiFile asynchronous, changing its signature to return Task<bool>.
- [SIL.Archiving] Changed the name of the third parameter in ArchivingDlgViewModel.AddFileGroup from progressMessage to addingToArchiveProgressMessage.

### Fixed
- [SIL.Archiving] Fixed typo in RampArchivingDlgViewModel for Ethnomusicology performance collection.
Expand Down
122 changes: 117 additions & 5 deletions SIL.Archiving.Tests/IMDIArchivingDlgViewModelTests.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using NUnit.Framework;
using SIL.Archiving.IMDI;
using SIL.Reporting;
using SIL.TestUtilities;
using static SIL.Archiving.ArchivingDlgViewModel.MessageType;
using CancellationToken = System.Threading.CancellationToken;

namespace SIL.Archiving.Tests
{
[TestFixture]
[OfflineSldr]
[Category("Archiving")]
internal class IMDIArchivingDlgViewModelTests
public class IMDIArchivingDlgViewModelTests
{
private class MessageData
{
Expand Down Expand Up @@ -100,7 +102,7 @@ public void PathIsAccessible_NonexistentPath_False()
Assert.False(writable);
Assert.AreEqual(1, m_messages.Count);
Assert.AreEqual("Test implementation message for PathNotWritable", m_messages[0].MsgText);
Assert.AreEqual(ArchivingDlgViewModel.MessageType.Warning, m_messages[0].MsgType);
Assert.AreEqual(Warning, m_messages[0].MsgType);
}

[Test]
Expand All @@ -112,7 +114,7 @@ public void IsPathWritable_WindowsInvalidPath_False()
Assert.False(writable);
Assert.AreEqual(1, m_messages.Count);
Assert.IsTrue(m_messages[0].MsgText.Contains("path"), "Error should mention the path in its explanation.");
Assert.AreEqual(ArchivingDlgViewModel.MessageType.Warning, m_messages[0].MsgType);
Assert.AreEqual(Warning, m_messages[0].MsgType);
}

[Test]
Expand All @@ -123,7 +125,7 @@ public void IsPathWritable_IllegalCharacterInPath_False()
Assert.False(writable);
Assert.AreEqual(1, m_messages.Count);
Assert.IsTrue(m_messages[0].MsgText.Contains("path"), "Error should mention the path in its explanation.");
Assert.AreEqual(ArchivingDlgViewModel.MessageType.Warning, m_messages[0].MsgType);
Assert.AreEqual(Warning, m_messages[0].MsgType);
}

#endregion
Expand Down Expand Up @@ -263,4 +265,114 @@ public void SetAbstract_MultipleLanguages_AddsDescriptionToCorpusImdiFile()

#endregion
}

[TestFixture]
[Category("Archiving")]
public class IMDIArchivingDlgViewModelWithOverrideDisplayInitialSummarySetTests
{
/// ------------------------------------------------------------------------------------
[Test]
public async Task DisplayInitialSummary_OverrideDisplayInitialSummaryIsSet_DefaultBehaviorOmitted()
{
ErrorReport.IsOkToInteractWithUser = false;

bool filesToArchiveCalled = false;

var model = new IMDIArchivingDlgViewModel("Test App", "Test Title", "tst", true,
(a, b) => { filesToArchiveCalled = true; }, "whatever");

var progress = new TestProgress("IMDI");
var customSummaryShown = 0;

model.OverrideDisplayInitialSummary = (d, c) =>
{
customSummaryShown++;
progress.IncrementProgress();
};
model.GetOverriddenPreArchivingMessages = d => throw new AssertionException(
$"{nameof(ArchivingDlgViewModel.GetOverriddenPreArchivingMessages)} should not have been invoked");
model.OverrideGetFileGroupDisplayMessage = s => throw new AssertionException(
$"{nameof(ArchivingDlgViewModel.OverrideGetFileGroupDisplayMessage)} should not have been invoked");

model.InitializationFailed += (sender, e) => Assert.Fail("Initialization failed");

try
{
await model.Initialize(progress, new CancellationToken()).ConfigureAwait(false);
}
catch (Exception ex)
{
Assert.Fail($"Initialization threw an exception: {ex}");
}

Assert.True(filesToArchiveCalled);
Assert.That(customSummaryShown, Is.EqualTo(1));
Assert.That(progress.Step, Is.EqualTo(1));
}
}

[TestFixture]
[Category("Archiving")]
public class IMDIArchivingDlgViewModelWithFineGrainedOverridesForDisplayInitialSummarySetTests
{
/// ------------------------------------------------------------------------------------
[Test]
public async Task DisplayInitialSummary_OverridenPropertiesForDisplayInitialSummaryAreSet_MessagesReflectOverrides()
{
ErrorReport.IsOkToInteractWithUser = false;

void SetFilesToArchive(ArchivingDlgViewModel model, CancellationToken cancellationToken)
{
model.AddFileGroup(String.Empty, new[] { "green.frog" }, "These messages should not be displayed");
model.AddFileGroup("Toads", new[] { "red.toad", "blue.toad" }, "because in this test we do not create a package.");
}

var model = new IMDIArchivingDlgViewModel("Test App", "Test Title", "tst", true,
SetFilesToArchive, "unused");

var messagesDisplayed = new List<Tuple<string, ArchivingDlgViewModel.MessageType>>();

void ReportMessage(string msg, ArchivingDlgViewModel.MessageType type)
{
messagesDisplayed.Add(new Tuple<string, ArchivingDlgViewModel.MessageType>(msg, type));
}

model.OnReportMessage += ReportMessage;

IEnumerable<Tuple<string, ArchivingDlgViewModel.MessageType>> GetMessages(IDictionary<string, Tuple<IEnumerable<string>, string>> arg)
{
yield return new Tuple<string, ArchivingDlgViewModel.MessageType>(
"First pre-archiving message", Warning);
yield return new Tuple<string, ArchivingDlgViewModel.MessageType>(
"Second pre-archiving message", Indented);
}

model.GetOverriddenPreArchivingMessages = GetMessages;
model.InitialFileGroupDisplayMessageType = Success;
model.OverrideGetFileGroupDisplayMessage = s => (s == String.Empty) ? "Frogs" : $"Label: {s}";
model.InitializationFailed += (sender, e) => Assert.Fail("Initialization failed");

var progress = new TestProgress("IMDI");
try
{
await model.Initialize(progress, new CancellationToken()).ConfigureAwait(false);
}
catch (Exception ex)
{
Assert.Fail($"Initialization threw an exception: {ex}");
}

Assert.That(messagesDisplayed, Is.EqualTo(new[]
{
("First pre-archiving message", Warning).ToTuple(),
("Second pre-archiving message", Indented).ToTuple(),
("Frogs", Success).ToTuple(),
("green.frog", Bullet).ToTuple(),
("Label: Toads", Success).ToTuple(),
("red.toad", Bullet).ToTuple(),
("blue.toad", Bullet).ToTuple()
}));
Assert.That(progress.Step, Is.EqualTo(1));
}
}
}
127 changes: 127 additions & 0 deletions SIL.Archiving.Tests/RampArchivingDlgViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using SIL.IO;
using SIL.Reporting;
using SIL.TestUtilities;
using static SIL.Archiving.ArchivingDlgViewModel.MessageType;

namespace SIL.Archiving.Tests
{
Expand Down Expand Up @@ -737,4 +738,130 @@ private void IgnoreTestIfRampIsNotInstalled()
}
#endregion
}

internal class TestRampArchivingDlgViewModel : RampArchivingDlgViewModel
{
public TestRampArchivingDlgViewModel(
Action<ArchivingDlgViewModel, CancellationToken> setFilesToArchive) :
base("Test App", "Test Title", "tst", setFilesToArchive,
(k, f) => throw new NotImplementedException())
{
}

protected override bool DoArchiveSpecificInitialization()
{
DisplayMessage("Base implementation overridden", MessageType.Volatile);
return true;
}
}

[TestFixture]
[Category("Archiving")]
public class RampArchivingDlgViewModelWithOverrideDisplayInitialSummarySetTests
{
/// ------------------------------------------------------------------------------------
[Test]
public async Task DisplayInitialSummary_OverrideDisplayInitialSummaryIsSet_DefaultBehaviorOmitted()
{
ErrorReport.IsOkToInteractWithUser = false;

bool filesToArchiveCalled = false;

var model = new TestRampArchivingDlgViewModel((a, b) => { filesToArchiveCalled = true; });

var progress = new TestProgress("RAMP");
var customSummaryShown = 0;

model.OverrideDisplayInitialSummary = (d, c) =>
{
customSummaryShown++;
progress.IncrementProgress();
};
model.GetOverriddenPreArchivingMessages = d => throw new AssertionException(
$"{nameof(ArchivingDlgViewModel.GetOverriddenPreArchivingMessages)} should not have been invoked");
model.OverrideGetFileGroupDisplayMessage = s => throw new AssertionException(
$"{nameof(ArchivingDlgViewModel.OverrideGetFileGroupDisplayMessage)} should not have been invoked");

try
{
await model.Initialize(progress, new CancellationToken()).ConfigureAwait(false);
}
catch (Exception ex)
{
Assert.Fail($"Initialization threw an exception: {ex}");
}

Assert.True(filesToArchiveCalled);
Assert.That(customSummaryShown, Is.EqualTo(1));
Assert.False(File.Exists(model.PackagePath));
Assert.That(progress.Step, Is.EqualTo(1));
}
}

[TestFixture]
[Category("Archiving")]
public class RampArchivingDlgViewModelWithFineGrainedOverridesForDisplayInitialSummarySetTests
{
/// ------------------------------------------------------------------------------------
[Test]
public async Task DisplayInitialSummary_OverridenPropertiesForDisplayInitialSummaryAreSet_MessagesReflectOverrides()
{
ErrorReport.IsOkToInteractWithUser = false;

void SetFilesToArchive(ArchivingDlgViewModel model, CancellationToken cancellationToken)
{
model.AddFileGroup(String.Empty, new[] { "green.frog" }, "These messages should not be displayed");
model.AddFileGroup("Toads", new[] { "red.toad", "blue.toad" }, "because in this test we do not create a package.");

}

var model = new TestRampArchivingDlgViewModel(SetFilesToArchive);

var messagesDisplayed = new List<Tuple<string, ArchivingDlgViewModel.MessageType>>();

void ReportMessage(string msg, ArchivingDlgViewModel.MessageType type)
{
messagesDisplayed.Add(new Tuple<string, ArchivingDlgViewModel.MessageType>(msg, type));
}

model.OnReportMessage += ReportMessage;

IEnumerable<Tuple<string, ArchivingDlgViewModel.MessageType>> GetMessages(IDictionary<string, Tuple<IEnumerable<string>, string>> arg)
{
yield return new Tuple<string, ArchivingDlgViewModel.MessageType>(
"First pre-archiving message", Warning);
yield return new Tuple<string, ArchivingDlgViewModel.MessageType>(
"Second pre-archiving message", Indented);
}

model.GetOverriddenPreArchivingMessages = GetMessages;
model.InitialFileGroupDisplayMessageType = Success;
model.OverrideGetFileGroupDisplayMessage = s => (s == String.Empty) ? "Frogs" : $"Label: {s}";

var progress = new TestProgress("RAMP");
try
{
await model.Initialize(progress, new CancellationToken()).ConfigureAwait(false);
}
catch (Exception ex)
{
Assert.Fail($"Initialization threw an exception: {ex}");
}

Assert.That(messagesDisplayed, Is.EqualTo(new[]
{
("Base implementation overridden", ArchivingDlgViewModel.MessageType.Volatile).ToTuple(),
("First pre-archiving message", Warning).ToTuple(),
("Second pre-archiving message", Indented).ToTuple(),
("Frogs", Success).ToTuple(),
("green.frog", Bullet).ToTuple(),
("Label: Toads", Success).ToTuple(),
("red.toad", Bullet).ToTuple(),
("blue.toad", Bullet).ToTuple()
}));

Assert.False(File.Exists(model.PackagePath));
Assert.That(progress.Step, Is.EqualTo(1));
}
}
}
Loading