Index: chrome/browser/chromeos/login/google_authenticator_unittest.cc |
diff --git a/chrome/browser/chromeos/login/google_authenticator_unittest.cc b/chrome/browser/chromeos/login/google_authenticator_unittest.cc |
index f866eba58b9e16357ae6529d982c9fec1fb11139..c29debafd38422963d4fd5225133b47fd25be075 100644 |
--- a/chrome/browser/chromeos/login/google_authenticator_unittest.cc |
+++ b/chrome/browser/chromeos/login/google_authenticator_unittest.cc |
@@ -51,6 +51,64 @@ class MockConsumer : public LoginStatusConsumer { |
void(const GaiaAuthConsumer::ClientLoginResult& result)); |
}; |
+// Responds as though ClientLogin returned from the server. |
+class TimedMockFetcher : public URLFetcher { |
+ public: |
+ TimedMockFetcher(bool success, |
+ const GURL& url, |
DaveMoore
2010/07/28 04:44:33
nit: indenting
Chris Masone
2010/07/28 05:55:52
Done.
|
+ URLFetcher::RequestType request_type, |
+ URLFetcher::Delegate* d) |
+ : URLFetcher(url, request_type, d), |
+ success_(success), |
+ url_(url), |
+ sleeper_(new Sleeper) { |
+ } |
DaveMoore
2010/07/28 04:44:33
nit: nl
Chris Masone
2010/07/28 05:55:52
Done.
|
+ ~TimedMockFetcher() { |
oshima
2010/07/28 08:00:10
virtual (just for style)
|
+ sleeper_->task_->Cancel(); |
+ } |
+ |
+ void Start() { |
+ LOG(INFO) << "Delaying fetch completion in mock"; |
+ sleeper_->DelayedCompleteFetch(success_, url_, delegate(), 100); |
+ } |
+ |
+ private: |
+ class Sleeper : public base::RefCountedThreadSafe<Sleeper> { |
+ public: |
+ Sleeper() {} |
+ virtual ~Sleeper() {} |
+ |
+ void DelayedCompleteFetch(bool success, |
+ const GURL& url, |
+ URLFetcher::Delegate* d, |
+ int delay_ms) { |
+ task_ = NewRunnableMethod(this, |
+ &TimedMockFetcher::Sleeper::CompleteFetch, |
+ success, |
+ url, |
+ d); |
+ ChromeThread::PostDelayedTask(ChromeThread::UI, |
+ FROM_HERE, |
+ task_, |
+ delay_ms); |
+ } |
+ |
+ void CompleteFetch(bool success, |
+ const GURL& url, |
+ URLFetcher::Delegate* d) { |
+ ADD_FAILURE() << "Should not have completed fetch in TimedMockFetcher"; |
+ MessageLoop::current()->Quit(); // Allow exiting even if we mess up. |
+ } |
DaveMoore
2010/07/28 04:44:33
nit: nl
Chris Masone
2010/07/28 05:55:52
Done.
|
+ CancelableTask* task_; |
oshima
2010/07/28 08:00:10
DISALLOW_COPY_AND_ASSIGN
|
+ }; |
+ bool success_; |
+ GURL url_; |
+ |
+ scoped_refptr<Sleeper> sleeper_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TimedMockFetcher); |
+}; |
+ |
class GoogleAuthenticatorTest : public ::testing::Test { |
public: |
GoogleAuthenticatorTest() |
@@ -134,12 +192,11 @@ class GoogleAuthenticatorTest : public ::testing::Test { |
} |
void CancelLogin(GoogleAuthenticator* auth) { |
- ChromeThread::PostDelayedTask( |
+ ChromeThread::PostTask( |
ChromeThread::UI, |
FROM_HERE, |
NewRunnableMethod(auth, |
- &GoogleAuthenticator::CancelClientLogin), |
- 50); |
+ &GoogleAuthenticator::CancelClientLogin)); |
} |
unsigned char fake_hash_[32]; |
@@ -486,12 +543,22 @@ TEST_F(GoogleAuthenticatorTest, CheckLocalaccount) { |
} |
// Compatible with LoginStatusConsumer::OnLoginSuccess() |
-static void Quit(const std::string& username, |
- const GaiaAuthConsumer::ClientLoginResult& credentials) { |
+static void OnSuccessQuit( |
+ const std::string& username, |
+ const GaiaAuthConsumer::ClientLoginResult& credentials) { |
+ MessageLoop::current()->Quit(); |
+} |
DaveMoore
2010/07/28 04:44:33
nit: nl
Chris Masone
2010/07/28 05:55:52
Done.
|
+static void OnSuccessQuitAndFail( |
+ const std::string& username, |
+ const GaiaAuthConsumer::ClientLoginResult& credentials) { |
+ ADD_FAILURE() << "Login should NOT have succeeded!"; |
MessageLoop::current()->Quit(); |
} |
DaveMoore
2010/07/28 04:44:33
nit: nl
Chris Masone
2010/07/28 05:55:52
Done.
|
// Compatible with LoginStatusConsumer::OnLoginFailure() |
-static void QuitAndFail(const std::string& error) { |
+static void OnFailQuit(const std::string& error) { |
+ MessageLoop::current()->Quit(); |
+} |
DaveMoore
2010/07/28 04:44:33
nit: nl
Chris Masone
2010/07/28 05:55:52
Done.
|
+static void OnFailQuitAndFail(const std::string& error) { |
ADD_FAILURE() << "Login should have succeeded!"; |
MessageLoop::current()->Quit(); |
} |
oshima
2010/07/28 08:00:10
better to move these static functions to anonymous
|
@@ -504,17 +571,15 @@ TEST_F(GoogleAuthenticatorTest, LocalaccountLogin) { |
ChromeThread ui_thread(ChromeThread::UI, &message_loop); |
MockConsumer consumer; |
- ON_CALL(consumer, OnLoginSuccess(username_, _)) |
- .WillByDefault(Invoke(Quit)); |
EXPECT_CALL(consumer, OnLoginSuccess(username_, _)) |
- .Times(1) |
+ .WillOnce(Invoke(OnSuccessQuit)) |
.RetiresOnSaturation(); |
EXPECT_CALL(*mock_library_, MountForBwsi(_)) |
.WillOnce(Return(true)) |
.RetiresOnSaturation(); |
// Enable the test to terminate (and fail), even if the login fails. |
ON_CALL(consumer, OnLoginFailure(_)) |
- .WillByDefault(Invoke(QuitAndFail)); |
+ .WillByDefault(Invoke(OnFailQuitAndFail)); |
// Manually prep for login, so that localaccount isn't set for us. |
scoped_refptr<GoogleAuthenticator> auth(new GoogleAuthenticator(&consumer)); |
@@ -562,7 +627,7 @@ TEST_F(GoogleAuthenticatorTest, FullLogin) { |
TestingProfile profile; |
- MockFactory factory; |
+ MockFactory<MockFetcher> factory; |
URLFetcher::set_factory(&factory); |
scoped_refptr<GoogleAuthenticator> auth(new GoogleAuthenticator(&consumer)); |
@@ -574,14 +639,16 @@ TEST_F(GoogleAuthenticatorTest, FullLogin) { |
} |
TEST_F(GoogleAuthenticatorTest, CancelLogin) { |
- MessageLoopForUI message_loop; |
+ MessageLoop message_loop(MessageLoop::TYPE_UI); |
oshima
2010/07/28 08:00:10
just for my understanding. what's the difference?
Chris Masone
2010/07/28 15:25:30
You can't call Run() on a MessageLoopForUI, only R
|
ChromeThread ui_thread(ChromeThread::UI, &message_loop); |
chromeos::CryptohomeBlob salt_v(fake_hash_, fake_hash_ + sizeof(fake_hash_)); |
MockConsumer consumer; |
EXPECT_CALL(consumer, OnLoginFailure(_)) |
- .Times(1) |
+ .WillOnce(Invoke(OnFailQuit)) |
.RetiresOnSaturation(); |
+ ON_CALL(consumer, OnLoginSuccess(username_, _)) |
+ .WillByDefault(Invoke(OnSuccessQuitAndFail)); |
EXPECT_CALL(*mock_library_, GetSystemSalt()) |
.WillOnce(Return(salt_v)) |
@@ -592,17 +659,56 @@ TEST_F(GoogleAuthenticatorTest, CancelLogin) { |
TestingProfile profile; |
- MockFactory factory; |
- factory.set_success(false); |
+ MockFactory<TimedMockFetcher> factory; |
URLFetcher::set_factory(&factory); |
scoped_refptr<GoogleAuthenticator> auth(new GoogleAuthenticator(&consumer)); |
- ReadLocalaccountFile(auth.get(), ""); // No localaccount file. |
+ |
+ // For when |auth| tries to load the localaccount file. |
+ ChromeThread file_thread(ChromeThread::FILE); |
+ file_thread.Start(); |
+ |
auth->AuthenticateToLogin( |
&profile, username_, hash_ascii_, std::string(), std::string()); |
CancelLogin(auth.get()); |
+ |
URLFetcher::set_factory(NULL); |
- message_loop.RunAllPending(); |
+ message_loop.Run(); |
+} |
+ |
+TEST_F(GoogleAuthenticatorTest, CancelLoginAlreadyGotLocalaccount) { |
+ MessageLoop message_loop(MessageLoop::TYPE_UI); |
+ ChromeThread ui_thread(ChromeThread::UI, &message_loop); |
+ chromeos::CryptohomeBlob salt_v(fake_hash_, fake_hash_ + sizeof(fake_hash_)); |
+ |
+ MockConsumer consumer; |
+ EXPECT_CALL(consumer, OnLoginFailure(_)) |
+ .WillOnce(Invoke(OnFailQuit)) |
+ .RetiresOnSaturation(); |
+ ON_CALL(consumer, OnLoginSuccess(username_, _)) |
+ .WillByDefault(Invoke(OnSuccessQuitAndFail)); |
+ |
+ EXPECT_CALL(*mock_library_, GetSystemSalt()) |
+ .WillOnce(Return(salt_v)) |
+ .RetiresOnSaturation(); |
+ EXPECT_CALL(*mock_library_, CheckKey(username_, _)) |
+ .WillOnce(Return(false)) |
+ .RetiresOnSaturation(); |
+ |
+ TestingProfile profile; |
+ |
+ MockFactory<TimedMockFetcher> factory; |
+ URLFetcher::set_factory(&factory); |
+ |
+ scoped_refptr<GoogleAuthenticator> auth(new GoogleAuthenticator(&consumer)); |
+ ReadLocalaccountFile(auth.get(), ""); |
+ |
+ auth->AuthenticateToLogin( |
+ &profile, username_, hash_ascii_, std::string(), std::string()); |
+ CancelLogin(auth.get()); |
+ |
+ URLFetcher::set_factory(NULL); |
+ message_loop.Run(); |
} |
} // namespace chromeos |