Skip to content

Commit

Permalink
Added UT
Browse files Browse the repository at this point in the history
  • Loading branch information
AmelBawa-msft committed Dec 3, 2024
1 parent 1d7fcd0 commit 1720e20
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/AppInstallerCLITests/AppInstallerCLITests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@
<ClCompile Include="Downloader.cpp" />
<ClCompile Include="DownloadFlow.cpp" />
<ClCompile Include="Errors.cpp" />
<ClCompile Include="Experiment.cpp" />
<ClCompile Include="ExperimentalFeature.cpp" />
<ClCompile Include="ExportFlow.cpp" />
<ClCompile Include="FileCache.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions src/AppInstallerCLITests/AppInstallerCLITests.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@
<ClCompile Include="Fonts.cpp">
<Filter>Source Files\Common</Filter>
</ClCompile>
<ClCompile Include="Experiment.cpp">
<Filter>Source Files\Repository</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="PropertySheet.props" />
Expand Down
76 changes: 76 additions & 0 deletions src/AppInstallerCLITests/Experiment.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "TestCommon.h"
#include "TestSettings.h"
#include <winget/Experiment.h>
#include <AppInstallerStrings.h>

using namespace TestCommon;
using namespace AppInstaller::Settings;

#define SET_POLICY_STATE(_policy_, _state_) \
GroupPolicyTestOverride policies; \
policies.SetState(_policy_, _state_);

#define SET_USER_SETTINGS(_enabled_, _disabled_) \
TestUserSettings settings; \
settings.Set<Setting::Experiments>({ \
{"TestExperimentEnabledByDefault", _enabled_}, \
{"TestExperimentDisabledByDefault", _disabled_} \
});

#define ASSERT_EXPERIMENTS(_enabled_, _disabled_) \
REQUIRE(_enabled_ == Experiment::IsEnabled(Experiment::Key::TestExperimentEnabledByDefault)); \
REQUIRE(_disabled_ == Experiment::IsEnabled(Experiment::Key::TestExperimentDisabledByDefault));

TEST_CASE("Experiment_GroupPolicyControl", "[experiment]")
{
SECTION("Not configured")
{
SET_POLICY_STATE(TogglePolicy::Policy::Experiments, PolicyState::NotConfigured);
ASSERT_EXPERIMENTS(true, false);
}

SECTION("Enabled")
{
SET_POLICY_STATE(TogglePolicy::Policy::Experiments, PolicyState::Enabled);
ASSERT_EXPERIMENTS(true, false);
}

SECTION("Disabled")
{
SET_POLICY_STATE(TogglePolicy::Policy::Experiments, PolicyState::Disabled);
ASSERT_EXPERIMENTS(false, false);
}
}

TEST_CASE("Experiment_GroupPolicyDisabled_ReturnFalse", "[experiment]")
{
// If the policy is disabled, then also the user settings should be ignored.
SET_POLICY_STATE(TogglePolicy::Policy::Experiments, PolicyState::Disabled);
SET_USER_SETTINGS(true, true);
ASSERT_EXPERIMENTS(false, false);
}

TEST_CASE("Experiment_UserSettingsControl", "[experiment]")
{
SECTION("Experiments not configured in user settings")
{
// Default values are used
ASSERT_EXPERIMENTS(true, false);
}

SECTION("Experiments enabled in user settings")
{
SET_USER_SETTINGS(true, true);
ASSERT_EXPERIMENTS(true, true);
}

SECTION("Experiments disabled in user settings")
{
SET_USER_SETTINGS(false, false);
ASSERT_EXPERIMENTS(false, false);
}
}
1 change: 1 addition & 0 deletions src/AppInstallerCLITests/GroupPolicy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ TEST_CASE("GroupPolicy_AllEnabled", "[groupPolicy]")
SetRegistryValue(policiesKey.get(), EnableWindowsPackageManagerCommandLineInterfaces, 1);
SetRegistryValue(policiesKey.get(), ConfigurationPolicyValueName, 1);
SetRegistryValue(policiesKey.get(), ProxyCommandLineOptionsPolicyValueName, 1);
SetRegistryValue(policiesKey.get(), EnableExperimentsPolicyValueName , 1);

