Commit a647c750 authored by Wez's avatar Wez Committed by Commit Bot

Update ChromeOS tests to use QuitClosure to terminate RunLoops.

These tests previously relied upon QuitCurrent*Deprecated() to terminate
RunLoops when the expected conditions arose.

Bug: 844016
Change-Id: I290599b94936b2ef253925f6f58b67d017b4603b
Reviewed-on: https://chromium-review.googlesource.com/1107242Reviewed-by: default avatarAlexander Alekseev <alemate@chromium.org>
Reviewed-by: default avatarMaksim Ivanov <emaxx@chromium.org>
Commit-Queue: Wez <wez@chromium.org>
Cr-Commit-Position: refs/heads/master@{#569563}
parent 35b56274
......@@ -128,25 +128,19 @@ class InputMethodEngineBrowserTest
class KeyEventDoneCallback {
public:
explicit KeyEventDoneCallback(bool expected_argument)
: expected_argument_(expected_argument),
is_called_(false) {}
: expected_argument_(expected_argument) {}
~KeyEventDoneCallback() {}
void Run(bool consumed) {
if (consumed == expected_argument_) {
base::RunLoop::QuitCurrentWhenIdleDeprecated();
is_called_ = true;
}
if (consumed == expected_argument_)
run_loop_.Quit();
}
void WaitUntilCalled() {
while (!is_called_)
content::RunMessageLoop();
}
void WaitUntilCalled() { run_loop_.Run(); }
private:
bool expected_argument_;
bool is_called_;
base::RunLoop run_loop_;
DISALLOW_COPY_AND_ASSIGN(KeyEventDoneCallback);
};
......
......@@ -197,6 +197,7 @@ class CryptohomeAuthenticatorTest : public testing::Test {
user_manager_(new chromeos::FakeChromeUserManager()),
user_manager_enabler_(base::WrapUnique(user_manager_)),
mock_caller_(NULL),
consumer_(run_loop_.QuitClosure()),
owner_key_util_(new ownership::MockOwnerKeyUtil()) {
// Testing profile must be initialized after user_manager_ +
// user_manager_enabler_, because it will create another UserManager
......@@ -267,45 +268,48 @@ class CryptohomeAuthenticatorTest : public testing::Test {
// wasn't supposed to happen.
void FailOnLoginFailure() {
ON_CALL(consumer_, OnAuthFailure(_))
.WillByDefault(Invoke(MockAuthStatusConsumer::OnFailQuitAndFail));
.WillByDefault(
Invoke(&consumer_, &MockAuthStatusConsumer::OnFailQuitAndFail));
}
// Allow test to fail and exit gracefully, even if OnAuthSuccess()
// wasn't supposed to happen.
void FailOnLoginSuccess() {
ON_CALL(consumer_, OnAuthSuccess(_))
.WillByDefault(Invoke(MockAuthStatusConsumer::OnSuccessQuitAndFail));
.WillByDefault(
Invoke(&consumer_, &MockAuthStatusConsumer::OnSuccessQuitAndFail));
}
// Allow test to fail and exit gracefully, even if
// OnOffTheRecordAuthSuccess() wasn't supposed to happen.
void FailOnGuestLoginSuccess() {
ON_CALL(consumer_, OnOffTheRecordAuthSuccess())
.WillByDefault(
Invoke(MockAuthStatusConsumer::OnGuestSuccessQuitAndFail));
.WillByDefault(Invoke(
&consumer_, &MockAuthStatusConsumer::OnGuestSuccessQuitAndFail));
}
void ExpectLoginFailure(const AuthFailure& failure) {
EXPECT_CALL(consumer_, OnAuthFailure(failure))
.WillOnce(Invoke(MockAuthStatusConsumer::OnFailQuit))
.WillOnce(Invoke(&consumer_, &MockAuthStatusConsumer::OnFailQuit))
.RetiresOnSaturation();
}
void ExpectLoginSuccess(const UserContext& user_context) {
EXPECT_CALL(consumer_, OnAuthSuccess(user_context))
.WillOnce(Invoke(MockAuthStatusConsumer::OnSuccessQuit))
.WillOnce(Invoke(&consumer_, &MockAuthStatusConsumer::OnSuccessQuit))
.RetiresOnSaturation();
}
void ExpectGuestLoginSuccess() {
EXPECT_CALL(consumer_, OnOffTheRecordAuthSuccess())
.WillOnce(Invoke(MockAuthStatusConsumer::OnGuestSuccessQuit))
.WillOnce(
Invoke(&consumer_, &MockAuthStatusConsumer::OnGuestSuccessQuit))
.RetiresOnSaturation();
}
void ExpectPasswordChange() {
EXPECT_CALL(consumer_, OnPasswordChangeDetected())
.WillOnce(Invoke(MockAuthStatusConsumer::OnMigrateQuit))
.WillOnce(Invoke(&consumer_, &MockAuthStatusConsumer::OnMigrateQuit))
.RetiresOnSaturation();
}
......@@ -390,6 +394,7 @@ class CryptohomeAuthenticatorTest : public testing::Test {
cryptohome::MockAsyncMethodCaller* mock_caller_;
base::RunLoop run_loop_;
MockAuthStatusConsumer consumer_;
scoped_refptr<CryptohomeAuthenticator> auth_;
......@@ -586,7 +591,7 @@ TEST_F(CryptohomeAuthenticatorTest, DriveGuestLogin) {
EXPECT_CALL(*mock_caller_, AsyncMountGuest(_)).Times(1).RetiresOnSaturation();
auth_->LoginOffTheRecord();
base::RunLoop().Run();
run_loop_.Run();
}
TEST_F(CryptohomeAuthenticatorTest, DriveGuestLoginButFail) {
......@@ -599,7 +604,7 @@ TEST_F(CryptohomeAuthenticatorTest, DriveGuestLoginButFail) {
EXPECT_CALL(*mock_caller_, AsyncMountGuest(_)).Times(1).RetiresOnSaturation();
auth_->LoginOffTheRecord();
base::RunLoop().Run();
run_loop_.Run();
}
TEST_F(CryptohomeAuthenticatorTest, DriveDataResync) {
......@@ -628,7 +633,7 @@ TEST_F(CryptohomeAuthenticatorTest, DriveDataResync) {
SetAttemptState(auth_.get(), state_.release());
auth_->ResyncEncryptedData();
base::RunLoop().Run();
run_loop_.Run();
}
TEST_F(CryptohomeAuthenticatorTest, DriveResyncFail) {
......@@ -646,7 +651,7 @@ TEST_F(CryptohomeAuthenticatorTest, DriveResyncFail) {
SetAttemptState(auth_.get(), state_.release());
auth_->ResyncEncryptedData();
base::RunLoop().Run();
run_loop_.Run();
}
TEST_F(CryptohomeAuthenticatorTest, DriveRequestOldPassword) {
......@@ -686,7 +691,7 @@ TEST_F(CryptohomeAuthenticatorTest, DriveDataRecover) {
SetAttemptState(auth_.get(), state_.release());
auth_->RecoverEncryptedData(std::string());
base::RunLoop().Run();
run_loop_.Run();
}
TEST_F(CryptohomeAuthenticatorTest, DriveDataRecoverButFail) {
......@@ -706,7 +711,7 @@ TEST_F(CryptohomeAuthenticatorTest, DriveDataRecoverButFail) {
SetAttemptState(auth_.get(), state_.release());
auth_->RecoverEncryptedData(std::string());
base::RunLoop().Run();
run_loop_.Run();
}
TEST_F(CryptohomeAuthenticatorTest, ResolveOfflineNoMount) {
......@@ -791,7 +796,7 @@ TEST_F(CryptohomeAuthenticatorTest, DriveUnlock) {
ExpectCheckKeyExCall();
auth_->AuthenticateToUnlock(user_context_);
base::RunLoop().Run();
run_loop_.Run();
}
TEST_F(CryptohomeAuthenticatorTest, DriveLoginWithPreHashedPassword) {
......@@ -812,7 +817,7 @@ TEST_F(CryptohomeAuthenticatorTest, DriveLoginWithPreHashedPassword) {
ExpectMountExCall(false /* expect_create_attempt */);
auth_->AuthenticateToLogin(NULL, user_context_);
base::RunLoop().Run();
run_loop_.Run();
}
TEST_F(CryptohomeAuthenticatorTest, FailLoginWithMissingSalt) {
......@@ -828,7 +833,7 @@ TEST_F(CryptohomeAuthenticatorTest, FailLoginWithMissingSalt) {
std::unique_ptr<std::string>());
auth_->AuthenticateToLogin(NULL, user_context_);
base::RunLoop().Run();
run_loop_.Run();
}
} // namespace chromeos
......@@ -47,7 +47,7 @@ namespace {
// An object that wait for lock state and fullscreen state.
class Waiter : public content::NotificationObserver {
public:
explicit Waiter(Browser* browser) : browser_(browser), running_(false) {
explicit Waiter(Browser* browser) : browser_(browser) {
registrar_.Add(this, chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
content::NotificationService::AllSources());
registrar_.Add(this, chrome::NOTIFICATION_FULLSCREEN_CHANGED,
......@@ -61,30 +61,29 @@ class Waiter : public content::NotificationObserver {
const content::NotificationDetails& details) override {
DCHECK(type == chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED ||
type == chrome::NOTIFICATION_FULLSCREEN_CHANGED);
if (running_)
base::RunLoop::QuitCurrentWhenIdleDeprecated();
if (quit_loop_)
std::move(quit_loop_).Run();
}
// Wait until the two conditions are met.
void Wait(bool locker_state, bool fullscreen) {
running_ = true;
std::unique_ptr<chromeos::test::ScreenLockerTester> tester(
chromeos::ScreenLocker::GetTester());
while (tester->IsLocked() != locker_state ||
browser_->window()->IsFullscreen() != fullscreen) {
content::RunMessageLoop();
base::RunLoop run_loop;
quit_loop_ = run_loop.QuitClosure();
run_loop.Run();
}
// Make sure all pending tasks are executed.
content::RunAllPendingInMessageLoop();
running_ = false;
}
private:
Browser* browser_;
content::NotificationRegistrar registrar_;
// Are we currently running the message loop?
bool running_;
base::OnceClosure quit_loop_;
DISALLOW_COPY_AND_ASSIGN(Waiter);
};
......
......@@ -2090,7 +2090,8 @@ IN_PROC_BROWSER_TEST_F(DeviceLocalAccountTest, TermsOfServiceWithLocaleSwitch) {
// Set up an observer that will quit the message loop when login has succeeded
// and the first wizard screen, if any, is being shown.
base::RunLoop login_wait_run_loop;
chromeos::MockAuthStatusConsumer login_status_consumer;
chromeos::MockAuthStatusConsumer login_status_consumer(
login_wait_run_loop.QuitClosure());
EXPECT_CALL(login_status_consumer, OnAuthSuccess(_)).Times(1).WillOnce(
InvokeWithoutArgs(&login_wait_run_loop, &base::RunLoop::Quit));
chromeos::ExistingUserController* controller =
......@@ -2308,7 +2309,8 @@ IN_PROC_BROWSER_TEST_P(TermsOfServiceDownloadTest, TermsOfServiceScreen) {
// Set up an observer that will quit the message loop when login has succeeded
// and the first wizard screen, if any, is being shown.
base::RunLoop login_wait_run_loop;
chromeos::MockAuthStatusConsumer login_status_consumer;
chromeos::MockAuthStatusConsumer login_status_consumer(
login_wait_run_loop.QuitClosure());
EXPECT_CALL(login_status_consumer, OnAuthSuccess(_)).Times(1).WillOnce(
InvokeWithoutArgs(&login_wait_run_loop, &base::RunLoop::Quit));
......
......@@ -10,66 +10,57 @@
namespace chromeos {
MockAuthStatusConsumer::MockAuthStatusConsumer() = default;
MockAuthStatusConsumer::MockAuthStatusConsumer(base::OnceClosure quit_closure)
: quit_closure_(std::move(quit_closure)) {}
MockAuthStatusConsumer::~MockAuthStatusConsumer() = default;
// static
void MockAuthStatusConsumer::OnRetailModeSuccessQuit(
const UserContext& user_context) {
base::RunLoop::QuitCurrentWhenIdleDeprecated();
std::move(quit_closure_).Run();
}
// static
void MockAuthStatusConsumer::OnRetailModeSuccessQuitAndFail(
const UserContext& user_context) {
ADD_FAILURE() << "Retail mode login should have failed!";
base::RunLoop::QuitCurrentWhenIdleDeprecated();
std::move(quit_closure_).Run();
}
// static
void MockAuthStatusConsumer::OnGuestSuccessQuit() {
base::RunLoop::QuitCurrentWhenIdleDeprecated();
std::move(quit_closure_).Run();
}
// static
void MockAuthStatusConsumer::OnGuestSuccessQuitAndFail() {
ADD_FAILURE() << "Guest login should have failed!";
base::RunLoop::QuitCurrentWhenIdleDeprecated();
std::move(quit_closure_).Run();
}
// static
void MockAuthStatusConsumer::OnSuccessQuit(const UserContext& user_context) {
base::RunLoop::QuitCurrentWhenIdleDeprecated();
std::move(quit_closure_).Run();
}
// static
void MockAuthStatusConsumer::OnSuccessQuitAndFail(
const UserContext& user_context) {
ADD_FAILURE() << "Login should NOT have succeeded!";
base::RunLoop::QuitCurrentWhenIdleDeprecated();
std::move(quit_closure_).Run();
}
// static
void MockAuthStatusConsumer::OnFailQuit(const AuthFailure& error) {
base::RunLoop::QuitCurrentWhenIdleDeprecated();
std::move(quit_closure_).Run();
}
// static
void MockAuthStatusConsumer::OnFailQuitAndFail(const AuthFailure& error) {
ADD_FAILURE() << "Login should not have failed!";
base::RunLoop::QuitCurrentWhenIdleDeprecated();
std::move(quit_closure_).Run();
}
// static
void MockAuthStatusConsumer::OnMigrateQuit() {
base::RunLoop::QuitCurrentWhenIdleDeprecated();
std::move(quit_closure_).Run();
}
// static
void MockAuthStatusConsumer::OnMigrateQuitAndFail() {
ADD_FAILURE() << "Should not have detected a PW change!";
base::RunLoop::QuitCurrentWhenIdleDeprecated();
std::move(quit_closure_).Run();
}
} // namespace chromeos
......@@ -5,6 +5,7 @@
#ifndef CHROMEOS_LOGIN_AUTH_MOCK_AUTH_STATUS_CONSUMER_H_
#define CHROMEOS_LOGIN_AUTH_MOCK_AUTH_STATUS_CONSUMER_H_
#include "base/callback.h"
#include "chromeos/chromeos_export.h"
#include "chromeos/login/auth/auth_status_consumer.h"
#include "chromeos/login/auth/user_context.h"
......@@ -14,7 +15,7 @@ namespace chromeos {
class CHROMEOS_EXPORT MockAuthStatusConsumer : public AuthStatusConsumer {
public:
MockAuthStatusConsumer();
explicit MockAuthStatusConsumer(base::OnceClosure quit_closure);
virtual ~MockAuthStatusConsumer();
MOCK_METHOD1(OnAuthFailure, void(const AuthFailure& error));
......@@ -26,24 +27,27 @@ class CHROMEOS_EXPORT MockAuthStatusConsumer : public AuthStatusConsumer {
// The following functions can be used in gmock Invoke() clauses.
// Compatible with AuthStatusConsumer::OnRetailModeAuthSuccess()
static void OnRetailModeSuccessQuit(const UserContext& user_context);
static void OnRetailModeSuccessQuitAndFail(const UserContext& user_context);
void OnRetailModeSuccessQuit(const UserContext& user_context);
void OnRetailModeSuccessQuitAndFail(const UserContext& user_context);
// Compatible with AuthStatusConsumer::OnOffTheRecordAuthSuccess()
static void OnGuestSuccessQuit();
static void OnGuestSuccessQuitAndFail();
void OnGuestSuccessQuit();
void OnGuestSuccessQuitAndFail();
// Compatible with AuthStatusConsumer::OnAuthSuccess()
static void OnSuccessQuit(const UserContext& user_context);
static void OnSuccessQuitAndFail(const UserContext& user_context);
void OnSuccessQuit(const UserContext& user_context);
void OnSuccessQuitAndFail(const UserContext& user_context);
// Compatible with AuthStatusConsumer::OnAuthFailure()
static void OnFailQuit(const AuthFailure& error);
static void OnFailQuitAndFail(const AuthFailure& error);
void OnFailQuit(const AuthFailure& error);
void OnFailQuitAndFail(const AuthFailure& error);
// Compatible with AuthStatusConsumer::OnPasswordChangeDetected()
static void OnMigrateQuit();
static void OnMigrateQuitAndFail();
void OnMigrateQuit();
void OnMigrateQuitAndFail();
private:
base::OnceClosure quit_closure_;
};
} // namespace chromeos
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment