| 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/create_directo
     ry.h" | 5 #include "chrome/browser/chromeos/file_system_provider/operations/observe_direct
     ory.h" | 
| 6 | 6 | 
| 7 #include <string> | 7 #include <string> | 
| 8 #include <vector> | 8 #include <vector> | 
| 9 | 9 | 
| 10 #include "base/files/file.h" | 10 #include "base/files/file.h" | 
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" | 
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" | 
| 13 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" | 
| 14 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h" | 14 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h" | 
| 15 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
     rface.h" | 15 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
     rface.h" | 
| 16 #include "chrome/common/extensions/api/file_system_provider.h" | 16 #include "chrome/common/extensions/api/file_system_provider.h" | 
| 17 #include "chrome/common/extensions/api/file_system_provider_internal.h" | 17 #include "chrome/common/extensions/api/file_system_provider_internal.h" | 
| 18 #include "extensions/browser/event_router.h" | 18 #include "extensions/browser/event_router.h" | 
| 19 #include "storage/browser/fileapi/async_file_util.h" | 19 #include "storage/browser/fileapi/async_file_util.h" | 
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" | 
| 21 | 21 | 
| 22 namespace chromeos { | 22 namespace chromeos { | 
| 23 namespace file_system_provider { | 23 namespace file_system_provider { | 
| 24 namespace operations { | 24 namespace operations { | 
| 25 namespace { | 25 namespace { | 
| 26 | 26 | 
| 27 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; | 27 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; | 
| 28 const char kFileSystemId[] = "testing-file-system"; | 28 const char kFileSystemId[] = "testing-file-system"; | 
| 29 const int kRequestId = 2; | 29 const int kRequestId = 2; | 
| 30 const base::FilePath::CharType kDirectoryPath[] = "/kitty/and/puppy/happy"; | 30 const base::FilePath::CharType kDirectoryPath[] = "/kitty/and/puppy/happy"; | 
| 31 | 31 | 
| 32 }  // namespace | 32 }  // namespace | 
| 33 | 33 | 
| 34 class FileSystemProviderOperationsCreateDirectoryTest : public testing::Test { | 34 class FileSystemProviderOperationsObserveDirectoryTest : public testing::Test { | 
| 35  protected: | 35  protected: | 
| 36   FileSystemProviderOperationsCreateDirectoryTest() {} | 36   FileSystemProviderOperationsObserveDirectoryTest() {} | 
| 37   virtual ~FileSystemProviderOperationsCreateDirectoryTest() {} | 37   virtual ~FileSystemProviderOperationsObserveDirectoryTest() {} | 
| 38 | 38 | 
| 39   virtual void SetUp() override { | 39   virtual void SetUp() override { | 
| 40     file_system_info_ = | 40     file_system_info_ = | 
| 41         ProvidedFileSystemInfo(kExtensionId, | 41         ProvidedFileSystemInfo(kExtensionId, | 
| 42                                kFileSystemId, | 42                                kFileSystemId, | 
| 43                                "" /* file_system_name */, | 43                                "" /* file_system_name */, | 
| 44                                true /* writable */, | 44                                false /* writable */, | 
|  | 45                                false /* supports_notify_tag */, | 
| 45                                base::FilePath() /* mount_path */); | 46                                base::FilePath() /* mount_path */); | 
| 46   } | 47   } | 
| 47 | 48 | 
| 48   ProvidedFileSystemInfo file_system_info_; | 49   ProvidedFileSystemInfo file_system_info_; | 
| 49 }; | 50 }; | 
| 50 | 51 | 
| 51 TEST_F(FileSystemProviderOperationsCreateDirectoryTest, Execute) { | 52 TEST_F(FileSystemProviderOperationsObserveDirectoryTest, Execute) { | 
| 52   using extensions::api::file_system_provider::CreateDirectoryRequestedOptions; | 53   using extensions::api::file_system_provider::ObserveDirectoryRequestedOptions; | 
| 53 | 54 | 
| 54   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 55   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 
| 55   util::StatusCallbackLog callback_log; | 56   util::StatusCallbackLog callback_log; | 
| 56 | 57 | 
| 57   CreateDirectory create_directory( | 58   ObserveDirectory observe_directory( | 
| 58       NULL, | 59       NULL, | 
| 59       file_system_info_, | 60       file_system_info_, | 
| 60       base::FilePath::FromUTF8Unsafe(kDirectoryPath), | 61       base::FilePath::FromUTF8Unsafe(kDirectoryPath), | 
| 61       true /* recursive */, | 62       true /* recursive */, | 
| 62       base::Bind(&util::LogStatusCallback, &callback_log)); | 63       base::Bind(&util::LogStatusCallback, &callback_log)); | 
| 63   create_directory.SetDispatchEventImplForTesting( | 64   observe_directory.SetDispatchEventImplForTesting( | 
| 64       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 65       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 
| 65                  base::Unretained(&dispatcher))); | 66                  base::Unretained(&dispatcher))); | 
| 66 | 67 | 
| 67   EXPECT_TRUE(create_directory.Execute(kRequestId)); | 68   EXPECT_TRUE(observe_directory.Execute(kRequestId)); | 
| 68 | 69 | 
| 69   ASSERT_EQ(1u, dispatcher.events().size()); | 70   ASSERT_EQ(1u, dispatcher.events().size()); | 
| 70   extensions::Event* event = dispatcher.events()[0]; | 71   extensions::Event* event = dispatcher.events()[0]; | 
| 71   EXPECT_EQ(extensions::api::file_system_provider::OnCreateDirectoryRequested:: | 72   EXPECT_EQ(extensions::api::file_system_provider::OnObserveDirectoryRequested:: | 
| 72                 kEventName, | 73                 kEventName, | 
| 73             event->event_name); | 74             event->event_name); | 
| 74   base::ListValue* event_args = event->event_args.get(); | 75   base::ListValue* event_args = event->event_args.get(); | 
| 75   ASSERT_EQ(1u, event_args->GetSize()); | 76   ASSERT_EQ(1u, event_args->GetSize()); | 
| 76 | 77 | 
| 77   const base::DictionaryValue* options_as_value = NULL; | 78   const base::DictionaryValue* options_as_value = NULL; | 
| 78   ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value)); | 79   ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value)); | 
| 79 | 80 | 
| 80   CreateDirectoryRequestedOptions options; | 81   ObserveDirectoryRequestedOptions options; | 
| 81   ASSERT_TRUE( | 82   ASSERT_TRUE( | 
| 82       CreateDirectoryRequestedOptions::Populate(*options_as_value, &options)); | 83       ObserveDirectoryRequestedOptions::Populate(*options_as_value, &options)); | 
| 83   EXPECT_EQ(kFileSystemId, options.file_system_id); | 84   EXPECT_EQ(kFileSystemId, options.file_system_id); | 
| 84   EXPECT_EQ(kRequestId, options.request_id); | 85   EXPECT_EQ(kRequestId, options.request_id); | 
| 85   EXPECT_EQ(kDirectoryPath, options.directory_path); | 86   EXPECT_EQ(kDirectoryPath, options.directory_path); | 
| 86   EXPECT_TRUE(options.recursive); | 87   EXPECT_TRUE(options.recursive); | 
| 87 } | 88 } | 
| 88 | 89 | 
| 89 TEST_F(FileSystemProviderOperationsCreateDirectoryTest, Execute_NoListener) { | 90 TEST_F(FileSystemProviderOperationsObserveDirectoryTest, Execute_NoListener) { | 
| 90   util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); | 91   util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); | 
| 91   util::StatusCallbackLog callback_log; | 92   util::StatusCallbackLog callback_log; | 
| 92 | 93 | 
| 93   CreateDirectory create_directory( | 94   ObserveDirectory observe_directory( | 
| 94       NULL, | 95       NULL, | 
| 95       file_system_info_, | 96       file_system_info_, | 
| 96       base::FilePath::FromUTF8Unsafe(kDirectoryPath), | 97       base::FilePath::FromUTF8Unsafe(kDirectoryPath), | 
| 97       true /* recursive */, | 98       true /* recursive */, | 
| 98       base::Bind(&util::LogStatusCallback, &callback_log)); | 99       base::Bind(&util::LogStatusCallback, &callback_log)); | 
| 99   create_directory.SetDispatchEventImplForTesting( | 100   observe_directory.SetDispatchEventImplForTesting( | 
| 100       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 101       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 
| 101                  base::Unretained(&dispatcher))); | 102                  base::Unretained(&dispatcher))); | 
| 102 | 103 | 
| 103   EXPECT_FALSE(create_directory.Execute(kRequestId)); | 104   EXPECT_FALSE(observe_directory.Execute(kRequestId)); | 
| 104 } | 105 } | 
| 105 | 106 | 
| 106 TEST_F(FileSystemProviderOperationsCreateDirectoryTest, Execute_ReadOnly) { | 107 TEST_F(FileSystemProviderOperationsObserveDirectoryTest, OnSuccess) { | 
| 107   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 108   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 
| 108   util::StatusCallbackLog callback_log; | 109   util::StatusCallbackLog callback_log; | 
| 109 | 110 | 
| 110   const ProvidedFileSystemInfo read_only_file_system_info( | 111   ObserveDirectory observe_directory( | 
| 111       kExtensionId, |  | 
| 112       kFileSystemId, |  | 
| 113       "" /* file_system_name */, |  | 
| 114       false /* writable */, |  | 
| 115       base::FilePath() /* mount_path */); |  | 
| 116 |  | 
| 117   CreateDirectory create_directory( |  | 
| 118       NULL, |  | 
| 119       read_only_file_system_info, |  | 
| 120       base::FilePath::FromUTF8Unsafe(kDirectoryPath), |  | 
| 121       true /* recursive */, |  | 
| 122       base::Bind(&util::LogStatusCallback, &callback_log)); |  | 
| 123   create_directory.SetDispatchEventImplForTesting( |  | 
| 124       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |  | 
| 125                  base::Unretained(&dispatcher))); |  | 
| 126 |  | 
| 127   EXPECT_FALSE(create_directory.Execute(kRequestId)); |  | 
| 128 } |  | 
| 129 |  | 
| 130 TEST_F(FileSystemProviderOperationsCreateDirectoryTest, OnSuccess) { |  | 
| 131   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |  | 
| 132   util::StatusCallbackLog callback_log; |  | 
| 133 |  | 
| 134   CreateDirectory create_directory( |  | 
| 135       NULL, | 112       NULL, | 
| 136       file_system_info_, | 113       file_system_info_, | 
| 137       base::FilePath::FromUTF8Unsafe(kDirectoryPath), | 114       base::FilePath::FromUTF8Unsafe(kDirectoryPath), | 
| 138       true /* recursive */, | 115       true /* recursive */, | 
| 139       base::Bind(&util::LogStatusCallback, &callback_log)); | 116       base::Bind(&util::LogStatusCallback, &callback_log)); | 
| 140   create_directory.SetDispatchEventImplForTesting( | 117   observe_directory.SetDispatchEventImplForTesting( | 
| 141       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 118       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 
| 142                  base::Unretained(&dispatcher))); | 119                  base::Unretained(&dispatcher))); | 
| 143 | 120 | 
| 144   EXPECT_TRUE(create_directory.Execute(kRequestId)); | 121   EXPECT_TRUE(observe_directory.Execute(kRequestId)); | 
| 145 | 122 | 
| 146   create_directory.OnSuccess(kRequestId, | 123   observe_directory.OnSuccess(kRequestId, | 
| 147                              scoped_ptr<RequestValue>(new RequestValue()), | 124                               scoped_ptr<RequestValue>(new RequestValue()), | 
| 148                              false /* has_more */); | 125                               false /* has_more */); | 
| 149   ASSERT_EQ(1u, callback_log.size()); | 126   ASSERT_EQ(1u, callback_log.size()); | 
| 150   EXPECT_EQ(base::File::FILE_OK, callback_log[0]); | 127   EXPECT_EQ(base::File::FILE_OK, callback_log[0]); | 
| 151 } | 128 } | 
| 152 | 129 | 
| 153 TEST_F(FileSystemProviderOperationsCreateDirectoryTest, OnError) { | 130 TEST_F(FileSystemProviderOperationsObserveDirectoryTest, OnError) { | 
| 154   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 131   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 
| 155   util::StatusCallbackLog callback_log; | 132   util::StatusCallbackLog callback_log; | 
| 156 | 133 | 
| 157   CreateDirectory create_directory( | 134   ObserveDirectory observe_directory( | 
| 158       NULL, | 135       NULL, | 
| 159       file_system_info_, | 136       file_system_info_, | 
| 160       base::FilePath::FromUTF8Unsafe(kDirectoryPath), | 137       base::FilePath::FromUTF8Unsafe(kDirectoryPath), | 
| 161       true /* recursive */, | 138       true /* recursive */, | 
| 162       base::Bind(&util::LogStatusCallback, &callback_log)); | 139       base::Bind(&util::LogStatusCallback, &callback_log)); | 
| 163   create_directory.SetDispatchEventImplForTesting( | 140   observe_directory.SetDispatchEventImplForTesting( | 
| 164       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 141       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 
| 165                  base::Unretained(&dispatcher))); | 142                  base::Unretained(&dispatcher))); | 
| 166 | 143 | 
| 167   EXPECT_TRUE(create_directory.Execute(kRequestId)); | 144   EXPECT_TRUE(observe_directory.Execute(kRequestId)); | 
| 168 | 145 | 
| 169   create_directory.OnError(kRequestId, | 146   observe_directory.OnError(kRequestId, | 
| 170                            scoped_ptr<RequestValue>(new RequestValue()), | 147                             scoped_ptr<RequestValue>(new RequestValue()), | 
| 171                            base::File::FILE_ERROR_TOO_MANY_OPENED); | 148                             base::File::FILE_ERROR_TOO_MANY_OPENED); | 
| 172   ASSERT_EQ(1u, callback_log.size()); | 149   ASSERT_EQ(1u, callback_log.size()); | 
| 173   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]); | 
| 174 } | 151 } | 
| 175 | 152 | 
| 176 }  // namespace operations | 153 }  // namespace operations | 
| 177 }  // namespace file_system_provider | 154 }  // namespace file_system_provider | 
| 178 }  // namespace chromeos | 155 }  // namespace chromeos | 
| OLD | NEW | 
|---|