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()
{
Trace::Destroyed();
for (FileExplorerPreviewSettings* previewHandler : this->m_previewHandlers)
for (auto previewHandler : this->m_previewHandlers)
{
if (previewHandler != NULL)
{
// Disable all the active preview handlers.
if (this->m_enabled && previewHandler->GetToggleSettingState())
{
previewHandler->DisablePreview();
}
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_TEXT));
for (FileExplorerPreviewSettings * previewHandler : this->m_previewHandlers)
for (auto previewHandler : this->m_previewHandlers)
{
settings.add_bool_toogle(
previewHandler->GetName(),
previewHandler->GetDescription(),
previewHandler->GetState());
previewHandler->GetToggleSettingName(),
previewHandler->GetToggleSettingDescription(),
previewHandler->GetToggleSettingState());
}
return settings.serialize_to_buffer(buffer, buffer_size);
@ -68,14 +74,14 @@ void PowerPreviewModule::set_config(const wchar_t* config)
{
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)
{
@ -86,17 +92,40 @@ void PowerPreviewModule::set_config(const wchar_t* config)
// Enable preview handlers.
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;
}
// Disable all preview handlers.
// Disable active preview handlers.
void PowerPreviewModule::disable()
{
for (FileExplorerPreviewSettings * previewHandler : this->m_previewHandlers)
for (auto previewHandler : this->m_previewHandlers)
{
previewHandler->DisablePreview();
}
if (this->m_enabled)
{
Trace::EnabledPowerPreview(false);
}
this->m_enabled = false;
}
@ -122,7 +151,7 @@ void PowerPreviewModule::init_settings()
PowerToysSettings::PowerToyValues::load_from_settings_file(PowerPreviewModule::get_name());
// Load settings states.
for (FileExplorerPreviewSettings * previewHandler : this->m_previewHandlers)
for (auto previewHandler : this->m_previewHandlers)
{
previewHandler->LoadState(settings);
}

View File

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

View File

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

View File

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

View File

@ -15,12 +15,12 @@ namespace PowerPreviewSettings
static LPCWSTR preview_handlers_subkey = L"Software\\Microsoft\\Windows\\CurrentVersion\\PreviewHandlers";
// Base Settinngs Class Implementation
FileExplorerPreviewSettings::FileExplorerPreviewSettings(bool enabled, const std::wstring& name, const std::wstring& description, LPCWSTR clsid, const std::wstring& displayname, RegistryWrapperIface * registryWrapper) :
m_isPreviewEnabled(enabled),
m_name(name),
m_description(description),
FileExplorerPreviewSettings::FileExplorerPreviewSettings(bool toggleSettingEnabled, const std::wstring& toggleSettingName, const std::wstring& toggleSettingDescription, LPCWSTR clsid, const std::wstring& registryValueData, RegistryWrapperIface* registryWrapper) :
m_toggleSettingEnabled(toggleSettingEnabled),
m_toggleSettingName(toggleSettingName),
m_toggleSettingDescription(toggleSettingDescription),
m_clsid(clsid),
m_displayName(displayname),
m_registryValueData(registryValueData),
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());
if (toggle != std::nullopt)
{
this->m_isPreviewEnabled = toggle.value();
}
return this->m_toggleSettingName;
}
void FileExplorerPreviewSettings::UpdateState(PowerToysSettings::PowerToyValues& values)
std::wstring FileExplorerPreviewSettings::GetToggleSettingDescription() const
{
auto toggle = values.get_bool_value(this->GetName());
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;
return this->m_toggleSettingDescription;
}
LPCWSTR FileExplorerPreviewSettings::GetCLSID() const
@ -102,45 +58,73 @@ namespace PowerPreviewSettings
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.
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)));
if (err == ERROR_SUCCESS)
{
this->SetState(true);
Trace::PreviewHandlerEnabled(true, this->GetDisplayName().c_str());
}
else
{
Trace::PowerPreviewSettingsUpDateFailed(this->GetName().c_str());
}
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)));
}
void FileExplorerPreviewSettings::DisablePreview()
LONG FileExplorerPreviewSettings::DisablePreview()
{
// Delete the registry key to disable preview.
LONG err = this->m_registryWrapper->DeleteRegistryValue(HKEY_CURRENT_USER, this->GetSubKey(), this->GetCLSID());
if (err == ERROR_SUCCESS)
{
this->SetState(false);
Trace::PreviewHandlerEnabled(false, this->GetDisplayName().c_str());
}
else
{
Trace::PowerPreviewSettingsUpDateFailed(this->GetName().c_str());
}
return this->m_registryWrapper->DeleteRegistryValue(HKEY_CURRENT_USER, preview_handlers_subkey, this->GetCLSID());
}
}

