Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / chrome / browser / chromeos / file_system_provider / operations / truncate_unittest.cc
blob23fa0f5e2e0ec9b927743524c48e117b0693af6a
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"
7 #include <string>
8 #include <vector>
10 #include "base/files/file.h"
11 #include "base/files/file_path.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h"
14 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
15 #include "chrome/common/extensions/api/file_system_provider.h"
16 #include "chrome/common/extensions/api/file_system_provider_capabilities/file_system_provider_capabilities_handler.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"
22 namespace chromeos {
23 namespace file_system_provider {
24 namespace operations {
25 namespace {
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;
34 } // namespace
36 class FileSystemProviderOperationsTruncateTest : public testing::Test {
37 protected:
38 FileSystemProviderOperationsTruncateTest() {}
39 ~FileSystemProviderOperationsTruncateTest() override {}
41 void SetUp() override {
42 MountOptions mount_options(kFileSystemId, "" /* display_name */);
43 mount_options.writable = true;
44 file_system_info_ = ProvidedFileSystemInfo(
45 kExtensionId, mount_options, base::FilePath(), false /* configurable */,
46 true /* watchable */, extensions::SOURCE_FILE);
49 ProvidedFileSystemInfo file_system_info_;
52 TEST_F(FileSystemProviderOperationsTruncateTest, Execute) {
53 using extensions::api::file_system_provider::TruncateRequestedOptions;
55 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
56 util::StatusCallbackLog callback_log;
58 Truncate truncate(NULL, file_system_info_, base::FilePath(kFilePath),
59 kTruncateLength,
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];
69 EXPECT_EQ(
70 extensions::api::file_system_provider::OnTruncateRequested::kEventName,
71 event->event_name);
72 base::ListValue* event_args = event->event_args.get();
73 ASSERT_EQ(1u, event_args->GetSize());
75 const base::DictionaryValue* options_as_value = NULL;
76 ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
78 TruncateRequestedOptions options;
79 ASSERT_TRUE(TruncateRequestedOptions::Populate(*options_as_value, &options));
80 EXPECT_EQ(kFileSystemId, options.file_system_id);
81 EXPECT_EQ(kRequestId, options.request_id);
82 EXPECT_EQ(kFilePath, options.file_path);
83 EXPECT_EQ(kTruncateLength, static_cast<double>(options.length));
86 TEST_F(FileSystemProviderOperationsTruncateTest, Execute_NoListener) {
87 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
88 util::StatusCallbackLog callback_log;
90 Truncate truncate(NULL, file_system_info_, base::FilePath(kFilePath),
91 kTruncateLength,
92 base::Bind(&util::LogStatusCallback, &callback_log));
93 truncate.SetDispatchEventImplForTesting(
94 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
95 base::Unretained(&dispatcher)));
97 EXPECT_FALSE(truncate.Execute(kRequestId));
100 TEST_F(FileSystemProviderOperationsTruncateTest, Execute_ReadOnly) {
101 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
102 util::StatusCallbackLog callback_log;
104 const ProvidedFileSystemInfo read_only_file_system_info(
105 kExtensionId, MountOptions(kFileSystemId, "" /* display_name */),
106 base::FilePath() /* mount_path */, false /* configurable */,
107 true /* watchable */, extensions::SOURCE_FILE);
109 Truncate truncate(NULL, file_system_info_, base::FilePath(kFilePath),
110 kTruncateLength,
111 base::Bind(&util::LogStatusCallback, &callback_log));
112 truncate.SetDispatchEventImplForTesting(
113 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
114 base::Unretained(&dispatcher)));
116 EXPECT_FALSE(truncate.Execute(kRequestId));
119 TEST_F(FileSystemProviderOperationsTruncateTest, OnSuccess) {
120 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
121 util::StatusCallbackLog callback_log;
123 Truncate truncate(NULL, file_system_info_, base::FilePath(kFilePath),
124 kTruncateLength,
125 base::Bind(&util::LogStatusCallback, &callback_log));
126 truncate.SetDispatchEventImplForTesting(
127 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
128 base::Unretained(&dispatcher)));
130 EXPECT_TRUE(truncate.Execute(kRequestId));
132 truncate.OnSuccess(kRequestId,
133 scoped_ptr<RequestValue>(new RequestValue()),
134 false /* has_more */);
135 ASSERT_EQ(1u, callback_log.size());
136 EXPECT_EQ(base::File::FILE_OK, callback_log[0]);
139 TEST_F(FileSystemProviderOperationsTruncateTest, OnError) {
140 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
141 util::StatusCallbackLog callback_log;
143 Truncate truncate(NULL, file_system_info_, base::FilePath(kFilePath),
144 kTruncateLength,
145 base::Bind(&util::LogStatusCallback, &callback_log));
146 truncate.SetDispatchEventImplForTesting(
147 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
148 base::Unretained(&dispatcher)));
150 EXPECT_TRUE(truncate.Execute(kRequestId));
152 truncate.OnError(kRequestId,
153 scoped_ptr<RequestValue>(new RequestValue()),
154 base::File::FILE_ERROR_TOO_MANY_OPENED);
155 ASSERT_EQ(1u, callback_log.size());
156 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]);
159 } // namespace operations
160 } // namespace file_system_provider
161 } // namespace chromeos