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/truncate.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_interface.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 22namespace chromeos { 23namespace file_system_provider { 24namespace operations { 25namespace { 26 27const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; 28const char kFileSystemId[] = "testing-file-system"; 29const int kRequestId = 2; 30const base::FilePath::CharType kFilePath[] = "/kitty/and/puppy/happy"; 31const int64 kTruncateLength = 64; 32 33} // namespace 34 35class FileSystemProviderOperationsTruncateTest : public testing::Test { 36 protected: 37 FileSystemProviderOperationsTruncateTest() {} 38 virtual ~FileSystemProviderOperationsTruncateTest() {} 39 40 virtual void SetUp() OVERRIDE { 41 file_system_info_ = 42 ProvidedFileSystemInfo(kExtensionId, 43 kFileSystemId, 44 "" /* file_system_name */, 45 true /* writable */, 46 base::FilePath() /* mount_path */); 47 } 48 49 ProvidedFileSystemInfo file_system_info_; 50}; 51 52TEST_F(FileSystemProviderOperationsTruncateTest, Execute) { 53 using extensions::api::file_system_provider::TruncateRequestedOptions; 54 55 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); 56 util::StatusCallbackLog callback_log; 57 58 Truncate truncate(NULL, 59 file_system_info_, 60 base::FilePath::FromUTF8Unsafe(kFilePath), 61 kTruncateLength, 62 base::Bind(&util::LogStatusCallback, &callback_log)); 63 truncate.SetDispatchEventImplForTesting( 64 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, 65 base::Unretained(&dispatcher))); 66 67 EXPECT_TRUE(truncate.Execute(kRequestId)); 68 69 ASSERT_EQ(1u, dispatcher.events().size()); 70 extensions::Event* event = dispatcher.events()[0]; 71 EXPECT_EQ( 72 extensions::api::file_system_provider::OnTruncateRequested::kEventName, 73 event->event_name); 74 base::ListValue* event_args = event->event_args.get(); 75 ASSERT_EQ(1u, event_args->GetSize()); 76 77 const base::DictionaryValue* options_as_value = NULL; 78 ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value)); 79 80 TruncateRequestedOptions options; 81 ASSERT_TRUE(TruncateRequestedOptions::Populate(*options_as_value, &options)); 82 EXPECT_EQ(kFileSystemId, options.file_system_id); 83 EXPECT_EQ(kRequestId, options.request_id); 84 EXPECT_EQ(kFilePath, options.file_path); 85 EXPECT_EQ(kTruncateLength, static_cast<double>(options.length)); 86} 87 88TEST_F(FileSystemProviderOperationsTruncateTest, Execute_NoListener) { 89 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); 90 util::StatusCallbackLog callback_log; 91 92 Truncate truncate(NULL, 93 file_system_info_, 94 base::FilePath::FromUTF8Unsafe(kFilePath), 95 kTruncateLength, 96 base::Bind(&util::LogStatusCallback, &callback_log)); 97 truncate.SetDispatchEventImplForTesting( 98 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, 99 base::Unretained(&dispatcher))); 100 101 EXPECT_FALSE(truncate.Execute(kRequestId)); 102} 103 104TEST_F(FileSystemProviderOperationsTruncateTest, Execute_ReadOnly) { 105 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); 106 util::StatusCallbackLog callback_log; 107 108 const ProvidedFileSystemInfo read_only_file_system_info( 109 kExtensionId, 110 kFileSystemId, 111 "" /* file_system_name */, 112 false /* writable */, 113 base::FilePath() /* mount_path */); 114 115 Truncate truncate(NULL, 116 file_system_info_, 117 base::FilePath::FromUTF8Unsafe(kFilePath), 118 kTruncateLength, 119 base::Bind(&util::LogStatusCallback, &callback_log)); 120 truncate.SetDispatchEventImplForTesting( 121 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, 122 base::Unretained(&dispatcher))); 123 124 EXPECT_FALSE(truncate.Execute(kRequestId)); 125} 126 127TEST_F(FileSystemProviderOperationsTruncateTest, OnSuccess) { 128 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); 129 util::StatusCallbackLog callback_log; 130 131 Truncate truncate(NULL, 132 file_system_info_, 133 base::FilePath::FromUTF8Unsafe(kFilePath), 134 kTruncateLength, 135 base::Bind(&util::LogStatusCallback, &callback_log)); 136 truncate.SetDispatchEventImplForTesting( 137 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, 138 base::Unretained(&dispatcher))); 139 140 EXPECT_TRUE(truncate.Execute(kRequestId)); 141 142 truncate.OnSuccess(kRequestId, 143 scoped_ptr<RequestValue>(new RequestValue()), 144 false /* has_more */); 145 ASSERT_EQ(1u, callback_log.size()); 146 EXPECT_EQ(base::File::FILE_OK, callback_log[0]); 147} 148 149TEST_F(FileSystemProviderOperationsTruncateTest, OnError) { 150 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); 151 util::StatusCallbackLog callback_log; 152 153 Truncate truncate(NULL, 154 file_system_info_, 155 base::FilePath::FromUTF8Unsafe(kFilePath), 156 kTruncateLength, 157 base::Bind(&util::LogStatusCallback, &callback_log)); 158 truncate.SetDispatchEventImplForTesting( 159 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, 160 base::Unretained(&dispatcher))); 161 162 EXPECT_TRUE(truncate.Execute(kRequestId)); 163 164 truncate.OnError(kRequestId, 165 scoped_ptr<RequestValue>(new RequestValue()), 166 base::File::FILE_ERROR_TOO_MANY_OPENED); 167 ASSERT_EQ(1u, callback_log.size()); 168 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]); 169} 170 171} // namespace operations 172} // namespace file_system_provider 173} // namespace chromeos 174