View File

@ -11,31 +11,26 @@ namespace PowerPreviewSettings
class FileExplorerPreviewSettings
{
private:
bool m_isPreviewEnabled;
std::wstring m_name;
std::wstring m_description;
std::wstring m_displayName;
bool m_toggleSettingEnabled;
std::wstring m_toggleSettingName;
std::wstring m_toggleSettingDescription;
std::wstring m_registryValueData;
RegistryWrapperIface * m_registryWrapper;
LPCWSTR m_clsid;
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();
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 SetDisplayName(const std::wstring& displayName);
virtual std::wstring GetDisplayName() const;
virtual LPCWSTR GetCLSID() const;
virtual LPCWSTR GetSubKey() const;
virtual void EnablePreview();
virtual void DisablePreview();
virtual bool GetToggleSettingState() const;
virtual void UpdateToggleSettingState(bool state);
virtual std::wstring GetToggleSettingName() const;
virtual std::wstring GetToggleSettingDescription() const;
virtual LPCWSTR GetCLSID() const;
virtual std::wstring GetRegistryValueData() const;
virtual void LoadState(PowerToysSettings::PowerToyValues& settings);
virtual void UpdateState(PowerToysSettings::PowerToyValues& settings, bool enabled);
virtual LONG EnablePreview();
virtual LONG DisablePreview();
};
}

View File

