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

Side by Side 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: Changes in response to David's suggestions 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ppapi/shared_impl/thread_aware_callback.h"
6
7 #include "base/bind_helpers.h"
8 #include "base/compiler_specific.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "ppapi/c/pp_errors.h"
12 #include "ppapi/proxy/ppapi_proxy_test.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace ppapi {
16
17 namespace {
18
19 class TestParameter {
20 public:
21 int value_;
22 };
23
24 int called_num = 0;
25
26 void TestCallback_0() {
27 ++called_num;
28 }
29
30 void TestCallback_1(int p1) {
31 ++called_num;
32 }
33
34 void TestCallback_2(int p1, const double* p2) {
35 ++called_num;
36 }
37
38 void TestCallback_3(int p1, const double* p2, bool* p3) {
39 ++called_num;
40 }
41
42 void TestCallback_4(int p1, const double* p2, bool* p3, TestParameter p4) {
43 ++called_num;
44 }
45
46 void TestCallback_5(int p1,
47 const double* p2,
48 bool* p3,
49 TestParameter p4,
50 const TestParameter& p5) {
51 ++called_num;
52 }
53
54 typedef proxy::PluginProxyTest ThreadAwareCallbackTest;
55
56 // Test that a callback created on the main thread will run on the main thread,
57 // even when requested from a different thread.
58 class ThreadAwareCallbackMultiThreadTest
59 : public proxy::PluginProxyMultiThreadTest {
60 public:
61 ThreadAwareCallbackMultiThreadTest() : main_thread_callback_called_(false) {
62 }
63 virtual ~ThreadAwareCallbackMultiThreadTest() {
64 CHECK(main_thread_callback_called_);
65 }
66
67 // proxy::PluginProxyMultiThreadTest implementation.
68 virtual void SetUpTestOnMainThread() OVERRIDE {
69 ProxyAutoLock auto_lock;
70
71 main_thread_callback_.reset(
72 new ThreadAwareCallback<CallbackFunc>(&MainThreadCallbackBody));
73 }
74
75 virtual void SetUpTestOnSecondaryThread() OVERRIDE {
76 {
77 ProxyAutoLock auto_lock;
78 main_thread_callback_->RunOnTargetThread(this);
79 }
80
81 PostQuitForSecondaryThread();
82 PostQuitForMainThread();
83 }
84
85 private:
86 typedef void (*CallbackFunc)(ThreadAwareCallbackMultiThreadTest*);
87
88 static void MainThreadCallbackBody(ThreadAwareCallbackMultiThreadTest* thiz) {
89 thiz->CheckOnValidThread(MAIN_THREAD);
90 thiz->main_thread_callback_called_ = true;
91
92 {
93 ProxyAutoLock auto_lock;
94 // We have to destroy it prior to the PluginGlobals instance held by the
95 // base class. Otherwise it has a ref to Pepper message loop for the main
96 // thread and the PluginGlobals destructor will complain.
97 thiz->main_thread_callback_.reset(NULL);
98 }
99 }
100
101 scoped_ptr<ThreadAwareCallback<CallbackFunc> > main_thread_callback_;
102 bool main_thread_callback_called_;
103 };
104
105 // Test that when a ThreadAwareCallback instance is destroyed, pending tasks to
106 // run the callback will be ignored.
107 class ThreadAwareCallbackAbortTest : public proxy::PluginProxyMultiThreadTest {
108 public:
109 ThreadAwareCallbackAbortTest() {
110 }
111 virtual ~ThreadAwareCallbackAbortTest() {
112 }
113
114 // proxy::PluginProxyMultiThreadTest implementation.
115 virtual void SetUpTestOnMainThread() OVERRIDE {
116 ProxyAutoLock auto_lock;
117
118 main_thread_callback_.reset(
119 new ThreadAwareCallback<CallbackFunc>(&MainThreadCallbackBody));
120 }
121
122 virtual void SetUpTestOnSecondaryThread() OVERRIDE {
123 {
124 ProxyAutoLock auto_lock;
125 main_thread_message_loop_proxy_->PostTask(
126 FROM_HERE,
127 base::Bind(&ThreadAwareCallbackAbortTest::DeleteCallback,
128 base::Unretained(this)));
129 // |main_thread_callback_| is still valid, even if DeleteCallback() can be
130 // called before this following statement. That is because |auto_lock| is
131 // still held by this method, which prevents DeleteCallback() from
132 // deleting the callback.
133 main_thread_callback_->RunOnTargetThread(this);
134 }
135
136 PostQuitForSecondaryThread();
137 PostQuitForMainThread();
138 }
139
140 private:
141 typedef void (*CallbackFunc)(ThreadAwareCallbackAbortTest*);
142
143 static void MainThreadCallbackBody(ThreadAwareCallbackAbortTest* thiz) {
144 // The callback should not be called.
145 ASSERT_TRUE(false);
146 }
147
148 void DeleteCallback() {
149 ProxyAutoLock auto_lock;
150 main_thread_callback_.reset(NULL);
151 }
152
153 scoped_ptr<ThreadAwareCallback<CallbackFunc> > main_thread_callback_;
154 };
155
156 } // namespace
157
158 TEST_F(ThreadAwareCallbackTest, Basics) {
159 // ThreadAwareCallback should only be used when the proxy lock has been
160 // acquired.
161 ProxyAutoLock auto_lock;
162
163 double double_arg = 0.0;
164 bool bool_arg = false;
165 TestParameter object_arg;
166
167 // Exercise all the template code.
168 called_num = 0;
169 ThreadAwareCallback<void (*)()> callback_0(TestCallback_0);
170 callback_0.RunOnTargetThread();
171
172 ThreadAwareCallback<void (*)(int)> callback_1(TestCallback_1);
173 callback_1.RunOnTargetThread(1);
174
175 ThreadAwareCallback<void (*)(int, const double*)> callback_2(TestCallback_2);
176 callback_2.RunOnTargetThread(1, &double_arg);
177
178 ThreadAwareCallback<void (*)(int, const double*, bool*)>
179 callback_3(TestCallback_3);
180 callback_3.RunOnTargetThread(1, &double_arg, &bool_arg);
181
182 ThreadAwareCallback<void (*)(int, const double*, bool*, TestParameter)>
183 callback_4(TestCallback_4);
184 callback_4.RunOnTargetThread(1, &double_arg, &bool_arg, object_arg);
185
186 ThreadAwareCallback<void (*)(int, const double*, bool*, TestParameter,
187 const TestParameter&)>
188 callback_5(TestCallback_5);
189 callback_5.RunOnTargetThread(1, &double_arg, &bool_arg, object_arg,
190 object_arg);
191
192 EXPECT_EQ(6, called_num);
193 }
194
195 TEST_F(ThreadAwareCallbackMultiThreadTest, RunOnTargetThread) {
196 RunTest();
197 }
198
199 TEST_F(ThreadAwareCallbackAbortTest, NotRunIfAborted) {
200 RunTest();
201 }
202
203 } // namespace ppapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698