summaryrefslogtreecommitdiffstats
path: root/ppapi/tests
diff options
context:
space:
mode:
authorraymes@chromium.org <raymes@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-12-08 00:46:23 +0000
committerraymes@chromium.org <raymes@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-12-08 00:46:23 +0000
commit0c92b0d1079123c39f6e8fbd7eb3ff6503e9bccd (patch)
treeb6defa7168b626436612114c0176b91c84284beb /ppapi/tests
parente61449d257bd231d718afe97a19a686feac2219d (diff)
downloadchromium_src-0c92b0d1079123c39f6e8fbd7eb3ff6503e9bccd.zip
chromium_src-0c92b0d1079123c39f6e8fbd7eb3ff6503e9bccd.tar.gz
chromium_src-0c92b0d1079123c39f6e8fbd7eb3ff6503e9bccd.tar.bz2
Refactored the PPB_Flash_File_ModuleLocal/FileRef to the new ppapi resource model
The refactors PPB_Flash_File_ModuleLocal/FileRef to the new resource model. Calls for both these interfaces are now made directly to the browser. This removes the in-process implementation for these interfaces also (as they are flash-only). Tests are added for PPB_Flash_File_ModuleLocal. Review URL: https://codereview.chromium.org/11359097 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@171902 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi/tests')
-rw-r--r--ppapi/tests/test_flash_file.cc210
-rw-r--r--ppapi/tests/test_flash_file.h15
2 files changed, 222 insertions, 3 deletions
diff --git a/ppapi/tests/test_flash_file.cc b/ppapi/tests/test_flash_file.cc
index a4a1169..86100d9 100644
--- a/ppapi/tests/test_flash_file.cc
+++ b/ppapi/tests/test_flash_file.cc
@@ -4,6 +4,11 @@
#include "ppapi/tests/test_flash_file.h"
+#include <algorithm>
+#include <vector>
+
+#include "ppapi/c/pp_file_info.h"
+#include "ppapi/c/ppb_file_io.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/private/flash_file.h"
#include "ppapi/tests/testing_instance.h"
@@ -74,6 +79,18 @@ bool ReadFile(PP_FileHandle file_handle, std::string* contents) {
return result;
}
+bool DirEntryEqual(FileModuleLocal::DirEntry i,
+ FileModuleLocal::DirEntry j) {
+ return i.name == j.name && i.is_dir == j.is_dir;
+}
+
+bool DirEntryLessThan(FileModuleLocal::DirEntry i,
+ FileModuleLocal::DirEntry j) {
+ if (i.name == j.name)
+ return i.is_dir < j.is_dir;
+ return i.name < j.name;
+}
+
} // namespace
REGISTER_TEST_CASE(FlashFile);
@@ -90,13 +107,204 @@ bool TestFlashFile::Init() {
}
void TestFlashFile::RunTests(const std::string& filter) {
+ RUN_TEST(OpenFile, filter);
+ RUN_TEST(RenameFile, filter);
+ RUN_TEST(DeleteFileOrDir, filter);
+ RUN_TEST(CreateDir, filter);
+ RUN_TEST(QueryFile, filter);
+ RUN_TEST(GetDirContents, filter);
RUN_TEST(CreateTemporaryFile, filter);
}
-std::string TestFlashFile::TestCreateTemporaryFile() {
+void TestFlashFile::SetUp() {
+ // Clear out existing test data.
+ FileModuleLocal::DeleteFileOrDir(instance_, std::string(), true);
// Make sure that the root directory exists.
FileModuleLocal::CreateDir(instance_, std::string());
+}
+
+std::string TestFlashFile::TestOpenFile() {
+ SetUp();
+ std::string filename = "abc.txt";
+ PP_FileHandle file_handle = FileModuleLocal::OpenFile(instance_,
+ filename,
+ PP_FILEOPENFLAG_WRITE |
+ PP_FILEOPENFLAG_CREATE);
+ ASSERT_NE(PP_kInvalidFileHandle, file_handle);
+
+ std::string contents = "This is file.";
+ std::string read_contents;
+ ASSERT_TRUE(WriteFile(file_handle, contents));
+ ASSERT_FALSE(ReadFile(file_handle, &read_contents));
+ CloseFileHandle(file_handle);
+
+ file_handle = FileModuleLocal::OpenFile(instance_,
+ filename,
+ PP_FILEOPENFLAG_READ);
+ ASSERT_NE(PP_kInvalidFileHandle, file_handle);
+
+ ASSERT_FALSE(WriteFile(file_handle, contents));
+ ASSERT_TRUE(ReadFile(file_handle, &read_contents));
+ ASSERT_EQ(contents, read_contents);
+ CloseFileHandle(file_handle);
+
+ PASS();
+}
+
+std::string TestFlashFile::TestRenameFile() {
+ SetUp();
+ std::string filename = "abc.txt";
+ std::string new_filename = "abc_new.txt";
+ std::string contents = "This is file.";
+ std::string read_contents;
+
+ PP_FileHandle file_handle = FileModuleLocal::OpenFile(instance_,
+ filename,
+ PP_FILEOPENFLAG_WRITE |
+ PP_FILEOPENFLAG_CREATE);
+ ASSERT_NE(PP_kInvalidFileHandle, file_handle);
+ ASSERT_TRUE(WriteFile(file_handle, contents));
+ CloseFileHandle(file_handle);
+
+ ASSERT_TRUE(FileModuleLocal::RenameFile(instance_, filename, new_filename));
+
+ file_handle = FileModuleLocal::OpenFile(instance_,
+ new_filename,
+ PP_FILEOPENFLAG_READ);
+ ASSERT_NE(PP_kInvalidFileHandle, file_handle);
+ ASSERT_TRUE(ReadFile(file_handle, &read_contents));
+ ASSERT_EQ(contents, read_contents);
+ CloseFileHandle(file_handle);
+
+ // Check that the old file no longer exists.
+ PP_FileInfo unused;
+ ASSERT_FALSE(FileModuleLocal::QueryFile(instance_, filename, &unused));
+
+ PASS();
+}
+
+std::string TestFlashFile::TestDeleteFileOrDir() {
+ SetUp();
+ std::string filename = "abc.txt";
+ std::string dirname = "def";
+ std::string contents = "This is file.";
+ // Test file deletion.
+ PP_FileHandle file_handle = FileModuleLocal::OpenFile(instance_,
+ filename,
+ PP_FILEOPENFLAG_WRITE |
+ PP_FILEOPENFLAG_CREATE);
+ ASSERT_NE(PP_kInvalidFileHandle, file_handle);
+ ASSERT_TRUE(WriteFile(file_handle, contents));
+ CloseFileHandle(file_handle);
+ ASSERT_TRUE(FileModuleLocal::DeleteFileOrDir(instance_, filename, false));
+ PP_FileInfo unused;
+ ASSERT_FALSE(FileModuleLocal::QueryFile(instance_, filename, &unused));
+
+ // Test directory deletion.
+ ASSERT_TRUE(FileModuleLocal::CreateDir(instance_, dirname));
+ ASSERT_TRUE(FileModuleLocal::DeleteFileOrDir(instance_, dirname, false));
+ ASSERT_FALSE(FileModuleLocal::QueryFile(instance_, dirname, &unused));
+
+ // Test recursive directory deletion.
+ ASSERT_TRUE(FileModuleLocal::CreateDir(instance_, dirname));
+ file_handle = FileModuleLocal::OpenFile(
+ instance_, dirname + "/" + filename,
+ PP_FILEOPENFLAG_WRITE | PP_FILEOPENFLAG_CREATE);
+ ASSERT_NE(PP_kInvalidFileHandle, file_handle);
+ ASSERT_TRUE(WriteFile(file_handle, contents));
+ CloseFileHandle(file_handle);
+ ASSERT_FALSE(FileModuleLocal::DeleteFileOrDir(instance_, dirname, false));
+ ASSERT_TRUE(FileModuleLocal::DeleteFileOrDir(instance_, dirname, true));
+ ASSERT_FALSE(FileModuleLocal::QueryFile(instance_, filename, &unused));
+
+ PASS();
+}
+
+std::string TestFlashFile::TestCreateDir() {
+ SetUp();
+ std::string dirname = "abc";
+ PP_FileInfo info;
+ ASSERT_FALSE(FileModuleLocal::QueryFile(instance_, dirname, &info));
+ ASSERT_TRUE(FileModuleLocal::CreateDir(instance_, dirname));
+ ASSERT_TRUE(FileModuleLocal::QueryFile(instance_, dirname, &info));
+ ASSERT_EQ(info.type, PP_FILETYPE_DIRECTORY);
+
+ PASS();
+}
+
+std::string TestFlashFile::TestQueryFile() {
+ std::string filename = "abc.txt";
+ std::string dirname = "def";
+ std::string contents = "This is file.";
+ PP_FileInfo info;
+
+ // Test querying a file.
+ PP_FileHandle file_handle = FileModuleLocal::OpenFile(instance_,
+ filename,
+ PP_FILEOPENFLAG_WRITE |
+ PP_FILEOPENFLAG_CREATE);
+ ASSERT_NE(PP_kInvalidFileHandle, file_handle);
+ ASSERT_TRUE(WriteFile(file_handle, contents));
+ CloseFileHandle(file_handle);
+ ASSERT_TRUE(FileModuleLocal::QueryFile(instance_, filename, &info));
+ ASSERT_EQ(static_cast<size_t>(info.size), contents.size());
+ ASSERT_EQ(info.type, PP_FILETYPE_REGULAR);
+ // TODO(raymes): Test the other fields.
+
+ // Test querying a directory.
+ ASSERT_TRUE(FileModuleLocal::CreateDir(instance_, dirname));
+ ASSERT_TRUE(FileModuleLocal::QueryFile(instance_, dirname, &info));
+ ASSERT_EQ(info.type, PP_FILETYPE_DIRECTORY);
+ // TODO(raymes): Test the other fields.
+
+ // Test querying a non-existent file.
+ ASSERT_FALSE(FileModuleLocal::QueryFile(instance_, "xx", &info));
+
+ PASS();
+}
+
+std::string TestFlashFile::TestGetDirContents() {
+ SetUp();
+ std::vector<FileModuleLocal::DirEntry> result;
+ ASSERT_TRUE(FileModuleLocal::GetDirContents(instance_, std::string(),
+ &result));
+ ASSERT_EQ(result.size(), 1);
+ ASSERT_EQ(result[0].name, "..");
+ ASSERT_EQ(result[0].is_dir, true);
+
+ std::string filename = "abc.txt";
+ std::string dirname = "def";
+ std::string contents = "This is file.";
+ PP_FileHandle file_handle = FileModuleLocal::OpenFile(instance_,
+ filename,
+ PP_FILEOPENFLAG_WRITE |
+ PP_FILEOPENFLAG_CREATE);
+ ASSERT_NE(PP_kInvalidFileHandle, file_handle);
+ ASSERT_TRUE(WriteFile(file_handle, contents));
+ CloseFileHandle(file_handle);
+ ASSERT_TRUE(FileModuleLocal::CreateDir(instance_, dirname));
+
+ ASSERT_TRUE(FileModuleLocal::GetDirContents(instance_, "", &result));
+ FileModuleLocal::DirEntry expected[] =
+ { {"..", true},
+ {filename, false},
+ {dirname, true}
+ };
+ size_t expected_size = sizeof(expected) / sizeof(expected[0]);
+
+ std::sort(expected, expected + expected_size, DirEntryLessThan);
+ std::sort(result.begin(), result.end(), DirEntryLessThan);
+
+ ASSERT_EQ(expected_size, result.size());
+ ASSERT_TRUE(std::equal(expected, expected + expected_size, result.begin(),
+ DirEntryEqual));
+
+ PASS();
+}
+
+std::string TestFlashFile::TestCreateTemporaryFile() {
+ SetUp();
size_t before_create = 0;
ASSERT_SUBTEST_SUCCESS(GetItemCountUnderModuleLocalRoot(&before_create));
diff --git a/ppapi/tests/test_flash_file.h b/ppapi/tests/test_flash_file.h
index b58890d..86055db 100644
--- a/ppapi/tests/test_flash_file.h
+++ b/ppapi/tests/test_flash_file.h
@@ -19,10 +19,21 @@ class TestFlashFile: public TestCase {
virtual void RunTests(const std::string& filter);
private:
- // TODO(yzshen): Add more test cases for PPB_Flash_File_ModuleLocal and
- // PPB_Flash_File_FileRef.
+ // TODO(raymes): We should have SetUp/TearDown methods for ppapi tests.
+ void SetUp();
+
+ std::string TestOpenFile();
+ std::string TestRenameFile();
+ std::string TestDeleteFileOrDir();
+ std::string TestCreateDir();
+ std::string TestQueryFile();
+ std::string TestGetDirContents();
std::string TestCreateTemporaryFile();
+ // TODO(raymes): Add these when we can test file chooser properly.
+ // std::string TestOpenFileRef();
+ // std::string TestQueryFileRef();
+
// Gets the number of files and directories under the module-local root
// directory.
std::string GetItemCountUnderModuleLocalRoot(size_t* item_count);