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.
5 #include "chrome/browser/chromeos/file_system_provider/operations/truncate.h"
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 "testing/gtest/include/gtest/gtest.h"
20 #include "webkit/browser/fileapi/async_file_util.h"
23 namespace file_system_provider {
24 namespace operations {
27 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
28 const char kFileSystemId[] = "testing-file-system";
29 const int kRequestId = 2;
30 const base::FilePath::CharType kFilePath[] = "/kitty/and/puppy/happy";
31 const int64 kTruncateLength = 64;
35 class FileSystemProviderOperationsTruncateTest : public testing::Test {
37 FileSystemProviderOperationsTruncateTest() {}
38 virtual ~FileSystemProviderOperationsTruncateTest() {}
40 virtual void SetUp() OVERRIDE {
42 ProvidedFileSystemInfo(kExtensionId,
44 "" /* file_system_name */,
46 base::FilePath() /* mount_path */);
49 ProvidedFileSystemInfo file_system_info_;
52 TEST_F(FileSystemProviderOperationsTruncateTest, Execute) {
53 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
54 util::StatusCallbackLog callback_log;
56 Truncate truncate(NULL,
58 base::FilePath::FromUTF8Unsafe(kFilePath),
60 base::Bind(&util::LogStatusCallback, &callback_log));
61 truncate.SetDispatchEventImplForTesting(
62 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
63 base::Unretained(&dispatcher)));
65 EXPECT_TRUE(truncate.Execute(kRequestId));
67 ASSERT_EQ(1u, dispatcher.events().size());
68 extensions::Event* event = dispatcher.events()[0];
70 extensions::api::file_system_provider::OnTruncateRequested::kEventName,
72 base::ListValue* event_args = event->event_args.get();
73 ASSERT_EQ(1u, event_args->GetSize());
75 base::DictionaryValue* options = NULL;
76 ASSERT_TRUE(event_args->GetDictionary(0, &options));
78 std::string event_file_system_id;
79 EXPECT_TRUE(options->GetString("fileSystemId", &event_file_system_id));
80 EXPECT_EQ(kFileSystemId, event_file_system_id);
82 int event_request_id = -1;
83 EXPECT_TRUE(options->GetInteger("requestId", &event_request_id));
84 EXPECT_EQ(kRequestId, event_request_id);
86 std::string event_file_path;
87 EXPECT_TRUE(options->GetString("filePath", &event_file_path));
88 EXPECT_EQ(kFilePath, event_file_path);
90 double event_length = -1;
91 EXPECT_TRUE(options->GetDouble("length", &event_length));
92 EXPECT_EQ(kTruncateLength, static_cast<double>(event_length));
95 TEST_F(FileSystemProviderOperationsTruncateTest, Execute_NoListener) {
96 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
97 util::StatusCallbackLog callback_log;
99 Truncate truncate(NULL,
101 base::FilePath::FromUTF8Unsafe(kFilePath),
103 base::Bind(&util::LogStatusCallback, &callback_log));
104 truncate.SetDispatchEventImplForTesting(
105 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
106 base::Unretained(&dispatcher)));
108 EXPECT_FALSE(truncate.Execute(kRequestId));
111 TEST_F(FileSystemProviderOperationsTruncateTest, Execute_ReadOnly) {
112 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
113 util::StatusCallbackLog callback_log;
115 const ProvidedFileSystemInfo read_only_file_system_info(
118 "" /* file_system_name */,
119 false /* writable */,
120 base::FilePath() /* mount_path */);
122 Truncate truncate(NULL,
124 base::FilePath::FromUTF8Unsafe(kFilePath),
126 base::Bind(&util::LogStatusCallback, &callback_log));
127 truncate.SetDispatchEventImplForTesting(
128 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
129 base::Unretained(&dispatcher)));
131 EXPECT_FALSE(truncate.Execute(kRequestId));
134 TEST_F(FileSystemProviderOperationsTruncateTest, OnSuccess) {
135 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
136 util::StatusCallbackLog callback_log;
138 Truncate truncate(NULL,
140 base::FilePath::FromUTF8Unsafe(kFilePath),
142 base::Bind(&util::LogStatusCallback, &callback_log));
143 truncate.SetDispatchEventImplForTesting(
144 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
145 base::Unretained(&dispatcher)));
147 EXPECT_TRUE(truncate.Execute(kRequestId));
149 truncate.OnSuccess(kRequestId,
150 scoped_ptr<RequestValue>(new RequestValue()),
151 false /* has_more */);
152 ASSERT_EQ(1u, callback_log.size());
153 EXPECT_EQ(base::File::FILE_OK, callback_log[0]);
156 TEST_F(FileSystemProviderOperationsTruncateTest, OnError) {
157 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
158 util::StatusCallbackLog callback_log;
160 Truncate truncate(NULL,
162 base::FilePath::FromUTF8Unsafe(kFilePath),
164 base::Bind(&util::LogStatusCallback, &callback_log));
165 truncate.SetDispatchEventImplForTesting(
166 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
167 base::Unretained(&dispatcher)));
169 EXPECT_TRUE(truncate.Execute(kRequestId));
171 truncate.OnError(kRequestId,
172 scoped_ptr<RequestValue>(new RequestValue()),
173 base::File::FILE_ERROR_TOO_MANY_OPENED);
174 ASSERT_EQ(1u, callback_log.size());
175 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]);
178 } // namespace operations
179 } // namespace file_system_provider
180 } // namespace chromeos