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

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: 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 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 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.
57 : public proxy::PluginProxyMultiThreadTest {
58 public:
59 ThreadAwareCallbackMultiThreadTest() : main_thread_callback_called_(false) {
60 }
61 virtual ~ThreadAwareCallbackMultiThreadTest() {
62 CHECK(main_thread_callback_called_);
63 }
64
65 // proxy::PluginProxyMultiThreadTest implementation.
66 virtual void SetUpTestOnMainThread() OVERRIDE {
67 ProxyAutoLock auto_lock;
68
69 main_thread_callback_.reset(
70 new ThreadAwareCallback<CallbackFunc>(&MainThreadCallbackBody));
71 }
72
73 virtual void SetUpTestOnSecondaryThread() OVERRIDE {
74 {
75 ProxyAutoLock auto_lock;
76 main_thread_callback_->RunOnTargetThread(this);
77 }
78
79 PostQuitForSecondaryThread();
80 PostQuitForMainThread();
81 }
82
83 private:
84 typedef void (*CallbackFunc)(ThreadAwareCallbackMultiThreadTest*);
85
86 static void MainThreadCallbackBody(ThreadAwareCallbackMultiThreadTest* thiz) {
87 thiz->CheckOnValidThread(true);
88 thiz->main_thread_callback_called_ = true;
89
90 {
91 ProxyAutoLock auto_lock;
92 // We have to destroy it prior to the PluginGlobals instance held by the
93 // base class. Otherwise it has a ref to Pepper message loop for the main
94 // thread and the PluginGlobals destructor will complain.
95 thiz->main_thread_callback_.reset(NULL);
96 }
97 }
98
99 scoped_ptr<ThreadAwareCallback<CallbackFunc> > main_thread_callback_;
100 bool main_thread_callback_called_;
101 };
102
103 class ThreadAwareCallbackAbortTest : public proxy::PluginProxyMultiThreadTest {
104 public:
105 ThreadAwareCallbackAbortTest() {
106 }
107 virtual ~ThreadAwareCallbackAbortTest() {
108 }
109
110 // proxy::PluginProxyMultiThreadTest implementation.
111 virtual void SetUpTestOnMainThread() OVERRIDE {
112 ProxyAutoLock auto_lock;
113
114 main_thread_callback_.reset(
115 new ThreadAwareCallback<CallbackFunc>(&MainThreadCallbackBody));
116 }
117
118 virtual void SetUpTestOnSecondaryThread() OVERRIDE {
119 {
120 ProxyAutoLock auto_lock;
121 main_thread_message_loop_proxy_->PostTask(
122 FROM_HERE,
123 base::Bind(&ThreadAwareCallbackAbortTest::DeleteCallback,
124 base::Unretained(this)));
125 // |main_thread_callback_| is still valid, even if DeleteCallback() is
126 // 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.
127 // |auto_lock| is still held by this method, which prevents
128 // DeleteCallback() from deleting the callback.
129 main_thread_callback_->RunOnTargetThread(this);
130 }
131
132 PostQuitForSecondaryThread();
133 PostQuitForMainThread();
134 }
135
136 private:
137 typedef void (*CallbackFunc)(ThreadAwareCallbackAbortTest*);
138
139 static void MainThreadCallbackBody(ThreadAwareCallbackAbortTest* thiz) {
140 // The callback should not be called.
141 ASSERT_TRUE(false);
142 }
143
144 void DeleteCallback() {
145 ProxyAutoLock auto_lock;
146 main_thread_callback_.reset(NULL);
147 }
148
149 scoped_ptr<ThreadAwareCallback<CallbackFunc> > main_thread_callback_;
150 };
151
152 } // namespace
153
154 TEST_F(ThreadAwareCallbackTest, Basics) {
155 // ThreadAwareCallback should only be used when the proxy lock has been
156 // acquired.
157 ProxyAutoLock auto_lock;
158
159 double double_arg = 0.0;
160 bool bool_arg = false;
161 TestParameter object_arg;
162
163 // Exercise all the template code.
164 called_num = 0;
165 ThreadAwareCallback<void (*)()> callback_0(TestCallback_0);
166 callback_0.RunOnTargetThread();
167
168 ThreadAwareCallback<void (*)(int)> callback_1(TestCallback_1);
169 callback_1.RunOnTargetThread(1);
170
171 ThreadAwareCallback<void (*)(int, const double*)> callback_2(TestCallback_2);
172 callback_2.RunOnTargetThread(1, &double_arg);
173
174 ThreadAwareCallback<void (*)(int, const double*, bool*)>
175 callback_3(TestCallback_3);
176 callback_3.RunOnTargetThread(1, &double_arg, &bool_arg);
177
178 ThreadAwareCallback<void (*)(int, const double*, bool*, TestParameter)>
179 callback_4(TestCallback_4);
180 callback_4.RunOnTargetThread(1, &double_arg, &bool_arg, object_arg);
181
182 ThreadAwareCallback<void (*)(int, const double*, bool*, TestParameter,
183 const TestParameter&)>
184 callback_5(TestCallback_5);
185 callback_5.RunOnTargetThread(1, &double_arg, &bool_arg, object_arg,
186 object_arg);
187
188 EXPECT_EQ(6, called_num);
189 }
190
191 TEST_F(ThreadAwareCallbackMultiThreadTest, RunOnTargetThread) {
192 RunTest();
193 }
194
195 TEST_F(ThreadAwareCallbackAbortTest, NotRunIfAborted) {
196 RunTest();
197 }
198
199 } // namespace ppapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698