diff options
author | yzshen@chromium.org <yzshen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-15 22:56:53 +0000 |
---|---|---|
committer | yzshen@chromium.org <yzshen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-15 22:56:53 +0000 |
commit | 481a4b7f34565ef087c8aa44c8e04f9f0e466b2d (patch) | |
tree | 58a27508ef38cecf35d478710ee9039aa9a2cea0 /ppapi/tests | |
parent | e5d5b0e8f45962bbb5cba1a6dd9a3b31eadb9429 (diff) | |
download | chromium_src-481a4b7f34565ef087c8aa44c8e04f9f0e466b2d.zip chromium_src-481a4b7f34565ef087c8aa44c8e04f9f0e466b2d.tar.gz chromium_src-481a4b7f34565ef087c8aa44c8e04f9f0e466b2d.tar.bz2 |
Add CreateTemporaryFile to PPB_Flash_File_ModuleLocal.
BUG=129807
TEST=None
Review URL: https://chromiumcodereview.appspot.com/10534045
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@142512 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi/tests')
-rw-r--r-- | ppapi/tests/test_flash_file.cc | 133 | ||||
-rw-r--r-- | ppapi/tests/test_flash_file.h | 34 |
2 files changed, 167 insertions, 0 deletions
diff --git a/ppapi/tests/test_flash_file.cc b/ppapi/tests/test_flash_file.cc new file mode 100644 index 0000000..b7baeae --- /dev/null +++ b/ppapi/tests/test_flash_file.cc @@ -0,0 +1,133 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ppapi/tests/test_flash_file.h" + +#include "ppapi/cpp/module.h" +#include "ppapi/tests/testing_instance.h" +#include "ppapi/tests/test_utils.h" + +#if defined(PPAPI_OS_WIN) +#include <windows.h> +#else +#include <errno.h> +#include <unistd.h> +#endif + +namespace { + +void CloseFileHandle(PP_FileHandle file_handle) { +#if defined(PPAPI_OS_WIN) + CloseHandle(file_handle); +#else + close(file_handle); +#endif +} + +bool WriteFile(PP_FileHandle file_handle, const std::string& contents) { +#if defined(PPAPI_OS_WIN) + DWORD bytes_written = 0; + BOOL result = ::WriteFile(file_handle, contents.c_str(), contents.size(), + &bytes_written, NULL); + return result && bytes_written == static_cast<DWORD>(contents.size()); +#else + ssize_t bytes_written = 0; + do { + bytes_written = write(file_handle, contents.c_str(), contents.size()); + } while (bytes_written == -1 && errno == EINTR); + return bytes_written == static_cast<ssize_t>(contents.size()); +#endif +} + +bool ReadFile(PP_FileHandle file_handle, std::string* contents) { + static const size_t kBufferSize = 1024; + char* buffer = new char[kBufferSize]; + bool result = false; + contents->clear(); + +#if defined(PPAPI_OS_WIN) + SetFilePointer(file_handle, 0, NULL, FILE_BEGIN); + DWORD bytes_read = 0; + do { + result = !!::ReadFile(file_handle, buffer, kBufferSize, &bytes_read, NULL); + if (result && bytes_read > 0) + contents->append(buffer, bytes_read); + } while (result && bytes_read > 0); +#else + lseek(file_handle, 0, SEEK_SET); + ssize_t bytes_read = 0; + do { + do { + bytes_read = read(file_handle, buffer, kBufferSize); + } while (bytes_read == -1 && errno == EINTR); + result = bytes_read != -1; + if (bytes_read > 0) + contents->append(buffer, bytes_read); + } while (bytes_read > 0); +#endif + + delete[] buffer; + return result; +} + +} // namespace + +REGISTER_TEST_CASE(FlashFile); + +TestFlashFile::TestFlashFile(TestingInstance* instance) + : TestCase(instance), module_local_interface_(NULL) { +} + +TestFlashFile::~TestFlashFile() { +} + +bool TestFlashFile::Init() { + module_local_interface_ = static_cast<const PPB_Flash_File_ModuleLocal*>( + pp::Module::Get()->GetBrowserInterface( + PPB_FLASH_FILE_MODULELOCAL_INTERFACE)); + return !!module_local_interface_; +} + +void TestFlashFile::RunTests(const std::string& filter) { + RUN_TEST(CreateTemporaryFile, filter); +} + +std::string TestFlashFile::TestCreateTemporaryFile() { + // Make sure that the root directory exists. + module_local_interface_->CreateDir(instance_->pp_instance(), ""); + + int32_t before_create = 0; + ASSERT_SUBTEST_SUCCESS(GetItemCountUnderModuleLocalRoot(&before_create)); + + PP_FileHandle file_handle = PP_kInvalidFileHandle; + int32_t result = module_local_interface_->CreateTemporaryFile( + instance_->pp_instance(), &file_handle); + ASSERT_EQ(result, PP_OK); + + std::string contents = "This is a temp file."; + ASSERT_TRUE(WriteFile(file_handle, contents)); + std::string read_contents; + ASSERT_TRUE(ReadFile(file_handle, &read_contents)); + ASSERT_EQ(contents, read_contents); + + CloseFileHandle(file_handle); + + int32_t after_close = 0; + ASSERT_SUBTEST_SUCCESS(GetItemCountUnderModuleLocalRoot(&after_close)); + ASSERT_EQ(before_create, after_close); + + PASS(); +} + +std::string TestFlashFile::GetItemCountUnderModuleLocalRoot( + int32_t* item_count) { + PP_DirContents_Dev* contents = NULL; + int32_t result = module_local_interface_->GetDirContents( + instance_->pp_instance(), "", &contents); + ASSERT_EQ(result, PP_OK); + + *item_count = contents->count; + module_local_interface_->FreeDirContents(instance_->pp_instance(), contents); + PASS(); +} diff --git a/ppapi/tests/test_flash_file.h b/ppapi/tests/test_flash_file.h new file mode 100644 index 0000000..36adfae --- /dev/null +++ b/ppapi/tests/test_flash_file.h @@ -0,0 +1,34 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PAPPI_TESTS_TEST_FLASH_FILE_H_ +#define PPAPI_TESTS_TEST_FLASH_FILE_H_ + +#include <string> + +#include "ppapi/c/private/ppb_flash_file.h" +#include "ppapi/tests/test_case.h" + +class TestFlashFile: public TestCase { + public: + explicit TestFlashFile(TestingInstance* instance); + virtual ~TestFlashFile(); + + // TestCase implementation. + virtual bool Init(); + virtual void RunTests(const std::string& filter); + + private: + // TODO(yzshen): Add more test cases for PPB_Flash_File_ModuleLocal and + // PPB_Flash_File_FileRef. + std::string TestCreateTemporaryFile(); + + // Gets the number of files and directories under the module-local root + // directory. + std::string GetItemCountUnderModuleLocalRoot(int32_t* item_count); + + const PPB_Flash_File_ModuleLocal* module_local_interface_; +}; + +#endif // PPAPI_TESTS_TEST_FLASH_FILE_H_ |