Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / ash / session_state_delegate_chromeos_unittest.cc
blob8c3b0776deb6ec6f0f622f1cbeec497e4e6a637f
1 // Copyright 2013 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 "chrome/browser/ui/ash/session_state_delegate_chromeos.h"
7 #include <string>
8 #include <vector>
10 #include "base/memory/scoped_ptr.h"
11 #include "base/run_loop.h"
12 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
13 #include "chrome/browser/chromeos/login/users/multi_profile_user_controller.h"
14 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
15 #include "chrome/browser/chromeos/policy/policy_cert_service.h"
16 #include "chrome/browser/chromeos/policy/policy_cert_service_factory.h"
17 #include "chrome/browser/chromeos/policy/policy_cert_verifier.h"
18 #include "chrome/browser/chromeos/profiles/profile_helper.h"
19 #include "chrome/common/pref_names.h"
20 #include "chrome/test/base/testing_browser_process.h"
21 #include "chrome/test/base/testing_profile_manager.h"
22 #include "components/user_manager/user_manager.h"
23 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "net/cert/x509_certificate.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 namespace chromeos {
29 namespace {
31 const char* kUser = "user@test.com";
33 // Weak ptr to PolicyCertVerifier - object is freed in test destructor once
34 // we've ensured the profile has been shut down.
35 policy::PolicyCertVerifier* g_policy_cert_verifier_for_factory = NULL;
37 scoped_ptr<KeyedService> CreateTestPolicyCertService(
38 content::BrowserContext* context) {
39 return policy::PolicyCertService::CreateForTesting(
40 kUser, g_policy_cert_verifier_for_factory,
41 user_manager::UserManager::Get());
44 } // namespace
46 class SessionStateDelegateChromeOSTest : public testing::Test {
47 protected:
48 SessionStateDelegateChromeOSTest() : user_manager_(NULL) {
51 ~SessionStateDelegateChromeOSTest() override {}
53 void SetUp() override {
54 // Initialize the UserManager singleton to a fresh FakeChromeUserManager
55 // instance.
56 user_manager_ = new FakeChromeUserManager;
57 user_manager_enabler_.reset(
58 new chromeos::ScopedUserManagerEnabler(user_manager_));
60 // Create our SessionStateDelegate to experiment with.
61 session_state_delegate_.reset(new SessionStateDelegateChromeos());
62 testing::Test::SetUp();
65 void TearDown() override {
66 testing::Test::TearDown();
67 session_state_delegate_.reset();
68 user_manager_enabler_.reset();
69 user_manager_ = NULL;
70 // Clear our cached pointer to the PolicyCertVerifier.
71 g_policy_cert_verifier_for_factory = NULL;
72 profile_manager_.reset();
74 // We must ensure that the PolicyCertVerifier outlives the
75 // PolicyCertService so shutdown the profile here. Additionally, we need
76 // to run the message loop between freeing the PolicyCertService and
77 // freeing the PolicyCertVerifier (see
78 // PolicyCertService::OnTrustAnchorsChanged() which is called from
79 // PolicyCertService::Shutdown()).
80 base::RunLoop().RunUntilIdle();
83 // Add and log in a user to the session.
84 void UserAddedToSession(std::string user) {
85 user_manager()->AddUser(user);
86 user_manager()->LoginUser(user);
89 // Get the active user.
90 const std::string& GetActiveUser() {
91 return user_manager::UserManager::Get()->GetActiveUser()->email();
94 FakeChromeUserManager* user_manager() { return user_manager_; }
95 SessionStateDelegateChromeos* session_state_delegate() {
96 return session_state_delegate_.get();
99 void InitForMultiProfile() {
100 profile_manager_.reset(
101 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
102 ASSERT_TRUE(profile_manager_->SetUp());
104 const std::string user_email(kUser);
105 const user_manager::User* user = user_manager()->AddUser(user_email);
107 // Note that user profiles are created after user login in reality.
108 user_profile_ = profile_manager_->CreateTestingProfile(user_email);
109 user_profile_->set_profile_name(user_email);
110 chromeos::ProfileHelper::Get()->SetUserToProfileMappingForTesting(
111 user, user_profile_);
114 content::TestBrowserThreadBundle threads_;
115 scoped_ptr<policy::PolicyCertVerifier> cert_verifier_;
116 scoped_ptr<TestingProfileManager> profile_manager_;
117 TestingProfile* user_profile_;
119 private:
120 scoped_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_;
121 scoped_ptr<SessionStateDelegateChromeos> session_state_delegate_;
123 // Not owned.
124 FakeChromeUserManager* user_manager_;
126 DISALLOW_COPY_AND_ASSIGN(SessionStateDelegateChromeOSTest);
129 // Make sure that cycling one user does not cause any harm.
130 TEST_F(SessionStateDelegateChromeOSTest, CyclingOneUser) {
131 UserAddedToSession("firstuser@test.com");
133 EXPECT_EQ("firstuser@test.com", GetActiveUser());
134 session_state_delegate()->CycleActiveUser(
135 ash::SessionStateDelegate::CYCLE_TO_NEXT_USER);
136 EXPECT_EQ("firstuser@test.com", GetActiveUser());
137 session_state_delegate()->CycleActiveUser(
138 ash::SessionStateDelegate::CYCLE_TO_PREVIOUS_USER);
139 EXPECT_EQ("firstuser@test.com", GetActiveUser());
142 // Cycle three users forwards and backwards to see that it works.
143 TEST_F(SessionStateDelegateChromeOSTest, CyclingThreeUsers) {
144 UserAddedToSession("firstuser@test.com");
145 UserAddedToSession("seconduser@test.com");
146 UserAddedToSession("thirduser@test.com");
147 const ash::SessionStateDelegate::CycleUser forward =
148 ash::SessionStateDelegate::CYCLE_TO_NEXT_USER;
150 // Cycle forward.
151 EXPECT_EQ("firstuser@test.com", GetActiveUser());
152 session_state_delegate()->CycleActiveUser(forward);
153 EXPECT_EQ("seconduser@test.com", GetActiveUser());
154 session_state_delegate()->CycleActiveUser(forward);
155 EXPECT_EQ("thirduser@test.com", GetActiveUser());
156 session_state_delegate()->CycleActiveUser(forward);
157 EXPECT_EQ("firstuser@test.com", GetActiveUser());
159 // Cycle backwards.
160 const ash::SessionStateDelegate::CycleUser backward =
161 ash::SessionStateDelegate::CYCLE_TO_PREVIOUS_USER;
162 session_state_delegate()->CycleActiveUser(backward);
163 EXPECT_EQ("thirduser@test.com", GetActiveUser());
164 session_state_delegate()->CycleActiveUser(backward);
165 EXPECT_EQ("seconduser@test.com", GetActiveUser());
166 session_state_delegate()->CycleActiveUser(backward);
167 EXPECT_EQ("firstuser@test.com", GetActiveUser());
170 // Make sure MultiProfile disabled by primary user policy.
171 TEST_F(SessionStateDelegateChromeOSTest, MultiProfileDisallowedByUserPolicy) {
172 InitForMultiProfile();
173 EXPECT_TRUE(
174 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy());
175 const std::string user_email(kUser);
176 user_manager()->LoginUser(user_email);
177 EXPECT_TRUE(
178 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy());
180 user_profile_->GetPrefs()->SetString(
181 prefs::kMultiProfileUserBehavior,
182 chromeos::MultiProfileUserController::kBehaviorNotAllowed);
183 EXPECT_FALSE(
184 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy());
187 // Make sure MultiProfile disabled by primary user policy certificates.
188 TEST_F(SessionStateDelegateChromeOSTest,
189 MultiProfileDisallowedByPolicyCertificates) {
190 InitForMultiProfile();
191 const std::string user_email(kUser);
192 user_manager()->LoginUser(user_email);
193 EXPECT_TRUE(
194 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy());
195 policy::PolicyCertServiceFactory::SetUsedPolicyCertificates(user_email);
196 EXPECT_FALSE(
197 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy());
199 // Flush tasks posted to IO.
200 base::RunLoop().RunUntilIdle();
203 // Make sure MultiProfile disabled by primary user certificates in memory.
204 TEST_F(SessionStateDelegateChromeOSTest,
205 MultiProfileDisallowedByPrimaryUserCertificatesInMemory) {
206 InitForMultiProfile();
207 const std::string user_email(kUser);
208 user_manager()->LoginUser(user_email);
209 EXPECT_TRUE(
210 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy());
211 cert_verifier_.reset(new policy::PolicyCertVerifier(base::Closure()));
212 g_policy_cert_verifier_for_factory = cert_verifier_.get();
213 ASSERT_TRUE(
214 policy::PolicyCertServiceFactory::GetInstance()->SetTestingFactoryAndUse(
215 user_profile_, CreateTestPolicyCertService));
216 policy::PolicyCertService* service =
217 policy::PolicyCertServiceFactory::GetForProfile(user_profile_);
218 ASSERT_TRUE(service);
220 EXPECT_FALSE(service->has_policy_certificates());
221 net::CertificateList certificates;
222 certificates.push_back(new net::X509Certificate(
223 "subject", "issuer", base::Time(), base::Time()));
224 service->OnTrustAnchorsChanged(certificates);
225 EXPECT_TRUE(service->has_policy_certificates());
226 EXPECT_FALSE(
227 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy());
229 // Flush tasks posted to IO.
230 base::RunLoop().RunUntilIdle();
233 // Make sure adding users to multiprofiles disabled by reaching maximum
234 // number of users in sessions.
235 TEST_F(SessionStateDelegateChromeOSTest,
236 AddUserToMultiprofileDisallowedByMaximumUsers) {
237 InitForMultiProfile();
238 ash::SessionStateDelegate::AddUserError add_user_error;
240 EXPECT_TRUE(
241 session_state_delegate()->CanAddUserToMultiProfile(&add_user_error));
242 const std::string user_email(kUser);
243 user_manager()->LoginUser(user_email);
244 while (session_state_delegate()->NumberOfLoggedInUsers() <
245 session_state_delegate()->GetMaximumNumberOfLoggedInUsers()) {
246 UserAddedToSession("bb@b.b");
248 EXPECT_FALSE(
249 session_state_delegate()->CanAddUserToMultiProfile(&add_user_error));
250 EXPECT_EQ(ash::SessionStateDelegate::ADD_USER_ERROR_MAXIMUM_USERS_REACHED,
251 add_user_error);
254 // Make sure adding users to multiprofiles disabled by logging in all possible
255 // users.
256 TEST_F(SessionStateDelegateChromeOSTest,
257 AddUserToMultiprofileDisallowedByAllUsersLogged) {
258 InitForMultiProfile();
259 ash::SessionStateDelegate::AddUserError add_user_error;
261 EXPECT_TRUE(
262 session_state_delegate()->CanAddUserToMultiProfile(&add_user_error));
263 const std::string user_email(kUser);
264 user_manager()->LoginUser(user_email);
265 UserAddedToSession("bb@b.b");
266 EXPECT_FALSE(
267 session_state_delegate()->CanAddUserToMultiProfile(&add_user_error));
268 EXPECT_EQ(ash::SessionStateDelegate::ADD_USER_ERROR_OUT_OF_USERS,
269 add_user_error);
272 // Make sure adding users to multiprofiles disabled by primary user policy.
273 TEST_F(SessionStateDelegateChromeOSTest,
274 AddUserToMultiprofileDisallowedByPrimaryUserPolicy) {
275 InitForMultiProfile();
276 ash::SessionStateDelegate::AddUserError add_user_error;
278 EXPECT_TRUE(
279 session_state_delegate()->CanAddUserToMultiProfile(&add_user_error));
280 const std::string user_email(kUser);
281 user_manager()->LoginUser(user_email);
282 user_profile_->GetPrefs()->SetString(
283 prefs::kMultiProfileUserBehavior,
284 chromeos::MultiProfileUserController::kBehaviorNotAllowed);
285 user_manager()->AddUser("bb@b.b");
286 EXPECT_FALSE(
287 session_state_delegate()->CanAddUserToMultiProfile(&add_user_error));
288 EXPECT_EQ(ash::SessionStateDelegate::ADD_USER_ERROR_NOT_ALLOWED_PRIMARY_USER,
289 add_user_error);
292 } // namespace chromeos