| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ppapi/tests/test_flash_file.h" |
| 6 |
| 7 #include "ppapi/cpp/module.h" |
| 8 #include "ppapi/tests/testing_instance.h" |
| 9 #include "ppapi/tests/test_utils.h" |
| 10 |
| 11 #if defined(PPAPI_OS_WIN) |
| 12 #include <windows.h> |
| 13 #else |
| 14 #include <errno.h> |
| 15 #include <unistd.h> |
| 16 #endif |
| 17 |
| 18 namespace { |
| 19 |
| 20 void CloseFileHandle(PP_FileHandle file_handle) { |
| 21 #if defined(PPAPI_OS_WIN) |
| 22 CloseHandle(file_handle); |
| 23 #else |
| 24 close(file_handle); |
| 25 #endif |
| 26 } |
| 27 |
| 28 bool WriteFile(PP_FileHandle file_handle, const std::string& contents) { |
| 29 #if defined(PPAPI_OS_WIN) |
| 30 DWORD bytes_written = 0; |
| 31 BOOL result = ::WriteFile(file_handle, contents.c_str(), contents.size(), |
| 32 &bytes_written, NULL); |
| 33 return result && bytes_written == static_cast<DWORD>(contents.size()); |
| 34 #else |
| 35 ssize_t bytes_written = 0; |
| 36 do { |
| 37 bytes_written = write(file_handle, contents.c_str(), contents.size()); |
| 38 } while (bytes_written == -1 && errno == EINTR); |
| 39 return bytes_written == static_cast<ssize_t>(contents.size()); |
| 40 #endif |
| 41 } |
| 42 |
| 43 bool ReadFile(PP_FileHandle file_handle, std::string* contents) { |
| 44 static const size_t kBufferSize = 1024; |
| 45 char* buffer = new char[kBufferSize]; |
| 46 bool result = false; |
| 47 contents->clear(); |
| 48 |
| 49 #if defined(PPAPI_OS_WIN) |
| 50 SetFilePointer(file_handle, 0, NULL, FILE_BEGIN); |
| 51 DWORD bytes_read = 0; |
| 52 do { |
| 53 result = !!::ReadFile(file_handle, buffer, kBufferSize, &bytes_read, NULL); |
| 54 if (result && bytes_read > 0) |
| 55 contents->append(buffer, bytes_read); |
| 56 } while (result && bytes_read > 0); |
| 57 #else |
| 58 lseek(file_handle, 0, SEEK_SET); |
| 59 ssize_t bytes_read = 0; |
| 60 do { |
| 61 do { |
| 62 bytes_read = read(file_handle, buffer, kBufferSize); |
| 63 } while (bytes_read == -1 && errno == EINTR); |
| 64 result = bytes_read != -1; |
| 65 if (bytes_read > 0) |
| 66 contents->append(buffer, bytes_read); |
| 67 } while (bytes_read > 0); |
| 68 #endif |
| 69 |
| 70 delete[] buffer; |
| 71 return result; |
| 72 } |
| 73 |
| 74 } // namespace |
| 75 |
| 76 REGISTER_TEST_CASE(FlashFile); |
| 77 |
| 78 TestFlashFile::TestFlashFile(TestingInstance* instance) |
| 79 : TestCase(instance), module_local_interface_(NULL) { |
| 80 } |
| 81 |
| 82 TestFlashFile::~TestFlashFile() { |
| 83 } |
| 84 |
| 85 bool TestFlashFile::Init() { |
| 86 module_local_interface_ = static_cast<const PPB_Flash_File_ModuleLocal*>( |
| 87 pp::Module::Get()->GetBrowserInterface( |
| 88 PPB_FLASH_FILE_MODULELOCAL_INTERFACE)); |
| 89 return !!module_local_interface_; |
| 90 } |
| 91 |
| 92 void TestFlashFile::RunTests(const std::string& filter) { |
| 93 RUN_TEST(CreateTemporaryFile, filter); |
| 94 } |
| 95 |
| 96 std::string TestFlashFile::TestCreateTemporaryFile() { |
| 97 // Make sure that the root directory exists. |
| 98 module_local_interface_->CreateDir(instance_->pp_instance(), ""); |
| 99 |
| 100 int32_t before_create = 0; |
| 101 ASSERT_SUBTEST_SUCCESS(GetItemCountUnderModuleLocalRoot(&before_create)); |
| 102 |
| 103 PP_FileHandle file_handle = PP_kInvalidFileHandle; |
| 104 int32_t result = module_local_interface_->CreateTemporaryFile( |
| 105 instance_->pp_instance(), &file_handle); |
| 106 ASSERT_EQ(result, PP_OK); |
| 107 |
| 108 std::string contents = "This is a temp file."; |
| 109 ASSERT_TRUE(WriteFile(file_handle, contents)); |
| 110 std::string read_contents; |
| 111 ASSERT_TRUE(ReadFile(file_handle, &read_contents)); |
| 112 ASSERT_EQ(contents, read_contents); |
| 113 |
| 114 CloseFileHandle(file_handle); |
| 115 |
| 116 int32_t after_close = 0; |
| 117 ASSERT_SUBTEST_SUCCESS(GetItemCountUnderModuleLocalRoot(&after_close)); |
| 118 ASSERT_EQ(before_create, after_close); |
| 119 |
| 120 PASS(); |
| 121 } |
| 122 |
| 123 std::string TestFlashFile::GetItemCountUnderModuleLocalRoot( |
| 124 int32_t* item_count) { |
| 125 PP_DirContents_Dev* contents = NULL; |
| 126 int32_t result = module_local_interface_->GetDirContents( |
| 127 instance_->pp_instance(), "", &contents); |
| 128 ASSERT_EQ(result, PP_OK); |
| 129 |
| 130 *item_count = contents->count; |
| 131 module_local_interface_->FreeDirContents(instance_->pp_instance(), contents); |
| 132 PASS(); |
| 133 } |
| OLD | NEW |