PowerToys/src/core/Microsoft.PowerToys.Settings.UI/Views/GeneralPage.xaml.cs
ryanbodrug-microsoft 0f6428eed0
User/ryanbod/mock settings disk access (#6188)
* 1) Making Directory Methods private.
2) Removing the CreateDirectory / DeleteDirectory functionality from all Settings Unit Tests.

* Abstracting disk access via IIOProvider to be able to provide mocks for unit tests instead of writing to disk.   This also prevents developers who are running unit tests from interfering with the PowerToys settings on their local dev box.

* Dependency Injecting stub SettingsUtils for all tests

* Removing ISettingsUtils from constructors of objects that need to be deserialized (ColorPickerSettings/PowerLauncherSettings) as this breaks System.Text.Json

* Removing unused namespace reference

* Removing redifined mock

* As per PR feedback.  Stub Settings utils should work with any settings type if the intent is to compile / avoid null ref exceptions.

Strangely when implementing this fix it became apparent that a stub settings isn't enough, and disk access needed to be mocked.  I can't explain why the tests were passing previously.

* Leveraging GetMockIOProviderForSaveLoadExists
2020-09-21 10:14:44 -07:00

99 lines
3.3 KiB
C#

// 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 Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
using Windows.ApplicationModel.Resources;
using Windows.Data.Json;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace Microsoft.PowerToys.Settings.UI.Views
{
/// <summary>
/// General Settings Page.
/// </summary>
public sealed partial class GeneralPage : Page
{
/// <summary>
/// Gets or sets view model.
/// </summary>
public GeneralViewModel ViewModel { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="GeneralPage"/> class.
/// General Settings page constructor.
/// </summary>
public GeneralPage()
{
InitializeComponent();
// Load string resources
ResourceLoader loader = ResourceLoader.GetForViewIndependentUse();
var settingsUtils = new SettingsUtils(new SystemIOProvider());
ViewModel = new GeneralViewModel(
settingsUtils,
loader.GetString("GeneralSettings_RunningAsAdminText"),
loader.GetString("GeneralSettings_RunningAsUserText"),
ShellPage.IsElevated,
ShellPage.IsUserAnAdmin,
UpdateUIThemeMethod,
ShellPage.SendDefaultIPCMessage,
ShellPage.SendRestartAdminIPCMessage,
ShellPage.SendCheckForUpdatesIPCMessage);
ShellPage.ShellHandler.IPCResponseHandleList.Add((JsonObject json) =>
{
try
{
string version = json.GetNamedString("version");
bool isLatest = json.GetNamedBoolean("isVersionLatest");
var str = string.Empty;
if (isLatest)
{
str = ResourceLoader.GetForCurrentView().GetString("GeneralSettings_VersionIsLatest");
}
else if (version != string.Empty)
{
str = ResourceLoader.GetForCurrentView().GetString("GeneralSettings_NewVersionIsAvailable");
if (str != string.Empty)
{
str += ": " + version;
}
}
ViewModel.LatestAvailableVersion = string.Format(str);
}
catch (Exception)
{
}
});
DataContext = ViewModel;
}
public int UpdateUIThemeMethod(string themeName)
{
switch (themeName)
{
case "light":
ShellPage.ShellHandler.RequestedTheme = ElementTheme.Light;
break;
case "dark":
ShellPage.ShellHandler.RequestedTheme = ElementTheme.Dark;
break;
case "system":
ShellPage.ShellHandler.RequestedTheme = ElementTheme.Default;
break;
}
return 0;
}
}
}