Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / chromeos / file_system_provider / operations / truncate_unittest.cc
blob007ba40cb6df0359056153b29c96a0c6f5c5a013
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 "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_capabilities/file_system_provider_capabilities_handler.h"
18 #include "chrome/common/extensions/api/file_system_provider_internal.h"
19 #include "extensions/browser/event_router.h"
20 #include "storage/browser/fileapi/async_file_util.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 namespace chromeos {
24 namespace file_system_provider {
25 namespace operations {
26 namespace {
28 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
29 const char kFileSystemId[] = "testing-file-system";
30 const int kRequestId = 2;
31 const base::FilePath::CharType kFilePath[] =
32 FILE_PATH_LITERAL("/kitty/and/puppy/happy");
33 const int64 kTruncateLength = 64;
35 } // namespace
37 class FileSystemProviderOperationsTruncateTest : public testing::Test {
38 protected:
39 FileSystemProviderOperationsTruncateTest() {}
40 ~FileSystemProviderOperationsTruncateTest() override {}
42 void SetUp() override {
43 MountOptions mount_options(kFileSystemId, "" /* display_name */);
44 mount_options.writable = true;
45 file_system_info_ = ProvidedFileSystemInfo(
46 kExtensionId, mount_options, base::FilePath(), false /* configurable */,
47 true /* watchable */, extensions::SOURCE_FILE);
50 ProvidedFileSystemInfo file_system_info_;
53 TEST_F(FileSystemProviderOperationsTruncateTest, Execute) {
54 using extensions::api::file_system_provider::TruncateRequestedOptions;
56 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
57 util::StatusCallbackLog callback_log;
59 Truncate truncate(NULL, file_system_info_, base::FilePath(kFilePath),
60 kTruncateLength,
61 base::Bind(&util::LogStatusCallback, &callback_log));
62 truncate.SetDispatchEventImplForTesting(
63 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
64 base::Unretained(&dispatcher)));
66 EXPECT_TRUE(truncate.Execute(kRequestId));
68 ASSERT_EQ(1u, dispatcher.events().size());
69 extensions::Event* event = dispatcher.events()[0];
70 EXPECT_EQ(
71 extensions::api::file_system_provider::OnTruncateRequested::kEventName,
72 event->event_name);
73 base::ListValue* event_args = event->event_args.get();
74 ASSERT_EQ(1u, event_args->GetSize());
76 const base::DictionaryValue* options_as_value = NULL;
77 ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
79 TruncateRequestedOptions options;
80 ASSERT_TRUE(TruncateRequestedOptions::Populate(*options_as_value, &options));
81 EXPECT_EQ(kFileSystemId, options.file_system_id);
82 EXPECT_EQ(kRequestId, options.request_id);
83 EXPECT_EQ(kFilePath, options.file_path);
84 EXPECT_EQ(kTruncateLength, static_cast<double>(options.length));
87 TEST_F(FileSystemProviderOperationsTruncateTest, Execute_NoListener) {
88 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
89 util::StatusCallbackLog callback_log;
91 Truncate truncate(NULL, file_system_info_, base::FilePath(kFilePath),
92 kTruncateLength,
93 base::Bind(&util::LogStatusCallback, &callback_log));
94 truncate.SetDispatchEventImplForTesting(
95 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
96 base::Unretained(&dispatcher)));
98 EXPECT_FALSE(truncate.Execute(kRequestId));
101 TEST_F(FileSystemProviderOperationsTruncateTest, Execute_ReadOnly) {
102 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
103 util::StatusCallbackLog callback_log;
105 const ProvidedFileSystemInfo read_only_file_system_info(
106 kExtensionId, MountOptions(kFileSystemId, "" /* display_name */),
107 base::FilePath() /* mount_path */, false /* configurable */,
108 true /* watchable */, extensions::SOURCE_FILE);
110 Truncate truncate(NULL, file_system_info_, base::FilePath(kFilePath),
111 kTruncateLength,
112 base::Bind(&util::LogStatusCallback, &callback_log));
113 truncate.SetDispatchEventImplForTesting(
114 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
115 base::Unretained(&dispatcher)));
117 EXPECT_FALSE(truncate.Execute(kRequestId));
120 TEST_F(FileSystemProviderOperationsTruncateTest, OnSuccess) {
121 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
122 util::StatusCallbackLog callback_log;
124 Truncate truncate(NULL, file_system_info_, base::FilePath(kFilePath),
125 kTruncateLength,
126 base::Bind(&util::LogStatusCallback, &callback_log));
127 truncate.SetDispatchEventImplForTesting(
128 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
129 base::Unretained(&dispatcher)));
131 EXPECT_TRUE(truncate.Execute(kRequestId));
133 truncate.OnSuccess(kRequestId,
134 scoped_ptr<RequestValue>(new RequestValue()),
135 false /* has_more */);
136 ASSERT_EQ(1u, callback_log.size());
137 EXPECT_EQ(base::File::FILE_OK, callback_log[0]);
140 TEST_F(FileSystemProviderOperationsTruncateTest, OnError) {
141 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
142 util::StatusCallbackLog callback_log;
144 Truncate truncate(NULL, file_system_info_, base::FilePath(kFilePath),
145 kTruncateLength,
146 base::Bind(&util::LogStatusCallback, &callback_log));
147 truncate.SetDispatchEventImplForTesting(
148 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
149 base::Unretained(&dispatcher)));
151 EXPECT_TRUE(truncate.Execute(kRequestId));
153 truncate.OnError(kRequestId,
154 scoped_ptr<RequestValue>(new RequestValue()),
155 base::File::FILE_ERROR_TOO_MANY_OPENED);
156 ASSERT_EQ(1u, callback_log.size());
157 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]);
160 } // namespace operations
161 } // namespace file_system_provider
162 } // namespace chromeos