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);
64 Profile
* profile
= CreateProfile(user_context
.username
);
66 if (UserManager::Get()->IsLoggedInAsLocallyManagedUser()) {
67 User
* active_user
= UserManager::Get()->GetActiveUser();
68 std::string managed_user_sync_id
=
69 UserManager::Get()->GetSupervisedUserManager()->
70 GetUserSyncId(active_user
->email());
71 if (managed_user_sync_id
.empty())
72 managed_user_sync_id
= "DUMMY ID";
73 profile
->GetPrefs()->SetString(prefs::kManagedUserId
,
74 managed_user_sync_id
);
77 content::NotificationService::current()->Notify(
78 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED
,
79 content::NotificationService::AllSources(),
80 content::Details
<Profile
>(profile
));
82 delegate
->OnProfilePrepared(profile
);
85 void FakeLoginUtils::DelegateDeleted(LoginUtils::Delegate
* delegate
) {
86 NOTREACHED() << "Method not implemented.";
89 void FakeLoginUtils::CompleteOffTheRecordLogin(const GURL
& start_url
) {
90 NOTREACHED() << "Method not implemented.";
93 void FakeLoginUtils::SetFirstLoginPrefs(PrefService
* prefs
) {
94 NOTREACHED() << "Method not implemented.";
97 scoped_refptr
<Authenticator
> FakeLoginUtils::CreateAuthenticator(
98 LoginStatusConsumer
* consumer
) {
100 new MockAuthenticator(consumer
, expected_username_
, expected_password_
);
101 return authenticator_
;
104 void FakeLoginUtils::RestoreAuthenticationSession(Profile
* profile
) {
105 NOTREACHED() << "Method not implemented.";
108 void FakeLoginUtils::InitRlzDelayed(Profile
* user_profile
) {
109 NOTREACHED() << "Method not implemented.";
112 Profile
* FakeLoginUtils::CreateProfile(const std::string
& username_hash
) {
114 PathService::Get(chrome::DIR_USER_DATA
, &path
);
115 path
= path
.AppendASCII(chrome::kProfileDirPrefix
+ username_hash
);
116 Profile
* profile
= g_browser_process
->profile_manager()->GetProfile(path
);
120 void FakeLoginUtils::SetExpectedCredentials(const std::string
& username
,
121 const std::string
& password
) {
122 expected_username_
= username
;
123 expected_password_
= password
;
124 if (authenticator_
.get()) {
125 static_cast<MockAuthenticator
*>(authenticator_
.get())->
126 SetExpectedCredentials(username
, password
);
130 } // namespace chromeos