- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / multi_profile_user_controller_unittest.cc
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.
4
5 #include "chrome/browser/chromeos/login/multi_profile_user_controller.h"
6
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"
20
21 namespace chromeos {
22
23 namespace {
24
25 const char* kUsers[] = {"a@gmail.com", "b@gmail.com" };
26
27 struct BehaviorTestCase {
28   const char* primary;
29   const char* secondary;
30   bool expected_allowed;
31 };
32
33 const BehaviorTestCase kBehaviorTestCases[] = {
34   {
35     MultiProfileUserController::kBehaviorUnrestricted,
36     MultiProfileUserController::kBehaviorUnrestricted,
37     true,
38   },
39   {
40     MultiProfileUserController::kBehaviorUnrestricted,
41     MultiProfileUserController::kBehaviorPrimaryOnly,
42     false,
43   },
44   {
45     MultiProfileUserController::kBehaviorUnrestricted,
46     MultiProfileUserController::kBehaviorNotAllowed,
47     false,
48   },
49   {
50     MultiProfileUserController::kBehaviorPrimaryOnly,
51     MultiProfileUserController::kBehaviorUnrestricted,
52     true,
53   },
54   {
55     MultiProfileUserController::kBehaviorPrimaryOnly,
56     MultiProfileUserController::kBehaviorPrimaryOnly,
57     false,
58   },
59   {
60     MultiProfileUserController::kBehaviorPrimaryOnly,
61     MultiProfileUserController::kBehaviorNotAllowed,
62     false,
63   },
64   {
65     MultiProfileUserController::kBehaviorNotAllowed,
66     MultiProfileUserController::kBehaviorUnrestricted,
67     false,
68   },
69   {
70     MultiProfileUserController::kBehaviorNotAllowed,
71     MultiProfileUserController::kBehaviorPrimaryOnly,
72     false,
73   },
74   {
75     MultiProfileUserController::kBehaviorNotAllowed,
76     MultiProfileUserController::kBehaviorNotAllowed,
77     false,
78   },
79 };
80
81 }  // namespace
82
83 class MultiProfileUserControllerTest
84     : public testing::Test,
85       public MultiProfileUserControllerDelegate {
86  public:
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() {}
93
94   virtual void SetUp() OVERRIDE {
95     ASSERT_TRUE(profile_manager_.SetUp());
96     controller_.reset(new MultiProfileUserController(
97         this, TestingBrowserProcess::GetGlobal()->local_state()));
98
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);
102
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);
108     }
109   }
110
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]);
115   }
116
117   void SetOwner(size_t user_index) {
118     fake_user_manager_->set_owner_email(kUsers[user_index]);
119   }
120
121   PrefService* GetUserPrefs(size_t user_index) {
122     return user_profiles_[user_index]->GetPrefs();
123   }
124
125   void SetPrefBehavior(size_t user_index, const std::string& behavior) {
126     GetUserPrefs(user_index)->SetString(prefs::kMultiProfileUserBehavior,
127                                         behavior);
128   }
129
130   std::string GetCachedBehavior(size_t user_index) {
131     return controller_->GetCachedValue(kUsers[user_index]);
132   }
133
134   void SetCachedBehavior(size_t user_index,
135                          const std::string& behavior) {
136     controller_->SetCachedValue(kUsers[user_index], behavior);
137   }
138
139   void ResetCounts() {
140     user_not_allowed_count_ = 0;
141   }
142
143   // MultiProfileUserControllerDeleagte overrides:
144   virtual void OnUserNotAllowed() OVERRIDE {
145     ++user_not_allowed_count_;
146   }
147
148   MultiProfileUserController* controller() { return controller_.get(); }
149   int user_not_allowed_count() const { return user_not_allowed_count_; }
150
151  private:
152   TestingProfileManager profile_manager_;
153   FakeUserManager* fake_user_manager_;  // Not owned
154   ScopedUserManagerEnabler user_manager_enabler_;
155
156   scoped_ptr<MultiProfileUserController> controller_;
157
158   std::vector<TestingProfile*> user_profiles_;
159
160   int user_not_allowed_count_;
161
162   DISALLOW_COPY_AND_ASSIGN(MultiProfileUserControllerTest);
163 };
164
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,
171   };
172   for (size_t i = 0; i < arraysize(kTestCases); ++i) {
173     SetCachedBehavior(0, kTestCases[i]);
174     EXPECT_TRUE(controller()->IsUserAllowedInSession(kUsers[0]))
175         << "Case " << i;
176   }
177 }
178
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));
185 }
186
187 // Tests that cached behavior value changes with user pref after login.
188 TEST_F(MultiProfileUserControllerTest, CachedBehaviorUpdate) {
189   LoginUser(0);
190
191   const char* kTestCases[] = {
192     MultiProfileUserController::kBehaviorUnrestricted,
193     MultiProfileUserController::kBehaviorPrimaryOnly,
194     MultiProfileUserController::kBehaviorNotAllowed,
195     MultiProfileUserController::kBehaviorUnrestricted,
196   };
197   for (size_t i = 0; i < arraysize(kTestCases); ++i) {
198     SetPrefBehavior(0, kTestCases[i]);
199     EXPECT_EQ(kTestCases[i], GetCachedBehavior(0));
200   }
201 }
202
203 // Tests that compromised cache value would be fixed and pref value is checked
204 // upon login.
205 TEST_F(MultiProfileUserControllerTest, CompromisedCacheFixedOnLogin) {
206   SetPrefBehavior(0, MultiProfileUserController::kBehaviorPrimaryOnly);
207   SetCachedBehavior(0, MultiProfileUserController::kBehaviorUnrestricted);
208   EXPECT_EQ(MultiProfileUserController::kBehaviorUnrestricted,
209             GetCachedBehavior(0));
210   LoginUser(0);
211   EXPECT_EQ(MultiProfileUserController::kBehaviorPrimaryOnly,
212             GetCachedBehavior(0));
213
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));
219   LoginUser(1);
220   EXPECT_EQ(MultiProfileUserController::kBehaviorPrimaryOnly,
221             GetCachedBehavior(1));
222   EXPECT_EQ(1, user_not_allowed_count());
223 }
224
225 // Tests cases before the second user login.
226 TEST_F(MultiProfileUserControllerTest, IsSecondaryAllowed) {
227   LoginUser(0);
228
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;
234   }
235 }
236
237 // Tests user behavior changes within a two-user session.
238 TEST_F(MultiProfileUserControllerTest, PrimaryBehaviorChange) {
239   LoginUser(0);
240   LoginUser(1);
241
242   for (size_t i = 0; i < arraysize(kBehaviorTestCases); ++i) {
243     SetPrefBehavior(0, MultiProfileUserController::kBehaviorUnrestricted);
244     SetPrefBehavior(1, MultiProfileUserController::kBehaviorUnrestricted);
245     ResetCounts();
246
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;
251   }
252 }
253
254 // Tests that owner could not be a secondary user.
255 TEST_F(MultiProfileUserControllerTest, NoSecondaryOwner) {
256   LoginUser(0);
257   SetOwner(1);
258
259   EXPECT_FALSE(controller()->IsUserAllowedInSession(kUsers[1]));
260
261   EXPECT_EQ(0, user_not_allowed_count());
262   LoginUser(1);
263   EXPECT_EQ(1, user_not_allowed_count());
264 }
265
266 }  // namespace chromeos