@ -29,24 +29,40 @@ void Trace::UnregisterProvider()
TraceLoggingUnregister(g_hProvider);
}
void Trace::PreviewHandlerEnabled(bool enabled, LPCWSTR previewHandlerName)
void Trace::EnabledPowerPreview(bool enabled)
{
TraceLoggingWrite(
g_hProvider,
"PowerPreview_TweakUISettings_Enabled",
TraceLoggingWideString(previewHandlerName, "PreviewHanlder_FileType"),
"PowerPreview_Enabled",
TraceLoggingBoolean(enabled, "Enabled"),
ProjectTelemetryPrivacyDataTag(ProjectTelemetryTag_ProductAndServicePerformance),
TraceLoggingBoolean(TRUE, "UTCReplace_AppSessionGuid"),
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(
g_hProvider,
"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),
TraceLoggingBoolean(TRUE, "UTCReplace_AppSessionGuid"),
TraceLoggingKeyword(PROJECT_KEYWORD_MEASURE));

View File

@ -7,7 +7,8 @@ public:
static void UnregisterProvider();
static void SetConfigInvalidJSON(const char* exceptionMessage);
static void InitSetErrorLoadingFile(const char* exceptionMessage);
static void PreviewHandlerEnabled(bool enabled, LPCWSTR previewHandlerName);
static void PowerPreviewSettingsUpDateFailed(LPCWSTR SettingsName);
static void EnabledPowerPreview(bool enabled);
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();
};

View File

@ -19,7 +19,7 @@ namespace PreviewHandlerSettingsTest
public:
LONG ReturnValue = ERROR_SUCCESS;
int NumOfCalls = 0;
HKEY Scope;
HKEY Scope = NULL;
LPCWSTR SubKey;
LPCWSTR ValueName;
};
@ -62,162 +62,141 @@ namespace PreviewHandlerSettingsTest
TEST_CLASS(BaseSettingsTest)
{
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
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)
TEST_METHOD (LoadState_ShouldLoadValidState_IfInitalStateIsPresent)
{
// Arrange
FileExplorerPreviewSettings tempSettings = GetSttingsObjects(new RegistryMock());
tempSettings.SetState(false); //preview handler initially disabled
bool defaultState = true;
RegistryMock* mockRegistryWrapper = new RegistryMock();
FileExplorerPreviewSettings previewSettings = GetSettingsObject(defaultState, mockRegistryWrapper);
auto settings = PowerToyValues::from_json_string(GetJSONSettings(previewSettings.GetToggleSettingName(), L"false"));
// Act
tempSettings.EnablePreview();
previewSettings.LoadState(settings);
// Assert
Assert::IsTrue(tempSettings.GetState());
Assert::IsFalse(previewSettings.GetToggleSettingState());
}
TEST_METHOD(DisableRender_ShouldUpdateStateToFalse_WhenSuccessful)
TEST_METHOD (LoadState_ShouldNotChangeDefaultState_IfNoInitalStateIsPresent)
{
// Arrange
FileExplorerPreviewSettings tempSettings = GetSttingsObjects(new RegistryMock());
tempSettings.SetState(true); //preview handler initially enabled
bool defaultState = true;
RegistryMock* mockRegistryWrapper = new RegistryMock();
FileExplorerPreviewSettings previewSettings = GetSettingsObject(defaultState, mockRegistryWrapper);
auto settings = PowerToyValues::from_json_string(L"{\"name\":\"Module Name\"}");
// Act
tempSettings.DisablePreview();
previewSettings.LoadState(settings);
// 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)
{
// Arrange
RegistryMock* mockRegistryWrapper = new RegistryMock();
FileExplorerPreviewSettings tempSettings = GetSttingsObjects(mockRegistryWrapper);
FileExplorerPreviewSettings previewSettings = GetSettingsObject(true, mockRegistryWrapper);
// Act
tempSettings.EnablePreview();
previewSettings.EnablePreview();
// Assert
Assert::AreEqual(mockRegistryWrapper->SetRegistryMockProperties.NumOfCalls, 1);
Assert::AreEqual(mockRegistryWrapper->SetRegistryMockProperties.SubKey, tempSettings.GetSubKey());
Assert::AreEqual(mockRegistryWrapper->SetRegistryMockProperties.ValueName, tempSettings.GetCLSID());
Assert::AreEqual(mockRegistryWrapper->SetRegistryMockProperties.SubKey, preview_handlers_subkey);
Assert::AreEqual(mockRegistryWrapper->SetRegistryMockProperties.ValueName, previewSettings.GetCLSID());
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)
{
// Arrange
RegistryMock* mockRegistryWrapper = new RegistryMock();
FileExplorerPreviewSettings tempSettings = GetSttingsObjects(mockRegistryWrapper);
FileExplorerPreviewSettings previewSettings = GetSettingsObject(true, mockRegistryWrapper);
// Act
tempSettings.DisablePreview();
previewSettings.DisablePreview();
// Assert
Assert::AreEqual(mockRegistryWrapper->DeleteRegistryMockProperties.NumOfCalls, 1);
Assert::AreEqual(mockRegistryWrapper->DeleteRegistryMockProperties.SubKey, tempSettings.GetSubKey());
Assert::AreEqual(mockRegistryWrapper->DeleteRegistryMockProperties.ValueName, tempSettings.GetCLSID());
Assert::AreEqual(mockRegistryWrapper->DeleteRegistryMockProperties.SubKey, preview_handlers_subkey);
Assert::AreEqual(mockRegistryWrapper->DeleteRegistryMockProperties.ValueName, previewSettings.GetCLSID());
Assert::AreEqual((ULONG_PTR)(mockRegistryWrapper->DeleteRegistryMockProperties.Scope), (ULONG_PTR)(HKEY_CURRENT_USER));
}
TEST_METHOD(DisablePreview_ShouldNotSetStateToFalse_IfDeleteRegistryValueFailed)
{
// 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)
FileExplorerPreviewSettings GetSettingsObject(bool defaultState, RegistryWrapperIface* registryMock)
{
return 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"),
defaultState,
L"valid-name",
L"valid-description",
L"valid-guid",
L"valid-handler",
registryMock);
}