Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public async Task DownloadCuesheet_GeneratesCuesheetFile_WhenCuesheetIsValid()
await detailView.EditTrackAsync("Track Artist 1", "Track Title 1");
await bar.OpenExportDialogAsync("Cuesheet");
var downloadTask = TestPage.WaitForDownloadAsync();
await TestPage.GetByRole(AriaRole.Button, new() { Name = "Download-Cuesheet.cue" }).ClickAsync();
await TestPage.GetByRole(AriaRole.Button, new() { Name = "Download" }).ClickAsync();
var download = await downloadTask;
using var stream = await download.CreateReadStreamAsync();
using var reader = new StreamReader(stream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task DownloadCuesheet_GeneratesCuesheetFile_WhenCuesheetIsValid()
await detailView.EditTrackAsync("Track Artist 1", "Track Title 1");
await bar.OpenExportDialogAsync("Cuesheet");
var downloadTask = TestPage.WaitForDownloadAsync();
await TestPage.GetByRole(AriaRole.Button, new() { Name = "Download-Cuesheet.cue" }).ClickAsync();
await TestPage.GetByRole(AriaRole.Button, new() { Name = "Download" }).ClickAsync();
var download = await downloadTask;
using var stream = await download.CreateReadStreamAsync();
using var reader = new StreamReader(stream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
//You should have received a copy of the GNU General Public License
//along with Foobar. If not, see
//<http: //www.gnu.org/licenses />.
using AudioCuesheetEditor.Data.Options;
using AudioCuesheetEditor.Model.AudioCuesheet;
using AudioCuesheetEditor.Model.AudioCuesheet.Import;
using AudioCuesheetEditor.Model.IO.Import;
using AudioCuesheetEditor.Model.Options;
using AudioCuesheetEditor.Model.Utility;
using AudioCuesheetEditor.Services.IO;
using AudioCuesheetEditor.Services.Options;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//This file is part of AudioCuesheetEditor.

//AudioCuesheetEditor is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//AudioCuesheetEditor is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with Foobar. If not, see
//<http: //www.gnu.org/licenses />.
using AudioCuesheetEditor.Model.AudioCuesheet;
using AudioCuesheetEditor.Model.Entity;
using AudioCuesheetEditor.Model.Options;
using AudioCuesheetEditor.Services.IO;
using AudioCuesheetEditor.Services.Options;
using AudioCuesheetEditor.Services.UI;
using Microsoft.Extensions.Localization;
using Microsoft.JSInterop;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace AudioCuesheetEditor.Tests.Services.Options
{
[TestClass()]
public class LocalStorageOptionsProviderTests
{
private readonly LocalStorageOptionsProvider _service;
private readonly Mock<IJSRuntime> _jsRuntime = new();
private readonly Mock<IStringLocalizer<ValidationMessage>> _localizer = new();

public LocalStorageOptionsProviderTests()
{
_service = new(_jsRuntime.Object, _localizer.Object);
}

[TestMethod]
public async Task SaveOptionsAsync_NonValidatableOptions_ReturnsSuccessAsync()
{
// Arrange
var options = new ExportOptions();
Boolean eventRaised = false;
_service.OptionSaved += delegate
{
eventRaised = true;
};
// Act
var result = await _service.SaveOptionsAsync(options);
// Assert
Assert.IsTrue(result.IsSuccess);
Assert.IsTrue(eventRaised);
}

[TestMethod]
public async Task SaveOptionsAsync_ValidOptions_ReturnsSuccessAsync()
{
// Arrange
var options = new DownloadOptions()
{
ProjectFilename = "Test.ace",
CuesheetFilename = "Test.cue"
};
Boolean eventRaised = false;
_service.OptionSaved += delegate
{
eventRaised = true;
};
// Act
var result = await _service.SaveOptionsAsync(options);
// Assert
Assert.IsTrue(result.IsSuccess);
Assert.IsTrue(eventRaised);
}

[TestMethod]
public async Task SaveOptionsAsync_InvalidOptions_ReturnsFailureAsync()
{
// Arrange
var options = new DownloadOptions()
{
CuesheetFilename = null,
ProjectFilename = null
};
Boolean eventRaised = false;
_service.OptionSaved += delegate
{
eventRaised = true;
};
// Act
var result = await _service.SaveOptionsAsync(options);
// Assert
Assert.IsFalse(result.IsSuccess);
Assert.IsFalse(eventRaised);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
//You should have received a copy of the GNU General Public License
//along with Foobar. If not, see
//<http: //www.gnu.org/licenses />.
using AudioCuesheetEditor.Data.Options;
using AudioCuesheetEditor.Model.AudioCuesheet;
using AudioCuesheetEditor.Model.Options;
using AudioCuesheetEditor.Model.Utility;
using AudioCuesheetEditor.Services.Options;
using AudioCuesheetEditor.Services.UI;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
//You should have received a copy of the GNU General Public License
//along with Foobar. If not, see
//<http: //www.gnu.org/licenses />.
using AudioCuesheetEditor.Data.Options;
using AudioCuesheetEditor.Model.AudioCuesheet;
using AudioCuesheetEditor.Model.IO.Import;
using AudioCuesheetEditor.Model.Options;
using AudioCuesheetEditor.Services.Options;
using AudioCuesheetEditor.Services.UI;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
Expand Down
4 changes: 2 additions & 2 deletions AudioCuesheetEditor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
//along with Foobar. If not, see
//<http: //www.gnu.org/licenses />.
using AudioCuesheetEditor;
using AudioCuesheetEditor.Data.Options;
using AudioCuesheetEditor.Data.Services;
using AudioCuesheetEditor.Extensions;
using AudioCuesheetEditor.Model.Options;
using AudioCuesheetEditor.Services.Audio;
using AudioCuesheetEditor.Services.AudioCuesheet;
using AudioCuesheetEditor.Services.IO;
using AudioCuesheetEditor.Services.Options;
using AudioCuesheetEditor.Services.Provider;
using AudioCuesheetEditor.Services.UI;
using AudioCuesheetEditor.Services.Validation;
using BlazorDownloadFile;
Expand Down
2 changes: 1 addition & 1 deletion AudioCuesheetEditor/Services/IO/TextImportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
//along with Foobar. If not, see
//<http: //www.gnu.org/licenses />.

using AudioCuesheetEditor.Data.Options;
using AudioCuesheetEditor.Model.AudioCuesheet;
using AudioCuesheetEditor.Model.AudioCuesheet.Import;
using AudioCuesheetEditor.Model.IO.Audio;
using AudioCuesheetEditor.Model.IO.Import;
using AudioCuesheetEditor.Model.Options;
using AudioCuesheetEditor.Model.Utility;
using AudioCuesheetEditor.Services.Options;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
using AudioCuesheetEditor.Model.Options;
using System.Linq.Expressions;

namespace AudioCuesheetEditor.Data.Options
namespace AudioCuesheetEditor.Services.Options
{
public interface ILocalStorageOptionsProvider
{
event EventHandler<IOptions>? OptionSaved;
Task<T> GetOptionsAsync<T>() where T : IOptions;
Task SaveOptionsAsync(IOptions options);
Task SaveOptionsValueAsync<T>(Expression<Func<T, object?>> propertyExpression, object? value) where T : class, IOptions, new();
Task<Result> SaveOptionsAsync(IOptions options);
Task<Result> SaveOptionsValueAsync<T>(Expression<Func<T, object?>> propertyExpression, object? value) where T : class, IOptions, new();
Task SaveNestedOptionValueAsync<T, TNested, TValue>(Expression<Func<T, TNested>> nestedPropertyExpression, Expression<Func<TNested, TValue>> valuePropertyExpression, TValue value) where T : class, IOptions, new();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@
//<http: //www.gnu.org/licenses />.
using AudioCuesheetEditor.Model.Entity;
using AudioCuesheetEditor.Model.Options;
using Microsoft.Extensions.Localization;
using Microsoft.JSInterop;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.Json;

namespace AudioCuesheetEditor.Data.Options
namespace AudioCuesheetEditor.Services.Options
{
public class LocalStorageOptionsProvider(IJSRuntime jsRuntime): ILocalStorageOptionsProvider
public class LocalStorageOptionsProvider(IJSRuntime jsRuntime, IStringLocalizer<ValidationMessage> localizer) : ILocalStorageOptionsProvider
{
public event EventHandler<IOptions>? OptionSaved;

private readonly IJSRuntime _jsRuntime = jsRuntime;

private readonly JsonSerializerOptions SerializerOptions = new()
private readonly IStringLocalizer<ValidationMessage> _localizer = localizer;
private readonly JsonSerializerOptions _serializerOptions = new()
{
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull
};
Expand Down Expand Up @@ -61,22 +62,21 @@ public async Task<T> GetOptionsAsync<T>() where T : IOptions
}
}

public async Task SaveOptionsAsync(IOptions options)
public async Task<Result> SaveOptionsAsync(IOptions options)
{
bool saveOptions = true;
if (options is IValidateable validateable)
{
saveOptions = validateable.Validate().Status != ValidationStatus.Error;
}
if (saveOptions)
{
var optionsJson = JsonSerializer.Serialize<object>(options, SerializerOptions);
await _jsRuntime.InvokeVoidAsync("AppSettings.set", options.GetType().Name, optionsJson);
OptionSaved?.Invoke(this, options);
var validationResult = validateable.Validate();
if (validationResult.Status == ValidationStatus.Error)
{
return Result.Failure(new Error(ErrorType.ValidationFailed, string.Join(Environment.NewLine, validationResult.ValidationMessages.Select(x => x.GetMessageLocalized(_localizer)))));
}
}
await WriteOptionsAsync(options);
return Result.Success();
}

public async Task SaveOptionsValueAsync<T>(Expression<Func<T, object?>> propertyExpression, object? value) where T : class, IOptions, new()
public async Task<Result> SaveOptionsValueAsync<T>(Expression<Func<T, object?>> propertyExpression, object? value) where T : class, IOptions, new()
{
var options = await GetOptionsAsync<T>();
PropertyInfo? propertyInfo = null;
Expand All @@ -99,7 +99,7 @@ public async Task SaveOptionsAsync(IOptions options)
{
throw new ArgumentException("The provided expression does not reference a valid property.");
}
await SaveOptionsAsync(options);
return await SaveOptionsAsync(options);
}

public async Task SaveNestedOptionValueAsync<T, TNested, TValue>(Expression<Func<T, TNested>> nestedPropertyExpression, Expression<Func<TNested, TValue>> valuePropertyExpression, TValue value) where T : class, IOptions, new()
Expand Down Expand Up @@ -152,5 +152,12 @@ public async Task SaveOptionsAsync(IOptions options)

return propertyInfo;
}

async Task WriteOptionsAsync(IOptions options)
{
var optionsJson = JsonSerializer.Serialize<object>(options, _serializerOptions);
await _jsRuntime.InvokeVoidAsync("AppSettings.set", options.GetType().Name, optionsJson);
OptionSaved?.Invoke(this, options);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
using MetaBrainz.MusicBrainz.Interfaces.Searches;
using System.Reflection;

namespace AudioCuesheetEditor.Data.Services
namespace AudioCuesheetEditor.Services.Provider
{
public class MusicBrainzArtist
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
//You should have received a copy of the GNU General Public License
//along with Foobar. If not, see
//<http: //www.gnu.org/licenses />.
using AudioCuesheetEditor.Data.Options;
using AudioCuesheetEditor.Model.Options;
using AudioCuesheetEditor.Model.Utility;
using AudioCuesheetEditor.Services.Options;
using System.Linq.Expressions;
using System.Reflection;

Expand Down
2 changes: 1 addition & 1 deletion AudioCuesheetEditor/Services/UI/AutocompleteManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//You should have received a copy of the GNU General Public License
//along with Foobar. If not, see
//<http: //www.gnu.org/licenses />.
using AudioCuesheetEditor.Data.Services;
using AudioCuesheetEditor.Services.Provider;

namespace AudioCuesheetEditor.Services.UI
{
Expand Down
2 changes: 1 addition & 1 deletion AudioCuesheetEditor/Services/UI/LocalizationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
//You should have received a copy of the GNU General Public License
//along with Foobar. If not, see
//<http: //www.gnu.org/licenses />.
using AudioCuesheetEditor.Data.Options;
using AudioCuesheetEditor.Model.Options;
using AudioCuesheetEditor.Services.Options;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System.Globalization;
Expand Down
2 changes: 1 addition & 1 deletion AudioCuesheetEditor/Services/UI/SessionStateContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
//You should have received a copy of the GNU General Public License
//along with Foobar. If not, see
//<http: //www.gnu.org/licenses />.
using AudioCuesheetEditor.Data.Options;
using AudioCuesheetEditor.Model.AudioCuesheet;
using AudioCuesheetEditor.Model.IO.Audio;
using AudioCuesheetEditor.Model.IO.Import;
using AudioCuesheetEditor.Model.Options;
using AudioCuesheetEditor.Services.Options;

namespace AudioCuesheetEditor.Services.UI
{
Expand Down
2 changes: 1 addition & 1 deletion AudioCuesheetEditor/Shared/BaseLocalizedComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//You should have received a copy of the GNU General Public License
//along with Foobar. If not, see
//<http: //www.gnu.org/licenses />.
using AudioCuesheetEditor.Data.Options;
using AudioCuesheetEditor.Services.Options;
using AudioCuesheetEditor.Services.UI;
using Microsoft.AspNetCore.Components;

Expand Down
Loading