This source file includes following definitions.
- called
- result
- Callback
- called
- result
- Reset
- Callback
- SendReply
- SendOpenReply
- OpenFileSystem
- OpenFile
- TEST_F
- TEST_F
#include "base/message_loop/message_loop.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/ppb_file_io.h"
#include "ppapi/c/ppb_file_ref.h"
#include "ppapi/c/ppb_file_system.h"
#include "ppapi/proxy/file_system_resource.h"
#include "ppapi/proxy/locking_resource_releaser.h"
#include "ppapi/proxy/plugin_message_filter.h"
#include "ppapi/proxy/ppapi_message_utils.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/proxy/ppapi_proxy_test.h"
#include "ppapi/shared_impl/proxy_lock.h"
#include "ppapi/shared_impl/scoped_pp_var.h"
#include "ppapi/shared_impl/var.h"
#include "ppapi/thunk/enter.h"
#include "ppapi/thunk/ppb_file_system_api.h"
#include "ppapi/thunk/thunk.h"
using ppapi::proxy::ResourceMessageTestSink;
using ppapi::thunk::EnterResource;
using ppapi::thunk::PPB_FileSystem_API;
namespace ppapi {
namespace proxy {
namespace {
const int64_t kExpectedFileSystemSize = 100;
const int64_t kQuotaRequestAmount1 = 10;
const int64_t kQuotaRequestAmount2 = 20;
class MockCompletionCallback {
public:
MockCompletionCallback() : called_(false) {}
bool called() { return called_; }
int32_t result() { return result_; }
static void Callback(void* user_data, int32_t result) {
MockCompletionCallback* that =
reinterpret_cast<MockCompletionCallback*>(user_data);
that->called_ = true;
that->result_ = result;
}
private:
bool called_;
int32_t result_;
};
class MockRequestQuotaCallback {
public:
MockRequestQuotaCallback() : called_(false) {}
bool called() { return called_; }
int64_t result() { return result_; }
void Reset() { called_ = false; }
void Callback(int64_t result) {
ASSERT_FALSE(called_);
called_ = true;
result_ = result;
}
private:
bool called_;
int64_t result_;
};
class FileSystemResourceTest : public PluginProxyTest {
public:
FileSystemResourceTest() {}
void SendReply(const ResourceMessageCallParams& params,
int32_t result,
const IPC::Message& nested_message) {
ResourceMessageReplyParams reply_params(params.pp_resource(),
params.sequence());
reply_params.set_result(result);
PluginMessageFilter::DispatchResourceReplyForTest(
reply_params, nested_message);
}
void SendOpenReply(const ResourceMessageCallParams& params, int32_t result) {
SendReply(params, result, PpapiPluginMsg_FileSystem_OpenReply());
}
void OpenFileSystem(PP_Resource file_system) {
MockCompletionCallback cb;
int32_t result = thunk::GetPPB_FileSystem_1_0_Thunk()->Open(
file_system,
kExpectedFileSystemSize,
PP_MakeCompletionCallback(&MockCompletionCallback::Callback, &cb));
ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
ResourceMessageTestSink::ResourceCallVector open_messages =
sink().GetAllResourceCallsMatching(PpapiHostMsg_FileSystem_Open::ID);
ASSERT_EQ(2U, open_messages.size());
sink().ClearMessages();
SendOpenReply(open_messages[0].first, PP_OK);
SendOpenReply(open_messages[1].first, PP_OK);
ASSERT_TRUE(cb.called());
ASSERT_EQ(PP_OK, cb.result());
}
void OpenFile(PP_Resource file_io,
PP_Resource file_ref,
PP_Resource file_system) {
MockCompletionCallback cb;
int32_t result = thunk::GetPPB_FileIO_1_1_Thunk()->Open(
file_io,
file_ref,
PP_FILEOPENFLAG_WRITE,
PP_MakeCompletionCallback(&MockCompletionCallback::Callback, &cb));
ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
ResourceMessageCallParams params;
IPC::Message msg;
ASSERT_TRUE(sink().GetFirstResourceCallMatching(
PpapiHostMsg_FileIO_Open::ID, ¶ms, &msg));
sink().ClearMessages();
ResourceMessageReplyParams reply_params(params.pp_resource(),
params.sequence());
reply_params.set_result(PP_OK);
PluginMessageFilter::DispatchResourceReplyForTest(
reply_params,
PpapiPluginMsg_FileIO_OpenReply(file_system,
0 ));
}
};
}
TEST_F(FileSystemResourceTest, OpenFailure) {
const PPB_FileSystem_1_0* fs_iface = thunk::GetPPB_FileSystem_1_0_Thunk();
{
LockingResourceReleaser file_system(
fs_iface->Create(pp_instance(), PP_FILESYSTEMTYPE_LOCALTEMPORARY));
MockCompletionCallback cb;
int32_t result = thunk::GetPPB_FileSystem_1_0_Thunk()->Open(
file_system.get(),
kExpectedFileSystemSize,
PP_MakeCompletionCallback(&MockCompletionCallback::Callback, &cb));
ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
ResourceMessageTestSink::ResourceCallVector open_messages =
sink().GetAllResourceCallsMatching(PpapiHostMsg_FileSystem_Open::ID);
ASSERT_EQ(2U, open_messages.size());
sink().ClearMessages();
SendOpenReply(open_messages[0].first, PP_ERROR_FAILED);
SendOpenReply(open_messages[1].first, PP_OK);
ASSERT_TRUE(cb.called());
ASSERT_EQ(PP_ERROR_FAILED, cb.result());
}
{
LockingResourceReleaser file_system(
fs_iface->Create(pp_instance(), PP_FILESYSTEMTYPE_LOCALTEMPORARY));
MockCompletionCallback cb;
int32_t result = thunk::GetPPB_FileSystem_1_0_Thunk()->Open(
file_system.get(),
kExpectedFileSystemSize,
PP_MakeCompletionCallback(&MockCompletionCallback::Callback, &cb));
ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
ResourceMessageTestSink::ResourceCallVector open_messages =
sink().GetAllResourceCallsMatching(PpapiHostMsg_FileSystem_Open::ID);
ASSERT_EQ(2U, open_messages.size());
sink().ClearMessages();
SendOpenReply(open_messages[0].first, PP_OK);
SendOpenReply(open_messages[1].first, PP_ERROR_FAILED);
ASSERT_TRUE(cb.called());
ASSERT_EQ(PP_ERROR_FAILED, cb.result());
}
}
TEST_F(FileSystemResourceTest, RequestQuota) {
const PPB_FileSystem_1_0* fs_iface = thunk::GetPPB_FileSystem_1_0_Thunk();
const PPB_FileRef_1_1* fref_iface = thunk::GetPPB_FileRef_1_1_Thunk();
const PPB_FileIO_1_1* fio_iface = thunk::GetPPB_FileIO_1_1_Thunk();
LockingResourceReleaser file_system(
fs_iface->Create(pp_instance(), PP_FILESYSTEMTYPE_LOCALTEMPORARY));
OpenFileSystem(file_system.get());
LockingResourceReleaser file_ref1(
fref_iface->Create(file_system.get(), "/file1"));
LockingResourceReleaser file_io1(fio_iface->Create(pp_instance()));
OpenFile(file_io1.get(), file_ref1.get(), file_system.get());
LockingResourceReleaser file_ref2(
fref_iface->Create(file_system.get(), "/file2"));
LockingResourceReleaser file_io2(fio_iface->Create(pp_instance()));
OpenFile(file_io2.get(), file_ref2.get(), file_system.get());
EnterResource<PPB_FileSystem_API> enter(file_system.get(), true);
ASSERT_FALSE(enter.failed());
PPB_FileSystem_API* file_system_api = enter.object();
MockRequestQuotaCallback cb1;
int64_t result = file_system_api->RequestQuota(
kQuotaRequestAmount1,
base::Bind(&MockRequestQuotaCallback::Callback, base::Unretained(&cb1)));
ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
ResourceMessageCallParams params;
IPC::Message msg;
ASSERT_TRUE(sink().GetFirstResourceCallMatching(
PpapiHostMsg_FileSystem_ReserveQuota::ID, ¶ms, &msg));
sink().ClearMessages();
int64_t amount = 0;
FileGrowthMap file_growths;
ASSERT_TRUE(UnpackMessage<PpapiHostMsg_FileSystem_ReserveQuota>(
msg, &amount, &file_growths));
ASSERT_EQ(kQuotaRequestAmount1, amount);
ASSERT_EQ(2U, file_growths.size());
ASSERT_EQ(0, file_growths[file_io1.get()].max_written_offset);
ASSERT_EQ(0, file_growths[file_io2.get()].max_written_offset);
MockRequestQuotaCallback cb2;
result = file_system_api->RequestQuota(
kQuotaRequestAmount2,
base::Bind(&MockRequestQuotaCallback::Callback, base::Unretained(&cb2)));
ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
ASSERT_FALSE(sink().GetFirstResourceCallMatching(
PpapiHostMsg_FileSystem_ReserveQuota::ID, ¶ms, &msg));
{
ProxyAutoUnlock unlock_to_prevent_deadlock;
SendReply(params,
PP_OK,
PpapiPluginMsg_FileSystem_ReserveQuotaReply(
kQuotaRequestAmount1 + kQuotaRequestAmount2,
FileGrowthMapToFileSizeMapForTesting(file_growths)));
}
ASSERT_TRUE(cb1.called());
ASSERT_EQ(kQuotaRequestAmount1, cb1.result());
ASSERT_TRUE(cb2.called());
ASSERT_EQ(kQuotaRequestAmount2, cb2.result());
cb1.Reset();
cb2.Reset();
result = file_system_api->RequestQuota(
kQuotaRequestAmount1,
base::Bind(&MockRequestQuotaCallback::Callback, base::Unretained(&cb1)));
ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
result = file_system_api->RequestQuota(
kQuotaRequestAmount2,
base::Bind(&MockRequestQuotaCallback::Callback, base::Unretained(&cb2)));
ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
ASSERT_TRUE(sink().GetFirstResourceCallMatching(
PpapiHostMsg_FileSystem_ReserveQuota::ID, ¶ms, &msg));
sink().ClearMessages();
{
ProxyAutoUnlock unlock_to_prevent_deadlock;
SendReply(params,
PP_OK,
PpapiPluginMsg_FileSystem_ReserveQuotaReply(
kQuotaRequestAmount1 - 1,
FileGrowthMapToFileSizeMapForTesting(file_growths)));
}
ASSERT_TRUE(cb1.called());
ASSERT_EQ(0, cb1.result());
ASSERT_TRUE(cb2.called());
ASSERT_EQ(0, cb2.result());
cb1.Reset();
cb2.Reset();
result = file_system_api->RequestQuota(
kQuotaRequestAmount1,
base::Bind(&MockRequestQuotaCallback::Callback, base::Unretained(&cb1)));
ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
result = file_system_api->RequestQuota(
kQuotaRequestAmount2,
base::Bind(&MockRequestQuotaCallback::Callback, base::Unretained(&cb2)));
ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
ASSERT_TRUE(sink().GetFirstResourceCallMatching(
PpapiHostMsg_FileSystem_ReserveQuota::ID, ¶ms, &msg));
sink().ClearMessages();
{
ProxyAutoUnlock unlock_to_prevent_deadlock;
SendReply(params,
PP_OK,
PpapiPluginMsg_FileSystem_ReserveQuotaReply(
kQuotaRequestAmount1,
FileGrowthMapToFileSizeMapForTesting(file_growths)));
}
ASSERT_TRUE(cb1.called());
ASSERT_EQ(kQuotaRequestAmount1, cb1.result());
ASSERT_FALSE(cb2.called());
ASSERT_TRUE(sink().GetFirstResourceCallMatching(
PpapiHostMsg_FileSystem_ReserveQuota::ID, ¶ms, &msg));
sink().ClearMessages();
{
ProxyAutoUnlock unlock_to_prevent_deadlock;
SendReply(params,
PP_OK,
PpapiPluginMsg_FileSystem_ReserveQuotaReply(
kQuotaRequestAmount1 + kQuotaRequestAmount2,
FileGrowthMapToFileSizeMapForTesting(file_growths)));
}
ASSERT_TRUE(cb2.called());
ASSERT_EQ(kQuotaRequestAmount2, cb2.result());
cb1.Reset();
cb2.Reset();
result = file_system_api->RequestQuota(
kQuotaRequestAmount1,
base::Bind(&MockRequestQuotaCallback::Callback, base::Unretained(&cb1)));
ASSERT_EQ(kQuotaRequestAmount1, result);
}
}
}