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