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 "chrome/browser/chromeos/login/fake_login_utils.h"
7 #include "base/command_line.h"
8 #include "base/path_service.h"
9 #include "base/prefs/pref_service.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/chromeos/login/login_display_host.h"
13 #include "chrome/browser/chromeos/login/mock_authenticator.h"
14 #include "chrome/browser/chromeos/login/supervised_user_manager.h"
15 #include "chrome/browser/first_run/first_run.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/ui/startup/startup_browser_creator.h"
18 #include "chrome/common/chrome_constants.h"
19 #include "chrome/common/chrome_paths.h"
20 #include "chrome/common/pref_names.h"
21 #include "chrome/test/base/testing_profile.h"
22 #include "content/public/browser/notification_service.h"
23 #include "testing/gtest/include/gtest/gtest.h"
27 FakeLoginUtils::FakeLoginUtils() : should_launch_browser_(false) {}
29 FakeLoginUtils::~FakeLoginUtils() {}
31 void FakeLoginUtils::DoBrowserLaunch(Profile* profile,
32 LoginDisplayHost* login_host) {
34 if (!UserManager::Get()->GetCurrentUserFlow()->ShouldLaunchBrowser()) {
35 UserManager::Get()->GetCurrentUserFlow()->LaunchExtraSteps(profile);
38 login_host->BeforeSessionStart();
39 if (should_launch_browser_) {
40 StartupBrowserCreator browser_creator;
41 chrome::startup::IsFirstRun first_run =
42 first_run::IsChromeFirstRun() ? chrome::startup::IS_FIRST_RUN
43 : chrome::startup::IS_NOT_FIRST_RUN;
45 browser_creator.LaunchBrowser(*CommandLine::ForCurrentProcess(),
48 chrome::startup::IS_PROCESS_STARTUP,
53 login_host->Finalize();
54 UserManager::Get()->SessionStarted();
57 void FakeLoginUtils::PrepareProfile(const UserContext& user_context,
58 const std::string& display_email,
60 bool has_active_session,
61 LoginUtils::Delegate* delegate) {
62 UserManager::Get()->UserLoggedIn(
63 user_context.username, user_context.username_hash, false);
65 if (should_launch_browser_) {
66 profile = CreateProfile(user_context.username);
68 TestingProfile* testing_profile = new TestingProfile();
69 testing_profile->set_profile_name(user_context.username);
71 profile = testing_profile;
72 g_browser_process->profile_manager()->
73 RegisterTestingProfile(profile, false, false);
76 if (UserManager::Get()->IsLoggedInAsLocallyManagedUser()) {
77 User* active_user = UserManager::Get()->GetActiveUser();
78 std::string managed_user_sync_id =
79 UserManager::Get()->GetSupervisedUserManager()->
80 GetUserSyncId(active_user->email());
81 if (managed_user_sync_id.empty())
82 managed_user_sync_id = "DUMMY ID";
83 profile->GetPrefs()->SetString(prefs::kManagedUserId,
84 managed_user_sync_id);
87 content::NotificationService::current()->Notify(
88 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
89 content::NotificationService::AllSources(),
90 content::Details<Profile>(profile));
92 delegate->OnProfilePrepared(profile);
95 void FakeLoginUtils::DelegateDeleted(LoginUtils::Delegate* delegate) {
96 NOTREACHED() << "Method not implemented.";
99 void FakeLoginUtils::CompleteOffTheRecordLogin(const GURL& start_url) {
100 NOTREACHED() << "Method not implemented.";
103 void FakeLoginUtils::SetFirstLoginPrefs(PrefService* prefs) {
104 NOTREACHED() << "Method not implemented.";
107 scoped_refptr<Authenticator> FakeLoginUtils::CreateAuthenticator(
108 LoginStatusConsumer* consumer) {
110 new MockAuthenticator(consumer, expected_username_, expected_password_);
111 return authenticator_;
114 void FakeLoginUtils::RestoreAuthenticationSession(Profile* profile) {
115 NOTREACHED() << "Method not implemented.";
118 void FakeLoginUtils::InitRlzDelayed(Profile* user_profile) {
119 NOTREACHED() << "Method not implemented.";
122 Profile* FakeLoginUtils::CreateProfile(const std::string& username_hash) {
124 PathService::Get(chrome::DIR_USER_DATA, &path);
125 path = path.AppendASCII(chrome::kProfileDirPrefix + username_hash);
126 Profile* profile = g_browser_process->profile_manager()->GetProfile(path);
130 void FakeLoginUtils::SetExpectedCredentials(const std::string& username,
131 const std::string& password) {
132 expected_username_ = username;
133 expected_password_ = password;
134 if (authenticator_.get()) {
135 static_cast<MockAuthenticator*>(authenticator_.get())->
136 SetExpectedCredentials(username, password);
140 } // namespace chromeos