mock_authenticator.h revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2010 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
8#include <string>
9
10#include "chrome/browser/chromeos/login/authenticator.h"
11#include "chrome/browser/chromeos/login/login_utils.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14class Profile;
15
16namespace chromeos {
17
18class LoginStatusConsumer;
19
20class MockAuthenticator : public Authenticator {
21 public:
22  MockAuthenticator(LoginStatusConsumer* consumer,
23                    const std::string& expected_username,
24                    const std::string& expected_password)
25      : Authenticator(consumer),
26        expected_username_(expected_username),
27        expected_password_(expected_password) {
28  }
29
30  // Returns true after posting task to UI thread to call OnLoginSuccess().
31  // This is called on the FILE thread now, so we need to do this.
32  virtual bool AuthenticateToLogin(Profile* profile,
33                                   const std::string& username,
34                                   const std::string& password,
35                                   const std::string& login_token,
36                                   const std::string& login_captcha) {
37    if (expected_username_ == username &&
38        expected_password_ == password) {
39      ChromeThread::PostTask(
40          ChromeThread::UI, FROM_HERE,
41          NewRunnableMethod(this,
42                            &MockAuthenticator::OnLoginSuccess,
43                            GaiaAuthConsumer::ClientLoginResult()));
44      return true;
45    } else {
46      ChromeThread::PostTask(
47          ChromeThread::UI, FROM_HERE,
48          NewRunnableMethod(this,
49                            &MockAuthenticator::OnLoginFailure,
50                            std::string("Login failed")));
51      return false;
52    }
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    // If we want to be more like the real thing, we could save username
67    // in AuthenticateToLogin, but there's not much of a point.
68    consumer_->OnLoginSuccess(expected_username_, credentials);
69  }
70
71  void OnLoginFailure(const std::string& data) {
72      consumer_->OnLoginFailure(data);
73      LOG(INFO) << "Posting a QuitTask to UI thread";
74      ChromeThread::PostTask(
75          ChromeThread::UI, FROM_HERE, new MessageLoop::QuitTask);
76  }
77
78  virtual void RecoverEncryptedData(
79      const std::string& old_password,
80      const GaiaAuthConsumer::ClientLoginResult& credentials) {}
81
82  virtual void ResyncEncryptedData(
83      const GaiaAuthConsumer::ClientLoginResult& credentials) {}
84
85 private:
86  std::string expected_username_;
87  std::string expected_password_;
88
89  DISALLOW_COPY_AND_ASSIGN(MockAuthenticator);
90};
91
92class MockLoginUtils : public LoginUtils {
93 public:
94  explicit MockLoginUtils(const std::string& expected_username,
95                          const std::string& expected_password)
96      : expected_username_(expected_username),
97        expected_password_(expected_password) {
98  }
99
100  virtual bool ShouldWaitForWifi() {
101    return false;
102  }
103
104  virtual void CompleteLogin(const std::string& username,
105                             const GaiaAuthConsumer::ClientLoginResult& res) {
106    EXPECT_EQ(expected_username_, username);
107  }
108
109  virtual void CompleteOffTheRecordLogin() {
110  }
111
112  virtual Authenticator* CreateAuthenticator(LoginStatusConsumer* consumer) {
113    return new MockAuthenticator(
114        consumer, expected_username_, expected_password_);
115  }
116
117  virtual void EnableBrowserLaunch(bool enable) {
118  }
119
120  virtual bool IsBrowserLaunchEnabled() const {
121    return true;
122  }
123
124  virtual const std::string& GetAuthToken() const {
125    return auth_token_;
126  }
127
128 private:
129  std::string expected_username_;
130  std::string expected_password_;
131  std::string auth_token_;
132
133  DISALLOW_COPY_AND_ASSIGN(MockLoginUtils);
134};
135
136}  // namespace chromeos
137
138#endif  // CHROME_BROWSER_CHROMEOS_LOGIN_MOCK_AUTHENTICATOR_H_
139