17d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
27d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
37d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// found in the LICENSE file.
47d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
57d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/browser/chromeos/login/fake_login_utils.h"
67d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
75f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "base/callback.h"
87d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/command_line.h"
94e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/prefs/pref_service.h"
10424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "chrome/browser/chrome_notification_types.h"
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/chromeos/login/ui/login_display_host.h"
120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "chrome/browser/chromeos/login/user_flow.h"
136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "chrome/browser/chromeos/login/users/chrome_user_manager.h"
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/chromeos/login/users/supervised_user_manager.h"
15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/chromeos/profiles/profile_helper.h"
167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/browser/first_run/first_run.h"
170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "chrome/browser/profiles/profile.h"
187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/browser/ui/startup/startup_browser_creator.h"
194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "chrome/common/pref_names.h"
207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/test/base/testing_profile.h"
215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "chromeos/login/auth/mock_authenticator.h"
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chromeos/login/auth/user_context.h"
235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/user_manager/user.h"
246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "components/user_manager/user_manager.h"
25424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "content/public/browser/notification_service.h"
267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace chromeos {
297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)FakeLoginUtils::FakeLoginUtils() : should_launch_browser_(false) {}
317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)FakeLoginUtils::~FakeLoginUtils() {}
337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void FakeLoginUtils::RespectLocalePreference(Profile*,
355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                             const base::Closure& callback) {
365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  callback.Run();
375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void FakeLoginUtils::DoBrowserLaunch(Profile* profile,
407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                     LoginDisplayHost* login_host) {
416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (!ChromeUserManager::Get()->GetCurrentUserFlow()->ShouldLaunchBrowser()) {
426e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    ChromeUserManager::Get()->GetCurrentUserFlow()->LaunchExtraSteps(profile);
43424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      return;
44424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  login_host->BeforeSessionStart();
467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (should_launch_browser_) {
477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    StartupBrowserCreator browser_creator;
487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    chrome::startup::IsFirstRun first_run =
497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        first_run::IsChromeFirstRun() ? chrome::startup::IS_FIRST_RUN
507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                      : chrome::startup::IS_NOT_FIRST_RUN;
517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    ASSERT_TRUE(
527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        browser_creator.LaunchBrowser(*CommandLine::ForCurrentProcess(),
537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                      profile,
547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                      base::FilePath(),
557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                      chrome::startup::IS_PROCESS_STARTUP,
567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                      first_run,
577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                      NULL));
587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (login_host)
607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    login_host->Finalize();
616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  user_manager::UserManager::Get()->SessionStarted();
627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void FakeLoginUtils::PrepareProfile(const UserContext& user_context,
657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                    bool has_cookies,
667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                    bool has_active_session,
677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                    LoginUtils::Delegate* delegate) {
686e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  user_manager::UserManager::Get()->UserLoggedIn(
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      user_context.GetUserID(), user_context.GetUserIDHash(), false);
705f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  user_manager::User* user =
716e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      user_manager::UserManager::Get()->FindUserAndModify(
726e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          user_context.GetUserID());
730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  DCHECK(user);
740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Make sure that we get the real Profile instead of the login Profile.
760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  user->set_profile_is_created();
776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  Profile* profile = ProfileHelper::Get()->GetProfileByUserUnsafe(user);
78f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  profile->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
79f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                 user_context.GetUserID());
804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
816e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (user_manager::UserManager::Get()->IsLoggedInAsSupervisedUser()) {
826e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    user_manager::User* active_user =
836e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        user_manager::UserManager::Get()->GetActiveUser();
84f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    std::string supervised_user_sync_id =
856e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        ChromeUserManager::Get()->GetSupervisedUserManager()->GetUserSyncId(
866e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)            active_user->email());
87f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    if (supervised_user_sync_id.empty())
88f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      supervised_user_sync_id = "DUMMY ID";
89f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    profile->GetPrefs()->SetString(prefs::kSupervisedUserId,
90f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                   supervised_user_sync_id);
914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
93424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  content::NotificationService::current()->Notify(
94424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
95424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      content::NotificationService::AllSources(),
96424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      content::Details<Profile>(profile));
977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (delegate)
987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    delegate->OnProfilePrepared(profile);
997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void FakeLoginUtils::DelegateDeleted(LoginUtils::Delegate* delegate) {
1027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  NOTREACHED() << "Method not implemented.";
1037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void FakeLoginUtils::CompleteOffTheRecordLogin(const GURL& start_url) {
1067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  NOTREACHED() << "Method not implemented.";
1077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)scoped_refptr<Authenticator> FakeLoginUtils::CreateAuthenticator(
110116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    AuthStatusConsumer* consumer) {
111cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  authenticator_ = new MockAuthenticator(consumer, expected_user_context_);
1127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return authenticator_;
1137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
115116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool FakeLoginUtils::RestartToApplyPerSessionFlagsIfNeed(Profile* profile,
116116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                                         bool early_restart) {
117116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  NOTREACHED() << "Method not implemented.";
118116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return false;
119116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
120116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
121cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void FakeLoginUtils::SetExpectedCredentials(const UserContext& user_context) {
122cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  expected_user_context_ = user_context;
1231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (authenticator_.get()) {
1247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    static_cast<MockAuthenticator*>(authenticator_.get())->
125cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        SetExpectedCredentials(user_context);
1267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
1277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}  //  namespace chromeos
130