Update settings logic for file explorer preview (#1651)

* Update logic of settings for File Explorer preview

* Updated logic and added Unit tests

* Fix compiler warning for powerpreview project

* Update enable logic

* Update Disable logic

* Updated telemetry events
This commit is contained in:
udit3333 2020-03-24 16:23:27 -07:00 committed by GitHub
parent badcccab76
commit 7f1a854769
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 251 additions and 247 deletions

View File

@ -12,10 +12,16 @@
void PowerPreviewModule::destroy() void PowerPreviewModule::destroy()
{ {
Trace::Destroyed(); Trace::Destroyed();
for (FileExplorerPreviewSettings* previewHandler : this->m_previewHandlers) for (auto previewHandler : this->m_previewHandlers)
{ {
if (previewHandler != NULL) if (previewHandler != NULL)
{ {
// Disable all the active preview handlers.
if (this->m_enabled && previewHandler->GetToggleSettingState())
{
previewHandler->DisablePreview();
}
delete previewHandler; delete previewHandler;
} }
} }
@ -52,12 +58,12 @@ bool PowerPreviewModule::get_config(_Out_ wchar_t* buffer, _Out_ int* buffer_siz
GET_RESOURCE_STRING(IDS_PRVPANE_FILE_PREV_STTNGS_GROUP_DESC), GET_RESOURCE_STRING(IDS_PRVPANE_FILE_PREV_STTNGS_GROUP_DESC),
GET_RESOURCE_STRING(IDS_PRVPANE_FILE_PREV_STTNGS_GROUP_TEXT)); GET_RESOURCE_STRING(IDS_PRVPANE_FILE_PREV_STTNGS_GROUP_TEXT));
for (FileExplorerPreviewSettings * previewHandler : this->m_previewHandlers) for (auto previewHandler : this->m_previewHandlers)
{ {
settings.add_bool_toogle( settings.add_bool_toogle(
previewHandler->GetName(), previewHandler->GetToggleSettingName(),
previewHandler->GetDescription(), previewHandler->GetToggleSettingDescription(),
previewHandler->GetState()); previewHandler->GetToggleSettingState());
} }
return settings.serialize_to_buffer(buffer, buffer_size); return settings.serialize_to_buffer(buffer, buffer_size);
@ -68,14 +74,14 @@ void PowerPreviewModule::set_config(const wchar_t* config)
{ {
try try
{ {
PowerToysSettings::PowerToyValues values = PowerToysSettings::PowerToyValues::from_json_string(config); PowerToysSettings::PowerToyValues settings = PowerToysSettings::PowerToyValues::from_json_string(config);
for (FileExplorerPreviewSettings * previewHandler : this->m_previewHandlers) for (auto previewHandler : this->m_previewHandlers)
{ {
previewHandler->UpdateState(values); previewHandler->UpdateState(settings, this->m_enabled);
} }
values.save_to_settings_file(); settings.save_to_settings_file();
} }
catch (std::exception const& e) catch (std::exception const& e)
{ {
@ -86,17 +92,40 @@ void PowerPreviewModule::set_config(const wchar_t* config)
// Enable preview handlers. // Enable preview handlers.
void PowerPreviewModule::enable() void PowerPreviewModule::enable()
{ {
init_settings(); for (auto previewHandler : this->m_previewHandlers)
{
if (previewHandler->GetToggleSettingState())
{
// Enable all the previews with intial state set as true.
previewHandler->EnablePreview();
}
else
{
previewHandler->DisablePreview();
}
}
if (!this->m_enabled)
{
Trace::EnabledPowerPreview(true);
}
this->m_enabled = true; this->m_enabled = true;
} }
// Disable all preview handlers. // Disable active preview handlers.
void PowerPreviewModule::disable() void PowerPreviewModule::disable()
{ {
for (FileExplorerPreviewSettings * previewHandler : this->m_previewHandlers) for (auto previewHandler : this->m_previewHandlers)
{ {
previewHandler->DisablePreview(); previewHandler->DisablePreview();
} }
if (this->m_enabled)
{
Trace::EnabledPowerPreview(false);
}
this->m_enabled = false; this->m_enabled = false;
} }
@ -122,7 +151,7 @@ void PowerPreviewModule::init_settings()
PowerToysSettings::PowerToyValues::load_from_settings_file(PowerPreviewModule::get_name()); PowerToysSettings::PowerToyValues::load_from_settings_file(PowerPreviewModule::get_name());
// Load settings states. // Load settings states.
for (FileExplorerPreviewSettings * previewHandler : this->m_previewHandlers) for (auto previewHandler : this->m_previewHandlers)
{ {
previewHandler->LoadState(settings); previewHandler->LoadState(settings);
} }

View File

@ -24,20 +24,20 @@ public:
m_previewHandlers( m_previewHandlers(
{ // SVG Preview Hanlder settings object. { // SVG Preview Hanlder settings object.
new FileExplorerPreviewSettings( new FileExplorerPreviewSettings(
false, true,
GET_RESOURCE_STRING(IDS_PREVPANE_SVG_BOOL_TOGGLE_CONTROLL), L"svg-previewer-toggle-setting",
GET_RESOURCE_STRING(IDS_PREVPANE_SVG_SETTINGS_DESCRIPTION), GET_RESOURCE_STRING(IDS_PREVPANE_SVG_SETTINGS_DESCRIPTION),
L"{ddee2b8a-6807-48a6-bb20-2338174ff779}", L"{ddee2b8a-6807-48a6-bb20-2338174ff779}",
GET_RESOURCE_STRING(IDS_PREVPANE_SVG_SETTINGS_DISPLAYNAME), L"SVG Preview Handler",
new RegistryWrapper()), new RegistryWrapper()),
// MarkDown Preview Handler Settings Object. // MarkDown Preview Handler Settings Object.
new FileExplorerPreviewSettings( new FileExplorerPreviewSettings(
false, true,
GET_RESOURCE_STRING(IDS_PREVPANE_MD_BOOL_TOGGLE_CONTROLL), L"md-previewer-toggle-setting",
GET_RESOURCE_STRING(IDS_PREVPANE_MD_SETTINGS_DESCRIPTION), GET_RESOURCE_STRING(IDS_PREVPANE_MD_SETTINGS_DESCRIPTION),
L"{45769bcc-e8fd-42d0-947e-02beef77a1f5}", L"{45769bcc-e8fd-42d0-947e-02beef77a1f5}",
GET_RESOURCE_STRING(IDS_PREVPANE_MD_SETTINGS_DISPLAYNAME), L"Markdown Preview Handler",
new RegistryWrapper()) new RegistryWrapper())
}) })
{ {

View File

@ -116,7 +116,7 @@
<PrecompiledHeaderFile Condition="'$(Configuration)|$(Platform)'=='Release|x64'">pch.h</PrecompiledHeaderFile> <PrecompiledHeaderFile Condition="'$(Configuration)|$(Platform)'=='Release|x64'">pch.h</PrecompiledHeaderFile>
</ClCompile> </ClCompile>
<ClCompile Include="powerpreview.cpp" /> <ClCompile Include="powerpreview.cpp" />
<ClCompile Include="powerpreview.h" /> <ClInclude Include="powerpreview.h" />
<ClCompile Include="registry_wrapper.cpp" /> <ClCompile Include="registry_wrapper.cpp" />
<ClCompile Include="settings.cpp" /> <ClCompile Include="settings.cpp" />
<ClCompile Include="trace.cpp" /> <ClCompile Include="trace.cpp" />

View File

@ -5,7 +5,6 @@
<ClCompile Include="dllmain.cpp" /> <ClCompile Include="dllmain.cpp" />
<ClCompile Include="trace.cpp" /> <ClCompile Include="trace.cpp" />
<ClCompile Include="settings.cpp" /> <ClCompile Include="settings.cpp" />
<ClCompile Include="powerpreview.h" />
<ClCompile Include="powerpreview.cpp" /> <ClCompile Include="powerpreview.cpp" />
<ClCompile Include="registry_wrapper.cpp" /> <ClCompile Include="registry_wrapper.cpp" />
</ItemGroup> </ItemGroup>
@ -17,6 +16,7 @@
<ClInclude Include="CLSID.h" /> <ClInclude Include="CLSID.h" />
<ClInclude Include="registry_wrapper.h" /> <ClInclude Include="registry_wrapper.h" />
<ClInclude Include="registry_wrapper_interface.h" /> <ClInclude Include="registry_wrapper_interface.h" />
<ClInclude Include="powerpreview.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="powerpreview.rc" /> <ResourceCompile Include="powerpreview.rc" />

View File

@ -15,12 +15,12 @@ namespace PowerPreviewSettings
static LPCWSTR preview_handlers_subkey = L"Software\\Microsoft\\Windows\\CurrentVersion\\PreviewHandlers"; static LPCWSTR preview_handlers_subkey = L"Software\\Microsoft\\Windows\\CurrentVersion\\PreviewHandlers";
// Base Settinngs Class Implementation // Base Settinngs Class Implementation
FileExplorerPreviewSettings::FileExplorerPreviewSettings(bool enabled, const std::wstring& name, const std::wstring& description, LPCWSTR clsid, const std::wstring& displayname, RegistryWrapperIface * registryWrapper) : FileExplorerPreviewSettings::FileExplorerPreviewSettings(bool toggleSettingEnabled, const std::wstring& toggleSettingName, const std::wstring& toggleSettingDescription, LPCWSTR clsid, const std::wstring& registryValueData, RegistryWrapperIface* registryWrapper) :
m_isPreviewEnabled(enabled), m_toggleSettingEnabled(toggleSettingEnabled),
m_name(name), m_toggleSettingName(toggleSettingName),
m_description(description), m_toggleSettingDescription(toggleSettingDescription),
m_clsid(clsid), m_clsid(clsid),
m_displayName(displayname), m_registryValueData(registryValueData),
m_registryWrapper(registryWrapper) m_registryWrapper(registryWrapper)
{ {
} }
@ -33,68 +33,24 @@ namespace PowerPreviewSettings
} }
} }
bool FileExplorerPreviewSettings::GetState() const bool FileExplorerPreviewSettings::GetToggleSettingState() const
{ {
return this->m_isPreviewEnabled; return this->m_toggleSettingEnabled;
} }
void FileExplorerPreviewSettings::SetState(bool state) void FileExplorerPreviewSettings::UpdateToggleSettingState(bool state)
{ {
this->m_isPreviewEnabled = state; this->m_toggleSettingEnabled = state;
} }
void FileExplorerPreviewSettings::LoadState(PowerToysSettings::PowerToyValues& settings) std::wstring FileExplorerPreviewSettings::GetToggleSettingName() const
{ {
auto toggle = settings.get_bool_value(this->GetName()); return this->m_toggleSettingName;
if (toggle != std::nullopt)
{
this->m_isPreviewEnabled = toggle.value();
}
} }
void FileExplorerPreviewSettings::UpdateState(PowerToysSettings::PowerToyValues& values) std::wstring FileExplorerPreviewSettings::GetToggleSettingDescription() const
{ {
auto toggle = values.get_bool_value(this->GetName()); return this->m_toggleSettingDescription;
if (toggle != std::nullopt)
{
if (toggle.value())
{
this->EnablePreview();
}
else
{
this->DisablePreview();
}
}
else
{
Trace::PowerPreviewSettingsUpDateFailed(this->GetName().c_str());
}
}
std::wstring FileExplorerPreviewSettings::GetName() const
{
return this->m_name;
}
void FileExplorerPreviewSettings::SetName(const std::wstring& name)
{
this->m_name = name;
}
std::wstring FileExplorerPreviewSettings::GetDescription() const
{
return this->m_description;
}
void FileExplorerPreviewSettings::SetDescription(const std::wstring& description)
{
this->m_description = description;
}
LPCWSTR FileExplorerPreviewSettings::GetSubKey() const
{
return preview_handlers_subkey;
} }
LPCWSTR FileExplorerPreviewSettings::GetCLSID() const LPCWSTR FileExplorerPreviewSettings::GetCLSID() const
@ -102,45 +58,73 @@ namespace PowerPreviewSettings
return this->m_clsid; return this->m_clsid;
} }
std::wstring FileExplorerPreviewSettings::GetDisplayName() const std::wstring FileExplorerPreviewSettings::GetRegistryValueData() const
{ {
return this->m_displayName; return this->m_registryValueData;
} }
void FileExplorerPreviewSettings::SetDisplayName(const std::wstring& displayName) // Load intital state of the Preview Handler. If no inital state present initialize setting with default value.
void FileExplorerPreviewSettings::LoadState(PowerToysSettings::PowerToyValues& settings)
{ {
this->m_displayName = displayName; auto toggle = settings.get_bool_value(this->GetToggleSettingName());
if (toggle)
{
// If no exisiting setting found leave the default intitialization value.
this->UpdateToggleSettingState(*toggle);
}
} }
void FileExplorerPreviewSettings::EnablePreview() // Manage change in state of Preview Handler settings.
void FileExplorerPreviewSettings::UpdateState(PowerToysSettings::PowerToyValues& settings, bool enabled)
{
auto toggle = settings.get_bool_value(this->GetToggleSettingName());
if (toggle)
{
auto lastState = this->GetToggleSettingState();
auto newState = *toggle;
if (lastState != newState)
{
this->UpdateToggleSettingState(newState);
// If global setting is enable. Add or remove the preview handler otherwise just change the UI and save the updated config.
if (enabled)
{
LONG err;
if (lastState)
{
err = this->DisablePreview();
}
else
{
err = this->EnablePreview();
}
if (err == ERROR_SUCCESS)
{
Trace::PowerPreviewSettingsUpdated(this->GetToggleSettingName().c_str(), lastState, newState, enabled);
}
else
{
Trace::PowerPreviewSettingsUpdateFailed(this->GetToggleSettingName().c_str(), lastState, newState, enabled);
}
}
else
{
Trace::PowerPreviewSettingsUpdated(this->GetToggleSettingName().c_str(), lastState, newState, enabled);
}
}
}
}
LONG FileExplorerPreviewSettings::EnablePreview()
{ {
// Add registry value to enable preview. // Add registry value to enable preview.
LONG err = this->m_registryWrapper->SetRegistryValue(HKEY_CURRENT_USER, this->GetSubKey(), this->GetCLSID(), REG_SZ, (LPBYTE)this->GetDisplayName().c_str(), (DWORD)(this->GetDisplayName().length() * sizeof(wchar_t))); return this->m_registryWrapper->SetRegistryValue(HKEY_CURRENT_USER, preview_handlers_subkey, this->GetCLSID(), REG_SZ, (LPBYTE)this->GetRegistryValueData().c_str(), (DWORD)(this->GetRegistryValueData().length() * sizeof(wchar_t)));
if (err == ERROR_SUCCESS)
{
this->SetState(true);
Trace::PreviewHandlerEnabled(true, this->GetDisplayName().c_str());
}
else
{
Trace::PowerPreviewSettingsUpDateFailed(this->GetName().c_str());
}
} }
void FileExplorerPreviewSettings::DisablePreview() LONG FileExplorerPreviewSettings::DisablePreview()
{ {
// Delete the registry key to disable preview. // Delete the registry key to disable preview.
LONG err = this->m_registryWrapper->DeleteRegistryValue(HKEY_CURRENT_USER, this->GetSubKey(), this->GetCLSID()); return this->m_registryWrapper->DeleteRegistryValue(HKEY_CURRENT_USER, preview_handlers_subkey, this->GetCLSID());
if (err == ERROR_SUCCESS)
{
this->SetState(false);
Trace::PreviewHandlerEnabled(false, this->GetDisplayName().c_str());
}
else
{
Trace::PowerPreviewSettingsUpDateFailed(this->GetName().c_str());
}
} }
} }

