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.
4
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"
24
25namespace {
26
27void TestAuthErrorCallback(const GoogleServiceAuthError& error) {}
28
29class SupervisedUserServiceTestSupervised : public InProcessBrowserTest {
30 public:
31  // content::BrowserTestBase:
32  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
33    command_line->AppendSwitchASCII(switches::kSupervisedUserId, "asdf");
34  }
35};
36
37}  // namespace
38
39typedef InProcessBrowserTest SupervisedUserServiceTest;
40
41// Crashes on Mac.
42// http://crbug.com/339501
43#if defined(OS_MACOSX)
44#define MAYBE_ClearOmitOnRegistration DISABLED_ClearOmitOnRegistration
45#else
46#define MAYBE_ClearOmitOnRegistration ClearOmitOnRegistration
47#endif
48// Ensure that a profile that has completed registration is included in the
49// list shown in the avatar menu.
50IN_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));
59
60  SupervisedUserService* supervised_user_service =
61      SupervisedUserServiceFactory::GetForProfile(profile);
62
63  // A registration error does not clear the flag (the profile should be deleted
64  // anyway).
65  supervised_user_service->OnSupervisedUserRegistered(
66      base::Bind(&TestAuthErrorCallback),
67      profile,
68      GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED),
69      std::string());
70  ASSERT_TRUE(cache.IsOmittedProfileAtIndex(index));
71
72  // Successfully completing registration clears the flag.
73  supervised_user_service->OnSupervisedUserRegistered(
74      base::Bind(&TestAuthErrorCallback),
75      profile,
76      GoogleServiceAuthError(GoogleServiceAuthError::NONE),
77      std::string("abcdef"));
78  EXPECT_FALSE(cache.IsOmittedProfileAtIndex(index));
79}
80
81IN_PROC_BROWSER_TEST_F(SupervisedUserServiceTest, LocalPolicies) {
82  Profile* profile = browser()->profile();
83  PrefService* prefs = profile->GetPrefs();
84  EXPECT_FALSE(prefs->GetBoolean(prefs::kForceSafeSearch));
85  EXPECT_TRUE(prefs->IsUserModifiablePreference(prefs::kForceSafeSearch));
86}
87
88IN_PROC_BROWSER_TEST_F(SupervisedUserServiceTest, ProfileName) {
89  Profile* profile = browser()->profile();
90  PrefService* prefs = profile->GetPrefs();
91  EXPECT_TRUE(prefs->IsUserModifiablePreference(prefs::kProfileName));
92
93  std::string original_name = prefs->GetString(prefs::kProfileName);
94  ProfileManager* profile_manager = g_browser_process->profile_manager();
95  const ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
96  size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
97  EXPECT_EQ(original_name,
98            base::UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index)));
99}
100
101IN_PROC_BROWSER_TEST_F(SupervisedUserServiceTestSupervised, LocalPolicies) {
102  Profile* profile = browser()->profile();
103  PrefService* prefs = profile->GetPrefs();
104  EXPECT_TRUE(prefs->GetBoolean(prefs::kForceSafeSearch));
105  EXPECT_FALSE(prefs->IsUserModifiablePreference(prefs::kForceSafeSearch));
106}
107
108IN_PROC_BROWSER_TEST_F(SupervisedUserServiceTestSupervised, ProfileName) {
109  Profile* profile = browser()->profile();
110  PrefService* prefs = profile->GetPrefs();
111  std::string original_name = prefs->GetString(prefs::kProfileName);
112  ProfileManager* profile_manager = g_browser_process->profile_manager();
113  const ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
114
115  SupervisedUserSettingsService* settings =
116      SupervisedUserSettingsServiceFactory::GetForProfile(profile);
117
118  std::string name = "Supervised User Test Name";
119  settings->SetLocalSettingForTesting(
120      supervised_users::kUserName,
121      scoped_ptr<base::Value>(new base::StringValue(name)));
122  EXPECT_FALSE(prefs->IsUserModifiablePreference(prefs::kProfileName));
123  EXPECT_EQ(name, prefs->GetString(prefs::kProfileName));
124  size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
125  EXPECT_EQ(name,
126            base::UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index)));
127
128  // Change the name once more.
129  std::string new_name = "New Supervised User Test Name";
130  settings->SetLocalSettingForTesting(
131      supervised_users::kUserName,
132      scoped_ptr<base::Value>(new base::StringValue(new_name)));
133  EXPECT_EQ(new_name, prefs->GetString(prefs::kProfileName));
134  profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
135  EXPECT_EQ(new_name,
136            base::UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index)));
137
138  // Remove the setting.
139  settings->SetLocalSettingForTesting(supervised_users::kUserName,
140                                      scoped_ptr<base::Value>());
141  EXPECT_EQ(original_name, prefs->GetString(prefs::kProfileName));
142  profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
143  EXPECT_EQ(original_name,
144            base::UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index)));
145}
146