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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_file_mapping.h"
6
7 #include <string>
8
9 #include "ppapi/c/pp_errors.h"
10 #include "ppapi/c/ppb_file_io.h"
11 #include "ppapi/c/ppb_file_mapping.h"
12 #include "ppapi/cpp/file_io.h"
13 #include "ppapi/cpp/file_ref.h"
14 #include "ppapi/cpp/file_system.h"
15 #include "ppapi/cpp/instance.h"
16 #include "ppapi/cpp/module.h"
17 #include "ppapi/tests/test_utils.h"
18
19 REGISTER_TEST_CASE(FileMapping);
20
21 namespace {
22
23 /*int32_t ReadEntireFile(PP_Instance instance,
24 pp::FileIO* file_io,
25 int32_t offset,
26 std::string* data,
27 CallbackType callback_type) {
28 TestCompletionCallback callback(instance, callback_type);
29 char buf[256];
30 int32_t read_offset = offset;
31
32 for (;;) {
33 callback.WaitForResult(
34 file_io->Read(read_offset, buf, sizeof(buf), callback.GetCallback()));
35 if (callback.result() < 0)
36 return callback.result();
37 if (callback.result() == 0)
38 break;
39 read_offset += callback.result();
40 data->append(buf, callback.result());
41 }
42
43 return PP_OK;
44 }*/
45
46 int32_t WriteEntireBuffer(PP_Instance instance,
47 pp::FileIO* file_io,
48 int32_t offset,
49 const std::string& data,
50 CallbackType callback_type) {
51 TestCompletionCallback callback(instance, callback_type);
52 int32_t write_offset = offset;
53 const char* buf = data.c_str();
54 int32_t size = data.size();
55
56 while (write_offset < offset + size) {
57 callback.WaitForResult(file_io->Write(write_offset,
58 &buf[write_offset - offset],
59 size - write_offset + offset,
60 callback.GetCallback()));
61 if (callback.result() < 0)
62 return callback.result();
63 if (callback.result() == 0)
64 return PP_ERROR_FAILED;
65 write_offset += callback.result();
66 }
67
68 return PP_OK;
69 }
70
71 } // namespace
72
73 bool TestFileMapping::Init() {
74 // TODO(dmichael): Use unversioned string when this goes to stable?
75 file_mapping_if_ = static_cast<const PPB_FileMapping_0_1*>(
76 pp::Module::Get()->GetBrowserInterface(PPB_FILEMAPPING_INTERFACE_0_1));
77 return !!file_mapping_if_ && CheckTestingInterface() &&
78 EnsureRunningOverHTTP();
79 }
80
81 void TestFileMapping::RunTests(const std::string& filter) {
82 RUN_CALLBACK_TEST(TestFileMapping, Map, filter);
83 }
84
85 std::string TestFileMapping::TestMap() {
86 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
87
88 pp::FileSystem file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY);
89 pp::FileRef file_ref(file_system, "/mapped_file");
90
91 callback.WaitForResult(file_system.Open(1024, callback.GetCallback()));
92 ASSERT_EQ(PP_OK, callback.result());
93
94 pp::FileIO file_io(instance_);
95 callback.WaitForResult(file_io.Open(file_ref,
96 PP_FILEOPENFLAG_CREATE |
97 PP_FILEOPENFLAG_TRUNCATE |
98 PP_FILEOPENFLAG_READ |
99 PP_FILEOPENFLAG_WRITE,
100 callback.GetCallback()));
101 ASSERT_EQ(PP_OK, callback.result());
102
103 const std::string file_contents = "Some data for the file.";
104 ASSERT_EQ(PP_OK, WriteEntireBuffer(instance_->pp_instance(),
105 &file_io,
106 0,
107 file_contents,
108 callback_type()));
109 const int64_t page_size =
110 file_mapping_if_->GetMapPageSize(instance_->pp_instance());
111 const int64_t num_pages =
112 (file_contents.size() + page_size - 1) / page_size;
113 // TODO(dmichael): Use C++ interface.
114 void* address;
115 callback.WaitForResult(
116 file_mapping_if_->Map(
117 instance_->pp_instance(),
118 file_io.pp_resource(),
119 num_pages * page_size,
120 PP_FILEMAPPROTECTION_READ | PP_FILEMAPPROTECTION_WRITE,
121 PP_FILEMAPFLAG_SHARED,
122 0,
123 &address,
124 callback.GetCallback().pp_completion_callback()));
125 CHECK_CALLBACK_BEHAVIOR(callback);
126 ASSERT_EQ(PP_OK, callback.result());
127 ASSERT_NE(NULL, address);
128
129 std::string mapped_data(static_cast<char*>(address), file_contents.size());
130 ASSERT_EQ(file_contents, mapped_data);
131
132 // Now write some data and flush it. Our mapped region should get updated.
133 const std::string file_contents2('-', file_contents.size());
134 ASSERT_EQ(PP_OK, WriteEntireBuffer(instance_->pp_instance(),
135 &file_io,
136 0,
137 file_contents2,
138 callback_type()));
139 callback.WaitForResult(file_io.Flush(callback.GetCallback()));
140 ASSERT_EQ(PP_OK, callback.result());
141 std::string mapped_data2(static_cast<char*>(address), file_contents.size());
142 ASSERT_EQ(file_contents2, mapped_data2);
143
144 PASS();
145 }
146
OLDNEW
« 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