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 "storage/browser/fileapi/async_file_util.h"
20 #include "testing/gtest/include/gtest/gtest.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
[] =
31 FILE_PATH_LITERAL("/kitty/and/puppy/happy");
32 const int64 kTruncateLength
= 64;
36 class FileSystemProviderOperationsTruncateTest
: public testing::Test
{
38 FileSystemProviderOperationsTruncateTest() {}
39 ~FileSystemProviderOperationsTruncateTest() override
{}
41 void SetUp() override
{
42 MountOptions
mount_options(kFileSystemId
, "" /* display_name */);
43 mount_options
.writable
= true;
45 ProvidedFileSystemInfo(kExtensionId
, mount_options
, base::FilePath());
48 ProvidedFileSystemInfo file_system_info_
;
51 TEST_F(FileSystemProviderOperationsTruncateTest
, Execute
) {
52 using extensions::api::file_system_provider::TruncateRequestedOptions
;
54 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
55 util::StatusCallbackLog callback_log
;
57 Truncate
truncate(NULL
, file_system_info_
, base::FilePath(kFilePath
),
59 base::Bind(&util::LogStatusCallback
, &callback_log
));
60 truncate
.SetDispatchEventImplForTesting(
61 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
62 base::Unretained(&dispatcher
)));
64 EXPECT_TRUE(truncate
.Execute(kRequestId
));
66 ASSERT_EQ(1u, dispatcher
.events().size());
67 extensions::Event
* event
= dispatcher
.events()[0];
69 extensions::api::file_system_provider::OnTruncateRequested::kEventName
,
71 base::ListValue
* event_args
= event
->event_args
.get();
72 ASSERT_EQ(1u, event_args
->GetSize());
74 const base::DictionaryValue
* options_as_value
= NULL
;
75 ASSERT_TRUE(event_args
->GetDictionary(0, &options_as_value
));
77 TruncateRequestedOptions options
;
78 ASSERT_TRUE(TruncateRequestedOptions::Populate(*options_as_value
, &options
));
79 EXPECT_EQ(kFileSystemId
, options
.file_system_id
);
80 EXPECT_EQ(kRequestId
, options
.request_id
);
81 EXPECT_EQ(kFilePath
, options
.file_path
);
82 EXPECT_EQ(kTruncateLength
, static_cast<double>(options
.length
));
85 TEST_F(FileSystemProviderOperationsTruncateTest
, Execute_NoListener
) {
86 util::LoggingDispatchEventImpl
dispatcher(false /* dispatch_reply */);
87 util::StatusCallbackLog callback_log
;
89 Truncate
truncate(NULL
, file_system_info_
, base::FilePath(kFilePath
),
91 base::Bind(&util::LogStatusCallback
, &callback_log
));
92 truncate
.SetDispatchEventImplForTesting(
93 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
94 base::Unretained(&dispatcher
)));
96 EXPECT_FALSE(truncate
.Execute(kRequestId
));
99 TEST_F(FileSystemProviderOperationsTruncateTest
, Execute_ReadOnly
) {
100 util::LoggingDispatchEventImpl
dispatcher(false /* dispatch_reply */);
101 util::StatusCallbackLog callback_log
;
103 const ProvidedFileSystemInfo
read_only_file_system_info(
105 MountOptions(kFileSystemId
, "" /* display_name */),
106 base::FilePath() /* mount_path */);
108 Truncate
truncate(NULL
, file_system_info_
, base::FilePath(kFilePath
),
110 base::Bind(&util::LogStatusCallback
, &callback_log
));
111 truncate
.SetDispatchEventImplForTesting(
112 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
113 base::Unretained(&dispatcher
)));
115 EXPECT_FALSE(truncate
.Execute(kRequestId
));
118 TEST_F(FileSystemProviderOperationsTruncateTest
, OnSuccess
) {
119 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
120 util::StatusCallbackLog callback_log
;
122 Truncate
truncate(NULL
, file_system_info_
, base::FilePath(kFilePath
),
124 base::Bind(&util::LogStatusCallback
, &callback_log
));
125 truncate
.SetDispatchEventImplForTesting(
126 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
127 base::Unretained(&dispatcher
)));
129 EXPECT_TRUE(truncate
.Execute(kRequestId
));
131 truncate
.OnSuccess(kRequestId
,
132 scoped_ptr
<RequestValue
>(new RequestValue()),
133 false /* has_more */);
134 ASSERT_EQ(1u, callback_log
.size());
135 EXPECT_EQ(base::File::FILE_OK
, callback_log
[0]);
138 TEST_F(FileSystemProviderOperationsTruncateTest
, OnError
) {
139 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
140 util::StatusCallbackLog callback_log
;
142 Truncate
truncate(NULL
, file_system_info_
, base::FilePath(kFilePath
),
144 base::Bind(&util::LogStatusCallback
, &callback_log
));
145 truncate
.SetDispatchEventImplForTesting(
146 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
147 base::Unretained(&dispatcher
)));
149 EXPECT_TRUE(truncate
.Execute(kRequestId
));
151 truncate
.OnError(kRequestId
,
152 scoped_ptr
<RequestValue
>(new RequestValue()),
153 base::File::FILE_ERROR_TOO_MANY_OPENED
);
154 ASSERT_EQ(1u, callback_log
.size());
155 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED
, callback_log
[0]);
158 } // namespace operations
159 } // namespace file_system_provider
160 } // namespace chromeos