[Settings] Migrate Color Picker Settings Tests (#5759)

* added MSTest project

* migrated color picker settings

* undo changes for the .xaml page
This commit is contained in:
Nkateko
2020-08-13 15:46:51 -07:00
committed by GitHub
parent 23a9fa40e7
commit 0b5749d491
4 changed files with 77 additions and 11 deletions

View File

@@ -1,20 +1,21 @@
// 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.Text.Json;
using Microsoft.PowerToys.Settings.UI.Lib.Helpers;
using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.Views;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
{
public class ColorPickerViewModel : Observable
{
private ColorPickerSettings _colorPickerSettings;
private bool _isEnabled;
public ColorPickerViewModel()
private Func<string, int> SendConfigMSG { get; }
public ColorPickerViewModel(Func<string, int> ipcMSGCallBackFunc)
{
if (SettingsUtils.SettingsExists(ColorPickerSettings.ModuleName))
{
@@ -30,6 +31,9 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
var generalSettings = SettingsUtils.GetSettings<GeneralSettings>();
_isEnabled = generalSettings.Enabled.ColorPicker;
}
// set the callback functions value to hangle outgoing IPC message.
SendConfigMSG = ipcMSGCallBackFunc;
}
public bool IsEnabled
@@ -50,7 +54,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
var generalSettings = SettingsUtils.GetSettings<GeneralSettings>();
generalSettings.Enabled.ColorPicker = value;
OutGoingGeneralSettings outgoing = new OutGoingGeneralSettings(generalSettings);
ShellPage.DefaultSndMSGCallback(outgoing.ToString());
SendConfigMSG(outgoing.ToString());
}
}
}
@@ -111,7 +115,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
private void NotifySettingsChanged()
{
ShellPage.DefaultSndMSGCallback(
SendConfigMSG(
string.Format("{{ \"powertoys\": {{ \"{0}\": {1} }} }}", ColorPickerSettings.ModuleName, JsonSerializer.Serialize(_colorPickerSettings)));
}
}

View File

@@ -0,0 +1,63 @@
// 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.IO;
using System.Text.Json;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ViewModelTests
{
[TestClass]
public class ColorPicker
{
private const string ModuleName = "ColorPicker";
[TestInitialize]
public void Setup()
{
var generalSettings = new GeneralSettings();
var colorPickerSettings = new ColorPickerSettings();
SettingsUtils.SaveSettings(generalSettings.ToJsonString());
SettingsUtils.SaveSettings(colorPickerSettings.ToJsonString(), colorPickerSettings.Name, ModuleName + ".json");
}
[TestCleanup]
public void CleanUp()
{
string generalSettings_file_name = string.Empty;
if (SettingsUtils.SettingsFolderExists(generalSettings_file_name))
{
DeleteFolder(generalSettings_file_name);
}
if (SettingsUtils.SettingsFolderExists(ModuleName))
{
DeleteFolder(ModuleName);
}
}
[TestMethod]
public void ColorPickerIsEnabledByDefault()
{
var viewModel = new ColorPickerViewModel(ColorPickerIsEnabledByDefault_IPC);
Assert.IsTrue(viewModel.IsEnabled);
}
public int ColorPickerIsEnabledByDefault_IPC(string msg)
{
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
Assert.IsTrue(snd.GeneralSettings.Enabled.ColorPicker);
return 0;
}
private static void DeleteFolder(string powertoy)
{
Directory.Delete(Path.Combine(SettingsUtils.LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"), true);
}
}
}

View File

@@ -108,7 +108,6 @@
<Compile Include="Interop.cs" />
<Compile Include="Services\ActivationService.cs" />
<Compile Include="Services\NavigationService.cs" />
<Compile Include="ViewModels\ColorPickerViewModel.cs" />
<Compile Include="ViewModels\Commands\ButtonClickCommand.cs" />
<Compile Include="ViewModels\FancyZonesViewModel.cs" />
<Compile Include="ViewModels\ImageResizerViewModel.cs" />

View File

@@ -2,7 +2,7 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.PowerToys.Settings.UI.ViewModels;
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
using Windows.UI.Xaml.Controls;
namespace Microsoft.PowerToys.Settings.UI.Views
@@ -13,7 +13,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
public ColorPickerPage()
{
ViewModel = new ColorPickerViewModel();
ViewModel = new ColorPickerViewModel(ShellPage.SendDefaultIPCMessage);
DataContext = ViewModel;
InitializeComponent();
}