Commit 186b2a3c authored by stepco@chromium.org's avatar stepco@chromium.org

Add ScopedStubEnterpriseInstallAttributes for tests to set test install attributes.

This change is motivated by the need to set the test install attributes before the browser policy connector is initialized for the first time. In particular, test class members may cause the policy connector to be initialized in their constructors, so we also need to set the install attributes in the constructor of a member.

BUG=243341

Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=266094

Review URL: https://codereview.chromium.org/247283007

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@266771 0039d316-1c4b-4281-b951-d872f2087c98
parent fca1db81
......@@ -84,8 +84,10 @@ BrowserPolicyConnectorChromeOS::BrowserPolicyConnectorChromeOS()
: device_cloud_policy_manager_(NULL),
global_user_cloud_policy_provider_(NULL),
weak_ptr_factory_(this) {
if (g_testing_install_attributes)
if (g_testing_install_attributes) {
install_attributes_.reset(g_testing_install_attributes);
g_testing_install_attributes = NULL;
}
// SystemSaltGetter or DBusThreadManager may be uninitialized on unit tests.
......@@ -95,7 +97,7 @@ BrowserPolicyConnectorChromeOS::BrowserPolicyConnectorChromeOS()
chromeos::DBusThreadManager::IsInitialized()) {
chromeos::CryptohomeClient* cryptohome_client =
chromeos::DBusThreadManager::Get()->GetCryptohomeClient();
if (!g_testing_install_attributes) {
if (!install_attributes_) {
install_attributes_.reset(
new EnterpriseInstallAttributes(cryptohome_client));
}
......@@ -243,6 +245,13 @@ void BrowserPolicyConnectorChromeOS::SetInstallAttributesForTesting(
g_testing_install_attributes = attributes;
}
void BrowserPolicyConnectorChromeOS::RemoveInstallAttributesForTesting() {
if (g_testing_install_attributes) {
delete g_testing_install_attributes;
g_testing_install_attributes = NULL;
}
}
// static
void BrowserPolicyConnectorChromeOS::RegisterPrefs(
PrefRegistrySimple* registry) {
......
......@@ -92,9 +92,11 @@ class BrowserPolicyConnectorChromeOS : public ChromeBrowserPolicyConnector {
}
// Sets the install attributes for testing. Must be called before the browser
// is created. Takes ownership of |attributes|.
// is created. RemoveInstallAttributesForTesting must be called after the test
// to free the attributes.
static void SetInstallAttributesForTesting(
EnterpriseInstallAttributes* attributes);
static void RemoveInstallAttributesForTesting();
// Registers device refresh rate pref.
static void RegisterPrefs(PrefRegistrySimple* registry);
......
......@@ -152,6 +152,10 @@ class DeviceStatusCollectorTest : public testing::Test {
: ui_thread_(content::BrowserThread::UI, &message_loop_),
file_thread_(content::BrowserThread::FILE, &message_loop_),
io_thread_(content::BrowserThread::IO, &message_loop_),
install_attributes_("managed.com",
"user@managed.com",
"device_id",
DEVICE_MODE_ENTERPRISE),
user_manager_(new chromeos::MockUserManager()),
user_manager_enabler_(user_manager_) {
// Run this test with a well-known timezone so that Time::LocalMidnight()
......@@ -173,13 +177,6 @@ class DeviceStatusCollectorTest : public testing::Test {
cros_settings_->RemoveSettingsProvider(device_settings_provider_));
cros_settings_->AddSettingsProvider(&stub_settings_provider_);
// Set up fake install attributes.
StubEnterpriseInstallAttributes* attributes =
new StubEnterpriseInstallAttributes();
attributes->SetDomain("managed.com");
attributes->SetRegistrationUser("user@managed.com");
BrowserPolicyConnectorChromeOS::SetInstallAttributesForTesting(attributes);
RestartStatusCollector();
}
......@@ -261,6 +258,7 @@ class DeviceStatusCollectorTest : public testing::Test {
content::TestBrowserThread file_thread_;
content::TestBrowserThread io_thread_;
ScopedStubEnterpriseInstallAttributes install_attributes_;
TestingPrefServiceSimple prefs_;
chromeos::system::MockStatisticsProvider statistics_provider_;
chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
......
......@@ -6,6 +6,7 @@
#include <string>
#include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
#include "components/policy/core/common/cloud/cloud_policy_constants.h"
namespace policy {
......@@ -32,4 +33,23 @@ void StubEnterpriseInstallAttributes::SetMode(DeviceMode mode) {
registration_mode_ = mode;
}
ScopedStubEnterpriseInstallAttributes::ScopedStubEnterpriseInstallAttributes(
const std::string& domain,
const std::string& registration_user,
const std::string& device_id,
DeviceMode mode) {
StubEnterpriseInstallAttributes* attributes =
new StubEnterpriseInstallAttributes();
attributes->SetDomain(domain);
attributes->SetRegistrationUser(registration_user);
attributes->SetDeviceId(device_id);
attributes->SetMode(mode);
BrowserPolicyConnectorChromeOS::SetInstallAttributesForTesting(attributes);
}
ScopedStubEnterpriseInstallAttributes::
~ScopedStubEnterpriseInstallAttributes() {
BrowserPolicyConnectorChromeOS::RemoveInstallAttributesForTesting();
}
} // namespace policy
......@@ -29,6 +29,16 @@ class StubEnterpriseInstallAttributes : public EnterpriseInstallAttributes {
DISALLOW_COPY_AND_ASSIGN(StubEnterpriseInstallAttributes);
};
// Helper class to set enterprise install attributes in the scope of a test.
class ScopedStubEnterpriseInstallAttributes {
public:
ScopedStubEnterpriseInstallAttributes(const std::string& domain,
const std::string& registration_user,
const std::string& device_id,
DeviceMode mode);
~ScopedStubEnterpriseInstallAttributes();
};
} // namespace policy
#endif // CHROME_BROWSER_CHROMEOS_POLICY_STUB_ENTERPRISE_INSTALL_ATTRIBUTES_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment