Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Unified Diff: ppapi/tests/test_file_mapping.cc

Issue 69663002: PPAPI: Implement PPB_FileMapping on POSIX (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rough patch. Starting testing. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ppapi/tests/test_file_mapping.h ('k') | ppapi/thunk/interfaces_ppb_public_dev_channel.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ppapi/tests/test_file_mapping.cc
diff --git a/ppapi/tests/test_file_mapping.cc b/ppapi/tests/test_file_mapping.cc
new file mode 100644
index 0000000000000000000000000000000000000000..422d78bba5706ba79740c1211de84561aa837da4
--- /dev/null
+++ b/ppapi/tests/test_file_mapping.cc
@@ -0,0 +1,146 @@
+// 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_file_mapping.h"
+
+#include <string>
+
+#include "ppapi/c/pp_errors.h"
+#include "ppapi/c/ppb_file_io.h"
+#include "ppapi/c/ppb_file_mapping.h"
+#include "ppapi/cpp/file_io.h"
+#include "ppapi/cpp/file_ref.h"
+#include "ppapi/cpp/file_system.h"
+#include "ppapi/cpp/instance.h"
+#include "ppapi/cpp/module.h"
+#include "ppapi/tests/test_utils.h"
+
+REGISTER_TEST_CASE(FileMapping);
+
+namespace {
+
+/*int32_t ReadEntireFile(PP_Instance instance,
+ pp::FileIO* file_io,
+ int32_t offset,
+ std::string* data,
+ CallbackType callback_type) {
+ TestCompletionCallback callback(instance, callback_type);
+ char buf[256];
+ int32_t read_offset = offset;
+
+ for (;;) {
+ callback.WaitForResult(
+ file_io->Read(read_offset, buf, sizeof(buf), callback.GetCallback()));
+ if (callback.result() < 0)
+ return callback.result();
+ if (callback.result() == 0)
+ break;
+ read_offset += callback.result();
+ data->append(buf, callback.result());
+ }
+
+ return PP_OK;
+}*/
+
+int32_t WriteEntireBuffer(PP_Instance instance,
+ pp::FileIO* file_io,
+ int32_t offset,
+ const std::string& data,
+ CallbackType callback_type) {
+ TestCompletionCallback callback(instance, callback_type);
+ int32_t write_offset = offset;
+ const char* buf = data.c_str();
+ int32_t size = data.size();
+
+ while (write_offset < offset + size) {
+ callback.WaitForResult(file_io->Write(write_offset,
+ &buf[write_offset - offset],
+ size - write_offset + offset,
+ callback.GetCallback()));
+ if (callback.result() < 0)
+ return callback.result();
+ if (callback.result() == 0)
+ return PP_ERROR_FAILED;
+ write_offset += callback.result();
+ }
+
+ return PP_OK;
+}
+
+} // namespace
+
+bool TestFileMapping::Init() {
+ // TODO(dmichael): Use unversioned string when this goes to stable?
+ file_mapping_if_ = static_cast<const PPB_FileMapping_0_1*>(
+ pp::Module::Get()->GetBrowserInterface(PPB_FILEMAPPING_INTERFACE_0_1));
+ return !!file_mapping_if_ && CheckTestingInterface() &&
+ EnsureRunningOverHTTP();
+}
+
+void TestFileMapping::RunTests(const std::string& filter) {
+ RUN_CALLBACK_TEST(TestFileMapping, Map, filter);
+}
+
+std::string TestFileMapping::TestMap() {
+ TestCompletionCallback callback(instance_->pp_instance(), callback_type());
+
+ pp::FileSystem file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY);
+ pp::FileRef file_ref(file_system, "/mapped_file");
+
+ callback.WaitForResult(file_system.Open(1024, callback.GetCallback()));
+ ASSERT_EQ(PP_OK, callback.result());
+
+ pp::FileIO file_io(instance_);
+ callback.WaitForResult(file_io.Open(file_ref,
+ PP_FILEOPENFLAG_CREATE |
+ PP_FILEOPENFLAG_TRUNCATE |
+ PP_FILEOPENFLAG_READ |
+ PP_FILEOPENFLAG_WRITE,
+ callback.GetCallback()));
+ ASSERT_EQ(PP_OK, callback.result());
+
+ const std::string file_contents = "Some data for the file.";
+ ASSERT_EQ(PP_OK, WriteEntireBuffer(instance_->pp_instance(),
+ &file_io,
+ 0,
+ file_contents,
+ callback_type()));
+ const int64_t page_size =
+ file_mapping_if_->GetMapPageSize(instance_->pp_instance());
+ const int64_t num_pages =
+ (file_contents.size() + page_size - 1) / page_size;
+ // TODO(dmichael): Use C++ interface.
+ void* address;
+ callback.WaitForResult(
+ file_mapping_if_->Map(
+ instance_->pp_instance(),
+ file_io.pp_resource(),
+ num_pages * page_size,
+ PP_FILEMAPPROTECTION_READ | PP_FILEMAPPROTECTION_WRITE,
+ PP_FILEMAPFLAG_SHARED,
+ 0,
+ &address,
+ callback.GetCallback().pp_completion_callback()));
+ CHECK_CALLBACK_BEHAVIOR(callback);
+ ASSERT_EQ(PP_OK, callback.result());
+ ASSERT_NE(NULL, address);
+
+ std::string mapped_data(static_cast<char*>(address), file_contents.size());
+ ASSERT_EQ(file_contents, mapped_data);
+
+ // Now write some data and flush it. Our mapped region should get updated.
+ const std::string file_contents2('-', file_contents.size());
+ ASSERT_EQ(PP_OK, WriteEntireBuffer(instance_->pp_instance(),
+ &file_io,
+ 0,
+ file_contents2,
+ callback_type()));
+ callback.WaitForResult(file_io.Flush(callback.GetCallback()));
+ ASSERT_EQ(PP_OK, callback.result());
+ std::string mapped_data2(static_cast<char*>(address), file_contents.size());
+ ASSERT_EQ(file_contents2, mapped_data2);
+
+ PASS();
+}
+
« no previous file with comments | « ppapi/tests/test_file_mapping.h ('k') | ppapi/thunk/interfaces_ppb_public_dev_channel.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698