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/chromeos/login/multi_profile_user_controller.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/chromeos/login/fake_user_manager.h"
10 #include "chrome/browser/chromeos/login/multi_profile_user_controller_delegate.h"
11 #include "chrome/browser/chromeos/login/user_manager.h"
12 #include "chrome/browser/prefs/browser_prefs.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/test/base/scoped_testing_local_state.h"
15 #include "chrome/test/base/testing_browser_process.h"
16 #include "chrome/test/base/testing_pref_service_syncable.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "chrome/test/base/testing_profile_manager.h"
19 #include "testing/gtest/include/gtest/gtest.h"
25 const char* kUsers[] = {"a@gmail.com", "b@gmail.com" };
27 struct BehaviorTestCase {
29 const char* secondary;
30 bool expected_allowed;
33 const BehaviorTestCase kBehaviorTestCases[] = {
35 MultiProfileUserController::kBehaviorUnrestricted,
36 MultiProfileUserController::kBehaviorUnrestricted,
40 MultiProfileUserController::kBehaviorUnrestricted,
41 MultiProfileUserController::kBehaviorPrimaryOnly,
45 MultiProfileUserController::kBehaviorUnrestricted,
46 MultiProfileUserController::kBehaviorNotAllowed,
50 MultiProfileUserController::kBehaviorPrimaryOnly,
51 MultiProfileUserController::kBehaviorUnrestricted,
55 MultiProfileUserController::kBehaviorPrimaryOnly,
56 MultiProfileUserController::kBehaviorPrimaryOnly,
60 MultiProfileUserController::kBehaviorPrimaryOnly,
61 MultiProfileUserController::kBehaviorNotAllowed,
65 MultiProfileUserController::kBehaviorNotAllowed,
66 MultiProfileUserController::kBehaviorUnrestricted,
70 MultiProfileUserController::kBehaviorNotAllowed,
71 MultiProfileUserController::kBehaviorPrimaryOnly,
75 MultiProfileUserController::kBehaviorNotAllowed,
76 MultiProfileUserController::kBehaviorNotAllowed,
83 class MultiProfileUserControllerTest
84 : public testing::Test,
85 public MultiProfileUserControllerDelegate {
87 MultiProfileUserControllerTest()
88 : profile_manager_(TestingBrowserProcess::GetGlobal()),
89 fake_user_manager_(new FakeUserManager),
90 user_manager_enabler_(fake_user_manager_),
91 user_not_allowed_count_(0) {}
92 virtual ~MultiProfileUserControllerTest() {}
94 virtual void SetUp() OVERRIDE {
95 ASSERT_TRUE(profile_manager_.SetUp());
96 controller_.reset(new MultiProfileUserController(
97 this, TestingBrowserProcess::GetGlobal()->local_state()));
99 for (size_t i = 0; i < arraysize(kUsers); ++i) {
100 const std::string user_email(kUsers[i]);
101 fake_user_manager_->AddUser(user_email);
103 // Note that user profiles are created after user login in reality.
104 TestingProfile* user_profile =
105 profile_manager_.CreateTestingProfile(user_email);
106 user_profile->set_profile_name(user_email);
107 user_profiles_.push_back(user_profile);
111 void LoginUser(size_t user_index) {
112 ASSERT_LT(user_index, arraysize(kUsers));
113 fake_user_manager_->LoginUser(kUsers[user_index]);
114 controller_->StartObserving(user_profiles_[user_index]);
117 void SetOwner(size_t user_index) {
118 fake_user_manager_->set_owner_email(kUsers[user_index]);
121 PrefService* GetUserPrefs(size_t user_index) {
122 return user_profiles_[user_index]->GetPrefs();
125 void SetPrefBehavior(size_t user_index, const std::string& behavior) {
126 GetUserPrefs(user_index)->SetString(prefs::kMultiProfileUserBehavior,
130 std::string GetCachedBehavior(size_t user_index) {
131 return controller_->GetCachedValue(kUsers[user_index]);
134 void SetCachedBehavior(size_t user_index,
135 const std::string& behavior) {
136 controller_->SetCachedValue(kUsers[user_index], behavior);
140 user_not_allowed_count_ = 0;
143 // MultiProfileUserControllerDeleagte overrides:
144 virtual void OnUserNotAllowed() OVERRIDE {
145 ++user_not_allowed_count_;
148 MultiProfileUserController* controller() { return controller_.get(); }
149 int user_not_allowed_count() const { return user_not_allowed_count_; }
152 TestingProfileManager profile_manager_;
153 FakeUserManager* fake_user_manager_; // Not owned
154 ScopedUserManagerEnabler user_manager_enabler_;
156 scoped_ptr<MultiProfileUserController> controller_;
158 std::vector<TestingProfile*> user_profiles_;
160 int user_not_allowed_count_;
162 DISALLOW_COPY_AND_ASSIGN(MultiProfileUserControllerTest);
165 // Tests that everyone is allowed before a session starts.
166 TEST_F(MultiProfileUserControllerTest, AllAllowedBeforeLogin) {
167 const char* kTestCases[] = {
168 MultiProfileUserController::kBehaviorUnrestricted,
169 MultiProfileUserController::kBehaviorPrimaryOnly,
170 MultiProfileUserController::kBehaviorNotAllowed,
172 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
173 SetCachedBehavior(0, kTestCases[i]);
174 EXPECT_TRUE(controller()->IsUserAllowedInSession(kUsers[0]))
179 // Tests that invalid cache value would become the default "unrestricted".
180 TEST_F(MultiProfileUserControllerTest, InvalidCacheBecomesDefault) {
181 const char kBad[] = "some invalid value";
182 SetCachedBehavior(0, kBad);
183 EXPECT_EQ(MultiProfileUserController::kBehaviorUnrestricted,
184 GetCachedBehavior(0));
187 // Tests that cached behavior value changes with user pref after login.
188 TEST_F(MultiProfileUserControllerTest, CachedBehaviorUpdate) {
191 const char* kTestCases[] = {
192 MultiProfileUserController::kBehaviorUnrestricted,
193 MultiProfileUserController::kBehaviorPrimaryOnly,
194 MultiProfileUserController::kBehaviorNotAllowed,
195 MultiProfileUserController::kBehaviorUnrestricted,
197 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
198 SetPrefBehavior(0, kTestCases[i]);
199 EXPECT_EQ(kTestCases[i], GetCachedBehavior(0));
203 // Tests that compromised cache value would be fixed and pref value is checked
205 TEST_F(MultiProfileUserControllerTest, CompromisedCacheFixedOnLogin) {
206 SetPrefBehavior(0, MultiProfileUserController::kBehaviorPrimaryOnly);
207 SetCachedBehavior(0, MultiProfileUserController::kBehaviorUnrestricted);
208 EXPECT_EQ(MultiProfileUserController::kBehaviorUnrestricted,
209 GetCachedBehavior(0));
211 EXPECT_EQ(MultiProfileUserController::kBehaviorPrimaryOnly,
212 GetCachedBehavior(0));
214 EXPECT_EQ(0, user_not_allowed_count());
215 SetPrefBehavior(1, MultiProfileUserController::kBehaviorPrimaryOnly);
216 SetCachedBehavior(1, MultiProfileUserController::kBehaviorUnrestricted);
217 EXPECT_EQ(MultiProfileUserController::kBehaviorUnrestricted,
218 GetCachedBehavior(1));
220 EXPECT_EQ(MultiProfileUserController::kBehaviorPrimaryOnly,
221 GetCachedBehavior(1));
222 EXPECT_EQ(1, user_not_allowed_count());
225 // Tests cases before the second user login.
226 TEST_F(MultiProfileUserControllerTest, IsSecondaryAllowed) {
229 for (size_t i = 0; i < arraysize(kBehaviorTestCases); ++i) {
230 SetPrefBehavior(0, kBehaviorTestCases[i].primary);
231 SetCachedBehavior(1, kBehaviorTestCases[i].secondary);
232 EXPECT_EQ(kBehaviorTestCases[i].expected_allowed,
233 controller()->IsUserAllowedInSession(kUsers[1])) << "Case " << i;
237 // Tests user behavior changes within a two-user session.
238 TEST_F(MultiProfileUserControllerTest, PrimaryBehaviorChange) {
242 for (size_t i = 0; i < arraysize(kBehaviorTestCases); ++i) {
243 SetPrefBehavior(0, MultiProfileUserController::kBehaviorUnrestricted);
244 SetPrefBehavior(1, MultiProfileUserController::kBehaviorUnrestricted);
247 SetPrefBehavior(0, kBehaviorTestCases[i].primary);
248 SetPrefBehavior(1, kBehaviorTestCases[i].secondary);
249 EXPECT_EQ(kBehaviorTestCases[i].expected_allowed,
250 user_not_allowed_count() == 0) << "Case " << i;
254 // Tests that owner could not be a secondary user.
255 TEST_F(MultiProfileUserControllerTest, NoSecondaryOwner) {
259 EXPECT_FALSE(controller()->IsUserAllowedInSession(kUsers[1]));
261 EXPECT_EQ(0, user_not_allowed_count());
263 EXPECT_EQ(1, user_not_allowed_count());
266 } // namespace chromeos