View File

@ -11,31 +11,26 @@ namespace PowerPreviewSettings
class FileExplorerPreviewSettings class FileExplorerPreviewSettings
{ {
private: private:
bool m_isPreviewEnabled; bool m_toggleSettingEnabled;
std::wstring m_name; std::wstring m_toggleSettingName;
std::wstring m_description; std::wstring m_toggleSettingDescription;
std::wstring m_displayName; std::wstring m_registryValueData;
RegistryWrapperIface * m_registryWrapper; RegistryWrapperIface * m_registryWrapper;
LPCWSTR m_clsid; LPCWSTR m_clsid;
public: public:
FileExplorerPreviewSettings(bool enabled, const std::wstring& name, const std::wstring& description, LPCWSTR clsid, const std::wstring& displayname, RegistryWrapperIface* registryWrapper); FileExplorerPreviewSettings(bool toggleSettingEnabled, const std::wstring& toggleSettingName, const std::wstring& toggleSettingDescription, LPCWSTR clsid, const std::wstring& registryValueData, RegistryWrapperIface* registryWrapper);
~ FileExplorerPreviewSettings(); ~ FileExplorerPreviewSettings();
virtual bool GetState() const; virtual bool GetToggleSettingState() const;
virtual void SetState(bool state); virtual void UpdateToggleSettingState(bool state);
virtual void LoadState(PowerToysSettings::PowerToyValues& settings); virtual std::wstring GetToggleSettingName() const;
virtual void UpdateState(PowerToysSettings::PowerToyValues& values); virtual std::wstring GetToggleSettingDescription() const;
virtual std::wstring GetName() const; virtual LPCWSTR GetCLSID() const;
virtual void SetName(const std::wstring& name); virtual std::wstring GetRegistryValueData() const;
virtual std::wstring GetDescription() const; virtual void LoadState(PowerToysSettings::PowerToyValues& settings);
virtual void SetDescription(const std::wstring& description); virtual void UpdateState(PowerToysSettings::PowerToyValues& settings, bool enabled);
virtual void SetDisplayName(const std::wstring& displayName); virtual LONG EnablePreview();
virtual std::wstring GetDisplayName() const; virtual LONG DisablePreview();
virtual LPCWSTR GetCLSID() const;
virtual LPCWSTR GetSubKey() const;
virtual void EnablePreview();
virtual void DisablePreview();
}; };
} }

