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, false)); 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(false); 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), // username_hash 48 false, 49 false); 50 } 51 52 void MockAuthenticator::LoginRetailMode() { 53 consumer_->OnRetailModeLoginSuccess(UserContext("demo-mode", 54 std::string(), 55 std::string(), 56 "demo-mode")); 57 } 58 59 void MockAuthenticator::LoginAsPublicAccount(const std::string& username) { 60 consumer_->OnLoginSuccess(UserContext(expected_username_, 61 std::string(), 62 std::string(), 63 expected_username_), 64 false, 65 false); 66 } 67 68 void MockAuthenticator::LoginOffTheRecord() { 69 consumer_->OnOffTheRecordLoginSuccess(); 70 } 71 72 void MockAuthenticator::OnRetailModeLoginSuccess() { 73 consumer_->OnRetailModeLoginSuccess(UserContext(expected_username_, 74 std::string(), 75 std::string(), 76 expected_username_)); 77 } 78 79 void MockAuthenticator::OnLoginSuccess(bool request_pending) { 80 // If we want to be more like the real thing, we could save username 81 // in AuthenticateToLogin, but there's not much of a point. 82 consumer_->OnLoginSuccess(UserContext(expected_username_, 83 expected_password_, 84 std::string(), 85 expected_username_), 86 request_pending, 87 false); 88 } 89 90 void MockAuthenticator::OnLoginFailure(const LoginFailure& failure) { 91 consumer_->OnLoginFailure(failure); 92 } 93 94 void MockAuthenticator::SetExpectedCredentials( 95 const std::string& expected_username, 96 const std::string& expected_password) { 97 expected_username_ = expected_username; 98 expected_password_ = expected_password; 99 } 100 101 } // namespace chromeos 102