2020-08-13 15:02:05 -07:00
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text.Json;
|
2020-10-22 09:45:48 -07:00
|
|
|
using Microsoft.PowerToys.Settings.UI.Library;
|
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library.Utilities;
|
2020-09-21 10:14:44 -07:00
|
|
|
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
|
2020-08-13 15:02:05 -07:00
|
|
|
using Microsoft.PowerToys.Settings.UnitTest;
|
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
2020-09-21 10:14:44 -07:00
|
|
|
using Moq;
|
2020-08-13 15:02:05 -07:00
|
|
|
|
|
|
|
|
namespace CommonLibTest
|
|
|
|
|
{
|
|
|
|
|
[TestClass]
|
|
|
|
|
public class SettingsUtilsTests
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2020-10-06 15:00:25 -07:00
|
|
|
public void SaveSettingsSaveSettingsToFileWhenFilePathExists()
|
2020-08-13 15:02:05 -07:00
|
|
|
{
|
|
|
|
|
// Arrange
|
2020-09-21 10:14:44 -07:00
|
|
|
var mockIOProvider = IIOProviderMocks.GetMockIOProviderForSaveLoadExists();
|
|
|
|
|
var settingsUtils = new SettingsUtils(mockIOProvider.Object);
|
|
|
|
|
|
2020-08-13 15:02:05 -07:00
|
|
|
string file_name = "\\test";
|
|
|
|
|
string file_contents_correct_json_content = "{\"name\":\"powertoy module name\",\"version\":\"powertoy version\"}";
|
|
|
|
|
|
|
|
|
|
BasePTSettingsTest expected_json = JsonSerializer.Deserialize<BasePTSettingsTest>(file_contents_correct_json_content);
|
|
|
|
|
|
|
|
|
|
// Act
|
2020-09-21 10:14:44 -07:00
|
|
|
settingsUtils.SaveSettings(file_contents_correct_json_content, file_name);
|
|
|
|
|
BasePTSettingsTest actual_json = settingsUtils.GetSettings<BasePTSettingsTest>(file_name);
|
2020-08-13 15:02:05 -07:00
|
|
|
|
|
|
|
|
// Assert
|
2020-08-25 09:07:44 -07:00
|
|
|
Assert.AreEqual(expected_json.ToJsonString(), actual_json.ToJsonString());
|
2020-08-13 15:02:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2020-10-06 15:00:25 -07:00
|
|
|
public void SaveSettingsShouldCreateFileWhenFilePathIsNotFound()
|
2020-08-13 15:02:05 -07:00
|
|
|
{
|
|
|
|
|
// Arrange
|
2020-09-21 10:14:44 -07:00
|
|
|
var mockIOProvider = IIOProviderMocks.GetMockIOProviderForSaveLoadExists();
|
|
|
|
|
var settingsUtils = new SettingsUtils(mockIOProvider.Object);
|
2020-08-13 15:02:05 -07:00
|
|
|
string file_name = "test\\Test Folder";
|
|
|
|
|
string file_contents_correct_json_content = "{\"name\":\"powertoy module name\",\"version\":\"powertoy version\"}";
|
|
|
|
|
|
|
|
|
|
BasePTSettingsTest expected_json = JsonSerializer.Deserialize<BasePTSettingsTest>(file_contents_correct_json_content);
|
|
|
|
|
|
2020-09-21 10:14:44 -07:00
|
|
|
settingsUtils.SaveSettings(file_contents_correct_json_content, file_name);
|
|
|
|
|
BasePTSettingsTest actual_json = settingsUtils.GetSettings<BasePTSettingsTest>(file_name);
|
2020-08-13 15:02:05 -07:00
|
|
|
|
|
|
|
|
// Assert
|
2020-08-25 09:07:44 -07:00
|
|
|
Assert.AreEqual(expected_json.ToJsonString(), actual_json.ToJsonString());
|
2020-08-13 15:02:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2020-10-06 15:00:25 -07:00
|
|
|
public void SettingsFolderExistsShouldReturnFalseWhenFilePathIsNotFound()
|
2020-08-13 15:02:05 -07:00
|
|
|
{
|
|
|
|
|
// Arrange
|
2020-09-21 10:14:44 -07:00
|
|
|
var mockIOProvider = IIOProviderMocks.GetMockIOProviderForSaveLoadExists();
|
|
|
|
|
var settingsUtils = new SettingsUtils(mockIOProvider.Object);
|
2020-08-13 15:02:05 -07:00
|
|
|
string file_name_random = "test\\" + RandomString();
|
|
|
|
|
string file_name_exists = "test\\exists";
|
|
|
|
|
string file_contents_correct_json_content = "{\"name\":\"powertoy module name\",\"version\":\"powertoy version\"}";
|
|
|
|
|
|
|
|
|
|
// Act
|
2020-09-21 10:14:44 -07:00
|
|
|
bool pathNotFound = settingsUtils.SettingsExists(file_name_random);
|
2020-08-13 15:02:05 -07:00
|
|
|
|
2020-09-21 10:14:44 -07:00
|
|
|
settingsUtils.SaveSettings(file_contents_correct_json_content, file_name_exists);
|
|
|
|
|
bool pathFound = settingsUtils.SettingsExists(file_name_exists);
|
2020-08-13 15:02:05 -07:00
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.IsFalse(pathNotFound);
|
|
|
|
|
Assert.IsTrue(pathFound);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string RandomString()
|
|
|
|
|
{
|
|
|
|
|
Random random = new Random();
|
|
|
|
|
int length = 20;
|
|
|
|
|
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
|
|
|
|
|
|
|
|
return new string(Enumerable.Repeat(chars, length)
|
|
|
|
|
.Select(s => s[random.Next(s.Length)]).ToArray());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|