This source file includes following definitions.
- ReadCallback
- HandleDocumentLoad
- TestHandleDocumentLoad
- TestIsFullFrame
- SetupTests
- SetupPluginInterfaces
#include <string.h>
#include "native_client/src/include/nacl_macros.h"
#include "native_client/src/shared/platform/nacl_check.h"
#include "ppapi/c/pp_bool.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/pp_input_event.h"
#include "ppapi/c/ppb_instance.h"
#include "ppapi/c/ppb_url_loader.h"
#include "ppapi/c/ppp_instance.h"
#include "ppapi/native_client/tests/ppapi_test_lib/get_browser_interface.h"
#include "ppapi/native_client/tests/ppapi_test_lib/test_interface.h"
namespace {
const int kDocLoadErrorNone = 0;
const int kDocLoadErrorInit = -1;
int error_in_doc_load_line = kDocLoadErrorInit;
#define EXPECT_ON_LOAD(condition) \
do { \
if (!(condition)) { \
if (kDocLoadErrorInit == error_in_doc_load_line) { \
error_in_doc_load_line = __LINE__; \
} \
} \
} while (0);
#define ON_LOAD_PASSED \
do { \
if (kDocLoadErrorInit == error_in_doc_load_line) \
error_in_doc_load_line = kDocLoadErrorNone; \
} while (0);
const uint32_t kMaxFileSize = 1024;
char buffer[kMaxFileSize];
uint32_t buffer_pos = 0;
const char kKnownFileContents[] =
"This is just a test file so we can verify HandleDocumentLoad.";
void ReadCallback(void* user_data, int32_t pp_error_or_bytes) {
PP_Resource url_loader = reinterpret_cast<PP_Resource>(user_data);
EXPECT_ON_LOAD(pp_error_or_bytes >= PP_OK);
if (pp_error_or_bytes < PP_OK) {
PPBCore()->ReleaseResource(url_loader);
return;
}
if (PP_OK == pp_error_or_bytes) {
int diff = strncmp(buffer,
kKnownFileContents,
strlen(kKnownFileContents));
EXPECT_ON_LOAD(diff == 0);
PPBURLLoader()->Close(url_loader);
PPBCore()->ReleaseResource(url_loader);
ON_LOAD_PASSED;
} else {
buffer_pos += pp_error_or_bytes;
PP_CompletionCallback callback =
PP_MakeCompletionCallback(ReadCallback, user_data);
pp_error_or_bytes =
PPBURLLoader()->ReadResponseBody(url_loader,
buffer + buffer_pos,
kMaxFileSize - buffer_pos,
callback);
EXPECT(pp_error_or_bytes == PP_OK_COMPLETIONPENDING);
}
}
PP_Bool HandleDocumentLoad(PP_Instance instance,
PP_Resource url_loader) {
PPBCore()->AddRefResource(url_loader);
void* user_data = reinterpret_cast<void*>(url_loader);
PP_CompletionCallback callback =
PP_MakeCompletionCallback(ReadCallback, user_data);
int32_t pp_error_or_bytes = PPBURLLoader()->ReadResponseBody(url_loader,
buffer,
kMaxFileSize,
callback);
EXPECT(pp_error_or_bytes == PP_OK_COMPLETIONPENDING);
return PP_TRUE;
}
const PPP_Instance ppp_instance_interface = {
DidCreateDefault,
DidDestroyDefault,
DidChangeViewDefault,
DidChangeFocusDefault,
HandleDocumentLoad
};
void TestHandleDocumentLoad() {
if (error_in_doc_load_line != kDocLoadErrorNone) {
char error[1024];
snprintf(error, sizeof(error),
"ERROR at %s:%d: Document Load Failed\n",
__FILE__, error_in_doc_load_line);
fprintf(stderr, "%s", error);
PostTestMessage(__FUNCTION__, error);
}
TEST_PASSED;
}
void TestIsFullFrame() {
PP_Bool full_frame = PPBInstance()->IsFullFrame(pp_instance());
EXPECT(full_frame == PP_TRUE);
TEST_PASSED;
}
}
void SetupTests() {
RegisterTest("TestHandleDocumentLoad", TestHandleDocumentLoad);
RegisterTest("TestIsFullFrame", TestIsFullFrame);
}
void SetupPluginInterfaces() {
RegisterPluginInterface(PPP_INSTANCE_INTERFACE, &ppp_instance_interface);
}