GroupPolicy groupPolicy{ policiesKey.get() };
for (const auto& policy : TogglePolicy::GetAllPolicies())
Expand Down
3 changes: 2 additions & 1 deletion src/AppInstallerCLITests/TestSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace TestCommon
const std::wstring EnableWindowsPackageManagerCommandLineInterfaces = L"EnableWindowsPackageManagerCommandLineInterfaces";
const std::wstring ConfigurationPolicyValueName = L"EnableWindowsPackageManagerConfiguration";
const std::wstring ProxyCommandLineOptionsPolicyValueName = L"EnableWindowsPackageManagerProxyCommandLineOptions";
const std::wstring EnableExperimentsPolicyValueName = L"EnableExperiments";

const std::wstring SourceUpdateIntervalPolicyValueName = L"SourceAutoUpdateInterval";
const std::wstring SourceUpdateIntervalPolicyOldValueName = L"SourceAutoUpdateIntervalInMinutes";
Expand Down Expand Up @@ -90,4 +91,4 @@ namespace TestCommon
};

#define REQUIRE_POLICY_EXCEPTION(_expr_, _policy_) REQUIRE_THROWS_MATCHES(_expr_, AppInstaller::Settings::GroupPolicyException, TestCommon::GroupPolicyExceptionMatcher(_policy_))
}
}
12 changes: 11 additions & 1 deletion src/AppInstallerCommonCore/Experiment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ namespace AppInstaller::Settings
bool Experiment::IsEnabled(Key key)
{
std::lock_guard lock(m_mutex);

#ifndef AICLI_DISABLE_TEST_HOOKS
m_isEnabledCache.clear();
#endif

if (m_isEnabledCache.find(key) == m_isEnabledCache.end())
{
m_isEnabledCache[key] = IsEnabledInternal(key, User());
Expand All @@ -66,7 +71,12 @@ namespace AppInstaller::Settings
{
case Key::CDN:
return Experiment{ "CDN experiment", "CDN", "https://aka.ms/winget-settings", "CDN"};

#ifndef AICLI_DISABLE_TEST_HOOKS
case Key::TestExperimentDisabledByDefault:
return Experiment{ "Test experiment disabled by default", "TestExperimentDisabledByDefault", "https://aka.ms/winget-settings", "TestExperimentDisabledByDefault" };
case Key::TestExperimentEnabledByDefault:
return Experiment{ "Test experiment enabled by default", "TestExperimentEnabledByDefault", "https://aka.ms/winget-settings", "TestExperimentEnabledByDefault" };
#endif
default:
THROW_HR(E_UNEXPECTED);
}
Expand Down
5 changes: 5 additions & 0 deletions src/AppInstallerCommonCore/Public/winget/Experiment.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ namespace AppInstaller::Settings
None = 0x0,
CDN = 0x1,
Max,

#ifndef AICLI_DISABLE_TEST_HOOKS
TestExperimentDisabledByDefault = 0xFFFFFFFE,
TestExperimentEnabledByDefault = 0xFFFFFFFF,
#endif
};

using Key_t = std::underlying_type_t<Key>;
Expand Down
15 changes: 13 additions & 2 deletions src/Internal/Experiment/Experiment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@

namespace AppInstaller::Experiment
{
bool IsEnabled(const std::string&)
bool IsEnabled(const std::string& key)
{
return true;
#ifndef AICLI_DISABLE_TEST_HOOKS
if (key == "TestExperimentEnabledByDefault")
{
return true;
}

if (key == "TestExperimentDisabledByDefault")
{
return false;
}
#endif
return false;
}
}

0 comments on commit 1720e20

Please sign in to comment.