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..b50906cd28f37a343e41cebb2846854d9e7c128b |
--- /dev/null |
+++ b/components/password_manager/core/browser/export/password_exporter_unittest.cc |
@@ -0,0 +1,94 @@ |
+ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// 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/memory/scoped_vector.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/run_loop.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: |
+ ScopedVector<autofill::PasswordForm> ConstructTestPasswordForms() { |
+ 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"); |
+ |
+ ScopedVector<autofill::PasswordForm> password_forms; |
+ password_forms.push_back(password_form_); |
+ return password_forms.Pass(); |
+ } |
+ |
+ void StartExportAndWaitUntilCompleteThenReadOutput( |
+ const base::FilePath::StringType& extension, |
+ ScopedVector<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(extension); |
+ |
+ PasswordExporter::Export(output_file, passwords.Pass(), |
+ message_loop_.task_runner(), |
+ base::Bind(&PasswordExporterTest::OnExportFinished, |
+ base::Unretained(this))); |
+ |
+ base::RunLoop run_loop; |
+ run_loop.RunUntilIdle(); |
+ |
+ 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) { |
+ const char kExpectedCSVOutput[] = |
+ "name,url,username,password\n" |
+ "accounts.google.com,http://accounts.google.com/a/" |
+ "LoginAuth,test@gmail.com,test1\n"; |
+ |
+ std::string output; |
+ ASSERT_NO_FATAL_FAILURE(StartExportAndWaitUntilCompleteThenReadOutput( |
+ FILE_PATH_LITERAL(".csv"), ConstructTestPasswordForms(), &output)); |
+ EXPECT_EQ(kExpectedCSVOutput, output); |
+} |
+ |
+TEST_F(PasswordExporterTest, JSONExport) { |
+ const char kExpectedJSONOutput[] = |
+ "[{" |
+ "\"password\":\"test1\"," |
+ "\"url\":\"http://accounts.google.com/a/LoginAuth\"," |
+ "\"username\":\"test@gmail.com\"" |
+ "}]"; |
+ |
+ std::string output; |
+ ASSERT_NO_FATAL_FAILURE(StartExportAndWaitUntilCompleteThenReadOutput( |
+ FILE_PATH_LITERAL(".json"), ConstructTestPasswordForms(), &output)); |
+ EXPECT_EQ(kExpectedJSONOutput, output); |
+} |
+ |
+} // namespace password_manager |