This source file includes following definitions.
- EmptyCompletionCallback
- InvokeCallOnMainThread
- ThreadStressCompletionCallback
- InvokeCallOnMainThreadStress
- InvokeIsMainThread
- TestGetTime
- TestGetTimeTicks
- TestCallOnMainThread_FromMainThread
- TestCallOnMainThread_FromMainThreadDelayed
- TestCallOnMainThread_FromNonMainThread
- TestCallOnMainThread_FromNonMainThreadStress
- TestIsMainThread_FromMainThread
- TestIsMainThread_FromNonMainThread
- TestAddRefAndReleaseResource
- TestAddRefAndReleaseInvalidResource
- SetupTests
- SetupPluginInterfaces
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "native_client/src/shared/platform/nacl_check.h"
#include "ppapi/native_client/tests/ppapi_test_lib/get_browser_interface.h"
#include "ppapi/native_client/tests/ppapi_test_lib/test_interface.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/ppb_core.h"
#include "ppapi/c/ppb_url_request_info.h"
namespace {
const int32_t kNotPPError = 12345;
const int kStressChecksum = 0x12345678;
void EmptyCompletionCallback(void* data, int32_t ) {
CHECK(data == NULL);
}
void* InvokeCallOnMainThread(void* thread_argument) {
PPB_Core* ppb_core = reinterpret_cast<PPB_Core*>(thread_argument);
PP_CompletionCallback callback = MakeTestableCompletionCallback(
"CallOnMainThreadCallback_FromNonMainThread",
EmptyCompletionCallback,
NULL );
ppb_core->CallOnMainThread(0 , callback, PP_OK);
return NULL;
}
struct StressData {
const PPB_Core* ppb_core_;
const int callbacks_per_thread_;
int callback_counter_;
const int checksum_;
StressData(const PPB_Core* ppb_core, int callbacks_per_thread, int total)
: ppb_core_(ppb_core),
callbacks_per_thread_(callbacks_per_thread),
callback_counter_(total),
checksum_(kStressChecksum) {
}
};
void ThreadStressCompletionCallback(void* data, int32_t result) {
if (PP_OK == result) {
StressData* stress = reinterpret_cast<StressData*>(data);
CHECK(kStressChecksum == stress->checksum_);
CHECK(NULL != stress->ppb_core_);
stress->callback_counter_ -= 1;
if (0 == stress->callback_counter_) {
PP_CompletionCallback callback = MakeTestableCompletionCallback(
"CallOnMainThreadCallback_ThreadStress",
EmptyCompletionCallback, NULL);
stress->ppb_core_->CallOnMainThread(0, callback, PP_OK);
memset(stress, 0, sizeof(*stress));
}
}
}
void* InvokeCallOnMainThreadStress(void* thread_argument) {
StressData* stress = reinterpret_cast<StressData*>(thread_argument);
PP_CompletionCallback callback = PP_MakeCompletionCallback(
ThreadStressCompletionCallback, stress);
for (int i = 0; i < stress->callbacks_per_thread_; ++i) {
CHECK(NULL != stress->ppb_core_);
CHECK(kStressChecksum == stress->checksum_);
stress->ppb_core_->CallOnMainThread(0, callback, PP_OK);
}
return NULL;
}
void* InvokeIsMainThread(void* thread_argument) {
PPB_Core* ppb_core = reinterpret_cast<PPB_Core*>(thread_argument);
return reinterpret_cast<void*>(ppb_core->IsMainThread());
}
void TestGetTime() {
PP_Time time1 = PPBCore()->GetTime();
EXPECT(time1 > 0);
usleep(100000);
PP_Time time2 = PPBCore()->GetTime();
EXPECT(time2 > time1);
TEST_PASSED;
}
void TestGetTimeTicks() {
PP_TimeTicks time_ticks1 = PPBCore()->GetTimeTicks();
EXPECT(time_ticks1 > 0);
usleep(100000);
PP_TimeTicks time_ticks2 = PPBCore()->GetTimeTicks();
EXPECT(time_ticks2 > time_ticks1);
TEST_PASSED;
}
void TestCallOnMainThread_FromMainThread() {
PP_CompletionCallback callback = MakeTestableCompletionCallback(
"CallOnMainThreadCallback_FromMainThread",
EmptyCompletionCallback,
NULL );
PPBCore()->CallOnMainThread(0 , callback, kNotPPError);
TEST_PASSED;
}
void TestCallOnMainThread_FromMainThreadDelayed() {
PP_CompletionCallback callback = MakeTestableCompletionCallback(
"CallOnMainThreadCallback_FromMainThreadDelayed",
EmptyCompletionCallback,
NULL );
PPBCore()->CallOnMainThread(1000 , callback, kNotPPError);
TEST_PASSED;
}
void TestCallOnMainThread_FromNonMainThread() {
pthread_t tid;
void* ppb_core = reinterpret_cast<void*>(const_cast<PPB_Core*>(PPBCore()));
CHECK(pthread_create(&tid, NULL, InvokeCallOnMainThread, ppb_core) == 0);
CHECK(pthread_detach(tid) == 0);
TEST_PASSED;
}
void TestCallOnMainThread_FromNonMainThreadStress() {
const int kNumThreads = 10;
const int kNumPerThread = 100;
const int kNumCallbacks = kNumThreads * kNumPerThread;
StressData* stress = new StressData(PPBCore(), kNumPerThread, kNumCallbacks);
for (int i = 0; i < kNumThreads; ++i) {
pthread_t tid;
CHECK(pthread_create(
&tid, NULL, InvokeCallOnMainThreadStress, stress) == 0);
CHECK(pthread_detach(tid) == 0);
}
TEST_PASSED;
}
void TestIsMainThread_FromMainThread() {
EXPECT(PPBCore()->IsMainThread() == PP_TRUE);
TEST_PASSED;
}
void TestIsMainThread_FromNonMainThread() {
pthread_t tid;
void* thread_result;
void* ppb_core = reinterpret_cast<void*>(const_cast<PPB_Core*>(PPBCore()));
CHECK(pthread_create(&tid, NULL, InvokeIsMainThread, ppb_core) == 0);
CHECK(pthread_join(tid, &thread_result) == 0);
EXPECT(reinterpret_cast<int>(thread_result) == PP_FALSE);
TEST_PASSED;
}
void TestAddRefAndReleaseResource() {
PP_Resource valid_resource = PPBURLRequestInfo()->Create(pp_instance());
EXPECT(valid_resource != kInvalidResource);
EXPECT(PPBURLRequestInfo()->IsURLRequestInfo(valid_resource) == PP_TRUE);
for (size_t j = 0; j < 100; ++j) PPBCore()->AddRefResource(valid_resource);
EXPECT(PPBURLRequestInfo()->IsURLRequestInfo(valid_resource) == PP_TRUE);
for (size_t j = 0; j < 100; ++j) PPBCore()->ReleaseResource(valid_resource);
EXPECT(PPBURLRequestInfo()->IsURLRequestInfo(valid_resource) == PP_TRUE);
PPBCore()->ReleaseResource(valid_resource);
EXPECT(PPBURLRequestInfo()->IsURLRequestInfo(valid_resource) != PP_TRUE);
TEST_PASSED;
}
void TestAddRefAndReleaseInvalidResource() {
for (size_t j = 0; j < 100; ++j) {
PPBCore()->AddRefResource(kInvalidResource);
PPBCore()->ReleaseResource(kInvalidResource);
}
TEST_PASSED;
}
}
void SetupTests() {
RegisterTest("TestGetTime", TestGetTime);
RegisterTest("TestGetTimeTicks", TestGetTimeTicks);
RegisterTest("TestIsMainThread_FromMainThread",
TestIsMainThread_FromMainThread);
RegisterTest("TestIsMainThread_FromNonMainThread",
TestIsMainThread_FromNonMainThread);
RegisterTest("TestAddRefAndReleaseResource",
TestAddRefAndReleaseResource);
RegisterTest("TestAddRefAndReleaseInvalidResource",
TestAddRefAndReleaseInvalidResource);
RegisterTest("TestCallOnMainThread_FromMainThread",
TestCallOnMainThread_FromMainThread);
RegisterTest("TestCallOnMainThread_FromMainThreadDelayed",
TestCallOnMainThread_FromMainThreadDelayed);
RegisterTest("TestCallOnMainThread_FromNonMainThread",
TestCallOnMainThread_FromNonMainThread);
RegisterTest("TestCallOnMainThread_FromNonMainThreadStress",
TestCallOnMainThread_FromNonMainThreadStress);
}
void SetupPluginInterfaces() {
}