Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / chromeos / login / user_manager_unittest.cc
blob7654601139e1bfa75db87f25adbcdec8ad76920a
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.
5 #include <cstdlib>
6 #include <cstring>
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"
26 namespace chromeos {
28 class UserManagerTest : public testing::Test {
29 protected:
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_);
37 EXPECT_TRUE(
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.
45 local_state_.reset(
46 new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal()));
48 ResetUserManager();
51 virtual void TearDown() OVERRIDE {
52 // Unregister the in-memory local settings instance.
53 local_state_.reset();
55 // Restore the real DeviceSettingsProvider.
56 EXPECT_TRUE(
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();
111 protected:
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);
139 ResetUserManager();
140 UserManager::Get()->UserLoggedIn(
141 "user0@invalid.domain", "owner@invalid.domain", false);
142 ResetUserManager();
143 UserManager::Get()->UserLoggedIn(
144 "user1@invalid.domain", "owner@invalid.domain", false);
145 ResetUserManager();
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);
167 ResetUserManager();
168 UserManager::Get()->UserLoggedIn(
169 "user0@invalid.domain", "user0@invalid.domain", false);
170 ResetUserManager();
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