blob: fb5f7625ab3927e8006ef27a7f7bd4541a9e086a [file] [log] [blame]
// Copyright (c) 2011 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_utils.h"
#include <stdio.h>
#include "ppapi/c/pp_errors.h"
#include "ppapi/cpp/module.h"
const PPB_Testing_Dev* GetTestingInterface() {
static const PPB_Testing_Dev* g_testing_interface =
reinterpret_cast<PPB_Testing_Dev const*>(
pp::Module::Get()->GetBrowserInterface(PPB_TESTING_DEV_INTERFACE));
return g_testing_interface;
}
std::string ReportError(const char* method, int32_t error) {
char error_as_string[12];
sprintf(error_as_string, "%d", static_cast<int>(error));
std::string result = method + std::string(" failed with error: ") +
error_as_string;
return result;
}
TestCompletionCallback::TestCompletionCallback(PP_Instance instance)
: have_result_(false),
result_(PP_OK_COMPLETIONPENDING),
force_async_(false),
post_quit_task_(false),
run_count_(0),
instance_(instance) {
}
TestCompletionCallback::TestCompletionCallback(PP_Instance instance,
bool force_async)
: have_result_(false),
result_(PP_OK_COMPLETIONPENDING),
force_async_(force_async),
post_quit_task_(false),
run_count_(0),
instance_(instance) {
}
int32_t TestCompletionCallback::WaitForResult() {
if (!have_result_) {
result_ = PP_OK_COMPLETIONPENDING; // Reset
post_quit_task_ = true;
GetTestingInterface()->RunMessageLoop(instance_);
}
have_result_ = false;
return result_;
}
TestCompletionCallback::operator pp::CompletionCallback() const {
int32_t flags = (force_async_ ? 0 : PP_COMPLETIONCALLBACK_FLAG_OPTIONAL);
return pp::CompletionCallback(&TestCompletionCallback::Handler,
const_cast<TestCompletionCallback*>(this),
flags);
}
// static
void TestCompletionCallback::Handler(void* user_data, int32_t result) {
TestCompletionCallback* callback =
static_cast<TestCompletionCallback*>(user_data);
callback->result_ = result;
callback->have_result_ = true;
callback->run_count_++;
if (callback->post_quit_task_) {
callback->post_quit_task_ = false;
GetTestingInterface()->QuitMessageLoop(callback->instance_);
}
}