Home | History | Annotate | Download | only in login
      1 // Copyright (c) 2012 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 "chrome/browser/chromeos/login/mock_authenticator.h"
      6 
      7 #include "base/bind.h"
      8 #include "chrome/browser/chromeos/login/user.h"
      9 #include "content/public/browser/browser_thread.h"
     10 
     11 using content::BrowserThread;
     12 
     13 namespace chromeos {
     14 
     15 void MockAuthenticator::AuthenticateToLogin(Profile* profile,
     16                                             const UserContext& user_context) {
     17   if (expected_username_ == user_context.username &&
     18       expected_password_ == user_context.password) {
     19     BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
     20         base::Bind(&MockAuthenticator::OnLoginSuccess, this));
     21     return;
     22   }
     23   GoogleServiceAuthError error(
     24       GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
     25   BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
     26       base::Bind(&MockAuthenticator::OnLoginFailure, this,
     27                  LoginFailure::FromNetworkAuthFailure(error)));
     28 }
     29 
     30 void MockAuthenticator::CompleteLogin(Profile* profile,
     31                                       const UserContext& user_context) {
     32   CHECK_EQ(expected_username_, user_context.username);
     33   CHECK_EQ(expected_password_, user_context.password);
     34   OnLoginSuccess();
     35 }
     36 
     37 void MockAuthenticator::AuthenticateToUnlock(
     38     const UserContext& user_context) {
     39   AuthenticateToLogin(NULL /* not used */, user_context);
     40 }
     41 
     42 void MockAuthenticator::LoginAsLocallyManagedUser(
     43     const UserContext& user_context) {
     44   consumer_->OnLoginSuccess(UserContext(expected_username_,
     45                                         std::string(),
     46                                         std::string(),
     47                                         user_context.username)); // hash
     48 }
     49 
     50 void MockAuthenticator::LoginRetailMode() {
     51   consumer_->OnRetailModeLoginSuccess(UserContext("demo-mode",
     52                                                   std::string(),
     53                                                   std::string(),
     54                                                   "demo-mode"));
     55 }
     56 
     57 void MockAuthenticator::LoginAsPublicAccount(const std::string& username) {
     58   consumer_->OnLoginSuccess(UserContext(expected_username_,
     59                                         std::string(),
     60                                         std::string(),
     61                                         expected_username_));
     62 }
     63 
     64 void MockAuthenticator::LoginAsKioskAccount(
     65     const std::string& app_user_id) {
     66   consumer_->OnLoginSuccess(UserContext(expected_username_,
     67                                         std::string(),
     68                                         std::string(),
     69                                         expected_username_));
     70 }
     71 
     72 void MockAuthenticator::LoginOffTheRecord() {
     73   consumer_->OnOffTheRecordLoginSuccess();
     74 }
     75 
     76 void MockAuthenticator::OnRetailModeLoginSuccess() {
     77   consumer_->OnRetailModeLoginSuccess(UserContext(expected_username_,
     78                                                   std::string(),
     79                                                   std::string(),
     80                                                   expected_username_));
     81 }
     82 
     83 void MockAuthenticator::OnLoginSuccess() {
     84   // If we want to be more like the real thing, we could save username
     85   // in AuthenticateToLogin, but there's not much of a point.
     86   consumer_->OnLoginSuccess(UserContext(expected_username_,
     87                                         expected_password_,
     88                                         std::string(),
     89                                         expected_username_));
     90 }
     91 
     92 void MockAuthenticator::OnLoginFailure(const LoginFailure& failure) {
     93     consumer_->OnLoginFailure(failure);
     94 }
     95 
     96 void MockAuthenticator::SetExpectedCredentials(
     97     const std::string& expected_username,
     98     const std::string& expected_password) {
     99   expected_username_ = expected_username;
    100   expected_password_ = expected_password;
    101 }
    102 
    103 }  // namespace chromeos
    104