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