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 "base/command_line.h"
6#include "base/prefs/pref_service.h"
7#include "base/strings/utf_string_conversions.h"
8#include "chrome/browser/browser_process.h"
9#include "chrome/browser/managed_mode/managed_user_constants.h"
10#include "chrome/browser/managed_mode/managed_user_service.h"
11#include "chrome/browser/managed_mode/managed_user_service_factory.h"
12#include "chrome/browser/managed_mode/managed_user_settings_service.h"
13#include "chrome/browser/managed_mode/managed_user_settings_service_factory.h"
14#include "chrome/browser/profiles/profile.h"
15#include "chrome/browser/profiles/profile_info_cache.h"
16#include "chrome/browser/profiles/profile_manager.h"
17#include "chrome/browser/ui/browser.h"
18#include "chrome/common/chrome_switches.h"
19#include "chrome/common/pref_names.h"
20#include "chrome/test/base/in_process_browser_test.h"
21#include "content/public/test/test_utils.h"
22
23typedef InProcessBrowserTest ManagedUserServiceTest;
24
25class ManagedUserServiceTestManaged : public InProcessBrowserTest {
26 public:
27  // content::BrowserTestBase:
28  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
29    command_line->AppendSwitchASCII(switches::kManagedUserId, "asdf");
30  }
31};
32
33IN_PROC_BROWSER_TEST_F(ManagedUserServiceTest, LocalPolicies) {
34  Profile* profile = browser()->profile();
35  PrefService* prefs = profile->GetPrefs();
36  EXPECT_FALSE(prefs->GetBoolean(prefs::kForceSafeSearch));
37  EXPECT_TRUE(prefs->IsUserModifiablePreference(prefs::kForceSafeSearch));
38}
39
40IN_PROC_BROWSER_TEST_F(ManagedUserServiceTest, ProfileName) {
41  Profile* profile = browser()->profile();
42  PrefService* prefs = profile->GetPrefs();
43  EXPECT_TRUE(prefs->IsUserModifiablePreference(prefs::kProfileName));
44
45  std::string original_name = prefs->GetString(prefs::kProfileName);
46  ProfileManager* profile_manager = g_browser_process->profile_manager();
47  const ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
48  size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
49  EXPECT_EQ(original_name,
50            UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index)));
51}
52
53IN_PROC_BROWSER_TEST_F(ManagedUserServiceTestManaged, LocalPolicies) {
54  Profile* profile = browser()->profile();
55  PrefService* prefs = profile->GetPrefs();
56  EXPECT_TRUE(prefs->GetBoolean(prefs::kForceSafeSearch));
57  EXPECT_FALSE(prefs->IsUserModifiablePreference(prefs::kForceSafeSearch));
58}
59
60IN_PROC_BROWSER_TEST_F(ManagedUserServiceTestManaged, ProfileName) {
61  Profile* profile = browser()->profile();
62  PrefService* prefs = profile->GetPrefs();
63  std::string original_name = prefs->GetString(prefs::kProfileName);
64  ProfileManager* profile_manager = g_browser_process->profile_manager();
65  const ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
66
67  ManagedUserSettingsService* settings =
68      ManagedUserSettingsServiceFactory::GetForProfile(profile);
69
70  std::string name = "Managed User Test Name";
71  settings->SetLocalSettingForTesting(
72      managed_users::kUserName,
73      scoped_ptr<base::Value>(new base::StringValue(name)));
74  EXPECT_FALSE(prefs->IsUserModifiablePreference(prefs::kProfileName));
75  EXPECT_EQ(name, prefs->GetString(prefs::kProfileName));
76  size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
77  EXPECT_EQ(name, UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index)));
78
79  // Change the name once more.
80  std::string new_name = "New Managed User Test Name";
81  settings->SetLocalSettingForTesting(
82      managed_users::kUserName,
83      scoped_ptr<base::Value>(new base::StringValue(new_name)));
84  EXPECT_EQ(new_name, prefs->GetString(prefs::kProfileName));
85  profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
86  EXPECT_EQ(new_name,
87            UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index)));
88
89  // Remove the setting.
90  settings->SetLocalSettingForTesting(managed_users::kUserName,
91                                      scoped_ptr<base::Value>());
92  EXPECT_EQ(original_name, prefs->GetString(prefs::kProfileName));
93  profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
94  EXPECT_EQ(original_name,
95            UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index)));
96}
97