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
->AppendSwitchASCII(switches::kManagedUserId
, "asdf");
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 base::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());
78 base::UTF16ToUTF8(cache
.GetNameOfProfileAtIndex(profile_index
)));
80 // Change the name once more.
81 std::string new_name
= "New Managed User Test Name";
82 settings
->SetLocalSettingForTesting(
83 managed_users::kUserName
,
84 scoped_ptr
<base::Value
>(new base::StringValue(new_name
)));
85 EXPECT_EQ(new_name
, prefs
->GetString(prefs::kProfileName
));
86 profile_index
= cache
.GetIndexOfProfileWithPath(profile
->GetPath());
88 base::UTF16ToUTF8(cache
.GetNameOfProfileAtIndex(profile_index
)));
90 // Remove the setting.
91 settings
->SetLocalSettingForTesting(managed_users::kUserName
,
92 scoped_ptr
<base::Value
>());
93 EXPECT_EQ(original_name
, prefs
->GetString(prefs::kProfileName
));
94 profile_index
= cache
.GetIndexOfProfileWithPath(profile
->GetPath());
95 EXPECT_EQ(original_name
,
96 base::UTF16ToUTF8(cache
.GetNameOfProfileAtIndex(profile_index
)));