From 6b0e6a42e613c515263175d1382f9c0db14b4143 Mon Sep 17 00:00:00 2001 From: Lavius Motileng <58791731+laviusmotileng-ms@users.noreply.github.com> Date: Fri, 7 Feb 2020 15:38:33 -0800 Subject: [PATCH] added registry methods and enable/disable preview handlers (#1230) * added registry methods and enable/disable preview handlers * formatted .rc file. * formatted resource file * formatted .rc file. * formatted settings.cpp * formatted settings.h * formatted SVGPreviewSettingsClassTest.cpp * Formatted MarkDownPreviewSettingsClassTest.cpp * using wide strings * formatted settings.h * FileExplorerPreviewSettingsTest.cpp formatting * fixed typo and formatting * closing Registry and fixing typos * formmarted code using ctrl+k+d * fixed naming * fixed typo * changed if/else reverse order --- .../previewpane/powerpreview/powerpreview.cpp | 14 -- .../previewpane/powerpreview/powerpreview.h | 2 - .../previewpane/powerpreview/powerpreview.rc | 2 + .../previewpane/powerpreview/resource.h | 32 +-- .../previewpane/powerpreview/settings.cpp | 219 ++++++++++++++---- .../previewpane/powerpreview/settings.h | 51 ++-- .../BaseSettingsClassTest.cpp | 12 +- .../powerpreviewTest/BaseSettingsClassTest.h | 4 +- .../FileExplorerPreviewSettingsTest.cpp | 104 +++++---- .../MarkDownPreviewSettingsClassTest.cpp | 56 +++++ .../SVGPreviewSettingsClassTest.cpp | 55 +++++ .../powerpreviewTest/powerpreviewTest.vcxproj | 2 + .../powerpreviewTest.vcxproj.filters | 6 + 13 files changed, 416 insertions(+), 143 deletions(-) create mode 100644 src/modules/previewpane/powerpreviewTest/MarkDownPreviewSettingsClassTest.cpp create mode 100644 src/modules/previewpane/powerpreviewTest/SVGPreviewSettingsClassTest.cpp diff --git a/src/modules/previewpane/powerpreview/powerpreview.cpp b/src/modules/previewpane/powerpreview/powerpreview.cpp index 03d02af996c6..aec1ec2ca375 100644 --- a/src/modules/previewpane/powerpreview/powerpreview.cpp +++ b/src/modules/previewpane/powerpreview/powerpreview.cpp @@ -38,18 +38,6 @@ bool PowerPreviewModule::get_config(_Out_ wchar_t* buffer, _Out_ int* buffer_siz settings.set_description(GET_RESOURCE_STRING(IDS_GENERAL_DESCRIPTION)); settings.set_icon_key(GET_RESOURCE_STRING(IDS_ICON_KEY_NAME)); - // Explorer: Settings Group Header. - settings.add_header_szLarge( - GET_RESOURCE_STRING(IDS_EXPLR_ICONS_PREV_STTNGS_GROUP_HEADER_ID), - GET_RESOURCE_STRING(IDS_EXPLR_ICONS_PREV_STTNGS_GROUP_DESC), - GET_RESOURCE_STRING(IDS_EXPLR_ICONS_PREV_STTNGS_GROUP_TEXT)); - - // Explorer: SVG Icon Settings. - settings.add_bool_toogle( - m_explrSVGSettings.GetName(), - m_explrSVGSettings.GetDescription(), - m_explrSVGSettings.GetState()); - // Preview Pane: Settings Group Header. settings.add_header_szLarge( GET_RESOURCE_STRING(IDS_PRVPANE_FILE_PREV_STTNGS_GROUP_HEADER_ID), @@ -77,7 +65,6 @@ void PowerPreviewModule::set_config(const wchar_t* config) try { PowerToysSettings::PowerToyValues values = PowerToysSettings::PowerToyValues::from_json_string(config); - m_explrSVGSettings.UpdateState(values); m_prevPaneSVGSettings.UpdateState(values); m_prevPaneMDSettings.UpdateState(values); values.save_to_settings_file(); @@ -124,7 +111,6 @@ void PowerPreviewModule::init_settings() PowerToysSettings::PowerToyValues::load_from_settings_file(PowerPreviewModule::get_name()); // Load settings states. - m_explrSVGSettings.LoadState(settings); m_prevPaneSVGSettings.LoadState(settings); m_prevPaneMDSettings.LoadState(settings); } diff --git a/src/modules/previewpane/powerpreview/powerpreview.h b/src/modules/previewpane/powerpreview/powerpreview.h index 0c14e9686c3e..20cc2ccb748a 100644 --- a/src/modules/previewpane/powerpreview/powerpreview.h +++ b/src/modules/previewpane/powerpreview/powerpreview.h @@ -14,7 +14,6 @@ class PowerPreviewModule : public PowertoyModuleIface private: // The PowerToy state. bool m_enabled = false; - ExplrSVGSttngs m_explrSVGSettings; PrevPaneSVGRendrSettings m_prevPaneSVGSettings; PrevPaneMDRendrSettings m_prevPaneMDSettings; std::wstring m_moduleName; @@ -23,7 +22,6 @@ class PowerPreviewModule : public PowertoyModuleIface PowerPreviewModule() : m_moduleName(GET_RESOURCE_STRING(IDS_MODULE_NAME)), - m_explrSVGSettings(ExplrSVGSttngs()), m_prevPaneSVGSettings(PrevPaneSVGRendrSettings()), m_prevPaneMDSettings(PrevPaneMDRendrSettings()) { diff --git a/src/modules/previewpane/powerpreview/powerpreview.rc b/src/modules/previewpane/powerpreview/powerpreview.rc index 1b6830812829..d912d6336de9 100644 --- a/src/modules/previewpane/powerpreview/powerpreview.rc +++ b/src/modules/previewpane/powerpreview/powerpreview.rc @@ -58,8 +58,10 @@ BEGIN IDS_PRVPANE_FILE_PREV_STTNGS_GROUP_TEXT L"Preview Pane"; IDS_PREVPANE_MD_BOOL_TOGGLE_CONTROLL L"PREVPANE_MD_BOOL_TOGGLE_CONTROLL_ID" IDS_PREVPANE_MD_SETTINGS_DESCRIPTION L"Show Markdown" + IDS_PREVPANE_MD_SETTINGS_DISPLAYNAME L"Markdown Preview Handler" IDS_PREVPANE_SVG_BOOL_TOGGLE_CONTROLL L"IDS_PREVPANE_SVG_BOOL_TOGGLE_CONTROLL" IDS_PREVPANE_SVG_SETTINGS_DESCRIPTION L"Show SVG" + IDS_PREVPANE_SVG_SETTINGS_DISPLAYNAME L"SVG Preview Handler" IDS_EXPLR_SVG_BOOL_TOGGLE_CONTROLL L"EXPLR_SVG_BOOL_TOGGLE_CONTROLL" IDS_EXPLR_SVG_SETTINGS_DESCRIPTION L"Render SVG images" END diff --git a/src/modules/previewpane/powerpreview/resource.h b/src/modules/previewpane/powerpreview/resource.h index 39ab7ecf523a..939a5addf66e 100644 --- a/src/modules/previewpane/powerpreview/resource.h +++ b/src/modules/previewpane/powerpreview/resource.h @@ -2,21 +2,23 @@ // Microsoft Visual C++ generated include file. // Used by powerpreview.rc -#define IDS_GENERAL_DESCRIPTION 200 -#define IDS_MODULE_NAME 201 -#define IDS_ICON_KEY_NAME 202 -#define IDS_EXPLR_ICONS_PREV_STTNGS_GROUP_HEADER_ID 203 -#define IDS_EXPLR_ICONS_PREV_STTNGS_GROUP_DESC 204 -#define IDS_EXPLR_ICONS_PREV_STTNGS_GROUP_TEXT 205 -#define IDS_PRVPANE_FILE_PREV_STTNGS_GROUP_HEADER_ID 206 -#define IDS_PRVPANE_FILE_PREV_STTNGS_GROUP_DESC 207 -#define IDS_PRVPANE_FILE_PREV_STTNGS_GROUP_TEXT 208 -#define IDS_PREVPANE_MD_BOOL_TOGGLE_CONTROLL 209 -#define IDS_PREVPANE_MD_SETTINGS_DESCRIPTION 210 -#define IDS_PREVPANE_SVG_BOOL_TOGGLE_CONTROLL 211 -#define IDS_PREVPANE_SVG_SETTINGS_DESCRIPTION 212 -#define IDS_EXPLR_SVG_BOOL_TOGGLE_CONTROLL 213 -#define IDS_EXPLR_SVG_SETTINGS_DESCRIPTION 214 +#define IDS_GENERAL_DESCRIPTION 200 +#define IDS_MODULE_NAME 201 +#define IDS_ICON_KEY_NAME 202 +#define IDS_EXPLR_ICONS_PREV_STTNGS_GROUP_HEADER_ID 203 +#define IDS_EXPLR_ICONS_PREV_STTNGS_GROUP_DESC 204 +#define IDS_EXPLR_ICONS_PREV_STTNGS_GROUP_TEXT 205 +#define IDS_PRVPANE_FILE_PREV_STTNGS_GROUP_HEADER_ID 206 +#define IDS_PRVPANE_FILE_PREV_STTNGS_GROUP_DESC 207 +#define IDS_PRVPANE_FILE_PREV_STTNGS_GROUP_TEXT 208 +#define IDS_PREVPANE_MD_BOOL_TOGGLE_CONTROLL 209 +#define IDS_PREVPANE_MD_SETTINGS_DESCRIPTION 210 +#define IDS_PREVPANE_SVG_BOOL_TOGGLE_CONTROLL 211 +#define IDS_PREVPANE_SVG_SETTINGS_DESCRIPTION 212 +#define IDS_EXPLR_SVG_BOOL_TOGGLE_CONTROLL 213 +#define IDS_EXPLR_SVG_SETTINGS_DESCRIPTION 214 +#define IDS_PREVPANE_SVG_SETTINGS_DISPLAYNAME 215 +#define IDS_PREVPANE_MD_SETTINGS_DISPLAYNAME 216 // Next default values for new objects // diff --git a/src/modules/previewpane/powerpreview/settings.cpp b/src/modules/previewpane/powerpreview/settings.cpp index f915240e48c1..53269acb4526 100644 --- a/src/modules/previewpane/powerpreview/settings.cpp +++ b/src/modules/previewpane/powerpreview/settings.cpp @@ -2,23 +2,31 @@ #include #include "settings.h" #include "trace.h" +#include +#include + +using namespace std; namespace PowerPreviewSettings { extern "C" IMAGE_DOS_HEADER __ImageBase; // Base Settinngs Class Implementation - FileExplorerPreviewSettings::FileExplorerPreviewSettings(bool state, const std::wstring name, const std::wstring description) - : + FileExplorerPreviewSettings::FileExplorerPreviewSettings(bool state, const std::wstring name, const std::wstring description, LPCWSTR clsid, const std::wstring displayname) + : m_isPreviewEnabled(state), - m_name(name), - m_description(description){} + m_name(name), + m_description(description), + m_clsid(clsid), + m_displayName(displayname) {} FileExplorerPreviewSettings::FileExplorerPreviewSettings() : - m_isPreviewEnabled(false), - m_name(L"_UNDEFINED_"), - m_description(L"_UNDEFINED_"){} + m_isPreviewEnabled(false), + m_name(L"_UNDEFINED_"), + m_description(L"_UNDEFINED_"), + m_clsid(L"_UNDEFINED_"), + m_displayName(L"_UNDEFINED_") {} bool FileExplorerPreviewSettings::GetState() const { @@ -27,13 +35,13 @@ namespace PowerPreviewSettings void FileExplorerPreviewSettings::SetState(bool state) { - this->m_isPreviewEnabled = state; + this->m_isPreviewEnabled = state; } void FileExplorerPreviewSettings::LoadState(PowerToysSettings::PowerToyValues& settings) { - auto toggle = settings.get_bool_value(this->GetName()); - if(toggle != std::nullopt) + auto toggle = settings.get_bool_value(this->GetName()); + if (toggle != std::nullopt) { this->m_isPreviewEnabled = toggle.value(); } @@ -41,17 +49,17 @@ namespace PowerPreviewSettings void FileExplorerPreviewSettings::UpdateState(PowerToysSettings::PowerToyValues& values) { - auto toggle = values.get_bool_value(this->GetName()); - if(toggle != std::nullopt) + auto toggle = values.get_bool_value(this->GetName()); + if (toggle != std::nullopt) { - this->m_isPreviewEnabled = toggle.value(); + this->m_isPreviewEnabled = toggle.value(); if (this->m_isPreviewEnabled) { this->EnablePreview(); } else { - this->DisabledPreview(); + this->DisablePreview(); } } else @@ -60,12 +68,103 @@ namespace PowerPreviewSettings } } + LONG FileExplorerPreviewSettings::SetRegistryValue() const + { + HKEY hKey = HKEY_CURRENT_USER; + const REGSAM WRITE_PERMISSION = KEY_WRITE; + DWORD options = 0; + HKEY OpenResult; + + LONG err = RegOpenKeyEx(hKey, this->GetSubKey(), options, WRITE_PERMISSION, &OpenResult); + + if (err == ERROR_SUCCESS) + { + err = RegSetValueExW( + OpenResult, + this->GetCLSID(), + 0, + REG_SZ, + (LPBYTE)this->GetDisplayName().c_str(), + this->GetDisplayName().length() * sizeof(wchar_t)); + RegCloseKey(OpenResult); + if (err != ERROR_SUCCESS) + { + return err; + } + } + else + { + return err; + } + } + + LONG FileExplorerPreviewSettings::DeleteRegistryValue() const + { + HKEY hKey = HKEY_CURRENT_USER; + const REGSAM WRITE_PERMISSION = KEY_WRITE; + DWORD options = 0; + HKEY OpenResult; + + LONG err = RegOpenKeyEx(hKey, this->GetSubKey(), options, WRITE_PERMISSION, &OpenResult); + if (err == ERROR_SUCCESS) + { + err = RegDeleteKeyValueW( + OpenResult, + NULL, + this->GetCLSID()); + RegCloseKey(OpenResult); + + if (err != ERROR_SUCCESS) + { + return err; + } + } + else + { + return err; + } + } + + bool FileExplorerPreviewSettings::GetRegistryValue() const + { + HKEY OpenResult; + LONG err = RegOpenKeyEx( + HKEY_CURRENT_USER, + this->GetSubKey(), + 0, + KEY_READ, + &OpenResult); + + if (err == ERROR_SUCCESS) + { + DWORD dataType; + err = RegGetValueW( + OpenResult, + NULL, + this->GetCLSID(), + RRF_RT_ANY, + &dataType, + NULL, + 0); + RegCloseKey(OpenResult); + if (err != ERROR_SUCCESS) + { + return false; + } + } + else + { + return false; + } + return true; + } + std::wstring FileExplorerPreviewSettings::GetName() const { return this->m_name; } - void FileExplorerPreviewSettings::SetName(const std::wstring name) + void FileExplorerPreviewSettings::SetName(const std::wstring& name) { this->m_name = name; } @@ -75,63 +174,101 @@ namespace PowerPreviewSettings return this->m_description; } - void FileExplorerPreviewSettings::SetDescription(const std::wstring description) + void FileExplorerPreviewSettings::SetDescription(const std::wstring& description) { - this->m_description = description; + this->m_description = description; } - // Explorer SVG Icons Preview Settings Implemention - ExplrSVGSttngs::ExplrSVGSttngs() - : - FileExplorerPreviewSettings( - false, - GET_RESOURCE_STRING(IDS_EXPLR_SVG_BOOL_TOGGLE_CONTROLL), - GET_RESOURCE_STRING(IDS_EXPLR_SVG_SETTINGS_DESCRIPTION)){} + LPCWSTR FileExplorerPreviewSettings::GetSubKey() const + { + return this->m_subKey; + } - void ExplrSVGSttngs::EnablePreview() + LPCWSTR FileExplorerPreviewSettings::GetCLSID() const { - Trace::ExplorerSVGRenderEnabled(); + return this->m_clsid; } - void ExplrSVGSttngs::DisabledPreview() + std::wstring FileExplorerPreviewSettings::GetDisplayName() const { - Trace::ExplorerSVGRenderDisabled(); + return this->m_displayName; + } + + void FileExplorerPreviewSettings::SetDisplayName(const std::wstring& displayName) + { + this->m_displayName = displayName; } // Preview Pane SVG Render Settings - PrevPaneSVGRendrSettings::PrevPaneSVGRendrSettings() + PrevPaneSVGRendrSettings::PrevPaneSVGRendrSettings() : FileExplorerPreviewSettings( false, - GET_RESOURCE_STRING(IDS_PREVPANE_SVG_BOOL_TOGGLE_CONTROLL), - GET_RESOURCE_STRING(IDS_PREVPANE_SVG_SETTINGS_DESCRIPTION)){} + GET_RESOURCE_STRING(IDS_PREVPANE_SVG_BOOL_TOGGLE_CONTROLL), + GET_RESOURCE_STRING(IDS_PREVPANE_SVG_SETTINGS_DESCRIPTION), + L"{ddee2b8a-6807-48a6-bb20-2338174ff779}", + GET_RESOURCE_STRING(IDS_PREVPANE_SVG_SETTINGS_DISPLAYNAME)) {} void PrevPaneSVGRendrSettings::EnablePreview() { - Trace::ExplorerSVGRenderEnabled(); + if (this->SetRegistryValue() == ERROR_SUCCESS) + { + Trace::ExplorerSVGRenderEnabled(); + } + else + { + Trace::PowerPreviewSettingsUpDateFailed(this->GetName().c_str()); + this->SetState(false); + } } - void PrevPaneSVGRendrSettings::DisabledPreview() + void PrevPaneSVGRendrSettings::DisablePreview() { - Trace::ExplorerSVGRenderDisabled(); + if (this->DeleteRegistryValue() == ERROR_SUCCESS) + { + Trace::ExplorerSVGRenderDisabled(); + } + else + { + Trace::PowerPreviewSettingsUpDateFailed(this->GetName().c_str()); + this->SetState(true); + } } // Preview Pane Mark Down Render Settings - PrevPaneMDRendrSettings::PrevPaneMDRendrSettings() + PrevPaneMDRendrSettings::PrevPaneMDRendrSettings() : FileExplorerPreviewSettings( false, - GET_RESOURCE_STRING(IDS_PREVPANE_MD_BOOL_TOGGLE_CONTROLL), - GET_RESOURCE_STRING(IDS_PREVPANE_MD_SETTINGS_DESCRIPTION)){} + GET_RESOURCE_STRING(IDS_PREVPANE_MD_BOOL_TOGGLE_CONTROLL), + GET_RESOURCE_STRING(IDS_PREVPANE_MD_SETTINGS_DESCRIPTION), + L"{45769bcc-e8fd-42d0-947e-02beef77a1f5}", + GET_RESOURCE_STRING(IDS_PREVPANE_MD_SETTINGS_DISPLAYNAME)) {} void PrevPaneMDRendrSettings::EnablePreview() { - Trace::ExplorerSVGRenderEnabled(); + if (this->SetRegistryValue() == ERROR_SUCCESS) + { + Trace::ExplorerSVGRenderEnabled(); + } + else + { + Trace::PowerPreviewSettingsUpDateFailed(this->GetName().c_str()); + this->SetState(false); + } } - void PrevPaneMDRendrSettings::DisabledPreview() + void PrevPaneMDRendrSettings::DisablePreview() { - Trace::ExplorerSVGRenderDisabled(); + if (this->DeleteRegistryValue() == ERROR_SUCCESS) + { + Trace::ExplorerSVGRenderDisabled(); + } + else + { + Trace::PowerPreviewSettingsUpDateFailed(this->GetName().c_str()); + this->SetState(true); + } } -} \ No newline at end of file +} diff --git a/src/modules/previewpane/powerpreview/settings.h b/src/modules/previewpane/powerpreview/settings.h index 51cfaa543575..e235c1f78c16 100644 --- a/src/modules/previewpane/powerpreview/settings.h +++ b/src/modules/previewpane/powerpreview/settings.h @@ -1,4 +1,5 @@ #pragma once +#include #include #include "resource.h" #include @@ -10,50 +11,52 @@ namespace PowerPreviewSettings { private: bool m_isPreviewEnabled; - std::wstring m_name; - std::wstring m_description; + std::wstring m_name; + std::wstring m_description; + std::wstring m_displayName; + + LPCWSTR m_clsid; + LPCWSTR m_subKey = L"Software\\Microsoft\\Windows\\CurrentVersion\\PreviewHandlers"; public: - FileExplorerPreviewSettings(bool state, const std::wstring name, const std::wstring description); + FileExplorerPreviewSettings(bool state, const std::wstring name, const std::wstring description, LPCWSTR clsid, const std::wstring displayname); FileExplorerPreviewSettings(); virtual bool GetState() const; virtual void SetState(bool state); - virtual void LoadState(PowerToysSettings::PowerToyValues &settings); - virtual void UpdateState(PowerToysSettings::PowerToyValues &values); - virtual std::wstring GetName() const; - virtual void SetName(const std::wstring name); - virtual std::wstring GetDescription() const; - virtual void SetDescription(const std::wstring description); + virtual void LoadState(PowerToysSettings::PowerToyValues& settings); + virtual void UpdateState(PowerToysSettings::PowerToyValues& values); + virtual std::wstring GetName() const; + virtual void SetName(const std::wstring& name); + virtual std::wstring GetDescription() const; + virtual void SetDescription(const std::wstring& description); + virtual void SetDisplayName(const std::wstring& displayName); + virtual LONG SetRegistryValue() const; + virtual LONG DeleteRegistryValue() const; + virtual bool GetRegistryValue() const; + virtual std::wstring GetDisplayName() const; + virtual LPCWSTR GetCLSID() const; + virtual LPCWSTR GetSubKey() const; virtual void EnablePreview() = 0; - virtual void DisabledPreview() = 0; - }; - - class ExplrSVGSttngs: public FileExplorerPreviewSettings - { - public: - ExplrSVGSttngs(); - - virtual void EnablePreview(); - virtual void DisabledPreview(); + virtual void DisablePreview() = 0; }; - class PrevPaneSVGRendrSettings: public FileExplorerPreviewSettings + class PrevPaneSVGRendrSettings : public FileExplorerPreviewSettings { public: PrevPaneSVGRendrSettings(); virtual void EnablePreview(); - virtual void DisabledPreview(); + virtual void DisablePreview(); }; - class PrevPaneMDRendrSettings: public FileExplorerPreviewSettings + class PrevPaneMDRendrSettings : public FileExplorerPreviewSettings { public: PrevPaneMDRendrSettings(); virtual void EnablePreview(); - virtual void DisabledPreview(); + virtual void DisablePreview(); }; -} \ No newline at end of file +} diff --git a/src/modules/previewpane/powerpreviewTest/BaseSettingsClassTest.cpp b/src/modules/previewpane/powerpreviewTest/BaseSettingsClassTest.cpp index acdf7ec0d298..cad755137215 100644 --- a/src/modules/previewpane/powerpreviewTest/BaseSettingsClassTest.cpp +++ b/src/modules/previewpane/powerpreviewTest/BaseSettingsClassTest.cpp @@ -6,11 +6,13 @@ extern "C" IMAGE_DOS_HEADER __ImageBase; BaseSettingsClassTest::BaseSettingsClassTest() : - FileExplorerPreviewSettings( - false, - GET_RESOURCE_STRING(IDS_PREVPANE_MD_BOOL_TOGGLE_CONTROLL), - GET_RESOURCE_STRING(IDS_PREVPANE_MD_SETTINGS_DESCRIPTION)){} + FileExplorerPreviewSettings( + false, + GET_RESOURCE_STRING(IDS_PREVPANE_MD_BOOL_TOGGLE_CONTROLL), + GET_RESOURCE_STRING(IDS_PREVPANE_MD_SETTINGS_DESCRIPTION), + L"{test-guid}", + TEXT("Test Handler\0")) {} void BaseSettingsClassTest::EnablePreview() {} -void BaseSettingsClassTest::DisabledPreview() {} +void BaseSettingsClassTest::DisablePreview() {} diff --git a/src/modules/previewpane/powerpreviewTest/BaseSettingsClassTest.h b/src/modules/previewpane/powerpreviewTest/BaseSettingsClassTest.h index d70205e99b7f..d05ddd01b60c 100644 --- a/src/modules/previewpane/powerpreviewTest/BaseSettingsClassTest.h +++ b/src/modules/previewpane/powerpreviewTest/BaseSettingsClassTest.h @@ -1,6 +1,6 @@ #include "pch.h" #include - +#include using namespace PowerPreviewSettings; @@ -11,5 +11,5 @@ class BaseSettingsClassTest : public FileExplorerPreviewSettings virtual void EnablePreview(); - virtual void DisabledPreview(); + virtual void DisablePreview(); }; diff --git a/src/modules/previewpane/powerpreviewTest/FileExplorerPreviewSettingsTest.cpp b/src/modules/previewpane/powerpreviewTest/FileExplorerPreviewSettingsTest.cpp index 2db5cf1e6542..fd7589f36737 100644 --- a/src/modules/previewpane/powerpreviewTest/FileExplorerPreviewSettingsTest.cpp +++ b/src/modules/previewpane/powerpreviewTest/FileExplorerPreviewSettingsTest.cpp @@ -1,7 +1,5 @@ #include "pch.h" #include "CppUnitTest.h" -#include -#include #include #include @@ -11,44 +9,70 @@ using namespace PowerPreviewSettings; namespace BaseSettingsTest { - TEST_CLASS(FileExplorerPreviewSettingsTest) + TEST_CLASS(FileExplorerPreviewSettingsTest) { - public: - TEST_METHOD(LoadState_ShouldLoadNewState_WhenSucessfull) - { - // Arrange - BaseSettingsClassTest tempSettings = BaseSettingsClassTest(); - PowerToyValues values = PowerToyValues::from_json_string(GetJSONSettings(tempSettings.GetName(), L"true")); - tempSettings.SetState(false); - bool expectedState = true; - - // Act - tempSettings.LoadState(values); - bool actualState = tempSettings.GetState(); - - // Assert - Assert::AreEqual(actualState, expectedState); - } - - TEST_METHOD(UpdateState_ShouldChangeState_WhenSucessfull) - { - // Arrange - BaseSettingsClassTest tempSettings = BaseSettingsClassTest(); - PowerToyValues values = PowerToyValues::from_json_string(GetJSONSettings(tempSettings.GetName(), L"true")); - tempSettings.SetState(false); - bool expectedState = true; - - // Act - tempSettings.UpdateState(values); - bool actualState = tempSettings.GetState(); - - // Assert - Assert::AreEqual(actualState, expectedState); - } - - std::wstring GetJSONSettings(std::wstring _settingsNameId, std::wstring _value) const - { - return L"{\"name\":\"Module Name\",\"properties\" : {\""+_settingsNameId+L"\":{\"value\":"+_value+L"}},\"version\" : \"1.0\" }"; - } + public: + TEST_METHOD(LoadState_ShouldLoadNewState_WhenSucessfull) + { + // Arrange + BaseSettingsClassTest tempSettings = BaseSettingsClassTest(); + PowerToyValues values = PowerToyValues::from_json_string(GetJSONSettings(tempSettings.GetName(), L"true")); + tempSettings.SetState(false); + bool expectedState = true; + + // Act + tempSettings.LoadState(values); + bool actualState = tempSettings.GetState(); + + // Assert + Assert::AreEqual(actualState, expectedState); + } + + TEST_METHOD(UpdateState_ShouldChangeState_WhenSucessfull) + { + // Arrange + BaseSettingsClassTest tempSettings = BaseSettingsClassTest(); + PowerToyValues values = PowerToyValues::from_json_string(GetJSONSettings(tempSettings.GetName(), L"true")); + tempSettings.SetState(false); + bool expectedState = true; + + // Act + tempSettings.UpdateState(values); + bool actualState = tempSettings.GetState(); + + // Assert + Assert::AreEqual(actualState, expectedState); + } + + TEST_METHOD(SetRegistryValue_ShouldCreateAValueInRegistry_WhenSucessfull) + { + // Arrange + BaseSettingsClassTest tempSettings = BaseSettingsClassTest(); + + // Act + tempSettings.SetRegistryValue(); + bool results = tempSettings.GetRegistryValue(); + + // Assert + Assert::IsTrue(results); + } + + TEST_METHOD(RemoveRegistryValue_ShouldDeleteAValueInRegistry_WhenSucessfull) + { + // Arrange + BaseSettingsClassTest tempSettings = BaseSettingsClassTest(); + + // Act + tempSettings.SetRegistryValue(); + bool results = tempSettings.DeleteRegistryValue(); + + // Assert + Assert::IsFalse(results); + } + + std::wstring GetJSONSettings(std::wstring _settingsNameId, std::wstring _value) const + { + return L"{\"name\":\"Module Name\",\"properties\" : {\"" + _settingsNameId + L"\":{\"value\":" + _value + L"}},\"version\" : \"1.0\" }"; + } }; } diff --git a/src/modules/previewpane/powerpreviewTest/MarkDownPreviewSettingsClassTest.cpp b/src/modules/previewpane/powerpreviewTest/MarkDownPreviewSettingsClassTest.cpp new file mode 100644 index 000000000000..d1269c0f5e3d --- /dev/null +++ b/src/modules/previewpane/powerpreviewTest/MarkDownPreviewSettingsClassTest.cpp @@ -0,0 +1,56 @@ +#include "pch.h" +#include "CppUnitTest.h" +#include +#include +#include + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace PowerToysSettings; +using namespace PowerPreviewSettings; + +namespace BaseSettingsTest +{ + TEST_CLASS(MarkDownPreviewSettingsClassTest) + { + public: + TEST_METHOD(EnableRender_ShouldUpdateStateToTrue_WhenSuccessful) + { + // Arrange + PrevPaneMDRendrSettings tempSettings = PrevPaneMDRendrSettings(); + PowerToyValues values = PowerToyValues::from_json_string(GetJSONSettings(tempSettings.GetName(), L"false")); + tempSettings.UpdateState(values); + + // Act + tempSettings.EnablePreview(); + + // Assert + Assert::IsTrue(tempSettings.GetState()); + } + + TEST_METHOD(DisableRender_ShouldUpdateStateToFalse_WhenSuccessful) + { + // Arrange + PrevPaneMDRendrSettings tempSettings = PrevPaneMDRendrSettings(); + bool valueExists = tempSettings.GetRegistryValue(); // check if key-value exists. + + // Act + tempSettings.DisablePreview(); // should set state to false if Value exists. + bool previewState = tempSettings.GetState(); + + // Assert + if (valueExists) + { + Assert::IsFalse(previewState); + } + else + { + Assert::IsTrue(previewState); + } + } + + std::wstring GetJSONSettings(std::wstring _settingsNameId, std::wstring _value) const + { + return L"{\"name\":\"Module Name\",\"properties\" : {\"" + _settingsNameId + L"\":{\"value\":" + _value + L"}},\"version\" : \"1.0\" }"; + } + }; +} diff --git a/src/modules/previewpane/powerpreviewTest/SVGPreviewSettingsClassTest.cpp b/src/modules/previewpane/powerpreviewTest/SVGPreviewSettingsClassTest.cpp new file mode 100644 index 000000000000..614d8aa0103d --- /dev/null +++ b/src/modules/previewpane/powerpreviewTest/SVGPreviewSettingsClassTest.cpp @@ -0,0 +1,55 @@ +#include "pch.h" +#include "CppUnitTest.h" +#include +#include + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace PowerToysSettings; +using namespace PowerPreviewSettings; + +namespace BaseSettingsTest +{ + TEST_CLASS(SVGPreviewSettingsClassTest) + { + public: + TEST_METHOD(EnableRender_ShouldUpdateStateToTrue_WhenSuccessful) + { + // Arrange + PrevPaneSVGRendrSettings tempSettings = PrevPaneSVGRendrSettings(); + PowerToyValues values = PowerToyValues::from_json_string(GetJSONSettings(tempSettings.GetName(), L"false")); + tempSettings.UpdateState(values); + + // Act + tempSettings.EnablePreview(); + + // Assert + Assert::IsTrue(tempSettings.GetState()); + } + + TEST_METHOD(DisableRender_ShouldUpdateStateToFalse_WhenSuccessful) + { + // Arrange + PrevPaneSVGRendrSettings tempSettings = PrevPaneSVGRendrSettings(); + bool valueExists = tempSettings.GetRegistryValue(); // check if key-value exists. + + // Act + tempSettings.DisablePreview(); // should set state to false if Value exists. + bool previewState = tempSettings.GetState(); + + // Assert + if (valueExists) + { + Assert::IsFalse(previewState); + } + else + { + Assert::IsTrue(previewState); + } + } + + std::wstring GetJSONSettings(std::wstring _settingsNameId, std::wstring _value) const + { + return L"{\"name\":\"Module Name\",\"properties\" : {\"" + _settingsNameId + L"\":{\"value\":" + _value + L"}},\"version\" : \"1.0\" }"; + } + }; +} diff --git a/src/modules/previewpane/powerpreviewTest/powerpreviewTest.vcxproj b/src/modules/previewpane/powerpreviewTest/powerpreviewTest.vcxproj index 5ee7d5dcd1be..ea73522e8d00 100644 --- a/src/modules/previewpane/powerpreviewTest/powerpreviewTest.vcxproj +++ b/src/modules/previewpane/powerpreviewTest/powerpreviewTest.vcxproj @@ -160,12 +160,14 @@ + Create Create Create Create + diff --git a/src/modules/previewpane/powerpreviewTest/powerpreviewTest.vcxproj.filters b/src/modules/previewpane/powerpreviewTest/powerpreviewTest.vcxproj.filters index e66b80f996bd..2985fe0fc912 100644 --- a/src/modules/previewpane/powerpreviewTest/powerpreviewTest.vcxproj.filters +++ b/src/modules/previewpane/powerpreviewTest/powerpreviewTest.vcxproj.filters @@ -24,6 +24,12 @@ Source Files + + Source Files + + + Source Files +