| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/file_system_provider/operations/close_file.h" | |
| 6 | |
| 7 #include <string> | 5 #include <string> |
| 8 #include <vector> | 6 #include <vector> |
| 9 | 7 |
| 10 #include "base/files/file.h" | 8 #include "base/files/file.h" |
| 11 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 12 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/memory/scoped_vector.h" |
| 12 #include "chrome/browser/chromeos/file_system_provider/operations/copy_entry.h" |
| 13 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h" | 13 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h" |
| 14 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
rface.h" | 14 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
rface.h" |
| 15 #include "chrome/common/extensions/api/file_system_provider.h" | 15 #include "chrome/common/extensions/api/file_system_provider.h" |
| 16 #include "chrome/common/extensions/api/file_system_provider_internal.h" | 16 #include "chrome/common/extensions/api/file_system_provider_internal.h" |
| 17 #include "extensions/browser/event_router.h" | 17 #include "extensions/browser/event_router.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "webkit/browser/fileapi/async_file_util.h" | 19 #include "webkit/browser/fileapi/async_file_util.h" |
| 20 | 20 |
| 21 namespace chromeos { | 21 namespace chromeos { |
| 22 namespace file_system_provider { | 22 namespace file_system_provider { |
| 23 namespace operations { | 23 namespace operations { |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; | 26 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; |
| 27 const char kFileSystemId[] = "testing-file-system"; | 27 const char kFileSystemId[] = "testing-file-system"; |
| 28 const int kRequestId = 2; | 28 const int kRequestId = 2; |
| 29 const int kOpenRequestId = 3; | 29 const base::FilePath::CharType kSourcePath[] = "/bunny/and/bear/happy"; |
| 30 const base::FilePath::CharType kTargetPath[] = "/kitty/and/puppy/happy"; |
| 30 | 31 |
| 31 } // namespace | 32 } // namespace |
| 32 | 33 |
| 33 class FileSystemProviderOperationsCloseFileTest : public testing::Test { | 34 class FileSystemProviderOperationsCopyEntryTest : public testing::Test { |
| 34 protected: | 35 protected: |
| 35 FileSystemProviderOperationsCloseFileTest() {} | 36 FileSystemProviderOperationsCopyEntryTest() {} |
| 36 virtual ~FileSystemProviderOperationsCloseFileTest() {} | 37 virtual ~FileSystemProviderOperationsCopyEntryTest() {} |
| 37 | 38 |
| 38 virtual void SetUp() OVERRIDE { | 39 virtual void SetUp() OVERRIDE { |
| 39 file_system_info_ = | 40 file_system_info_ = |
| 40 ProvidedFileSystemInfo(kExtensionId, | 41 ProvidedFileSystemInfo(kExtensionId, |
| 41 kFileSystemId, | 42 kFileSystemId, |
| 42 "" /* display_name */, | 43 "" /* file_system_name */, |
| 43 base::FilePath() /* mount_path */); | 44 base::FilePath() /* mount_path */); |
| 44 } | 45 } |
| 45 | 46 |
| 46 ProvidedFileSystemInfo file_system_info_; | 47 ProvidedFileSystemInfo file_system_info_; |
| 47 }; | 48 }; |
| 48 | 49 |
| 49 TEST_F(FileSystemProviderOperationsCloseFileTest, Execute) { | 50 TEST_F(FileSystemProviderOperationsCopyEntryTest, Execute) { |
| 50 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 51 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 51 util::StatusCallbackLog callback_log; | 52 util::StatusCallbackLog callback_log; |
| 52 | 53 |
| 53 CloseFile close_file(NULL, | 54 CopyEntry copy_entry(NULL, |
| 54 file_system_info_, | 55 file_system_info_, |
| 55 kOpenRequestId, | 56 base::FilePath::FromUTF8Unsafe(kSourcePath), |
| 57 base::FilePath::FromUTF8Unsafe(kTargetPath), |
| 56 base::Bind(&util::LogStatusCallback, &callback_log)); | 58 base::Bind(&util::LogStatusCallback, &callback_log)); |
| 57 close_file.SetDispatchEventImplForTesting( | 59 copy_entry.SetDispatchEventImplForTesting( |
| 58 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 60 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 59 base::Unretained(&dispatcher))); | 61 base::Unretained(&dispatcher))); |
| 60 | 62 |
| 61 EXPECT_TRUE(close_file.Execute(kRequestId)); | 63 EXPECT_TRUE(copy_entry.Execute(kRequestId)); |
| 62 | 64 |
| 63 ASSERT_EQ(1u, dispatcher.events().size()); | 65 ASSERT_EQ(1u, dispatcher.events().size()); |
| 64 extensions::Event* event = dispatcher.events()[0]; | 66 extensions::Event* event = dispatcher.events()[0]; |
| 65 EXPECT_EQ( | 67 EXPECT_EQ( |
| 66 extensions::api::file_system_provider::OnCloseFileRequested::kEventName, | 68 extensions::api::file_system_provider::OnCopyEntryRequested::kEventName, |
| 67 event->event_name); | 69 event->event_name); |
| 68 base::ListValue* event_args = event->event_args.get(); | 70 base::ListValue* event_args = event->event_args.get(); |
| 69 ASSERT_EQ(1u, event_args->GetSize()); | 71 ASSERT_EQ(1u, event_args->GetSize()); |
| 70 | 72 |
| 71 base::DictionaryValue* options = NULL; | 73 base::DictionaryValue* options = NULL; |
| 72 ASSERT_TRUE(event_args->GetDictionary(0, &options)); | 74 ASSERT_TRUE(event_args->GetDictionary(0, &options)); |
| 73 | 75 |
| 74 std::string event_file_system_id; | 76 std::string event_file_system_id; |
| 75 EXPECT_TRUE(options->GetString("fileSystemId", &event_file_system_id)); | 77 EXPECT_TRUE(options->GetString("fileSystemId", &event_file_system_id)); |
| 76 EXPECT_EQ(kFileSystemId, event_file_system_id); | 78 EXPECT_EQ(kFileSystemId, event_file_system_id); |
| 77 | 79 |
| 78 int event_request_id = -1; | 80 int event_request_id = -1; |
| 79 EXPECT_TRUE(options->GetInteger("requestId", &event_request_id)); | 81 EXPECT_TRUE(options->GetInteger("requestId", &event_request_id)); |
| 80 EXPECT_EQ(kRequestId, event_request_id); | 82 EXPECT_EQ(kRequestId, event_request_id); |
| 81 | 83 |
| 82 int event_open_request_id = -1; | 84 std::string event_source_path; |
| 83 EXPECT_TRUE(options->GetInteger("openRequestId", &event_open_request_id)); | 85 EXPECT_TRUE(options->GetString("sourcePath", &event_source_path)); |
| 84 EXPECT_EQ(kOpenRequestId, event_open_request_id); | 86 EXPECT_EQ(kSourcePath, event_source_path); |
| 87 |
| 88 std::string event_target_path; |
| 89 EXPECT_TRUE(options->GetString("targetPath", &event_target_path)); |
| 90 EXPECT_EQ(kTargetPath, event_target_path); |
| 85 } | 91 } |
| 86 | 92 |
| 87 TEST_F(FileSystemProviderOperationsCloseFileTest, Execute_NoListener) { | 93 TEST_F(FileSystemProviderOperationsCopyEntryTest, Execute_NoListener) { |
| 88 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); | 94 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); |
| 89 util::StatusCallbackLog callback_log; | 95 util::StatusCallbackLog callback_log; |
| 90 | 96 |
| 91 CloseFile close_file(NULL, | 97 CopyEntry copy_entry(NULL, |
| 92 file_system_info_, | 98 file_system_info_, |
| 93 kOpenRequestId, | 99 base::FilePath::FromUTF8Unsafe(kSourcePath), |
| 100 base::FilePath::FromUTF8Unsafe(kTargetPath), |
| 94 base::Bind(&util::LogStatusCallback, &callback_log)); | 101 base::Bind(&util::LogStatusCallback, &callback_log)); |
| 95 close_file.SetDispatchEventImplForTesting( | 102 copy_entry.SetDispatchEventImplForTesting( |
| 96 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 103 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 97 base::Unretained(&dispatcher))); | 104 base::Unretained(&dispatcher))); |
| 98 | 105 |
| 99 EXPECT_FALSE(close_file.Execute(kRequestId)); | 106 EXPECT_FALSE(copy_entry.Execute(kRequestId)); |
| 100 } | 107 } |
| 101 | 108 |
| 102 TEST_F(FileSystemProviderOperationsCloseFileTest, OnSuccess) { | 109 TEST_F(FileSystemProviderOperationsCopyEntryTest, OnSuccess) { |
| 103 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 110 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 104 util::StatusCallbackLog callback_log; | 111 util::StatusCallbackLog callback_log; |
| 105 | 112 |
| 106 CloseFile close_file(NULL, | 113 CopyEntry copy_entry(NULL, |
| 107 file_system_info_, | 114 file_system_info_, |
| 108 kOpenRequestId, | 115 base::FilePath::FromUTF8Unsafe(kSourcePath), |
| 116 base::FilePath::FromUTF8Unsafe(kTargetPath), |
| 109 base::Bind(&util::LogStatusCallback, &callback_log)); | 117 base::Bind(&util::LogStatusCallback, &callback_log)); |
| 110 close_file.SetDispatchEventImplForTesting( | 118 copy_entry.SetDispatchEventImplForTesting( |
| 111 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 119 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 112 base::Unretained(&dispatcher))); | 120 base::Unretained(&dispatcher))); |
| 113 | 121 |
| 114 EXPECT_TRUE(close_file.Execute(kRequestId)); | 122 EXPECT_TRUE(copy_entry.Execute(kRequestId)); |
| 115 | 123 |
| 116 close_file.OnSuccess(kRequestId, | 124 copy_entry.OnSuccess(kRequestId, |
| 117 scoped_ptr<RequestValue>(new RequestValue()), | 125 scoped_ptr<RequestValue>(new RequestValue()), |
| 118 false /* has_more */); | 126 false /* has_more */); |
| 119 ASSERT_EQ(1u, callback_log.size()); | 127 ASSERT_EQ(1u, callback_log.size()); |
| 120 EXPECT_EQ(base::File::FILE_OK, callback_log[0]); | 128 EXPECT_EQ(base::File::FILE_OK, callback_log[0]); |
| 121 } | 129 } |
| 122 | 130 |
| 123 TEST_F(FileSystemProviderOperationsCloseFileTest, OnError) { | 131 TEST_F(FileSystemProviderOperationsCopyEntryTest, OnError) { |
| 124 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 132 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 125 util::StatusCallbackLog callback_log; | 133 util::StatusCallbackLog callback_log; |
| 126 | 134 |
| 127 CloseFile close_file(NULL, | 135 CopyEntry copy_entry(NULL, |
| 128 file_system_info_, | 136 file_system_info_, |
| 129 kOpenRequestId, | 137 base::FilePath::FromUTF8Unsafe(kSourcePath), |
| 138 base::FilePath::FromUTF8Unsafe(kTargetPath), |
| 130 base::Bind(&util::LogStatusCallback, &callback_log)); | 139 base::Bind(&util::LogStatusCallback, &callback_log)); |
| 131 close_file.SetDispatchEventImplForTesting( | 140 copy_entry.SetDispatchEventImplForTesting( |
| 132 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 141 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 133 base::Unretained(&dispatcher))); | 142 base::Unretained(&dispatcher))); |
| 134 | 143 |
| 135 EXPECT_TRUE(close_file.Execute(kRequestId)); | 144 EXPECT_TRUE(copy_entry.Execute(kRequestId)); |
| 136 | 145 |
| 137 close_file.OnError(kRequestId, | 146 copy_entry.OnError(kRequestId, |
| 138 scoped_ptr<RequestValue>(new RequestValue()), | 147 scoped_ptr<RequestValue>(new RequestValue()), |
| 139 base::File::FILE_ERROR_TOO_MANY_OPENED); | 148 base::File::FILE_ERROR_TOO_MANY_OPENED); |
| 140 ASSERT_EQ(1u, callback_log.size()); | 149 ASSERT_EQ(1u, callback_log.size()); |
| 141 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]); | 150 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]); |
| 142 } | 151 } |
| 143 | 152 |
| 144 } // namespace operations | 153 } // namespace operations |
| 145 } // namespace file_system_provider | 154 } // namespace file_system_provider |
| 146 } // namespace chromeos | 155 } // namespace chromeos |
| OLD | NEW |