View File

@ -29,24 +29,40 @@ void Trace::UnregisterProvider()
TraceLoggingUnregister(g_hProvider); TraceLoggingUnregister(g_hProvider);
} }
void Trace::PreviewHandlerEnabled(bool enabled, LPCWSTR previewHandlerName) void Trace::EnabledPowerPreview(bool enabled)
{ {
TraceLoggingWrite( TraceLoggingWrite(
g_hProvider, g_hProvider,
"PowerPreview_TweakUISettings_Enabled", "PowerPreview_Enabled",
TraceLoggingWideString(previewHandlerName, "PreviewHanlder_FileType"),
TraceLoggingBoolean(enabled, "Enabled"), TraceLoggingBoolean(enabled, "Enabled"),
ProjectTelemetryPrivacyDataTag(ProjectTelemetryTag_ProductAndServicePerformance), ProjectTelemetryPrivacyDataTag(ProjectTelemetryTag_ProductAndServicePerformance),
TraceLoggingBoolean(TRUE, "UTCReplace_AppSessionGuid"), TraceLoggingBoolean(TRUE, "UTCReplace_AppSessionGuid"),
TraceLoggingKeyword(PROJECT_KEYWORD_MEASURE)); TraceLoggingKeyword(PROJECT_KEYWORD_MEASURE));
} }
void Trace::PowerPreviewSettingsUpDateFailed(LPCWSTR SettingsName) void Trace::PowerPreviewSettingsUpdated(LPCWSTR SettingsName, bool oldState, bool newState, bool globalState)
{
TraceLoggingWrite(
g_hProvider,
"PowerPreview_TweakUISettings_SuccesfullyUpdatedSettings",
TraceLoggingWideString(SettingsName, "Previewer_Settings_Name"),
TraceLoggingBoolean(oldState, "Old_Settings_State"),
TraceLoggingBoolean(newState, "New_Settings_State"),
TraceLoggingBoolean(globalState, "Global_File_Explorer_Settings_State"),
ProjectTelemetryPrivacyDataTag(ProjectTelemetryTag_ProductAndServicePerformance),
TraceLoggingBoolean(TRUE, "UTCReplace_AppSessionGuid"),
TraceLoggingKeyword(PROJECT_KEYWORD_MEASURE));
}
void Trace::PowerPreviewSettingsUpdateFailed(LPCWSTR SettingsName, bool oldState, bool newState, bool globalState)
{ {
TraceLoggingWrite( TraceLoggingWrite(
g_hProvider, g_hProvider,
"PowerPreview_TweakUISettings_FailedUpdatingSettings", "PowerPreview_TweakUISettings_FailedUpdatingSettings",
TraceLoggingWideString(SettingsName, "ExceptionMessage"), TraceLoggingWideString(SettingsName, "Previewer_Settings_Name"),
TraceLoggingBoolean(oldState, "Old_Settings_State"),
TraceLoggingBoolean(newState, "New_Settings_State"),
TraceLoggingBoolean(globalState, "Global_File_Explorer_Settings_State"),
ProjectTelemetryPrivacyDataTag(ProjectTelemetryTag_ProductAndServicePerformance), ProjectTelemetryPrivacyDataTag(ProjectTelemetryTag_ProductAndServicePerformance),
TraceLoggingBoolean(TRUE, "UTCReplace_AppSessionGuid"), TraceLoggingBoolean(TRUE, "UTCReplace_AppSessionGuid"),
TraceLoggingKeyword(PROJECT_KEYWORD_MEASURE)); TraceLoggingKeyword(PROJECT_KEYWORD_MEASURE));

View File

@ -7,7 +7,8 @@ public:
static void UnregisterProvider(); static void UnregisterProvider();
static void SetConfigInvalidJSON(const char* exceptionMessage); static void SetConfigInvalidJSON(const char* exceptionMessage);
static void InitSetErrorLoadingFile(const char* exceptionMessage); static void InitSetErrorLoadingFile(const char* exceptionMessage);
static void PreviewHandlerEnabled(bool enabled, LPCWSTR previewHandlerName); static void EnabledPowerPreview(bool enabled);
static void PowerPreviewSettingsUpDateFailed(LPCWSTR SettingsName); static void PowerPreviewSettingsUpdated(LPCWSTR SettingsName, bool oldState, bool newState, bool globalState);
static void PowerPreviewSettingsUpdateFailed(LPCWSTR SettingsName, bool oldState, bool newState, bool globalState);
static void Destroyed(); static void Destroyed();
}; };

