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

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

Issue 513683002: [fsp] Add support for providing thumbnails. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed a bug. Created 6 years, 3 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
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/get_metadata.h " 5 #include "chrome/browser/chromeos/file_system_provider/operations/get_metadata.h "
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/files/file.h" 9 #include "base/files/file.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/json/json_reader.h" 11 #include "base/json/json_reader.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 "base/values.h" 14 #include "base/values.h"
15 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h" 15 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.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 char kMimeType[] = "text/plain"; 29 const char kMimeType[] = "text/plain";
30 const char kThumbnail[] = "data:image/png;base64,";
30 const int kRequestId = 2; 31 const int kRequestId = 2;
31 const base::FilePath::CharType kDirectoryPath[] = "/directory"; 32 const base::FilePath::CharType kDirectoryPath[] = "/directory";
32 33
33 // Callback invocation logger. Acts as a fileapi end-point. 34 // Callback invocation logger. Acts as a fileapi end-point.
34 class CallbackLogger { 35 class CallbackLogger {
35 public: 36 public:
36 class Event { 37 class Event {
37 public: 38 public:
38 Event(const EntryMetadata& metadata, base::File::Error result) 39 Event(const EntryMetadata& metadata, base::File::Error result)
39 : metadata_(metadata), result_(result) {} 40 : metadata_(metadata), result_(result) {}
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 base::FilePath() /* mount_path */); 82 base::FilePath() /* mount_path */);
82 } 83 }
83 84
84 ProvidedFileSystemInfo file_system_info_; 85 ProvidedFileSystemInfo file_system_info_;
85 }; 86 };
86 87
87 TEST_F(FileSystemProviderOperationsGetMetadataTest, Execute) { 88 TEST_F(FileSystemProviderOperationsGetMetadataTest, Execute) {
88 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); 89 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
89 CallbackLogger callback_logger; 90 CallbackLogger callback_logger;
90 91
91 GetMetadata get_metadata(NULL, 92 GetMetadata get_metadata(
92 file_system_info_, 93 NULL,
93 base::FilePath::FromUTF8Unsafe(kDirectoryPath), 94 file_system_info_,
94 base::Bind(&CallbackLogger::OnGetMetadata, 95 base::FilePath::FromUTF8Unsafe(kDirectoryPath),
95 base::Unretained(&callback_logger))); 96 ProvidedFileSystemInterface::METADATA_FIELD_THUMBNAIL,
97 base::Bind(&CallbackLogger::OnGetMetadata,
98 base::Unretained(&callback_logger)));
96 get_metadata.SetDispatchEventImplForTesting( 99 get_metadata.SetDispatchEventImplForTesting(
97 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, 100 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
98 base::Unretained(&dispatcher))); 101 base::Unretained(&dispatcher)));
99 102
100 EXPECT_TRUE(get_metadata.Execute(kRequestId)); 103 EXPECT_TRUE(get_metadata.Execute(kRequestId));
101 104
102 ASSERT_EQ(1u, dispatcher.events().size()); 105 ASSERT_EQ(1u, dispatcher.events().size());
103 extensions::Event* event = dispatcher.events()[0]; 106 extensions::Event* event = dispatcher.events()[0];
104 EXPECT_EQ( 107 EXPECT_EQ(
105 extensions::api::file_system_provider::OnGetMetadataRequested::kEventName, 108 extensions::api::file_system_provider::OnGetMetadataRequested::kEventName,
106 event->event_name); 109 event->event_name);
107 base::ListValue* event_args = event->event_args.get(); 110 base::ListValue* event_args = event->event_args.get();
108 ASSERT_EQ(1u, event_args->GetSize()); 111 ASSERT_EQ(1u, event_args->GetSize());
109 112
110 base::DictionaryValue* options = NULL; 113 base::DictionaryValue* options = NULL;
111 ASSERT_TRUE(event_args->GetDictionary(0, &options)); 114 ASSERT_TRUE(event_args->GetDictionary(0, &options));
112 115
113 std::string event_file_system_id; 116 std::string event_file_system_id;
114 EXPECT_TRUE(options->GetString("fileSystemId", &event_file_system_id)); 117 EXPECT_TRUE(options->GetString("fileSystemId", &event_file_system_id));
115 EXPECT_EQ(kFileSystemId, event_file_system_id); 118 EXPECT_EQ(kFileSystemId, event_file_system_id);
116 119
117 int event_request_id = -1; 120 int event_request_id = -1;
118 EXPECT_TRUE(options->GetInteger("requestId", &event_request_id)); 121 EXPECT_TRUE(options->GetInteger("requestId", &event_request_id));
119 EXPECT_EQ(kRequestId, event_request_id); 122 EXPECT_EQ(kRequestId, event_request_id);
120 123
121 std::string event_entry_path; 124 std::string event_entry_path;
122 EXPECT_TRUE(options->GetString("entryPath", &event_entry_path)); 125 EXPECT_TRUE(options->GetString("entryPath", &event_entry_path));
123 EXPECT_EQ(kDirectoryPath, event_entry_path); 126 EXPECT_EQ(kDirectoryPath, event_entry_path);
127
128 bool event_thumbnail;
129 EXPECT_TRUE(options->GetBoolean("thumbnail", &event_thumbnail));
130 EXPECT_TRUE(event_thumbnail);
124 } 131 }
125 132
126 TEST_F(FileSystemProviderOperationsGetMetadataTest, Execute_NoListener) { 133 TEST_F(FileSystemProviderOperationsGetMetadataTest, Execute_NoListener) {
127 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); 134 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
128 CallbackLogger callback_logger; 135 CallbackLogger callback_logger;
129 136
130 GetMetadata get_metadata(NULL, 137 GetMetadata get_metadata(
131 file_system_info_, 138 NULL,
132 base::FilePath::FromUTF8Unsafe(kDirectoryPath), 139 file_system_info_,
133 base::Bind(&CallbackLogger::OnGetMetadata, 140 base::FilePath::FromUTF8Unsafe(kDirectoryPath),
134 base::Unretained(&callback_logger))); 141 ProvidedFileSystemInterface::METADATA_FIELD_THUMBNAIL,
142 base::Bind(&CallbackLogger::OnGetMetadata,
143 base::Unretained(&callback_logger)));
135 get_metadata.SetDispatchEventImplForTesting( 144 get_metadata.SetDispatchEventImplForTesting(
136 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, 145 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
137 base::Unretained(&dispatcher))); 146 base::Unretained(&dispatcher)));
138 147
139 EXPECT_FALSE(get_metadata.Execute(kRequestId)); 148 EXPECT_FALSE(get_metadata.Execute(kRequestId));
140 } 149 }
141 150
142 TEST_F(FileSystemProviderOperationsGetMetadataTest, OnSuccess) { 151 TEST_F(FileSystemProviderOperationsGetMetadataTest, OnSuccess) {
143 using extensions::api::file_system_provider_internal:: 152 using extensions::api::file_system_provider_internal::
144 GetMetadataRequestedSuccess::Params; 153 GetMetadataRequestedSuccess::Params;
145 154
146 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); 155 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
147 CallbackLogger callback_logger; 156 CallbackLogger callback_logger;
148 157
149 GetMetadata get_metadata(NULL, 158 GetMetadata get_metadata(
150 file_system_info_, 159 NULL,
151 base::FilePath::FromUTF8Unsafe(kDirectoryPath), 160 file_system_info_,
152 base::Bind(&CallbackLogger::OnGetMetadata, 161 base::FilePath::FromUTF8Unsafe(kDirectoryPath),
153 base::Unretained(&callback_logger))); 162 ProvidedFileSystemInterface::METADATA_FIELD_THUMBNAIL,
163 base::Bind(&CallbackLogger::OnGetMetadata,
164 base::Unretained(&callback_logger)));
154 get_metadata.SetDispatchEventImplForTesting( 165 get_metadata.SetDispatchEventImplForTesting(
155 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, 166 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
156 base::Unretained(&dispatcher))); 167 base::Unretained(&dispatcher)));
157 168
158 EXPECT_TRUE(get_metadata.Execute(kRequestId)); 169 EXPECT_TRUE(get_metadata.Execute(kRequestId));
159 170
160 // Sample input as JSON. Keep in sync with file_system_provider_api.idl. 171 // Sample input as JSON. Keep in sync with file_system_provider_api.idl.
161 // As for now, it is impossible to create *::Params class directly, not from 172 // As for now, it is impossible to create *::Params class directly, not from
162 // base::Value. 173 // base::Value.
163 const std::string input = 174 const std::string input =
164 "[\n" 175 "[\n"
165 " \"testing-file-system\",\n" // kFileSystemId 176 " \"testing-file-system\",\n" // kFileSystemId
166 " 2,\n" // kRequestId 177 " 2,\n" // kRequestId
167 " {\n" 178 " {\n"
168 " \"isDirectory\": false,\n" 179 " \"isDirectory\": false,\n"
169 " \"name\": \"blueberries.txt\",\n" 180 " \"name\": \"blueberries.txt\",\n"
170 " \"size\": 4096,\n" 181 " \"size\": 4096,\n"
171 " \"modificationTime\": {\n" 182 " \"modificationTime\": {\n"
172 " \"value\": \"Thu Apr 24 00:46:52 UTC 2014\"\n" 183 " \"value\": \"Thu Apr 24 00:46:52 UTC 2014\"\n"
173 " },\n" 184 " },\n"
174 " \"mimeType\": \"text/plain\"\n" // kMimeType 185 " \"mimeType\": \"text/plain\",\n" // kMimeType
186 " \"thumbnail\": \"data:image/png;base64,\"\n" // kThumbnail
175 " },\n" 187 " },\n"
176 " 0\n" // execution_time 188 " 0\n" // execution_time
177 "]\n"; 189 "]\n";
178 190
179 int json_error_code; 191 int json_error_code;
180 std::string json_error_msg; 192 std::string json_error_msg;
181 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( 193 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError(
182 input, base::JSON_PARSE_RFC, &json_error_code, &json_error_msg)); 194 input, base::JSON_PARSE_RFC, &json_error_code, &json_error_msg));
183 ASSERT_TRUE(value.get()) << json_error_msg; 195 ASSERT_TRUE(value.get()) << json_error_msg;
184 196
(...skipping 13 matching lines...) Expand all
198 EXPECT_EQ(base::File::FILE_OK, event->result()); 210 EXPECT_EQ(base::File::FILE_OK, event->result());
199 211
200 const EntryMetadata& metadata = event->metadata(); 212 const EntryMetadata& metadata = event->metadata();
201 EXPECT_FALSE(metadata.is_directory); 213 EXPECT_FALSE(metadata.is_directory);
202 EXPECT_EQ(4096, metadata.size); 214 EXPECT_EQ(4096, metadata.size);
203 base::Time expected_time; 215 base::Time expected_time;
204 EXPECT_TRUE( 216 EXPECT_TRUE(
205 base::Time::FromString("Thu Apr 24 00:46:52 UTC 2014", &expected_time)); 217 base::Time::FromString("Thu Apr 24 00:46:52 UTC 2014", &expected_time));
206 EXPECT_EQ(expected_time, metadata.modification_time); 218 EXPECT_EQ(expected_time, metadata.modification_time);
207 EXPECT_EQ(kMimeType, metadata.mime_type); 219 EXPECT_EQ(kMimeType, metadata.mime_type);
220 EXPECT_EQ(kThumbnail, metadata.thumbnail);
208 } 221 }
209 222
210 TEST_F(FileSystemProviderOperationsGetMetadataTest, OnError) { 223 TEST_F(FileSystemProviderOperationsGetMetadataTest, OnError) {
211 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); 224 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
212 CallbackLogger callback_logger; 225 CallbackLogger callback_logger;
213 226
214 GetMetadata get_metadata(NULL, 227 GetMetadata get_metadata(
215 file_system_info_, 228 NULL,
216 base::FilePath::FromUTF8Unsafe(kDirectoryPath), 229 file_system_info_,
217 base::Bind(&CallbackLogger::OnGetMetadata, 230 base::FilePath::FromUTF8Unsafe(kDirectoryPath),
218 base::Unretained(&callback_logger))); 231 ProvidedFileSystemInterface::METADATA_FIELD_THUMBNAIL,
232 base::Bind(&CallbackLogger::OnGetMetadata,
233 base::Unretained(&callback_logger)));
219 get_metadata.SetDispatchEventImplForTesting( 234 get_metadata.SetDispatchEventImplForTesting(
220 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, 235 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
221 base::Unretained(&dispatcher))); 236 base::Unretained(&dispatcher)));
222 237
223 EXPECT_TRUE(get_metadata.Execute(kRequestId)); 238 EXPECT_TRUE(get_metadata.Execute(kRequestId));
224 239
225 get_metadata.OnError(kRequestId, 240 get_metadata.OnError(kRequestId,
226 scoped_ptr<RequestValue>(new RequestValue()), 241 scoped_ptr<RequestValue>(new RequestValue()),
227 base::File::FILE_ERROR_TOO_MANY_OPENED); 242 base::File::FILE_ERROR_TOO_MANY_OPENED);
228 243
229 ASSERT_EQ(1u, callback_logger.events().size()); 244 ASSERT_EQ(1u, callback_logger.events().size());
230 CallbackLogger::Event* event = callback_logger.events()[0]; 245 CallbackLogger::Event* event = callback_logger.events()[0];
231 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result()); 246 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result());
232 } 247 }
233 248
234 } // namespace operations 249 } // namespace operations
235 } // namespace file_system_provider 250 } // namespace file_system_provider
236 } // namespace chromeos 251 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698