Chromium Code Reviews| Index: components/password_manager/core/browser/export/password_exporter_unittest.cc |
| diff --git a/components/password_manager/core/browser/export/password_exporter_unittest.cc b/components/password_manager/core/browser/export/password_exporter_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9c028edede4ad10c85091efda5593d61d21530a5 |
| --- /dev/null |
| +++ b/components/password_manager/core/browser/export/password_exporter_unittest.cc |
| @@ -0,0 +1,106 @@ |
| + |
|
vabr (Chromium)
2016/03/10 10:53:32
nit: remove the blank line
xunlu
2016/03/16 07:23:59
Done.
|
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
|
vabr (Chromium)
2016/03/10 10:53:32
2016
xunlu
2016/03/16 07:23:59
Done.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/password_manager/core/browser/export/password_exporter.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/files/file_util.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/run_loop.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "components/autofill/core/common/password_form.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace password_manager { |
| + |
| +class PasswordExporterTest : public testing::Test { |
| + public: |
| + PasswordExporterTest() : callback_called_(false) {} |
| + |
| + protected: |
| + std::vector<scoped_ptr<autofill::PasswordForm>> ConstructTestPasswordForms() { |
| + scoped_ptr<autofill::PasswordForm> password_form_( |
| + new autofill::PasswordForm()); |
| + password_form_->origin = GURL("http://accounts.google.com/a/LoginAuth"); |
| + password_form_->username_value = base::ASCIIToUTF16("test@gmail.com"); |
| + password_form_->password_value = base::ASCIIToUTF16("test1"); |
| + |
| + std::vector<scoped_ptr<autofill::PasswordForm>> password_forms; |
| + password_forms.push_back(std::move(password_form_)); |
| + return password_forms; |
| + } |
| + |
| + void StartExportAndWaitUntilCompleteThenReadOutput( |
| + const base::FilePath::StringType& provided_extension, |
| + const base::FilePath::StringType& expected_extension, |
| + std::vector<scoped_ptr<autofill::PasswordForm>> passwords, |
| + std::string* output) { |
| + base::FilePath temporary_dir; |
| + ASSERT_TRUE(base::CreateNewTempDirectory(base::FilePath::StringType(), |
| + &temporary_dir)); |
| + base::FilePath output_file = |
| + temporary_dir.AppendASCII("passwords").AddExtension(provided_extension); |
| + |
| + PasswordExporter::Export(output_file, std::move(passwords), |
| + message_loop_.task_runner(), |
| + base::Bind(&PasswordExporterTest::OnExportFinished, |
| + base::Unretained(this))); |
| + |
| + base::RunLoop run_loop; |
| + run_loop.RunUntilIdle(); |
| + |
| + if (provided_extension != expected_extension) { |
| + output_file = output_file.ReplaceExtension(expected_extension); |
| + } |
| + |
| + EXPECT_TRUE(callback_called_); |
| + EXPECT_TRUE(base::ReadFileToString(output_file, output)); |
| + base::DeleteFile(temporary_dir, true); |
| + } |
| + |
| + void OnExportFinished() { callback_called_ = true; } |
| + |
| + private: |
| + base::MessageLoop message_loop_; |
| + bool callback_called_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(PasswordExporterTest); |
| +}; |
| + |
| +TEST_F(PasswordExporterTest, CSVExport) { |
| +#if defined(OS_WIN) |
| + const char kLineEnding[] = "\r\n"; |
| +#else |
| + const char kLineEnding[] = "\n"; |
| +#endif |
| + std::string kExpectedCSVOutput = base::StringPrintf( |
| + "name,url,username,password%s" |
| + "accounts.google.com,http://accounts.google.com/a/" |
| + "LoginAuth,test@gmail.com,test1%s", |
| + kLineEnding, kLineEnding); |
| + |
| + std::string output; |
| + ASSERT_NO_FATAL_FAILURE(StartExportAndWaitUntilCompleteThenReadOutput( |
| + FILE_PATH_LITERAL(".csv"), FILE_PATH_LITERAL(".csv"), |
| + ConstructTestPasswordForms(), &output)); |
| + EXPECT_EQ(kExpectedCSVOutput, output); |
| + |
| + // Verify that exporter use ".csv" extension when no extension is provided |
|
vabr (Chromium)
2016/03/10 10:53:32
nit: Add a full-stop (.) at the end of the sentenc
xunlu
2016/03/16 07:23:59
Done.
|
| + ASSERT_NO_FATAL_FAILURE(StartExportAndWaitUntilCompleteThenReadOutput( |
| + FILE_PATH_LITERAL(""), FILE_PATH_LITERAL(".csv"), |
| + ConstructTestPasswordForms(), &output)); |
| + EXPECT_EQ(kExpectedCSVOutput, output); |
| + |
| + // Verify that exporter use ".csv" extension when unknown extension is |
| + // provided |
| + ASSERT_NO_FATAL_FAILURE(StartExportAndWaitUntilCompleteThenReadOutput( |
|
vabr (Chromium)
2016/03/10 10:53:32
(As noted above, this behaviour seems rather risky
xunlu
2016/03/16 07:23:59
See my comments above.
|
| + FILE_PATH_LITERAL(".vsc"), FILE_PATH_LITERAL(".csv"), |
| + ConstructTestPasswordForms(), &output)); |
| + EXPECT_EQ(kExpectedCSVOutput, output); |
| +} |
| + |
| +} // namespace password_manager |