Commit 705ee654 authored by Christos Froussios's avatar Christos Froussios Committed by Commit Bot

[Password Manager] Simplify export tests

pulling out setting the password list and allowing to use a
base::MockCallback<WriteCallback> in SetWriteForTesting
directly to reduce noise in the PasswordManagerExporter test code.

Bug: 789561
Change-Id: Ib703b8a720e38f75d57519b9f96f926e93e9a561
Reviewed-on: https://chromium-review.googlesource.com/924422
Commit-Queue: Christos Froussios <cfroussios@chromium.org>
Reviewed-by: default avatarVaclav Brozek <vabr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#539044}
parent ecccef6f
...@@ -31,13 +31,12 @@ base::LazySingleThreadTaskRunner g_task_runner = ...@@ -31,13 +31,12 @@ base::LazySingleThreadTaskRunner g_task_runner =
// A wrapper for |write_function|, which can be bound and keep a copy of its // A wrapper for |write_function|, which can be bound and keep a copy of its
// data on the closure. // data on the closure.
bool Write(int (*write_function)(const base::FilePath& filename, bool Write(
const char* data, password_manager::PasswordManagerExporter::WriteCallback write_function,
int size), const base::FilePath& destination,
const base::FilePath& destination, const std::string& serialised) {
const std::string& serialised) {
int written = int written =
write_function(destination, serialised.c_str(), serialised.size()); write_function.Run(destination, serialised.c_str(), serialised.size());
return written == static_cast<int>(serialised.size()); return written == static_cast<int>(serialised.size());
} }
...@@ -54,7 +53,7 @@ PasswordManagerExporter::PasswordManagerExporter( ...@@ -54,7 +53,7 @@ PasswordManagerExporter::PasswordManagerExporter(
: credential_provider_interface_(credential_provider_interface), : credential_provider_interface_(credential_provider_interface),
on_progress_(std::move(on_progress)), on_progress_(std::move(on_progress)),
last_progress_status_(ExportProgressStatus::NOT_STARTED), last_progress_status_(ExportProgressStatus::NOT_STARTED),
write_function_(&base::WriteFile), write_function_(base::BindRepeating(&base::WriteFile)),
delete_function_(base::BindRepeating(&base::DeleteFile)), delete_function_(base::BindRepeating(&base::DeleteFile)),
task_runner_(g_task_runner.Get()), task_runner_(g_task_runner.Get()),
weak_factory_(this) {} weak_factory_(this) {}
...@@ -127,16 +126,13 @@ PasswordManagerExporter::GetProgressStatus() { ...@@ -127,16 +126,13 @@ PasswordManagerExporter::GetProgressStatus() {
return last_progress_status_; return last_progress_status_;
} }
void PasswordManagerExporter::SetWriteForTesting( void PasswordManagerExporter::SetWriteForTesting(WriteCallback write_function) {
int (*write_function)(const base::FilePath& filename, write_function_ = std::move(write_function);
const char* data,
int size)) {
write_function_ = write_function;
} }
void PasswordManagerExporter::SetDeleteForTesting( void PasswordManagerExporter::SetDeleteForTesting(
DeleteCallback delete_callback) { DeleteCallback delete_callback) {
delete_function_ = delete_callback; delete_function_ = std::move(delete_callback);
} }
bool PasswordManagerExporter::IsReadyForExport() { bool PasswordManagerExporter::IsReadyForExport() {
......
...@@ -29,6 +29,8 @@ class PasswordManagerExporter { ...@@ -29,6 +29,8 @@ class PasswordManagerExporter {
using ProgressCallback = using ProgressCallback =
base::RepeatingCallback<void(password_manager::ExportProgressStatus, base::RepeatingCallback<void(password_manager::ExportProgressStatus,
const std::string&)>; const std::string&)>;
using WriteCallback =
base::RepeatingCallback<int(const base::FilePath&, const char*, int)>;
using DeleteCallback = using DeleteCallback =
base::RepeatingCallback<bool(const base::FilePath&, bool)>; base::RepeatingCallback<bool(const base::FilePath&, bool)>;
...@@ -55,9 +57,7 @@ class PasswordManagerExporter { ...@@ -55,9 +57,7 @@ class PasswordManagerExporter {
// Replace the function which writes to the filesystem with a custom action. // Replace the function which writes to the filesystem with a custom action.
// The return value is -1 on error, otherwise the number of bytes written. // The return value is -1 on error, otherwise the number of bytes written.
void SetWriteForTesting(int (*write_function)(const base::FilePath& filename, void SetWriteForTesting(WriteCallback write_callback);
const char* data,
int size));
// Replace the function which writes to the filesystem with a custom action. // Replace the function which writes to the filesystem with a custom action.
// The return value is true when deleting successfully. // The return value is true when deleting successfully.
...@@ -112,11 +112,9 @@ class PasswordManagerExporter { ...@@ -112,11 +112,9 @@ class PasswordManagerExporter {
// list. Useful for metrics. // list. Useful for metrics.
base::Time export_preparation_started_; base::Time export_preparation_started_;
// The function which does the actual writing. It should point to // The function which does the actual writing. It should wrap
// base::WriteFile, unless it's changed for testing purposes. // base::WriteFile, unless it's changed for testing purposes.
int (*write_function_)(const base::FilePath& filename, WriteCallback write_function_;
const char* data,
int size);
// The function which does the actual deleting of a file. It should wrap // The function which does the actual deleting of a file. It should wrap
// base::DeleteFile, unless it's changed for testing purposes. // base::DeleteFile, unless it's changed for testing purposes.
......
...@@ -78,17 +78,6 @@ class FakeCredentialProvider ...@@ -78,17 +78,6 @@ class FakeCredentialProvider
DISALLOW_COPY_AND_ASSIGN(FakeCredentialProvider); DISALLOW_COPY_AND_ASSIGN(FakeCredentialProvider);
}; };
// WriteFunction will delegate to this callback, if set. Use for setting
// expectations for base::WriteFile in PasswordManagerExporter.
base::MockCallback<WriteCallback>* g_write_callback = nullptr;
// Mock for base::WriteFile. Expectations should be set on |g_write_callback|.
int WriteFunction(const base::FilePath& filename, const char* data, int size) {
if (g_write_callback)
return g_write_callback->Get().Run(filename, data, size);
return size;
}
// Creates a hardcoded set of credentials for tests. // Creates a hardcoded set of credentials for tests.
std::vector<std::unique_ptr<autofill::PasswordForm>> CreatePasswordList() { std::vector<std::unique_ptr<autofill::PasswordForm>> CreatePasswordList() {
auto password_form = std::make_unique<autofill::PasswordForm>(); auto password_form = std::make_unique<autofill::PasswordForm>();
...@@ -108,15 +97,17 @@ class PasswordManagerExporterTest : public testing::Test { ...@@ -108,15 +97,17 @@ class PasswordManagerExporterTest : public testing::Test {
base::test::ScopedTaskEnvironment::MainThreadType::UI), base::test::ScopedTaskEnvironment::MainThreadType::UI),
exporter_(&fake_credential_provider_, mock_on_progress_.Get()), exporter_(&fake_credential_provider_, mock_on_progress_.Get()),
destination_path_(kNullFileName) { destination_path_(kNullFileName) {
g_write_callback = &mock_write_file_; exporter_.SetWriteForTesting(mock_write_file_.Get());
exporter_.SetWriteForTesting(&WriteFunction);
exporter_.SetDeleteForTesting(mock_delete_file_.Get()); exporter_.SetDeleteForTesting(mock_delete_file_.Get());
password_list_ = CreatePasswordList();
fake_credential_provider_.SetPasswordList(password_list_);
} }
~PasswordManagerExporterTest() override { g_write_callback = nullptr; } ~PasswordManagerExporterTest() override = default;
protected: protected:
base::test::ScopedTaskEnvironment scoped_task_environment_; base::test::ScopedTaskEnvironment scoped_task_environment_;
std::vector<std::unique_ptr<autofill::PasswordForm>> password_list_;
FakeCredentialProvider fake_credential_provider_; FakeCredentialProvider fake_credential_provider_;
base::MockCallback<base::RepeatingCallback< base::MockCallback<base::RepeatingCallback<
void(password_manager::ExportProgressStatus, const std::string&)>> void(password_manager::ExportProgressStatus, const std::string&)>>
...@@ -132,11 +123,8 @@ class PasswordManagerExporterTest : public testing::Test { ...@@ -132,11 +123,8 @@ class PasswordManagerExporterTest : public testing::Test {
}; };
TEST_F(PasswordManagerExporterTest, PasswordExportSetPasswordListFirst) { TEST_F(PasswordManagerExporterTest, PasswordExportSetPasswordListFirst) {
std::vector<std::unique_ptr<autofill::PasswordForm>> password_list =
CreatePasswordList();
fake_credential_provider_.SetPasswordList(password_list);
const std::string serialised( const std::string serialised(
password_manager::PasswordCSVWriter::SerializePasswords(password_list)); password_manager::PasswordCSVWriter::SerializePasswords(password_list_));
EXPECT_CALL(mock_write_file_, EXPECT_CALL(mock_write_file_,
Run(destination_path_, StrEq(serialised), serialised.size())) Run(destination_path_, StrEq(serialised), serialised.size()))
...@@ -153,7 +141,8 @@ TEST_F(PasswordManagerExporterTest, PasswordExportSetPasswordListFirst) { ...@@ -153,7 +141,8 @@ TEST_F(PasswordManagerExporterTest, PasswordExportSetPasswordListFirst) {
scoped_task_environment_.RunUntilIdle(); scoped_task_environment_.RunUntilIdle();
histogram_tester_.ExpectUniqueSample( histogram_tester_.ExpectUniqueSample(
"PasswordManager.ExportedPasswordsPerUserInCSV", password_list.size(), 1); "PasswordManager.ExportedPasswordsPerUserInCSV", password_list_.size(),
1);
histogram_tester_.ExpectTotalCount( histogram_tester_.ExpectTotalCount(
"PasswordManager.TimeReadingExportedPasswords", 1); "PasswordManager.TimeReadingExportedPasswords", 1);
histogram_tester_.ExpectUniqueSample( histogram_tester_.ExpectUniqueSample(
...@@ -164,9 +153,6 @@ TEST_F(PasswordManagerExporterTest, PasswordExportSetPasswordListFirst) { ...@@ -164,9 +153,6 @@ TEST_F(PasswordManagerExporterTest, PasswordExportSetPasswordListFirst) {
// When writing fails, we should notify the UI of the failure and try to cleanup // When writing fails, we should notify the UI of the failure and try to cleanup
// a possibly partial passwords file. // a possibly partial passwords file.
TEST_F(PasswordManagerExporterTest, WriteFileFailed) { TEST_F(PasswordManagerExporterTest, WriteFileFailed) {
std::vector<std::unique_ptr<autofill::PasswordForm>> password_list =
CreatePasswordList();
fake_credential_provider_.SetPasswordList(password_list);
const std::string destination_folder_name( const std::string destination_folder_name(
destination_path_.DirName().BaseName().AsUTF8Unsafe()); destination_path_.DirName().BaseName().AsUTF8Unsafe());
...@@ -193,11 +179,8 @@ TEST_F(PasswordManagerExporterTest, WriteFileFailed) { ...@@ -193,11 +179,8 @@ TEST_F(PasswordManagerExporterTest, WriteFileFailed) {
// Test that GetProgressStatus() returns the last ExportProgressStatus sent // Test that GetProgressStatus() returns the last ExportProgressStatus sent
// to the callback. // to the callback.
TEST_F(PasswordManagerExporterTest, GetProgressReturnsLastCallbackStatus) { TEST_F(PasswordManagerExporterTest, GetProgressReturnsLastCallbackStatus) {
std::vector<std::unique_ptr<autofill::PasswordForm>> password_list =
CreatePasswordList();
fake_credential_provider_.SetPasswordList(password_list);
const std::string serialised( const std::string serialised(
password_manager::PasswordCSVWriter::SerializePasswords(password_list)); password_manager::PasswordCSVWriter::SerializePasswords(password_list_));
const std::string destination_folder_name( const std::string destination_folder_name(
destination_path_.DirName().BaseName().AsUTF8Unsafe()); destination_path_.DirName().BaseName().AsUTF8Unsafe());
...@@ -218,10 +201,6 @@ TEST_F(PasswordManagerExporterTest, GetProgressReturnsLastCallbackStatus) { ...@@ -218,10 +201,6 @@ TEST_F(PasswordManagerExporterTest, GetProgressReturnsLastCallbackStatus) {
} }
TEST_F(PasswordManagerExporterTest, DontExportWithOnlyDestination) { TEST_F(PasswordManagerExporterTest, DontExportWithOnlyDestination) {
std::vector<std::unique_ptr<autofill::PasswordForm>> password_list =
CreatePasswordList();
fake_credential_provider_.SetPasswordList(password_list);
EXPECT_CALL(mock_write_file_, Run(_, _, _)).Times(0); EXPECT_CALL(mock_write_file_, Run(_, _, _)).Times(0);
EXPECT_CALL( EXPECT_CALL(
mock_on_progress_, mock_on_progress_,
...@@ -239,10 +218,6 @@ TEST_F(PasswordManagerExporterTest, DontExportWithOnlyDestination) { ...@@ -239,10 +218,6 @@ TEST_F(PasswordManagerExporterTest, DontExportWithOnlyDestination) {
} }
TEST_F(PasswordManagerExporterTest, CancelAfterPasswords) { TEST_F(PasswordManagerExporterTest, CancelAfterPasswords) {
std::vector<std::unique_ptr<autofill::PasswordForm>> password_list =
CreatePasswordList();
fake_credential_provider_.SetPasswordList(password_list);
EXPECT_CALL(mock_write_file_, Run(_, _, _)).Times(0); EXPECT_CALL(mock_write_file_, Run(_, _, _)).Times(0);
EXPECT_CALL( EXPECT_CALL(
mock_on_progress_, mock_on_progress_,
...@@ -260,10 +235,6 @@ TEST_F(PasswordManagerExporterTest, CancelAfterPasswords) { ...@@ -260,10 +235,6 @@ TEST_F(PasswordManagerExporterTest, CancelAfterPasswords) {
} }
TEST_F(PasswordManagerExporterTest, CancelWhileExporting) { TEST_F(PasswordManagerExporterTest, CancelWhileExporting) {
std::vector<std::unique_ptr<autofill::PasswordForm>> password_list =
CreatePasswordList();
fake_credential_provider_.SetPasswordList(password_list);
EXPECT_CALL(mock_write_file_, Run(_, _, _)).Times(0); EXPECT_CALL(mock_write_file_, Run(_, _, _)).Times(0);
EXPECT_CALL(mock_delete_file_, Run(destination_path_, false)); EXPECT_CALL(mock_delete_file_, Run(destination_path_, false));
EXPECT_CALL( EXPECT_CALL(
...@@ -288,10 +259,6 @@ TEST_F(PasswordManagerExporterTest, CancelWhileExporting) { ...@@ -288,10 +259,6 @@ TEST_F(PasswordManagerExporterTest, CancelWhileExporting) {
// The "Cancel" button may still be visible on the UI after we've completed // The "Cancel" button may still be visible on the UI after we've completed
// exporting. If they choose to cancel, we should clear the file. // exporting. If they choose to cancel, we should clear the file.
TEST_F(PasswordManagerExporterTest, CancelAfterExporting) { TEST_F(PasswordManagerExporterTest, CancelAfterExporting) {
std::vector<std::unique_ptr<autofill::PasswordForm>> password_list =
CreatePasswordList();
fake_credential_provider_.SetPasswordList(password_list);
EXPECT_CALL(mock_write_file_, Run(_, _, _)).WillOnce(ReturnArg<2>()); EXPECT_CALL(mock_write_file_, Run(_, _, _)).WillOnce(ReturnArg<2>());
EXPECT_CALL(mock_delete_file_, Run(destination_path_, false)); EXPECT_CALL(mock_delete_file_, Run(destination_path_, false));
EXPECT_CALL( EXPECT_CALL(
......
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