ColorPicker.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System.Text.Json; 6 7 using Microsoft.PowerToys.Settings.UI.Library; 8 using Microsoft.PowerToys.Settings.UI.UnitTests.BackwardsCompatibility; 9 using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks; 10 using Microsoft.PowerToys.Settings.UI.ViewModels; 11 using Microsoft.VisualStudio.TestTools.UnitTesting; 12 using Moq; 13 14 namespace ViewModelTests 15 { 16 [TestClass] 17 public class ColorPicker 18 { 19 /// <summary> 20 /// Test if the original settings files were modified. 21 /// </summary> 22 [TestMethod] 23 [DataRow("v0.20.1", "settings.json")] // Color picker was introduced in .20 24 [DataRow("v0.21.1", "settings.json")] 25 [DataRow("v0.22.0", "settings.json")] 26 public void OriginalFilesModificationTest(string version, string fileName) 27 { 28 // Arrange 29 var mockIOProvider = BackCompatTestProperties.GetModuleIOProvider(version, ColorPickerSettings.ModuleName, fileName); 30 var settingPathMock = new Mock<SettingPath>(); 31 32 var mockSettingsUtils = new SettingsUtils(mockIOProvider.Object, settingPathMock.Object); 33 ColorPickerSettings originalSettings = mockSettingsUtils.GetSettingsOrDefault<ColorPickerSettings>(ColorPickerSettings.ModuleName); 34 35 var mockGeneralIOProvider = BackCompatTestProperties.GetGeneralSettingsIOProvider(version); 36 37 var mockGeneralSettingsUtils = new SettingsUtils(mockGeneralIOProvider.Object, settingPathMock.Object); 38 GeneralSettings originalGeneralSettings = mockGeneralSettingsUtils.GetSettingsOrDefault<GeneralSettings>(); 39 var generalSettingsRepository = new BackCompatTestProperties.MockSettingsRepository<GeneralSettings>(mockGeneralSettingsUtils); 40 var colorPickerSettingsRepository = new BackCompatTestProperties.MockSettingsRepository<ColorPickerSettings>(mockSettingsUtils); 41 42 // Act 43 // Initialise View Model with test Config files 44 using (var viewModel = new ColorPickerViewModel( 45 mockSettingsUtils, 46 generalSettingsRepository, 47 colorPickerSettingsRepository, 48 ColorPickerIsEnabledByDefaultIPC)) 49 { 50 // Assert 51 // Verify that the old settings persisted 52 Assert.AreEqual(originalGeneralSettings.Enabled.ColorPicker, viewModel.IsEnabled); 53 Assert.AreEqual(originalSettings.Properties.ActivationShortcut.ToString(), viewModel.ActivationShortcut.ToString()); 54 Assert.AreEqual(originalSettings.Properties.ChangeCursor, viewModel.ChangeCursor); 55 56 // Verify that the stub file was used 57 var expectedCallCount = 2; // once via the view model, and once by the test (GetSettings<T>) 58 BackCompatTestProperties.VerifyModuleIOProviderWasRead(mockIOProvider, ColorPickerSettings.ModuleName, expectedCallCount); 59 BackCompatTestProperties.VerifyGeneralSettingsIOProviderWasRead(mockGeneralIOProvider, expectedCallCount); 60 } 61 } 62 63 [TestMethod] 64 public void ColorPickerIsEnabledByDefault() 65 { 66 var mockSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<ColorPickerSettings>(); 67 using (var viewModel = new ColorPickerViewModel( 68 ISettingsUtilsMocks.GetStubSettingsUtils<ColorPickerSettings>().Object, 69 SettingsRepository<GeneralSettings>.GetInstance(ISettingsUtilsMocks.GetStubSettingsUtils<GeneralSettings>().Object), 70 SettingsRepository<ColorPickerSettings>.GetInstance(SettingsUtils.Default), 71 ColorPickerIsEnabledByDefaultIPC)) 72 { 73 Assert.IsTrue(viewModel.IsEnabled); 74 } 75 } 76 77 private static int ColorPickerIsEnabledByDefaultIPC(string msg) 78 { 79 OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg); 80 Assert.IsTrue(snd.GeneralSettings.Enabled.ColorPicker); 81 return 0; 82 } 83 } 84 }