Skip to content

Commit

Permalink
#19 support multiple config versions, add some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeclayton committed Jul 11, 2023
1 parent 840386b commit 6895f30
Show file tree
Hide file tree
Showing 26 changed files with 633 additions and 127 deletions.
1 change: 0 additions & 1 deletion src/FancyMouse.UnitTests/Helpers/LayoutHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using FancyMouse.Helpers;
using FancyMouse.Models.Drawing;
using FancyMouse.Models.Layout;
using FancyMouse.Models.Screen;
using FancyMouse.Models.Styles;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using static FancyMouse.NativeMethods.Core;
Expand Down
262 changes: 262 additions & 0 deletions src/FancyMouse.UnitTests/Models/Settings/AppSettingsReaderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
using System.Drawing;
using System.Text.Json;
using FancyMouse.Models.Settings;
using FancyMouse.Models.Styles;
using FancyMouse.WindowsHotKeys;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FancyMouse.UnitTests.Models.Settings;

[TestClass]
public sealed class AppSettingsReaderTests
{
[TestClass]
public sealed class ParseJsonTests
{
[TestMethod]
public void NullJsonShouldReturnDefaultSettings()
{
var actual = AppSettingsReader.ParseJson(null);
var expected = AppSettings.DefaultSettings;
Assert.AreSame(expected, actual);
}

[TestMethod]
public void EmptyJsonShouldReturnDefaultSettings()
{
var actual = AppSettingsReader.ParseJson(string.Empty);
var expected = AppSettings.DefaultSettings;
Assert.AreSame(expected, actual);
}

[TestMethod]
public void InvalidJsonShouldReturnDefaultSettings()
{
var actual = AppSettingsReader.ParseJson("xxx");
var expected = AppSettings.DefaultSettings;
Assert.AreSame(expected, actual);
}

[TestMethod]
public void MissingVersionShouldBeTreatedAsVersion1()
{
var actual = AppSettingsReader.ParseJson("{}");
var expected = AppSettings.DefaultSettings;
Assert.AreEqual(
JsonSerializer.Serialize(expected),
JsonSerializer.Serialize(actual));
}

[TestMethod]
public void EmptyVersion1ShouldParse()
{
var json = ParseJsonTests.SerializeAnonymousType(
new
{
version = 1,
});
var actual = AppSettingsReader.ParseJson(json);
var expected = AppSettings.DefaultSettings;
Assert.AreEqual(
JsonSerializer.Serialize(expected),
JsonSerializer.Serialize(actual));
}

[TestMethod]
public void Version1WithNullRootKeysShouldParse()
{
var json = ParseJsonTests.SerializeAnonymousType(
new
{
version = 1,
fancymouse = (object?)null,
});
var actual = AppSettingsReader.ParseJson(json);
var expected = AppSettings.DefaultSettings;
Assert.AreEqual(
JsonSerializer.Serialize(expected),
JsonSerializer.Serialize(actual));
}

[TestMethod]
public void Version1WithNullChildKeysShouldParse()
{
var json = ParseJsonTests.SerializeAnonymousType(
new
{
version = 1,
fancymouse = new
{
hotkey = (string?)null,
},
});
var actual = AppSettingsReader.ParseJson(json);
var expected = AppSettings.DefaultSettings;
Assert.AreEqual(
JsonSerializer.Serialize(expected),
JsonSerializer.Serialize(actual));
}

[TestMethod]
public void Version1WithAllValuesShouldParse()
{
var json = ParseJsonTests.SerializeAnonymousType(
new
{
version = 1,
fancymouse = new
{
hotkey = "CTRL + ALT + X",
preview = "800 x 600",
},
});
var actual = AppSettingsReader.ParseJson(json);
var expected = new AppSettings(
hotkey: new(
key: Keys.X,
modifiers: KeyModifiers.Control | KeyModifiers.Alt
),
previewStyle: new(
canvasSize: new(
width: 800,
height: 600
),
canvasStyle: AppSettings.DefaultSettings.PreviewStyle.CanvasStyle,
screenshotStyle: AppSettings.DefaultSettings.PreviewStyle.ScreenshotStyle
));
Assert.AreEqual(
JsonSerializer.Serialize(expected),
JsonSerializer.Serialize(actual));
}

[TestMethod]
public void Version2WithNullRootKeysShouldParse()
{
var json = ParseJsonTests.SerializeAnonymousType(
new
{
version = 2,
hotkey = (object?)null,
preview = (object?)null,
});
var actual = AppSettingsReader.ParseJson(json);
var expected = AppSettings.DefaultSettings;
Assert.AreEqual(
JsonSerializer.Serialize(expected),
JsonSerializer.Serialize(actual));
}

[TestMethod]
public void Version2WithAllValuesShouldParse()
{
var json = ParseJsonTests.SerializeAnonymousType(
new
{
version = 2,
hotkey = new
{
key = Keys.X.ToString(),
modifiers = (KeyModifiers.Control | KeyModifiers.Alt).ToString(),
},
preview = new
{
size = new
{
width = 800,
height = 600,
},
canvas = new
{
border = new
{
color = $"{nameof(SystemColors)}.{nameof(SystemColors.Control)}",
width = 5,
depth = 2,
},
padding = new
{
width = 2,
},
background = new
{
color1 = $"{nameof(Color)}.{nameof(Color.Green)}",
color2 = $"{nameof(Color)}.{nameof(Color.Blue)}",
},
},
screenshot = new
{
margin = new
{
width = 10,
},
border = new
{
color = $"{nameof(SystemColors)}.{nameof(SystemColors.Control)}",
width = 5,
depth = 2,
},
background = new
{
color1 = $"{nameof(Color)}.{nameof(Color.Yellow)}",
color2 = $"{nameof(Color)}.{nameof(Color.Pink)}",
},
},
},
});
var actual = AppSettingsReader.ParseJson(json);
var expected = new AppSettings(
hotkey: new(
key: Keys.X,
modifiers: KeyModifiers.Control | KeyModifiers.Alt
),
previewStyle: new(
canvasSize: new(
width: 800,
height: 600
),
canvasStyle: new(
marginStyle: MarginStyle.Empty,
borderStyle: new(
color: SystemColors.Control,
all: 5,
depth: 2
),
paddingStyle: new(
all: 2
),
backgroundStyle: new(
color1: Color.Green,
color2: Color.Blue
)
),
screenshotStyle: new(
marginStyle: new(
all: 10
),
borderStyle: new(
color: SystemColors.Control,
all: 5,
depth: 2
),
paddingStyle: PaddingStyle.Empty,
backgroundStyle: new(
color1: Color.Yellow,
color2: Color.Pink
)
)
));
Assert.AreEqual(
JsonSerializer.Serialize(expected),
JsonSerializer.Serialize(actual));
}

private static string SerializeAnonymousType<T>(T value)
{
var options = new JsonSerializerOptions
{
WriteIndented = true,
};
return JsonSerializer.Serialize(value, options);
}
}
}
10 changes: 10 additions & 0 deletions src/FancyMouse.WindowsHotKeys/Keystroke.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public KeyModifiers Modifiers
get;
}