View File

@ -19,7 +19,7 @@ namespace PreviewHandlerSettingsTest
public: public:
LONG ReturnValue = ERROR_SUCCESS; LONG ReturnValue = ERROR_SUCCESS;
int NumOfCalls = 0; int NumOfCalls = 0;
HKEY Scope; HKEY Scope = NULL;
LPCWSTR SubKey; LPCWSTR SubKey;
LPCWSTR ValueName; LPCWSTR ValueName;
}; };
@ -62,162 +62,141 @@ namespace PreviewHandlerSettingsTest
TEST_CLASS(BaseSettingsTest) TEST_CLASS(BaseSettingsTest)
{ {
public: public:
TEST_METHOD(LoadState_ShouldLoadNewState_WhenSucessfull)
{
// Arrange
FileExplorerPreviewSettings tempSettings = GetSttingsObjects(new RegistryMock());
PowerToyValues values = PowerToyValues::from_json_string(GetJSONSettings(tempSettings.GetName(), L"true"));
tempSettings.SetState(false);
bool expectedState = true;
// Act TEST_METHOD (LoadState_ShouldLoadValidState_IfInitalStateIsPresent)
tempSettings.LoadState(values);
bool actualState = tempSettings.GetState();
// Assert
Assert::AreEqual(actualState, expectedState);
}
TEST_METHOD(UpdateState_ShouldChangeState_WhenSucessfull)
{
// Arrange
FileExplorerPreviewSettings tempSettings = GetSttingsObjects(new RegistryMock());
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(EnableRender_ShouldUpdateStateToTrue_WhenSuccessful)
{ {
// Arrange // Arrange
FileExplorerPreviewSettings tempSettings = GetSttingsObjects(new RegistryMock()); bool defaultState = true;
tempSettings.SetState(false); //preview handler initially disabled RegistryMock* mockRegistryWrapper = new RegistryMock();
FileExplorerPreviewSettings previewSettings = GetSettingsObject(defaultState, mockRegistryWrapper);
auto settings = PowerToyValues::from_json_string(GetJSONSettings(previewSettings.GetToggleSettingName(), L"false"));
// Act // Act
tempSettings.EnablePreview(); previewSettings.LoadState(settings);
// Assert // Assert
Assert::IsTrue(tempSettings.GetState()); Assert::IsFalse(previewSettings.GetToggleSettingState());
} }
TEST_METHOD(DisableRender_ShouldUpdateStateToFalse_WhenSuccessful) TEST_METHOD (LoadState_ShouldNotChangeDefaultState_IfNoInitalStateIsPresent)
{ {
// Arrange // Arrange
FileExplorerPreviewSettings tempSettings = GetSttingsObjects(new RegistryMock()); bool defaultState = true;
tempSettings.SetState(true); //preview handler initially enabled RegistryMock* mockRegistryWrapper = new RegistryMock();
FileExplorerPreviewSettings previewSettings = GetSettingsObject(defaultState, mockRegistryWrapper);
auto settings = PowerToyValues::from_json_string(L"{\"name\":\"Module Name\"}");
// Act // Act
tempSettings.DisablePreview(); previewSettings.LoadState(settings);
// Assert // Assert
Assert::IsFalse(tempSettings.GetState()); Assert::AreEqual(previewSettings.GetToggleSettingState(), defaultState);
}
TEST_METHOD (UpdateState_ShouldDisablePreview_IfPreviewsAreEnabledAndNewSettingsStateIsFalse)
{
// Arrange
bool enabled = true;
RegistryMock* mockRegistryWrapper = new RegistryMock();
FileExplorerPreviewSettings previewSettings = GetSettingsObject(true, mockRegistryWrapper);
auto settings = PowerToyValues::from_json_string(GetJSONSettings(previewSettings.GetToggleSettingName(), L"false"));
previewSettings.UpdateToggleSettingState(true);
// Act
previewSettings.UpdateState(settings, enabled);
// Assert
Assert::IsFalse(previewSettings.GetToggleSettingState());
Assert::AreEqual(mockRegistryWrapper->DeleteRegistryMockProperties.NumOfCalls, 1);
}
TEST_METHOD (UpdateState_ShouldEnablePreview_IfPreviewsAreEnabledAndNewSettingsStateIsTrue)
{
// Arrange
bool enabled = true;
RegistryMock* mockRegistryWrapper = new RegistryMock();
FileExplorerPreviewSettings previewSettings = GetSettingsObject(true, mockRegistryWrapper);
auto settings = PowerToyValues::from_json_string(GetJSONSettings(previewSettings.GetToggleSettingName(), L"true"));
previewSettings.UpdateToggleSettingState(false);
// Act
previewSettings.UpdateState(settings, enabled);
// Assert
Assert::IsTrue(previewSettings.GetToggleSettingState());
Assert::AreEqual(mockRegistryWrapper->SetRegistryMockProperties.NumOfCalls, 1);
}
TEST_METHOD (UpdateState_ShouldOnlyUpdateToggleSettingState_IfPreviewsAreDisabled)
{
// Arrange
bool enabled = false;
RegistryMock* mockRegistryWrapper = new RegistryMock();
FileExplorerPreviewSettings previewSettings = GetSettingsObject(true, mockRegistryWrapper);
auto settings = PowerToyValues::from_json_string(GetJSONSettings(previewSettings.GetToggleSettingName(), L"false"));
// Act
previewSettings.UpdateState(settings, enabled);
// Assert
Assert::IsFalse(previewSettings.GetToggleSettingState());
Assert::AreEqual(mockRegistryWrapper->SetRegistryMockProperties.NumOfCalls, 0);
Assert::AreEqual(mockRegistryWrapper->DeleteRegistryMockProperties.NumOfCalls, 0);
}
TEST_METHOD (UpdateToggleSettingState_ShouldUpdateState_WhenCalled)
{
// Arrange
bool updatedState = false;
FileExplorerPreviewSettings previewSettings = GetSettingsObject(true, new RegistryMock());
// Act
previewSettings.UpdateToggleSettingState(updatedState);
// Assert
Assert::AreEqual(previewSettings.GetToggleSettingState(), updatedState);
} }
TEST_METHOD(EnablePreview_ShouldCallSetRegistryValueWithValidArguments_WhenCalled) TEST_METHOD(EnablePreview_ShouldCallSetRegistryValueWithValidArguments_WhenCalled)
{ {
// Arrange // Arrange
RegistryMock* mockRegistryWrapper = new RegistryMock(); RegistryMock* mockRegistryWrapper = new RegistryMock();
FileExplorerPreviewSettings tempSettings = GetSttingsObjects(mockRegistryWrapper); FileExplorerPreviewSettings previewSettings = GetSettingsObject(true, mockRegistryWrapper);
// Act // Act
tempSettings.EnablePreview(); previewSettings.EnablePreview();
// Assert // Assert
Assert::AreEqual(mockRegistryWrapper->SetRegistryMockProperties.NumOfCalls, 1); Assert::AreEqual(mockRegistryWrapper->SetRegistryMockProperties.NumOfCalls, 1);
Assert::AreEqual(mockRegistryWrapper->SetRegistryMockProperties.SubKey, tempSettings.GetSubKey()); Assert::AreEqual(mockRegistryWrapper->SetRegistryMockProperties.SubKey, preview_handlers_subkey);
Assert::AreEqual(mockRegistryWrapper->SetRegistryMockProperties.ValueName, tempSettings.GetCLSID()); Assert::AreEqual(mockRegistryWrapper->SetRegistryMockProperties.ValueName, previewSettings.GetCLSID());
Assert::AreEqual((ULONG_PTR)(mockRegistryWrapper->SetRegistryMockProperties.Scope), (ULONG_PTR)(HKEY_CURRENT_USER)); Assert::AreEqual((ULONG_PTR)(mockRegistryWrapper->SetRegistryMockProperties.Scope), (ULONG_PTR)(HKEY_CURRENT_USER));
} }
TEST_METHOD(EnablePreview_ShouldNotSetStateToTrue_IfSetRegistryValueFailed)
{
// Arrange
RegistryMock* mockRegistryWrapper = new RegistryMock();
mockRegistryWrapper->SetRegistryMockProperties.ReturnValue = ERROR_OUTOFMEMORY;
FileExplorerPreviewSettings tempSettings = GetSttingsObjects(mockRegistryWrapper);
tempSettings.SetState(false);
// Act
tempSettings.EnablePreview();
// Assert
Assert::IsFalse(tempSettings.GetState());
}
TEST_METHOD(EnablePreview_ShouldSetStateToTrue_IfSetRegistryValueReturnSuccessErrorCode)
{
// Arrange
RegistryMock* mockRegistryWrapper = new RegistryMock();
FileExplorerPreviewSettings tempSettings = GetSttingsObjects(mockRegistryWrapper);
tempSettings.SetState(false);
// Act
tempSettings.EnablePreview();
// Assert
Assert::IsTrue(tempSettings.GetState());
}
TEST_METHOD(DisablePreview_ShouldCallDeleteRegistryValueWithValidArguments_WhenCalled) TEST_METHOD(DisablePreview_ShouldCallDeleteRegistryValueWithValidArguments_WhenCalled)
{ {
// Arrange // Arrange
RegistryMock* mockRegistryWrapper = new RegistryMock(); RegistryMock* mockRegistryWrapper = new RegistryMock();
FileExplorerPreviewSettings tempSettings = GetSttingsObjects(mockRegistryWrapper); FileExplorerPreviewSettings previewSettings = GetSettingsObject(true, mockRegistryWrapper);
// Act // Act
tempSettings.DisablePreview(); previewSettings.DisablePreview();
// Assert // Assert
Assert::AreEqual(mockRegistryWrapper->DeleteRegistryMockProperties.NumOfCalls, 1); Assert::AreEqual(mockRegistryWrapper->DeleteRegistryMockProperties.NumOfCalls, 1);
Assert::AreEqual(mockRegistryWrapper->DeleteRegistryMockProperties.SubKey, tempSettings.GetSubKey()); Assert::AreEqual(mockRegistryWrapper->DeleteRegistryMockProperties.SubKey, preview_handlers_subkey);
Assert::AreEqual(mockRegistryWrapper->DeleteRegistryMockProperties.ValueName, tempSettings.GetCLSID()); Assert::AreEqual(mockRegistryWrapper->DeleteRegistryMockProperties.ValueName, previewSettings.GetCLSID());
Assert::AreEqual((ULONG_PTR)(mockRegistryWrapper->DeleteRegistryMockProperties.Scope), (ULONG_PTR)(HKEY_CURRENT_USER)); Assert::AreEqual((ULONG_PTR)(mockRegistryWrapper->DeleteRegistryMockProperties.Scope), (ULONG_PTR)(HKEY_CURRENT_USER));
} }
TEST_METHOD(DisablePreview_ShouldNotSetStateToFalse_IfDeleteRegistryValueFailed) FileExplorerPreviewSettings GetSettingsObject(bool defaultState, RegistryWrapperIface* registryMock)
{
// Arrange
RegistryMock* mockRegistryWrapper = new RegistryMock();
mockRegistryWrapper->DeleteRegistryMockProperties.ReturnValue = ERROR_OUTOFMEMORY;
FileExplorerPreviewSettings tempSettings = GetSttingsObjects(mockRegistryWrapper);
tempSettings.SetState(true);
// Act
tempSettings.DisablePreview();
// Assert
Assert::IsTrue(tempSettings.GetState());
}
TEST_METHOD(DisablePreview_ShouldSetStateToFalse_IfDeleteRegistryValueReturnSuccessErrorCode)
{
// Arrange
RegistryMock* mockRegistryWrapper = new RegistryMock();
FileExplorerPreviewSettings tempSettings = GetSttingsObjects(mockRegistryWrapper);
tempSettings.SetState(true);
// Act
tempSettings.DisablePreview();
// Assert
Assert::IsFalse(tempSettings.GetState());
}
FileExplorerPreviewSettings GetSttingsObjects(RegistryMock * registryMock)
{ {
return FileExplorerPreviewSettings( return FileExplorerPreviewSettings(
false, defaultState,
GET_RESOURCE_STRING(IDS_PREVPANE_MD_BOOL_TOGGLE_CONTROLL), L"valid-name",
GET_RESOURCE_STRING(IDS_PREVPANE_MD_SETTINGS_DESCRIPTION), L"valid-description",
L"{test-guid}", L"valid-guid",
TEXT("Test Handler\0"), L"valid-handler",
registryMock); registryMock);
} }