1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 #include "base/memory/scoped_ptr.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/run_loop.h"
11 #include "base/values.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/chromeos/login/user.h"
14 #include "chrome/browser/chromeos/login/user_manager.h"
15 #include "chrome/browser/chromeos/login/user_manager_impl.h"
16 #include "chrome/browser/chromeos/settings/cros_settings.h"
17 #include "chrome/browser/chromeos/settings/device_settings_service.h"
18 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h"
19 #include "chrome/test/base/scoped_testing_local_state.h"
20 #include "chrome/test/base/testing_browser_process.h"
21 #include "chromeos/settings/cros_settings_names.h"
22 #include "chromeos/settings/cros_settings_provider.h"
23 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "testing/gtest/include/gtest/gtest.h"
28 class UserManagerTest
: public testing::Test
{
30 virtual void SetUp() OVERRIDE
{
31 cros_settings_
= CrosSettings::Get();
33 // Replace the real DeviceSettingsProvider with a stub.
34 device_settings_provider_
=
35 cros_settings_
->GetProvider(chromeos::kReportDeviceVersionInfo
);
36 EXPECT_TRUE(device_settings_provider_
);
38 cros_settings_
->RemoveSettingsProvider(device_settings_provider_
));
39 cros_settings_
->AddSettingsProvider(&stub_settings_provider_
);
41 // Populate the stub DeviceSettingsProvider with valid values.
42 SetDeviceSettings(false, "", false);
44 // Register an in-memory local settings instance.
46 new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal()));
51 virtual void TearDown() OVERRIDE
{
52 // Unregister the in-memory local settings instance.
55 // Restore the real DeviceSettingsProvider.
57 cros_settings_
->RemoveSettingsProvider(&stub_settings_provider_
));
58 cros_settings_
->AddSettingsProvider(device_settings_provider_
);
60 // Shut down the DeviceSettingsService.
61 DeviceSettingsService::Get()->UnsetSessionManager();
63 base::RunLoop().RunUntilIdle();
66 UserManagerImpl
* GetUserManagerImpl() const {
67 return static_cast<UserManagerImpl
*>(UserManager::Get());
70 bool GetUserManagerEphemeralUsersEnabled() const {
71 return GetUserManagerImpl()->ephemeral_users_enabled_
;
74 void SetUserManagerEphemeralUsersEnabled(bool ephemeral_users_enabled
) {
75 GetUserManagerImpl()->ephemeral_users_enabled_
= ephemeral_users_enabled
;
78 const std::string
& GetUserManagerOwnerEmail() const {
79 return GetUserManagerImpl()-> owner_email_
;
82 void SetUserManagerOwnerEmail(const std::string
& owner_email
) {
83 GetUserManagerImpl()->owner_email_
= owner_email
;
86 void ResetUserManager() {
87 // Reset the UserManager singleton.
88 user_manager_enabler_
.reset();
89 // Initialize the UserManager singleton to a fresh UserManagerImpl instance.
90 user_manager_enabler_
.reset(
91 new ScopedUserManagerEnabler(new UserManagerImpl
));
94 void SetDeviceSettings(bool ephemeral_users_enabled
,
95 const std::string
&owner
,
96 bool locally_managed_users_enabled
) {
97 base::FundamentalValue
98 ephemeral_users_enabled_value(ephemeral_users_enabled
);
99 stub_settings_provider_
.Set(kAccountsPrefEphemeralUsersEnabled
,
100 ephemeral_users_enabled_value
);
101 base::StringValue
owner_value(owner
);
102 stub_settings_provider_
.Set(kDeviceOwner
, owner_value
);
103 stub_settings_provider_
.Set(kAccountsPrefSupervisedUsersEnabled
,
104 base::FundamentalValue(locally_managed_users_enabled
));
107 void RetrieveTrustedDevicePolicies() {
108 GetUserManagerImpl()->RetrieveTrustedDevicePolicies();
112 content::TestBrowserThreadBundle thread_bundle_
;
114 CrosSettings
* cros_settings_
;
115 CrosSettingsProvider
* device_settings_provider_
;
116 StubCrosSettingsProvider stub_settings_provider_
;
117 scoped_ptr
<ScopedTestingLocalState
> local_state_
;
119 ScopedTestDeviceSettingsService test_device_settings_service_
;
120 ScopedTestCrosSettings test_cros_settings_
;
122 scoped_ptr
<ScopedUserManagerEnabler
> user_manager_enabler_
;
125 TEST_F(UserManagerTest
, RetrieveTrustedDevicePolicies
) {
126 SetUserManagerEphemeralUsersEnabled(true);
127 SetUserManagerOwnerEmail("");
129 SetDeviceSettings(false, "owner@invalid.domain", false);
130 RetrieveTrustedDevicePolicies();
132 EXPECT_FALSE(GetUserManagerEphemeralUsersEnabled());
133 EXPECT_EQ(GetUserManagerOwnerEmail(), "owner@invalid.domain");
136 TEST_F(UserManagerTest
, RemoveAllExceptOwnerFromList
) {
137 UserManager::Get()->UserLoggedIn(
138 "owner@invalid.domain", "owner@invalid.domain", false);
140 UserManager::Get()->UserLoggedIn(
141 "user0@invalid.domain", "owner@invalid.domain", false);
143 UserManager::Get()->UserLoggedIn(
144 "user1@invalid.domain", "owner@invalid.domain", false);
147 const UserList
* users
= &UserManager::Get()->GetUsers();
148 ASSERT_EQ(3U, users
->size());
149 EXPECT_EQ((*users
)[0]->email(), "user1@invalid.domain");
150 EXPECT_EQ((*users
)[1]->email(), "user0@invalid.domain");
151 EXPECT_EQ((*users
)[2]->email(), "owner@invalid.domain");
153 SetDeviceSettings(true, "owner@invalid.domain", false);
154 RetrieveTrustedDevicePolicies();
156 users
= &UserManager::Get()->GetUsers();
157 EXPECT_EQ(1U, users
->size());
158 EXPECT_EQ((*users
)[0]->email(), "owner@invalid.domain");
161 TEST_F(UserManagerTest
, RegularUserLoggedInAsEphemeral
) {
162 SetDeviceSettings(true, "owner@invalid.domain", false);
163 RetrieveTrustedDevicePolicies();
165 UserManager::Get()->UserLoggedIn(
166 "owner@invalid.domain", "user0@invalid.domain", false);
168 UserManager::Get()->UserLoggedIn(
169 "user0@invalid.domain", "user0@invalid.domain", false);
172 const UserList
* users
= &UserManager::Get()->GetUsers();
173 EXPECT_EQ(1U, users
->size());
174 EXPECT_EQ((*users
)[0]->email(), "owner@invalid.domain");
177 } // namespace chromeos