mock_authenticator.h revision ddb351dbec246cf1fab5ec20d2d5520909041de1
1// Copyright (c) 2011 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#ifndef CHROME_BROWSER_CHROMEOS_LOGIN_MOCK_AUTHENTICATOR_H_
6#define CHROME_BROWSER_CHROMEOS_LOGIN_MOCK_AUTHENTICATOR_H_
7#pragma once
8
9#include <string>
10
11#include "chrome/browser/chromeos/login/authenticator.h"
12#include "chrome/browser/chromeos/login/background_view.h"
13#include "chrome/browser/chromeos/login/login_utils.h"
14#include "chrome/common/net/gaia/google_service_auth_error.h"
15#include "content/browser/browser_thread.h"
16#include "testing/gtest/include/gtest/gtest.h"
17
18class Profile;
19
20namespace chromeos {
21
22class LoginStatusConsumer;
23
24class MockAuthenticator : public Authenticator {
25 public:
26  MockAuthenticator(LoginStatusConsumer* consumer,
27                    const std::string& expected_username,
28                    const std::string& expected_password)
29      : Authenticator(consumer),
30        expected_username_(expected_username),
31        expected_password_(expected_password) {
32  }
33
34  // Returns true after posting task to UI thread to call OnLoginSuccess().
35  // This is called on the FILE thread now, so we need to do this.
36  virtual bool AuthenticateToLogin(Profile* profile,
37                                   const std::string& username,
38                                   const std::string& password,
39                                   const std::string& login_token,
40                                   const std::string& login_captcha) {
41    if (expected_username_ == username && expected_password_ == password) {
42      BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
43          NewRunnableMethod(this, &MockAuthenticator::OnLoginSuccess,
44                            GaiaAuthConsumer::ClientLoginResult(), false));
45      return true;
46    }
47    GoogleServiceAuthError error(
48        GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
49    BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
50        NewRunnableMethod(this, &MockAuthenticator::OnLoginFailure,
51                          LoginFailure::FromNetworkAuthFailure(error)));
52    return false;
53  }
54
55  virtual bool AuthenticateToUnlock(const std::string& username,
56                                    const std::string& password) {
57    return AuthenticateToLogin(NULL /* not used */, username, password,
58                               std::string(), std::string());
59  }
60
61  virtual void LoginOffTheRecord() {
62    consumer_->OnOffTheRecordLoginSuccess();
63  }
64
65  void OnLoginSuccess(const GaiaAuthConsumer::ClientLoginResult& credentials,
66                      bool request_pending) {
67    // If we want to be more like the real thing, we could save username
68    // in AuthenticateToLogin, but there's not much of a point.
69    consumer_->OnLoginSuccess(expected_username_,
70                              expected_password_,
71                              credentials,
72                              request_pending);
73  }
74
75  void OnLoginFailure(const LoginFailure& failure) {
76      consumer_->OnLoginFailure(failure);
77      VLOG(1) << "Posting a QuitTask to UI thread";
78      BrowserThread::PostTask(
79          BrowserThread::UI, FROM_HERE, new MessageLoop::QuitTask);
80  }
81
82  virtual void RecoverEncryptedData(
83      const std::string& old_password,
84      const GaiaAuthConsumer::ClientLoginResult& credentials) {}
85
86  virtual void ResyncEncryptedData(
87      const GaiaAuthConsumer::ClientLoginResult& credentials) {}
88
89  virtual void RetryAuth(Profile* profile,
90                         const std::string& username,
91                         const std::string& password,
92                         const std::string& login_token,
93                         const std::string& login_captcha) {}
94
95 private:
96  std::string expected_username_;
97  std::string expected_password_;
98
99  DISALLOW_COPY_AND_ASSIGN(MockAuthenticator);
100};
101
102class MockLoginUtils : public LoginUtils {
103 public:
104  explicit MockLoginUtils(const std::string& expected_username,
105                          const std::string& expected_password)
106      : expected_username_(expected_username),
107        expected_password_(expected_password) {
108  }
109
110  virtual bool ShouldWaitForWifi() {
111    return false;
112  }
113
114  virtual void PrepareProfile(const std::string& username,
115                              const std::string& password,
116                              const GaiaAuthConsumer::ClientLoginResult& res,
117                              bool pending_requests,
118                              Delegate* delegate) {
119    EXPECT_EQ(expected_username_, username);
120    EXPECT_EQ(expected_password_, password);
121    // Profile hasn't been loaded.
122    delegate->OnProfilePrepared(NULL);
123  }
124
125  virtual void CompleteOffTheRecordLogin(const GURL& start_url) {
126  }
127
128  virtual void SetFirstLoginPrefs(PrefService* prefs) {
129  }
130
131  virtual Authenticator* CreateAuthenticator(LoginStatusConsumer* consumer) {
132    return new MockAuthenticator(
133        consumer, expected_username_, expected_password_);
134  }
135
136  virtual void PrewarmAuthentication() {
137  }
138
139  virtual void FetchCookies(
140      Profile* profile,
141      const GaiaAuthConsumer::ClientLoginResult& credentials) {
142  }
143
144  virtual void FetchTokens(
145      Profile* profile,
146      const GaiaAuthConsumer::ClientLoginResult& credentials) {
147  }
148
149  void SetBackgroundView(BackgroundView* background_view) {
150    background_view_ = background_view;
151  }
152
153  BackgroundView* GetBackgroundView() {
154    return background_view_;
155  }
156
157  virtual std::string GetOffTheRecordCommandLine(
158      const GURL& start_url,
159      const CommandLine& base_command_line,
160      CommandLine* command_line) {
161    return std::string();
162  }
163
164 private:
165  std::string expected_username_;
166  std::string expected_password_;
167  std::string auth_token_;
168  chromeos::BackgroundView* background_view_;
169
170  DISALLOW_COPY_AND_ASSIGN(MockLoginUtils);
171};
172
173}  // namespace chromeos
174
175#endif  // CHROME_BROWSER_CHROMEOS_LOGIN_MOCK_AUTHENTICATOR_H_
176