Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: chrome/browser/chromeos/file_system_provider/operations/truncate_unittest.cc

Issue 417983002: [fsp] Add support for truncating files. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/file_system_provider/operations/create_file.h" 5 #include "chrome/browser/chromeos/file_system_provider/operations/truncate.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/files/file.h" 10 #include "base/files/file.h"
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h" 13 #include "base/memory/scoped_vector.h"
14 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h" 14 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h"
15 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte rface.h" 15 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte rface.h"
16 #include "chrome/common/extensions/api/file_system_provider.h" 16 #include "chrome/common/extensions/api/file_system_provider.h"
17 #include "chrome/common/extensions/api/file_system_provider_internal.h" 17 #include "chrome/common/extensions/api/file_system_provider_internal.h"
18 #include "extensions/browser/event_router.h" 18 #include "extensions/browser/event_router.h"
19 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "webkit/browser/fileapi/async_file_util.h" 20 #include "webkit/browser/fileapi/async_file_util.h"
21 21
22 namespace chromeos { 22 namespace chromeos {
23 namespace file_system_provider { 23 namespace file_system_provider {
24 namespace operations { 24 namespace operations {
25 namespace { 25 namespace {
26 26
27 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; 27 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
28 const char kFileSystemId[] = "testing-file-system"; 28 const char kFileSystemId[] = "testing-file-system";
29 const int kRequestId = 2; 29 const int kRequestId = 2;
30 const base::FilePath::CharType kFilePath[] = "/kitty/and/puppy/happy"; 30 const base::FilePath::CharType kFilePath[] = "/kitty/and/puppy/happy";
31 const int64 kTruncateLength = 64;
31 32
32 } // namespace 33 } // namespace
33 34
34 class FileSystemProviderOperationsCreateFileTest : public testing::Test { 35 class FileSystemProviderOperationsTruncateTest : public testing::Test {
35 protected: 36 protected:
36 FileSystemProviderOperationsCreateFileTest() {} 37 FileSystemProviderOperationsTruncateTest() {}
37 virtual ~FileSystemProviderOperationsCreateFileTest() {} 38 virtual ~FileSystemProviderOperationsTruncateTest() {}
38 39
39 virtual void SetUp() OVERRIDE { 40 virtual void SetUp() OVERRIDE {
40 file_system_info_ = 41 file_system_info_ =
41 ProvidedFileSystemInfo(kExtensionId, 42 ProvidedFileSystemInfo(kExtensionId,
42 kFileSystemId, 43 kFileSystemId,
43 "" /* file_system_name */, 44 "" /* file_system_name */,
44 true /* writable */, 45 true /* writable */,
45 base::FilePath() /* mount_path */); 46 base::FilePath() /* mount_path */);
46 } 47 }
47 48
48 ProvidedFileSystemInfo file_system_info_; 49 ProvidedFileSystemInfo file_system_info_;
49 }; 50 };
50 51
51 TEST_F(FileSystemProviderOperationsCreateFileTest, Execute) { 52 TEST_F(FileSystemProviderOperationsTruncateTest, Execute) {
52 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); 53 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
53 util::StatusCallbackLog callback_log; 54 util::StatusCallbackLog callback_log;
54 55
55 CreateFile create_file(NULL, 56 Truncate truncate(NULL,
56 file_system_info_, 57 file_system_info_,
57 base::FilePath::FromUTF8Unsafe(kFilePath), 58 base::FilePath::FromUTF8Unsafe(kFilePath),
58 base::Bind(&util::LogStatusCallback, &callback_log)); 59 kTruncateLength,
59 create_file.SetDispatchEventImplForTesting( 60 base::Bind(&util::LogStatusCallback, &callback_log));
61 truncate.SetDispatchEventImplForTesting(
60 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, 62 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
61 base::Unretained(&dispatcher))); 63 base::Unretained(&dispatcher)));
62 64
63 EXPECT_TRUE(create_file.Execute(kRequestId)); 65 EXPECT_TRUE(truncate.Execute(kRequestId));
64 66
65 ASSERT_EQ(1u, dispatcher.events().size()); 67 ASSERT_EQ(1u, dispatcher.events().size());
66 extensions::Event* event = dispatcher.events()[0]; 68 extensions::Event* event = dispatcher.events()[0];
67 EXPECT_EQ( 69 EXPECT_EQ(
68 extensions::api::file_system_provider::OnCreateFileRequested::kEventName, 70 extensions::api::file_system_provider::OnTruncateRequested::kEventName,
69 event->event_name); 71 event->event_name);
70 base::ListValue* event_args = event->event_args.get(); 72 base::ListValue* event_args = event->event_args.get();
71 ASSERT_EQ(1u, event_args->GetSize()); 73 ASSERT_EQ(1u, event_args->GetSize());
72 74
73 base::DictionaryValue* options = NULL; 75 base::DictionaryValue* options = NULL;
74 ASSERT_TRUE(event_args->GetDictionary(0, &options)); 76 ASSERT_TRUE(event_args->GetDictionary(0, &options));
75 77
76 std::string event_file_system_id; 78 std::string event_file_system_id;
77 EXPECT_TRUE(options->GetString("fileSystemId", &event_file_system_id)); 79 EXPECT_TRUE(options->GetString("fileSystemId", &event_file_system_id));
78 EXPECT_EQ(kFileSystemId, event_file_system_id); 80 EXPECT_EQ(kFileSystemId, event_file_system_id);
79 81
80 int event_request_id = -1; 82 int event_request_id = -1;
81 EXPECT_TRUE(options->GetInteger("requestId", &event_request_id)); 83 EXPECT_TRUE(options->GetInteger("requestId", &event_request_id));
82 EXPECT_EQ(kRequestId, event_request_id); 84 EXPECT_EQ(kRequestId, event_request_id);
83 85
84 std::string event_file_path; 86 std::string event_file_path;
85 EXPECT_TRUE(options->GetString("filePath", &event_file_path)); 87 EXPECT_TRUE(options->GetString("filePath", &event_file_path));
86 EXPECT_EQ(kFilePath, event_file_path); 88 EXPECT_EQ(kFilePath, event_file_path);
89
90 double event_length = -1;
91 EXPECT_TRUE(options->GetDouble("length", &event_length));
92 EXPECT_EQ(kTruncateLength, static_cast<double>(event_length));
87 } 93 }
88 94
89 TEST_F(FileSystemProviderOperationsCreateFileTest, Execute_NoListener) { 95 TEST_F(FileSystemProviderOperationsTruncateTest, Execute_NoListener) {
90 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); 96 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
91 util::StatusCallbackLog callback_log; 97 util::StatusCallbackLog callback_log;
92 98
93 CreateFile create_file(NULL, 99 Truncate truncate(NULL,
94 file_system_info_, 100 file_system_info_,
95 base::FilePath::FromUTF8Unsafe(kFilePath), 101 base::FilePath::FromUTF8Unsafe(kFilePath),
96 base::Bind(&util::LogStatusCallback, &callback_log)); 102 kTruncateLength,
97 create_file.SetDispatchEventImplForTesting( 103 base::Bind(&util::LogStatusCallback, &callback_log));
104 truncate.SetDispatchEventImplForTesting(
98 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, 105 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
99 base::Unretained(&dispatcher))); 106 base::Unretained(&dispatcher)));
100 107
101 EXPECT_FALSE(create_file.Execute(kRequestId)); 108 EXPECT_FALSE(truncate.Execute(kRequestId));
102 } 109 }
103 110
104 TEST_F(FileSystemProviderOperationsCreateFileTest, OnSuccess) { 111 TEST_F(FileSystemProviderOperationsTruncateTest, OnSuccess) {
105 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); 112 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
106 util::StatusCallbackLog callback_log; 113 util::StatusCallbackLog callback_log;
107 114
108 CreateFile create_file(NULL, 115 Truncate truncate(NULL,
109 file_system_info_, 116 file_system_info_,
110 base::FilePath::FromUTF8Unsafe(kFilePath), 117 base::FilePath::FromUTF8Unsafe(kFilePath),
111 base::Bind(&util::LogStatusCallback, &callback_log)); 118 kTruncateLength,
112 create_file.SetDispatchEventImplForTesting( 119 base::Bind(&util::LogStatusCallback, &callback_log));
120 truncate.SetDispatchEventImplForTesting(
113 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, 121 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
114 base::Unretained(&dispatcher))); 122 base::Unretained(&dispatcher)));
115 123
116 EXPECT_TRUE(create_file.Execute(kRequestId)); 124 EXPECT_TRUE(truncate.Execute(kRequestId));
117 125
118 create_file.OnSuccess(kRequestId, 126 truncate.OnSuccess(kRequestId,
119 scoped_ptr<RequestValue>(new RequestValue()), 127 scoped_ptr<RequestValue>(new RequestValue()),
120 false /* has_more */); 128 false /* has_more */);
121 ASSERT_EQ(1u, callback_log.size()); 129 ASSERT_EQ(1u, callback_log.size());
122 EXPECT_EQ(base::File::FILE_OK, callback_log[0]); 130 EXPECT_EQ(base::File::FILE_OK, callback_log[0]);
123 } 131 }
124 132
125 TEST_F(FileSystemProviderOperationsCreateFileTest, OnError) { 133 TEST_F(FileSystemProviderOperationsTruncateTest, OnError) {
126 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); 134 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
127 util::StatusCallbackLog callback_log; 135 util::StatusCallbackLog callback_log;
128 136
129 CreateFile create_file(NULL, 137 Truncate truncate(NULL,
130 file_system_info_, 138 file_system_info_,
131 base::FilePath::FromUTF8Unsafe(kFilePath), 139 base::FilePath::FromUTF8Unsafe(kFilePath),
132 base::Bind(&util::LogStatusCallback, &callback_log)); 140 kTruncateLength,
133 create_file.SetDispatchEventImplForTesting( 141 base::Bind(&util::LogStatusCallback, &callback_log));
142 truncate.SetDispatchEventImplForTesting(
134 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, 143 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
135 base::Unretained(&dispatcher))); 144 base::Unretained(&dispatcher)));
136 145
137 EXPECT_TRUE(create_file.Execute(kRequestId)); 146 EXPECT_TRUE(truncate.Execute(kRequestId));
138 147
139 create_file.OnError(kRequestId, 148 truncate.OnError(kRequestId,
140 scoped_ptr<RequestValue>(new RequestValue()), 149 scoped_ptr<RequestValue>(new RequestValue()),
141 base::File::FILE_ERROR_TOO_MANY_OPENED); 150 base::File::FILE_ERROR_TOO_MANY_OPENED);
142 ASSERT_EQ(1u, callback_log.size()); 151 ASSERT_EQ(1u, callback_log.size());
143 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]); 152 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]);
144 } 153 }
145 154
146 } // namespace operations 155 } // namespace operations
147 } // namespace file_system_provider 156 } // namespace file_system_provider
148 } // namespace chromeos 157 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698