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

Side by Side Diff: ppapi/tests/test_flash_file.cc

Issue 11359097: Refactored the PPB_Flash_File_ModuleLocal/FileRef to the new ppapi resource model (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years 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
« no previous file with comments | « ppapi/tests/test_flash_file.h ('k') | ppapi/thunk/ppb_file_ref_api.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "ppapi/tests/test_flash_file.h" 5 #include "ppapi/tests/test_flash_file.h"
6 6
7 #include <algorithm>
8 #include <vector>
9
10 #include "ppapi/c/pp_file_info.h"
11 #include "ppapi/c/ppb_file_io.h"
7 #include "ppapi/cpp/module.h" 12 #include "ppapi/cpp/module.h"
8 #include "ppapi/cpp/private/flash_file.h" 13 #include "ppapi/cpp/private/flash_file.h"
9 #include "ppapi/tests/testing_instance.h" 14 #include "ppapi/tests/testing_instance.h"
10 #include "ppapi/tests/test_utils.h" 15 #include "ppapi/tests/test_utils.h"
11 16
12 #if defined(PPAPI_OS_WIN) 17 #if defined(PPAPI_OS_WIN)
13 #include <windows.h> 18 #include <windows.h>
14 #else 19 #else
15 #include <errno.h> 20 #include <errno.h>
16 #include <unistd.h> 21 #include <unistd.h>
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 result = bytes_read != -1; 72 result = bytes_read != -1;
68 if (bytes_read > 0) 73 if (bytes_read > 0)
69 contents->append(buffer, bytes_read); 74 contents->append(buffer, bytes_read);
70 } while (bytes_read > 0); 75 } while (bytes_read > 0);
71 #endif 76 #endif
72 77
73 delete[] buffer; 78 delete[] buffer;
74 return result; 79 return result;
75 } 80 }
76 81
82 bool DirEntryEqual(FileModuleLocal::DirEntry i,
83 FileModuleLocal::DirEntry j) {
84 return i.name == j.name && i.is_dir == j.is_dir;
85 }
86
87 bool DirEntryLessThan(FileModuleLocal::DirEntry i,
88 FileModuleLocal::DirEntry j) {
89 if (i.name == j.name)
90 return i.is_dir < j.is_dir;
91 return i.name < j.name;
92 }
93
77 } // namespace 94 } // namespace
78 95
79 REGISTER_TEST_CASE(FlashFile); 96 REGISTER_TEST_CASE(FlashFile);
80 97
81 TestFlashFile::TestFlashFile(TestingInstance* instance) 98 TestFlashFile::TestFlashFile(TestingInstance* instance)
82 : TestCase(instance) { 99 : TestCase(instance) {
83 } 100 }
84 101
85 TestFlashFile::~TestFlashFile() { 102 TestFlashFile::~TestFlashFile() {
86 } 103 }
87 104
88 bool TestFlashFile::Init() { 105 bool TestFlashFile::Init() {
89 return FileModuleLocal::IsAvailable(); 106 return FileModuleLocal::IsAvailable();
90 } 107 }
91 108
92 void TestFlashFile::RunTests(const std::string& filter) { 109 void TestFlashFile::RunTests(const std::string& filter) {
110 RUN_TEST(OpenFile, filter);
111 RUN_TEST(RenameFile, filter);
112 RUN_TEST(DeleteFileOrDir, filter);
113 RUN_TEST(CreateDir, filter);
114 RUN_TEST(QueryFile, filter);
115 RUN_TEST(GetDirContents, filter);
93 RUN_TEST(CreateTemporaryFile, filter); 116 RUN_TEST(CreateTemporaryFile, filter);
94 } 117 }
95 118
96 std::string TestFlashFile::TestCreateTemporaryFile() { 119 void TestFlashFile::SetUp() {
120 // Clear out existing test data.
121 FileModuleLocal::DeleteFileOrDir(instance_, std::string(), true);
97 // Make sure that the root directory exists. 122 // Make sure that the root directory exists.
98 FileModuleLocal::CreateDir(instance_, std::string()); 123 FileModuleLocal::CreateDir(instance_, std::string());
99 124 }
125
126 std::string TestFlashFile::TestOpenFile() {
127 SetUp();
128 std::string filename = "abc.txt";
129 PP_FileHandle file_handle = FileModuleLocal::OpenFile(instance_,
130 filename,
131 PP_FILEOPENFLAG_WRITE |
132 PP_FILEOPENFLAG_CREATE);
133 ASSERT_NE(PP_kInvalidFileHandle, file_handle);
134
135 std::string contents = "This is file.";
136 std::string read_contents;
137 ASSERT_TRUE(WriteFile(file_handle, contents));
138 ASSERT_FALSE(ReadFile(file_handle, &read_contents));
139 CloseFileHandle(file_handle);
140
141 file_handle = FileModuleLocal::OpenFile(instance_,
142 filename,
143 PP_FILEOPENFLAG_READ);
144 ASSERT_NE(PP_kInvalidFileHandle, file_handle);
145
146 ASSERT_FALSE(WriteFile(file_handle, contents));
147 ASSERT_TRUE(ReadFile(file_handle, &read_contents));
148 ASSERT_EQ(contents, read_contents);
149 CloseFileHandle(file_handle);
150
151 PASS();
152 }
153
154 std::string TestFlashFile::TestRenameFile() {
155 SetUp();
156 std::string filename = "abc.txt";
157 std::string new_filename = "abc_new.txt";
158 std::string contents = "This is file.";
159 std::string read_contents;
160
161 PP_FileHandle file_handle = FileModuleLocal::OpenFile(instance_,
162 filename,
163 PP_FILEOPENFLAG_WRITE |
164 PP_FILEOPENFLAG_CREATE);
165 ASSERT_NE(PP_kInvalidFileHandle, file_handle);
166 ASSERT_TRUE(WriteFile(file_handle, contents));
167 CloseFileHandle(file_handle);
168
169 ASSERT_TRUE(FileModuleLocal::RenameFile(instance_, filename, new_filename));
170
171 file_handle = FileModuleLocal::OpenFile(instance_,
172 new_filename,
173 PP_FILEOPENFLAG_READ);
174 ASSERT_NE(PP_kInvalidFileHandle, file_handle);
175 ASSERT_TRUE(ReadFile(file_handle, &read_contents));
176 ASSERT_EQ(contents, read_contents);
177 CloseFileHandle(file_handle);
178
179 // Check that the old file no longer exists.
180 PP_FileInfo unused;
181 ASSERT_FALSE(FileModuleLocal::QueryFile(instance_, filename, &unused));
182
183 PASS();
184 }
185
186 std::string TestFlashFile::TestDeleteFileOrDir() {
187 SetUp();
188 std::string filename = "abc.txt";
189 std::string dirname = "def";
190 std::string contents = "This is file.";
191
192 // Test file deletion.
193 PP_FileHandle file_handle = FileModuleLocal::OpenFile(instance_,
194 filename,
195 PP_FILEOPENFLAG_WRITE |
196 PP_FILEOPENFLAG_CREATE);
197 ASSERT_NE(PP_kInvalidFileHandle, file_handle);
198 ASSERT_TRUE(WriteFile(file_handle, contents));
199 CloseFileHandle(file_handle);
200 ASSERT_TRUE(FileModuleLocal::DeleteFileOrDir(instance_, filename, false));
201 PP_FileInfo unused;
202 ASSERT_FALSE(FileModuleLocal::QueryFile(instance_, filename, &unused));
203
204 // Test directory deletion.
205 ASSERT_TRUE(FileModuleLocal::CreateDir(instance_, dirname));
206 ASSERT_TRUE(FileModuleLocal::DeleteFileOrDir(instance_, dirname, false));
207 ASSERT_FALSE(FileModuleLocal::QueryFile(instance_, dirname, &unused));
208
209 // Test recursive directory deletion.
210 ASSERT_TRUE(FileModuleLocal::CreateDir(instance_, dirname));
211 file_handle = FileModuleLocal::OpenFile(
212 instance_, dirname + "/" + filename,
213 PP_FILEOPENFLAG_WRITE | PP_FILEOPENFLAG_CREATE);
214 ASSERT_NE(PP_kInvalidFileHandle, file_handle);
215 ASSERT_TRUE(WriteFile(file_handle, contents));
216 CloseFileHandle(file_handle);
217 ASSERT_FALSE(FileModuleLocal::DeleteFileOrDir(instance_, dirname, false));
218 ASSERT_TRUE(FileModuleLocal::DeleteFileOrDir(instance_, dirname, true));
219 ASSERT_FALSE(FileModuleLocal::QueryFile(instance_, filename, &unused));
220
221 PASS();
222 }
223
224 std::string TestFlashFile::TestCreateDir() {
225 SetUp();
226 std::string dirname = "abc";
227 PP_FileInfo info;
228 ASSERT_FALSE(FileModuleLocal::QueryFile(instance_, dirname, &info));
229 ASSERT_TRUE(FileModuleLocal::CreateDir(instance_, dirname));
230 ASSERT_TRUE(FileModuleLocal::QueryFile(instance_, dirname, &info));
231 ASSERT_EQ(info.type, PP_FILETYPE_DIRECTORY);
232
233 PASS();
234 }
235
236 std::string TestFlashFile::TestQueryFile() {
237 std::string filename = "abc.txt";
238 std::string dirname = "def";
239 std::string contents = "This is file.";
240 PP_FileInfo info;
241
242 // Test querying a file.
243 PP_FileHandle file_handle = FileModuleLocal::OpenFile(instance_,
244 filename,
245 PP_FILEOPENFLAG_WRITE |
246 PP_FILEOPENFLAG_CREATE);
247 ASSERT_NE(PP_kInvalidFileHandle, file_handle);
248 ASSERT_TRUE(WriteFile(file_handle, contents));
249 CloseFileHandle(file_handle);
250 ASSERT_TRUE(FileModuleLocal::QueryFile(instance_, filename, &info));
251 ASSERT_EQ(static_cast<size_t>(info.size), contents.size());
252 ASSERT_EQ(info.type, PP_FILETYPE_REGULAR);
253 // TODO(raymes): Test the other fields.
254
255 // Test querying a directory.
256 ASSERT_TRUE(FileModuleLocal::CreateDir(instance_, dirname));
257 ASSERT_TRUE(FileModuleLocal::QueryFile(instance_, dirname, &info));
258 ASSERT_EQ(info.type, PP_FILETYPE_DIRECTORY);
259 // TODO(raymes): Test the other fields.
260
261 // Test querying a non-existent file.
262 ASSERT_FALSE(FileModuleLocal::QueryFile(instance_, "xx", &info));
263
264 PASS();
265 }
266
267 std::string TestFlashFile::TestGetDirContents() {
268 SetUp();
269 std::vector<FileModuleLocal::DirEntry> result;
270 ASSERT_TRUE(FileModuleLocal::GetDirContents(instance_, std::string(),
271 &result));
272 ASSERT_EQ(result.size(), 1);
273 ASSERT_EQ(result[0].name, "..");
274 ASSERT_EQ(result[0].is_dir, true);
275
276 std::string filename = "abc.txt";
277 std::string dirname = "def";
278 std::string contents = "This is file.";
279 PP_FileHandle file_handle = FileModuleLocal::OpenFile(instance_,
280 filename,
281 PP_FILEOPENFLAG_WRITE |
282 PP_FILEOPENFLAG_CREATE);
283 ASSERT_NE(PP_kInvalidFileHandle, file_handle);
284 ASSERT_TRUE(WriteFile(file_handle, contents));
285 CloseFileHandle(file_handle);
286 ASSERT_TRUE(FileModuleLocal::CreateDir(instance_, dirname));
287
288 ASSERT_TRUE(FileModuleLocal::GetDirContents(instance_, "", &result));
289 FileModuleLocal::DirEntry expected[] =
290 { {"..", true},
291 {filename, false},
292 {dirname, true}
293 };
294 size_t expected_size = sizeof(expected) / sizeof(expected[0]);
295
296 std::sort(expected, expected + expected_size, DirEntryLessThan);
297 std::sort(result.begin(), result.end(), DirEntryLessThan);
298
299 ASSERT_EQ(expected_size, result.size());
300 ASSERT_TRUE(std::equal(expected, expected + expected_size, result.begin(),
301 DirEntryEqual));
302
303 PASS();
304 }
305
306 std::string TestFlashFile::TestCreateTemporaryFile() {
307 SetUp();
100 size_t before_create = 0; 308 size_t before_create = 0;
101 ASSERT_SUBTEST_SUCCESS(GetItemCountUnderModuleLocalRoot(&before_create)); 309 ASSERT_SUBTEST_SUCCESS(GetItemCountUnderModuleLocalRoot(&before_create));
102 310
103 PP_FileHandle file_handle = FileModuleLocal::CreateTemporaryFile(instance_); 311 PP_FileHandle file_handle = FileModuleLocal::CreateTemporaryFile(instance_);
104 ASSERT_NE(PP_kInvalidFileHandle, file_handle); 312 ASSERT_NE(PP_kInvalidFileHandle, file_handle);
105 313
106 std::string contents = "This is a temp file."; 314 std::string contents = "This is a temp file.";
107 ASSERT_TRUE(WriteFile(file_handle, contents)); 315 ASSERT_TRUE(WriteFile(file_handle, contents));
108 std::string read_contents; 316 std::string read_contents;
109 ASSERT_TRUE(ReadFile(file_handle, &read_contents)); 317 ASSERT_TRUE(ReadFile(file_handle, &read_contents));
110 ASSERT_EQ(contents, read_contents); 318 ASSERT_EQ(contents, read_contents);
111 319
112 CloseFileHandle(file_handle); 320 CloseFileHandle(file_handle);
113 321
114 size_t after_close = 0; 322 size_t after_close = 0;
115 ASSERT_SUBTEST_SUCCESS(GetItemCountUnderModuleLocalRoot(&after_close)); 323 ASSERT_SUBTEST_SUCCESS(GetItemCountUnderModuleLocalRoot(&after_close));
116 ASSERT_EQ(before_create, after_close); 324 ASSERT_EQ(before_create, after_close);
117 325
118 PASS(); 326 PASS();
119 } 327 }
120 328
121 std::string TestFlashFile::GetItemCountUnderModuleLocalRoot( 329 std::string TestFlashFile::GetItemCountUnderModuleLocalRoot(
122 size_t* item_count) { 330 size_t* item_count) {
123 std::vector<FileModuleLocal::DirEntry> contents; 331 std::vector<FileModuleLocal::DirEntry> contents;
124 ASSERT_TRUE(FileModuleLocal::GetDirContents(instance_, "", &contents)); 332 ASSERT_TRUE(FileModuleLocal::GetDirContents(instance_, "", &contents));
125 *item_count = contents.size(); 333 *item_count = contents.size();
126 PASS(); 334 PASS();
127 } 335 }
OLDNEW
« no previous file with comments | « ppapi/tests/test_flash_file.h ('k') | ppapi/thunk/ppb_file_ref_api.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698