OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Native Client 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 "native_client/tests/ppapi_browser/ppb_file_io/common.h" |
| 6 |
| 7 #include <string.h> |
| 8 #include <string> |
| 9 |
| 10 #include "native_client/src/shared/platform/nacl_check.h" |
| 11 #include "native_client/src/third_party/ppapi/c/pp_errors.h" |
| 12 #include "native_client/src/third_party/ppapi/c/ppb_core.h" |
| 13 #include "native_client/src/third_party/ppapi/c/ppb_file_io.h" |
| 14 #include "native_client/src/third_party/ppapi/c/ppb_file_ref.h" |
| 15 #include "native_client/src/third_party/ppapi/c/ppb_file_system.h" |
| 16 #include "native_client/src/third_party/ppapi/c/ppb_messaging.h" |
| 17 #include "native_client/src/third_party/ppapi/c/ppb_url_loader.h" |
| 18 #include "native_client/src/third_party/ppapi/c/ppb_var.h" |
| 19 #include "native_client/tests/ppapi_test_lib/get_browser_interface.h" |
| 20 #include "native_client/tests/ppapi_test_lib/test_interface.h" |
| 21 |
| 22 namespace common { |
| 23 |
| 24 const char* kTestData = |
| 25 "Everywhere is within walking distance if you have the time"; |
| 26 |
| 27 void InitFileInfo(PP_FileSystemType system_type, PP_FileInfo* file_info) { |
| 28 memset(file_info, 0, sizeof(PP_FileInfo)); |
| 29 file_info->system_type = system_type; |
| 30 file_info->type = PP_FILETYPE_REGULAR; |
| 31 file_info->last_access_time = 200; // dummy value |
| 32 file_info->last_modified_time = 100; // something less than last access time |
| 33 } |
| 34 |
| 35 BoundPPAPIFunc OpenFileForTest::GetCompletionCallbackInitiatingPPAPIFunction( |
| 36 TestCallbackData* callback_data) { |
| 37 return std::tr1::bind(PPBFileIO()->Open, |
| 38 callback_data->existing_file_io, |
| 39 callback_data->existing_file_ref, |
| 40 PP_FILEOPENFLAG_READ | PP_FILEOPENFLAG_WRITE, |
| 41 std::tr1::placeholders::_1); |
| 42 } |
| 43 |
| 44 void FileIOTester::AddSequenceElement(TestSequenceElement* element) { |
| 45 test_sequence_.push_back(element); |
| 46 } |
| 47 |
| 48 void FileIOTester::Run() { |
| 49 if (test_sequence_.empty()) |
| 50 return; |
| 51 |
| 52 PP_Resource file_system = PPBFileSystem()->Create(pp_instance(), |
| 53 file_info_.system_type); |
| 54 EXPECT(file_system != kInvalidResource); |
| 55 |
| 56 TestCallbackData* callback_data = new TestCallbackData(file_system, |
| 57 file_info_, |
| 58 test_sequence_); |
| 59 |
| 60 int32_t pp_error = PPBFileSystem()->Open( |
| 61 callback_data->file_system, |
| 62 1024, |
| 63 MakeTestableCompletionCallback( |
| 64 "OpenFileSystemForSetupCallback", |
| 65 OpenFileSystemForSetupCallback, |
| 66 callback_data)); |
| 67 EXPECT(pp_error == PP_OK_COMPLETIONPENDING); |
| 68 } |
| 69 |
| 70 void FileIOTester::FlushFileForSetupCallback(void* data, int32_t result) { |
| 71 // This callback should be preceded by a write operation. |
| 72 EXPECT(result >= PP_OK); |
| 73 |
| 74 TestCallbackData* callback_data = reinterpret_cast<TestCallbackData*>(data); |
| 75 PP_FileInfo file_info = callback_data->file_info; |
| 76 int32_t pp_error = PPBFileIO()->Touch(callback_data->existing_file_io, |
| 77 file_info.last_access_time, |
| 78 file_info.last_modified_time, |
| 79 MakeTestableCompletionCallback( |
| 80 "TouchFileForSetupCallback", |
| 81 TouchFileForSetupCallback, |
| 82 callback_data)); |
| 83 EXPECT(pp_error == PP_OK_COMPLETIONPENDING); |
| 84 } |
| 85 |
| 86 // This is the first callback in the chain of callbacks. The first several in |
| 87 // the chain are initialization for the tests that come later in the chain. |
| 88 void FileIOTester::OpenFileSystemForSetupCallback(void* data, int32_t result) { |
| 89 EXPECT(result == PP_OK); |
| 90 |
| 91 // Need to retrieve file system from the data in order to create file ref |
| 92 TestCallbackData* callback_data = reinterpret_cast<TestCallbackData*>(data); |
| 93 const PP_Resource file_system = callback_data->file_system; |
| 94 // Create file ref for non-existing file |
| 95 callback_data->non_existing_file_ref = PPBFileRef()->Create( |
| 96 file_system, |
| 97 "/non_existing_file"); |
| 98 EXPECT(callback_data->non_existing_file_ref != kInvalidResource); |
| 99 |
| 100 // Create file io for non-existing file |
| 101 callback_data->non_existing_file_io = PPBFileIO()->Create(pp_instance()); |
| 102 EXPECT(callback_data->non_existing_file_io != kInvalidResource); |
| 103 |
| 104 // Create file ref for existing file |
| 105 callback_data->existing_file_ref = PPBFileRef()->Create(file_system, |
| 106 "/existing_file"); |
| 107 EXPECT(callback_data->existing_file_ref != kInvalidResource); |
| 108 |
| 109 // Create file io for existing file |
| 110 callback_data->existing_file_io = PPBFileIO()->Create(pp_instance()); |
| 111 EXPECT(callback_data->existing_file_io != kInvalidResource); |
| 112 |
| 113 const PP_FileInfo& file_info = callback_data->file_info; |
| 114 if (file_info.type == PP_FILETYPE_REGULAR) { |
| 115 int32_t pp_error = PPBFileIO()->Open(callback_data->existing_file_io, |
| 116 callback_data->existing_file_ref, |
| 117 PP_FILEOPENFLAG_TRUNCATE | |
| 118 PP_FILEOPENFLAG_CREATE | |
| 119 PP_FILEOPENFLAG_WRITE, |
| 120 MakeTestableCompletionCallback( |
| 121 "OpenFileForSetupCallback", |
| 122 OpenFileForSetupCallback, |
| 123 callback_data)); |
| 124 EXPECT(pp_error == PP_OK_COMPLETIONPENDING); |
| 125 } else if (file_info.type == PP_FILETYPE_DIRECTORY) { |
| 126 // TODO(sanga): Log a message indicating directories are not yet supported |
| 127 // in these tests. |
| 128 } |
| 129 } |
| 130 |
| 131 void FileIOTester::OpenFileForSetupCallback(void* data, int32_t result) { |
| 132 EXPECT(result == PP_OK); |
| 133 |
| 134 TestCallbackData* callback_data = reinterpret_cast<TestCallbackData*>(data); |
| 135 PP_FileInfo file_info = callback_data->file_info; |
| 136 int32_t pp_error = PPBFileIO()->Write(callback_data->existing_file_io, |
| 137 0, // no offset |
| 138 kTestData, |
| 139 strlen(kTestData), |
| 140 MakeTestableCompletionCallback( |
| 141 "WriteFileForSetupCallback", |
| 142 WriteFileForSetupCallback, |
| 143 callback_data)); |
| 144 EXPECT(pp_error == PP_OK_COMPLETIONPENDING); |
| 145 } |
| 146 |
| 147 void FileIOTester::StartTestSequence(TestCallbackData* callback_data) { |
| 148 if (!callback_data->test_sequence.empty()) { |
| 149 TestSequenceElement* first_test_element = |
| 150 callback_data->test_sequence.front(); |
| 151 PP_CompletionCallback callback = MakeTestableCompletionCallback( |
| 152 first_test_element->name(), |
| 153 TestSequenceElement::TestSequenceElementForwardingCallback, |
| 154 callback_data); |
| 155 PPBCore()->CallOnMainThread(0, // no delay |
| 156 callback, PP_OK); |
| 157 } else { |
| 158 TestSequenceElement::CleanupResources(callback_data); |
| 159 } |
| 160 } |
| 161 |
| 162 // Assign testing file properties with data from given callback data. |
| 163 void FileIOTester::TouchFileForSetupCallback(void* data, int32_t result) { |
| 164 EXPECT(result == PP_OK); |
| 165 |
| 166 TestCallbackData* callback_data = reinterpret_cast<TestCallbackData*>(data); |
| 167 PPBFileIO()->Close(callback_data->existing_file_io); |
| 168 StartTestSequence(callback_data); |
| 169 } |
| 170 |
| 171 void FileIOTester::WriteFileForSetupCallback(void* data, int32_t result) { |
| 172 EXPECT(result >= PP_OK); |
| 173 |
| 174 TestCallbackData* callback_data = reinterpret_cast<TestCallbackData*>(data); |
| 175 int32_t pp_error = PPBFileIO()->Flush(callback_data->existing_file_io, |
| 176 MakeTestableCompletionCallback( |
| 177 "FlushFileForSetupCallback", |
| 178 FlushFileForSetupCallback, |
| 179 callback_data)); |
| 180 EXPECT(pp_error == PP_OK_COMPLETIONPENDING); |
| 181 } |
| 182 |
| 183 } // namespace common |
OLD | NEW |