Skip to content

Commit

Permalink
Tested MetaDraw Settings loading under numerous conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
nbollis committed Sep 18, 2024
1 parent 7f3d591 commit 76c9435
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 12 deletions.
14 changes: 7 additions & 7 deletions MetaMorpheus/GuiFunctions/MetaDraw/MetaDrawSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,9 @@ public static MetaDrawSettingsSnapshot MakeSnapShot()
/// <summary>
/// Loads in settings based upon SettingsSnapshot parameter
/// </summary>
public static void LoadSettings(MetaDrawSettingsSnapshot settings)
public static void LoadSettings(MetaDrawSettingsSnapshot settings, out bool flaggedErrorOnRead)
{
flaggedErrorOnRead = false;
DisplayIonAnnotations = settings.DisplayIonAnnotations;
AnnotateMzValues = settings.AnnotateMzValues;
AnnotateCharges = settings.AnnotateCharges;
Expand All @@ -479,7 +480,6 @@ public static void LoadSettings(MetaDrawSettingsSnapshot settings)
UnannotatedPeakColor = DrawnSequence.ParseOxyColorFromName(settings.UnannotatedPeakColor);
InternalIonColor = DrawnSequence.ParseOxyColorFromName(settings.InternalIonColor);

bool flaggedError = false;
try // Product Type Colors
{
var firstSplit = settings.ProductTypeToColorValues.First().Split(',');
Expand Down Expand Up @@ -513,7 +513,7 @@ public static void LoadSettings(MetaDrawSettingsSnapshot settings)
{
Debugger.Break();
SetDefaultProductTypeColors();
flaggedError = true;
flaggedErrorOnRead = true;
}

try // Beta Product Type Colors
Expand Down Expand Up @@ -550,7 +550,7 @@ public static void LoadSettings(MetaDrawSettingsSnapshot settings)
{
Debugger.Break();
SetDefaultBetaProductTypeColors();
flaggedError = true;
flaggedErrorOnRead = true;
}

try // Modification Type Colors
Expand Down Expand Up @@ -586,7 +586,7 @@ public static void LoadSettings(MetaDrawSettingsSnapshot settings)
{
Debugger.Break();
SetDefaultModificationColors();
flaggedError = true;
flaggedErrorOnRead = true;
}

try // Coverage Type Colors
Expand Down Expand Up @@ -622,7 +622,7 @@ public static void LoadSettings(MetaDrawSettingsSnapshot settings)
{
Debugger.Break();
SetDefaultCoverageTypeColors();
flaggedError = true;
flaggedErrorOnRead = true;
}

try // Spectrum Descriptors
Expand Down Expand Up @@ -661,7 +661,7 @@ public static void LoadSettings(MetaDrawSettingsSnapshot settings)
{
Debugger.Break();
SetDefaultProductTypeColors();
flaggedError = true;
flaggedErrorOnRead = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ public void LoadSettings()
{
MetaDrawSettingsSnapshot settings = null;
settings = XmlReaderWriter.ReadFromXmlFile<MetaDrawSettingsSnapshot>(SettingsPath);
MetaDrawSettings.LoadSettings(settings);
MetaDrawSettings.LoadSettings(settings, out bool flaggedErrorOnRead);

if (flaggedErrorOnRead)
SaveAsDefault();
}


Expand Down
91 changes: 87 additions & 4 deletions MetaMorpheus/Test/MetaDraw/MetaDrawSettingsAndViewsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public static void TestMetaDrawSettingsSnapshot()
snapshot.ShowLegend = false;
snapshot.DrawNumbersUnderStationary = false;
snapshot.SubAndSuperScriptIons = false;
MetaDrawSettings.LoadSettings(snapshot);
MetaDrawSettings.LoadSettings(snapshot, out bool flaggedError);
Assert.That(!flaggedError);
Assert.That(snapshot.DisplayIonAnnotations.Equals(MetaDrawSettings.DisplayIonAnnotations));
Assert.That(snapshot.AnnotateMzValues.Equals(MetaDrawSettings.AnnotateMzValues));
Assert.That(snapshot.AnnotateCharges.Equals(MetaDrawSettings.AnnotateCharges));
Expand Down Expand Up @@ -115,7 +116,8 @@ public static void TestMetaDrawSettingsSnapshot()
snapshot.AxisTitleTextSize = 0;
snapshot.StrokeThicknessAnnotated = 0;
snapshot.StrokeThicknessUnannotated = 0;
MetaDrawSettings.LoadSettings(snapshot);
MetaDrawSettings.LoadSettings(snapshot, out flaggedError);
Assert.That(!flaggedError);
Assert.That(MetaDrawSettings.AnnotatedFontSize, Is.EqualTo(14));
Assert.That(MetaDrawSettings.AxisLabelTextSize, Is.EqualTo(12));
Assert.That(MetaDrawSettings.AxisTitleTextSize, Is.EqualTo(14));
Expand Down Expand Up @@ -188,6 +190,79 @@ public static void TestSettingsViewLoading()
Assert.That(BlankSettingsView.CanOpen);
}


[Test]
public static void TestLoadMetaDrawSettings()
{
// Set up testing environment
var metaDrawTestingDirectory = Path.Combine(TestContext.CurrentContext.TestDirectory, "MetaDraw");
var testingDir = Path.Combine(metaDrawTestingDirectory, "TempTestData");
if (Directory.Exists(testingDir))
Directory.Delete(testingDir, true);
Directory.CreateDirectory(testingDir);

var masterSettings = Path.Combine(metaDrawTestingDirectory, @"105MetaDrawSettingsSaved.xml");
string outdatedSettingsPath = Path.Combine(testingDir, @"105MetaDrawSettingsSaved_COPY.xml");
File.Copy(masterSettings, outdatedSettingsPath);

// Save default values currently stored in MetaDrawSettings
var defaultCoverageColors = MetaDrawSettings.CoverageTypeToColor.Values.ToList();
var defaultColorValues = MetaDrawSettings.ProductTypeToColor.Values.ToList();

// CASE: user does not have a settings config file stored
// Desired outcome: All default values are used
MetaDrawSettingsViewModel viewModel = new MetaDrawSettingsViewModel(false);
var modelSettingsPath = MetaDrawSettingsViewModel.SettingsPath;
Assert.False(File.Exists(modelSettingsPath));
NUnit.Framework.Assert.That(!viewModel.HasDefaultSaved);

Assert.AreEqual(MetaDrawSettings.CoverageTypeToColor.Values.ElementAt(0), defaultCoverageColors[0]);
Assert.AreEqual(MetaDrawSettings.CoverageTypeToColor.Values.ElementAt(1), defaultCoverageColors[1]);
Assert.AreEqual(MetaDrawSettings.CoverageTypeToColor.Values.ElementAt(2), defaultCoverageColors[2]);

// Make one change and save this viewModel as a modern settings config for next test case
viewModel.CoverageColors.First().SelectionChanged("Yellow");
string modernSettingsPath = Path.Combine(testingDir, "temporaryCurrentSettingsConfig.xml");
MetaDrawSettingsViewModel.SettingsPath = modernSettingsPath;
viewModel.SaveAsDefault();

// CASE: user has modern settings config file stored
// Desired outcome: Read in correctly
modelSettingsPath = MetaDrawSettingsViewModel.SettingsPath;
Assert.True(File.Exists(modelSettingsPath));
var creationTime = File.GetLastWriteTime(modernSettingsPath);
viewModel = new MetaDrawSettingsViewModel(false);
// check that no new file was generated
var creationTimeAfterLoad = File.GetLastWriteTime(modernSettingsPath);
Assert.AreEqual(creationTimeAfterLoad, creationTime);

Assert.AreNotEqual(MetaDrawSettings.CoverageTypeToColor.Values.ElementAt(0), defaultCoverageColors[0]);
Assert.AreEqual(MetaDrawSettings.CoverageTypeToColor.Values.ElementAt(0), OxyColors.Yellow);
Assert.AreEqual(MetaDrawSettings.CoverageTypeToColor.Values.ElementAt(1), defaultCoverageColors[1]);
Assert.AreEqual(MetaDrawSettings.CoverageTypeToColor.Values.ElementAt(2), defaultCoverageColors[2]);


// CASE: user has old settings config file stored
// Desired outcome: We use what we can salvage, default the rest, delete old file, replace with modern
MetaDrawSettingsViewModel.SettingsPath = outdatedSettingsPath;

// Ensure settings are in outdated format
int commaCount = File.ReadLines(outdatedSettingsPath).Sum(p => p.Count(m => m.Equals(',')));
Assert.AreEqual(0, commaCount);

creationTime = File.GetLastWriteTime(outdatedSettingsPath);
viewModel = new MetaDrawSettingsViewModel(false);
creationTimeAfterLoad = File.GetLastWriteTime(outdatedSettingsPath);
Assert.AreNotEqual(creationTimeAfterLoad, creationTime);

Assert.AreEqual(MetaDrawSettings.ProductTypeToColor.Values.ElementAt(0), defaultColorValues[0]);
Assert.AreEqual(MetaDrawSettings.ProductTypeToColor.Values.ElementAt(1), defaultColorValues[1]);
Assert.AreEqual(MetaDrawSettings.ProductTypeToColor.Values.ElementAt(2), defaultColorValues[2]);

Directory.Delete(testingDir, true);
}


[Test]
public static void TestOldMetaDrawSettingsFileDoesNotCrash()
{
Expand Down Expand Up @@ -227,16 +302,24 @@ public static void TestOldMetaDrawSettingsFileDoesNotCrash()
Assert.That(MetaDrawSettings.CoverageTypeToColor.Values.ElementAt(2), Is.Not.EqualTo(defaultCoverageColors[2]));
}

[Test]
public static void TestMetaDrawSettingsLoadSettingsCases2()
{

}

[Test] // This test passes by not crashing
public static void TestMetaDrawSettingsLoadSettingsCases()
{
string path = Path.Combine(TestContext.CurrentContext.TestDirectory, "MetaDraw", @"105MetaDrawSettingsSavedEditedForTestCoverageFailures.xml");
var snapShot = XmlReaderWriter.ReadFromXmlFile<MetaDrawSettingsSnapshot>(path);
MetaDrawSettings.LoadSettings(snapShot);
MetaDrawSettings.LoadSettings(snapShot, out bool flaggedError);
Assert.That(flaggedError);

path = Path.Combine(TestContext.CurrentContext.TestDirectory, "MetaDraw", @"105MetaDrawSettingsSavedEditedForTestCoverageSuccess.xml");
snapShot = XmlReaderWriter.ReadFromXmlFile<MetaDrawSettingsSnapshot>(path);
MetaDrawSettings.LoadSettings(snapShot);
MetaDrawSettings.LoadSettings(snapShot, out flaggedError);
Assert.That(flaggedError);
}

[Test]
Expand Down

0 comments on commit 76c9435

Please sign in to comment.