1 // Copyright 2014 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/callback.h"
6 #include "base/command_line.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/profiles/profile_info_cache.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/browser/supervised_user/supervised_user_constants.h"
14 #include "chrome/browser/supervised_user/supervised_user_service.h"
15 #include "chrome/browser/supervised_user/supervised_user_service_factory.h"
16 #include "chrome/browser/supervised_user/supervised_user_settings_service.h"
17 #include "chrome/browser/supervised_user/supervised_user_settings_service_factory.h"
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/pref_names.h"
21 #include "chrome/test/base/in_process_browser_test.h"
22 #include "content/public/test/test_utils.h"
23 #include "google_apis/gaia/google_service_auth_error.h"
27 void TestAuthErrorCallback(const GoogleServiceAuthError
& error
) {}
29 class SupervisedUserServiceTestSupervised
: public InProcessBrowserTest
{
31 // content::BrowserTestBase:
32 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
33 command_line
->AppendSwitchASCII(switches::kSupervisedUserId
, "asdf");
39 typedef InProcessBrowserTest SupervisedUserServiceTest
;
42 // http://crbug.com/339501
43 #if defined(OS_MACOSX)
44 #define MAYBE_ClearOmitOnRegistration DISABLED_ClearOmitOnRegistration
46 #define MAYBE_ClearOmitOnRegistration ClearOmitOnRegistration
48 // Ensure that a profile that has completed registration is included in the
49 // list shown in the avatar menu.
50 IN_PROC_BROWSER_TEST_F(SupervisedUserServiceTest
,
51 MAYBE_ClearOmitOnRegistration
) {
52 // Artificially mark the profile as omitted.
53 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
54 ProfileInfoCache
& cache
= profile_manager
->GetProfileInfoCache();
55 Profile
* profile
= browser()->profile();
56 size_t index
= cache
.GetIndexOfProfileWithPath(profile
->GetPath());
57 cache
.SetIsOmittedProfileAtIndex(index
, true);
58 ASSERT_TRUE(cache
.IsOmittedProfileAtIndex(index
));
60 SupervisedUserService
* supervised_user_service
=
61 SupervisedUserServiceFactory::GetForProfile(profile
);
63 // A registration error does not clear the flag (the profile should be deleted
65 supervised_user_service
->OnSupervisedUserRegistered(
66 base::Bind(&TestAuthErrorCallback
),
68 GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED
),
70 ASSERT_TRUE(cache
.IsOmittedProfileAtIndex(index
));
72 // Successfully completing registration clears the flag.
73 supervised_user_service
->OnSupervisedUserRegistered(
74 base::Bind(&TestAuthErrorCallback
),
76 GoogleServiceAuthError(GoogleServiceAuthError::NONE
),
77 std::string("abcdef"));
78 EXPECT_FALSE(cache
.IsOmittedProfileAtIndex(index
));
81 IN_PROC_BROWSER_TEST_F(SupervisedUserServiceTest
, LocalPolicies
) {
82 Profile
* profile
= browser()->profile();
83 PrefService
* prefs
= profile
->GetPrefs();
84 EXPECT_FALSE(prefs
->GetBoolean(prefs::kForceGoogleSafeSearch
));
85 EXPECT_FALSE(prefs
->GetBoolean(prefs::kForceYouTubeSafetyMode
));
86 EXPECT_TRUE(prefs
->IsUserModifiablePreference(prefs::kForceGoogleSafeSearch
));
88 prefs
->IsUserModifiablePreference(prefs::kForceYouTubeSafetyMode
));
91 IN_PROC_BROWSER_TEST_F(SupervisedUserServiceTest
, ProfileName
) {
92 Profile
* profile
= browser()->profile();
93 PrefService
* prefs
= profile
->GetPrefs();
94 EXPECT_TRUE(prefs
->IsUserModifiablePreference(prefs::kProfileName
));
96 std::string original_name
= prefs
->GetString(prefs::kProfileName
);
97 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
98 const ProfileInfoCache
& cache
= profile_manager
->GetProfileInfoCache();
99 size_t profile_index
= cache
.GetIndexOfProfileWithPath(profile
->GetPath());
100 EXPECT_EQ(original_name
,
101 base::UTF16ToUTF8(cache
.GetNameOfProfileAtIndex(profile_index
)));
104 IN_PROC_BROWSER_TEST_F(SupervisedUserServiceTestSupervised
, LocalPolicies
) {
105 Profile
* profile
= browser()->profile();
106 PrefService
* prefs
= profile
->GetPrefs();
107 EXPECT_TRUE(prefs
->GetBoolean(prefs::kForceGoogleSafeSearch
));
108 EXPECT_TRUE(prefs
->GetBoolean(prefs::kForceYouTubeSafetyMode
));
110 prefs
->IsUserModifiablePreference(prefs::kForceGoogleSafeSearch
));
112 prefs
->IsUserModifiablePreference(prefs::kForceYouTubeSafetyMode
));
115 IN_PROC_BROWSER_TEST_F(SupervisedUserServiceTestSupervised
, ProfileName
) {
116 Profile
* profile
= browser()->profile();
117 PrefService
* prefs
= profile
->GetPrefs();
118 std::string original_name
= prefs
->GetString(prefs::kProfileName
);
119 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
120 const ProfileInfoCache
& cache
= profile_manager
->GetProfileInfoCache();
122 SupervisedUserSettingsService
* settings
=
123 SupervisedUserSettingsServiceFactory::GetForProfile(profile
);
125 std::string name
= "Supervised User Test Name";
126 settings
->SetLocalSetting(
127 supervised_users::kUserName
,
128 scoped_ptr
<base::Value
>(new base::StringValue(name
)));
129 EXPECT_FALSE(prefs
->IsUserModifiablePreference(prefs::kProfileName
));
130 EXPECT_EQ(name
, prefs
->GetString(prefs::kProfileName
));
131 size_t profile_index
= cache
.GetIndexOfProfileWithPath(profile
->GetPath());
133 base::UTF16ToUTF8(cache
.GetNameOfProfileAtIndex(profile_index
)));
135 // Change the name once more.
136 std::string new_name
= "New Supervised User Test Name";
137 settings
->SetLocalSetting(
138 supervised_users::kUserName
,
139 scoped_ptr
<base::Value
>(new base::StringValue(new_name
)));
140 EXPECT_EQ(new_name
, prefs
->GetString(prefs::kProfileName
));
141 profile_index
= cache
.GetIndexOfProfileWithPath(profile
->GetPath());
143 base::UTF16ToUTF8(cache
.GetNameOfProfileAtIndex(profile_index
)));
145 // Remove the setting.
146 settings
->SetLocalSetting(supervised_users::kUserName
,
147 scoped_ptr
<base::Value
>());
148 EXPECT_EQ(original_name
, prefs
->GetString(prefs::kProfileName
));
149 profile_index
= cache
.GetIndexOfProfileWithPath(profile
->GetPath());
150 EXPECT_EQ(original_name
,
151 base::UTF16ToUTF8(cache
.GetNameOfProfileAtIndex(profile_index
)));