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

Unified Diff: ppapi/shared_impl/thread_aware_callback_unittest.cc

Issue 11859015: Pepper: Introduce ThreadAwareCallback. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sync and update tests accordingly. Created 7 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 side-by-side diff with in-line comments
Download patch
Index: ppapi/shared_impl/thread_aware_callback_unittest.cc
diff --git a/ppapi/shared_impl/thread_aware_callback_unittest.cc b/ppapi/shared_impl/thread_aware_callback_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f907ab2067d1c8327fd00a3ad704407b84156ccb
--- /dev/null
+++ b/ppapi/shared_impl/thread_aware_callback_unittest.cc
@@ -0,0 +1,199 @@
+// Copyright (c) 2012 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/shared_impl/thread_aware_callback.h"
+
+#include "base/bind_helpers.h"
+#include "base/compiler_specific.h"
+#include "base/logging.h"
+#include "base/memory/scoped_ptr.h"
+#include "ppapi/c/pp_errors.h"
+#include "ppapi/proxy/ppapi_proxy_test.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace ppapi {
+
+namespace {
+
+class TestParameter {
+ public:
+ int value_;
+};
+
+int called_num = 0;
+
+void TestCallback_0() {
+ ++called_num;
+}
+
+void TestCallback_1(int p1) {
+ ++called_num;
+}
+
+void TestCallback_2(int p1, const double* p2) {
+ ++called_num;
+}
+
+void TestCallback_3(int p1, const double* p2, bool* p3) {
+ ++called_num;
+}
+
+void TestCallback_4(int p1, const double* p2, bool* p3, TestParameter p4) {
+ ++called_num;
+}
+
+void TestCallback_5(int p1,
+ const double* p2,
+ bool* p3,
+ TestParameter p4,
+ const TestParameter& p5) {
+ ++called_num;
+}
+
+typedef proxy::PluginProxyTest ThreadAwareCallbackTest;
+
+class ThreadAwareCallbackMultiThreadTest
dmichael (off chromium) 2013/01/15 22:43:52 A quick note for each test about what the test is
yzshen1 2013/01/16 18:55:59 Done.
+ : public proxy::PluginProxyMultiThreadTest {
+ public:
+ ThreadAwareCallbackMultiThreadTest() : main_thread_callback_called_(false) {
+ }
+ virtual ~ThreadAwareCallbackMultiThreadTest() {
+ CHECK(main_thread_callback_called_);
+ }
+
+ // proxy::PluginProxyMultiThreadTest implementation.
+ virtual void SetUpTestOnMainThread() OVERRIDE {
+ ProxyAutoLock auto_lock;
+
+ main_thread_callback_.reset(
+ new ThreadAwareCallback<CallbackFunc>(&MainThreadCallbackBody));
+ }
+
+ virtual void SetUpTestOnSecondaryThread() OVERRIDE {
+ {
+ ProxyAutoLock auto_lock;
+ main_thread_callback_->RunOnTargetThread(this);
+ }
+
+ PostQuitForSecondaryThread();
+ PostQuitForMainThread();
+ }
+
+ private:
+ typedef void (*CallbackFunc)(ThreadAwareCallbackMultiThreadTest*);
+
+ static void MainThreadCallbackBody(ThreadAwareCallbackMultiThreadTest* thiz) {
+ thiz->CheckOnValidThread(true);
+ thiz->main_thread_callback_called_ = true;
+
+ {
+ ProxyAutoLock auto_lock;
+ // We have to destroy it prior to the PluginGlobals instance held by the
+ // base class. Otherwise it has a ref to Pepper message loop for the main
+ // thread and the PluginGlobals destructor will complain.
+ thiz->main_thread_callback_.reset(NULL);
+ }
+ }
+
+ scoped_ptr<ThreadAwareCallback<CallbackFunc> > main_thread_callback_;
+ bool main_thread_callback_called_;
+};
+
+class ThreadAwareCallbackAbortTest : public proxy::PluginProxyMultiThreadTest {
+ public:
+ ThreadAwareCallbackAbortTest() {
+ }
+ virtual ~ThreadAwareCallbackAbortTest() {
+ }
+
+ // proxy::PluginProxyMultiThreadTest implementation.
+ virtual void SetUpTestOnMainThread() OVERRIDE {
+ ProxyAutoLock auto_lock;
+
+ main_thread_callback_.reset(
+ new ThreadAwareCallback<CallbackFunc>(&MainThreadCallbackBody));
+ }
+
+ virtual void SetUpTestOnSecondaryThread() OVERRIDE {
+ {
+ ProxyAutoLock auto_lock;
+ main_thread_message_loop_proxy_->PostTask(
+ FROM_HERE,
+ base::Bind(&ThreadAwareCallbackAbortTest::DeleteCallback,
+ base::Unretained(this)));
+ // |main_thread_callback_| is still valid, even if DeleteCallback() is
+ // possible to be called before this following statement. That is because
dmichael (off chromium) 2013/01/15 22:43:52 s/is possible to be/can
yzshen1 2013/01/16 18:55:59 Done.
+ // |auto_lock| is still held by this method, which prevents
+ // DeleteCallback() from deleting the callback.
+ main_thread_callback_->RunOnTargetThread(this);
+ }
+
+ PostQuitForSecondaryThread();
+ PostQuitForMainThread();
+ }
+
+ private:
+ typedef void (*CallbackFunc)(ThreadAwareCallbackAbortTest*);
+
+ static void MainThreadCallbackBody(ThreadAwareCallbackAbortTest* thiz) {
+ // The callback should not be called.
+ ASSERT_TRUE(false);
+ }
+
+ void DeleteCallback() {
+ ProxyAutoLock auto_lock;
+ main_thread_callback_.reset(NULL);
+ }
+
+ scoped_ptr<ThreadAwareCallback<CallbackFunc> > main_thread_callback_;
+};
+
+} // namespace
+
+TEST_F(ThreadAwareCallbackTest, Basics) {
+ // ThreadAwareCallback should only be used when the proxy lock has been
+ // acquired.
+ ProxyAutoLock auto_lock;
+
+ double double_arg = 0.0;
+ bool bool_arg = false;
+ TestParameter object_arg;
+
+ // Exercise all the template code.
+ called_num = 0;
+ ThreadAwareCallback<void (*)()> callback_0(TestCallback_0);
+ callback_0.RunOnTargetThread();
+
+ ThreadAwareCallback<void (*)(int)> callback_1(TestCallback_1);
+ callback_1.RunOnTargetThread(1);
+
+ ThreadAwareCallback<void (*)(int, const double*)> callback_2(TestCallback_2);
+ callback_2.RunOnTargetThread(1, &double_arg);
+
+ ThreadAwareCallback<void (*)(int, const double*, bool*)>
+ callback_3(TestCallback_3);
+ callback_3.RunOnTargetThread(1, &double_arg, &bool_arg);
+
+ ThreadAwareCallback<void (*)(int, const double*, bool*, TestParameter)>
+ callback_4(TestCallback_4);
+ callback_4.RunOnTargetThread(1, &double_arg, &bool_arg, object_arg);
+
+ ThreadAwareCallback<void (*)(int, const double*, bool*, TestParameter,
+ const TestParameter&)>
+ callback_5(TestCallback_5);
+ callback_5.RunOnTargetThread(1, &double_arg, &bool_arg, object_arg,
+ object_arg);
+
+ EXPECT_EQ(6, called_num);
+}
+
+TEST_F(ThreadAwareCallbackMultiThreadTest, RunOnTargetThread) {
+ RunTest();
+}
+
+TEST_F(ThreadAwareCallbackAbortTest, NotRunIfAborted) {
+ RunTest();
+}
+
+} // namespace ppapi

Powered by Google App Engine
This is Rietveld 408576698