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 "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"
23 typedef InProcessBrowserTest ManagedUserServiceTest;
25 class ManagedUserServiceTestManaged : public InProcessBrowserTest {
27 // content::BrowserTestBase:
28 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
29 command_line->AppendSwitch(switches::kNewProfileIsSupervised);
33 IN_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));
40 IN_PROC_BROWSER_TEST_F(ManagedUserServiceTest, ProfileName) {
41 Profile* profile = browser()->profile();
42 PrefService* prefs = profile->GetPrefs();
43 EXPECT_TRUE(prefs->IsUserModifiablePreference(prefs::kProfileName));
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)));
53 IN_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));
60 IN_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();
67 ManagedUserSettingsService* settings =
68 ManagedUserSettingsServiceFactory::GetForProfile(profile);
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)));
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());
87 UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index)));
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)));