1 // Copyright (c) 2013 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.
8 #include "base/callback.h"
9 #include "base/compiler_specific.h"
10 #include "base/logging.h"
11 #include "base/memory/ref_counted.h"
12 #include "ppapi/shared_impl/proxy_lock.h"
13 #include "ppapi/shared_impl/test_globals.h"
14 #include "testing/gtest/include/gtest/gtest.h"
20 bool expect_to_be_locked
= false;
21 void CheckLockState() {
22 if (expect_to_be_locked
) {
23 ProxyLock::AssertAcquired();
25 // If we expect to be unlocked, try to lock. We rely on the checking inside
26 // base::Lock that prevents recursive locking.
33 class CheckLockStateInDestructor
34 : public base::RefCounted
<CheckLockStateInDestructor
> {
36 CheckLockStateInDestructor() {}
37 void Method() { ++called_num
; }
40 friend class base::RefCounted
<CheckLockStateInDestructor
>;
41 ~CheckLockStateInDestructor() { CheckLockState(); }
42 DISALLOW_COPY_AND_ASSIGN(CheckLockStateInDestructor
);
45 void TestCallback_0() {
50 void TestCallback_1(int p1
) {
55 void TestCallback_2(int p1
, const std::string
& p2
) {
61 void TestCallback_3(int p1
, const std::string
& p2
, Param p3
) {
68 TEST(PpapiProxyLockTest
, Locking
) {
70 expect_to_be_locked
= true;
72 base::Callback
<void()> cb0
;
75 cb0
= RunWhileLocked(base::Bind(TestCallback_0
));
78 ASSERT_EQ(1, called_num
);
83 cb0
= RunWhileLocked(base::Bind(TestCallback_1
, 123));
86 ASSERT_EQ(1, called_num
);
91 scoped_refptr
<CheckLockStateInDestructor
> object
=
92 new CheckLockStateInDestructor();
94 RunWhileLocked(base::Bind(&CheckLockStateInDestructor::Method
, object
));
95 // Note after this scope, the Callback owns the only reference.
98 ASSERT_EQ(1, called_num
);
101 base::Callback
<void(int)> cb1
;
104 cb1
= RunWhileLocked(base::Bind(TestCallback_1
));
107 ASSERT_EQ(1, called_num
);
110 base::Callback
<void(int, const std::string
&)> cb2
;
113 cb2
= RunWhileLocked(base::Bind(TestCallback_2
));
115 cb2
.Run(123, std::string("yo"));
116 ASSERT_EQ(1, called_num
);
119 base::Callback
<void(int, const std::string
&, Param
)> cb3
;
122 cb3
= RunWhileLocked(base::Bind(TestCallback_3
));
124 cb3
.Run(123, std::string("yo"), Param());
125 ASSERT_EQ(1, called_num
);
128 base::Callback
<void(const std::string
&)> cb1_string
;
131 cb1_string
= RunWhileLocked(base::Bind(TestCallback_2
, 123));
133 cb1_string
.Run(std::string("yo"));
134 ASSERT_EQ(1, called_num
);
139 cb0
= RunWhileLocked(base::Bind(TestCallback_2
, 123, std::string("yo")));
142 ASSERT_EQ(1, called_num
);
146 TEST(PpapiProxyLockTest
, Unlocking
) {
148 expect_to_be_locked
= false;
149 // These calls should all try to _unlock_, so we must be locked before
151 ProxyAutoLock auto_lock
;
154 CallWhileUnlocked(TestCallback_0
);
155 ASSERT_EQ(1, called_num
);
159 CallWhileUnlocked(TestCallback_1
, 123);
160 ASSERT_EQ(1, called_num
);
164 // TODO(dmichael): Make const-ref arguments work properly with type
166 CallWhileUnlocked
<void, int, const std::string
&>(
167 TestCallback_2
, 123, std::string("yo"));
168 ASSERT_EQ(1, called_num
);
172 base::Callback
<void()> callback(base::Bind(TestCallback_0
));
173 CallWhileUnlocked(callback
);
174 ASSERT_EQ(1, called_num
);