public static Keystroke Parse(string s)
{
if (!Keystroke.TryParse(s, out var result))
{
throw new ArgumentException($"Invalid argument format.", nameof(s));
}

return result;
}

public static bool TryParse(string s, [NotNullWhen(true)] out Keystroke? result)
{
// see https://github.com/microsoft/terminal/blob/14919073a12fc0ecb4a9805cc183fdd68d30c4b6/src/cascadia/TerminalSettingsModel/KeyChordSerialization.cpp#L124
Expand Down
1 change: 0 additions & 1 deletion src/FancyMouse/Helpers/DrawingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Drawing.Imaging;
using FancyMouse.Models.Drawing;
using FancyMouse.Models.Layout;
using FancyMouse.Models.Screen;
using FancyMouse.Models.Styles;
using FancyMouse.NativeMethods;
using static FancyMouse.NativeMethods.Core;
Expand Down
1 change: 0 additions & 1 deletion src/FancyMouse/Helpers/LayoutHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using FancyMouse.Models.Drawing;
using FancyMouse.Models.Layout;
using FancyMouse.Models.Screen;
using FancyMouse.Models.Styles;

namespace FancyMouse.Helpers;
Expand Down
1 change: 0 additions & 1 deletion src/FancyMouse/Helpers/ScreenHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.ComponentModel;
using FancyMouse.Models.Drawing;
using FancyMouse.Models.Screen;
using FancyMouse.NativeMethods;
using static FancyMouse.NativeMethods.Core;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using FancyMouse.Models.Drawing;
using FancyMouse.NativeMethods;
using FancyMouse.NativeMethods;

namespace FancyMouse.Models.Screen;
namespace FancyMouse.Models.Drawing;

/// <summary>
/// Immutable version of a System.Windows.Forms.Screen object so we don't need to
Expand Down
58 changes: 54 additions & 4 deletions src/FancyMouse/Models/Settings/AppSettings.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,73 @@
using FancyMouse.WindowsHotKeys;
using FancyMouse.Models.Styles;
using FancyMouse.WindowsHotKeys;
using Keys = FancyMouse.WindowsHotKeys.Keys;

namespace FancyMouse.Models.Settings;

/// <summary>
/// Represents the settings used to control application behaviour.
/// This is different to the AppConfig class that is used to
/// serialize / deserialize settings into the application config file.
/// </summary>
internal sealed class AppSettings
{
public static readonly AppSettings DefaultSettings = new(
hotkey: new(
key: Keys.F,
modifiers: KeyModifiers.Control | KeyModifiers.Alt | KeyModifiers.Shift
),
previewStyle: new(
canvasSize: new(
width: 1600,
height: 1200
),
canvasStyle: new(
marginStyle: MarginStyle.Empty,
borderStyle: new(
color: SystemColors.Highlight,
all: 6,
depth: 0
),
paddingStyle: new(
all: 6
),
backgroundStyle: new(
color1: Color.FromArgb(0xFF, 0x0D, 0x57, 0xD2),
color2: Color.FromArgb(0xFF, 0x03, 0x44, 0xC0)
)
),
screenshotStyle: new(
marginStyle: new(
all: 2
),
borderStyle: new(
color: Color.FromArgb(0xFF, 0x22, 0x22, 0x22),
all: 10,
depth: 3
),
paddingStyle: PaddingStyle.Empty,
backgroundStyle: new(
color1: Color.MidnightBlue,
color2: Color.MidnightBlue
)
)
)
);

public AppSettings(
Keystroke hotkey,
PreviewSettings preview)
PreviewStyle previewStyle)
{
this.Hotkey = hotkey ?? throw new ArgumentNullException(nameof(hotkey));
this.Preview = preview ?? throw new ArgumentNullException(nameof(preview));
this.PreviewStyle = previewStyle ?? throw new ArgumentNullException(nameof(previewStyle));
}

public Keystroke Hotkey
{
get;
}

public PreviewSettings Preview
public PreviewStyle PreviewStyle
{
get;
}
Expand Down
Loading

0 comments on commit 6895f30

Please sign in to comment.