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