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 "base/message_loop/message_loop.h"
13 #include "ppapi/shared_impl/proxy_lock.h"
14 #include "ppapi/shared_impl/test_globals.h"
15 #include "testing/gtest/include/gtest/gtest.h"
21 bool expect_to_be_locked
= false;
22 void CheckLockState() {
23 if (expect_to_be_locked
) {
24 ProxyLock::AssertAcquired();
26 // If we expect to be unlocked, try to lock. We rely on the checking inside
27 // base::Lock that prevents recursive locking.
34 class CheckLockStateInDestructor
35 : public base::RefCounted
<CheckLockStateInDestructor
> {
37 CheckLockStateInDestructor() {}
38 void Method() { ++called_num
; }
41 friend class base::RefCounted
<CheckLockStateInDestructor
>;
42 ~CheckLockStateInDestructor() { CheckLockState(); }
43 DISALLOW_COPY_AND_ASSIGN(CheckLockStateInDestructor
);
46 void TestCallback_0() {
51 void TestCallback_1(int p1
) {
56 void TestCallback_2(int p1
, const std::string
& p2
) {
62 void TestCallback_3(int p1
, const std::string
& p2
, Param p3
) {
69 class PpapiProxyLockTest
: public testing::Test
{
70 base::MessageLoop message_loop_
; // Required to receive callbacks.
73 TEST_F(PpapiProxyLockTest
, Locking
) {
75 expect_to_be_locked
= true;
77 base::Callback
<void()> cb0
;
80 cb0
= RunWhileLocked(base::Bind(TestCallback_0
));
83 ASSERT_EQ(1, called_num
);
88 cb0
= RunWhileLocked(base::Bind(TestCallback_1
, 123));
91 ASSERT_EQ(1, called_num
);
96 scoped_refptr
<CheckLockStateInDestructor
> object
=
97 new CheckLockStateInDestructor();
99 RunWhileLocked(base::Bind(&CheckLockStateInDestructor::Method
, object
));
100 // Note after this scope, the Callback owns the only reference.
103 ASSERT_EQ(1, called_num
);
106 base::Callback
<void(int)> cb1
;
109 cb1
= RunWhileLocked(base::Bind(TestCallback_1
));
112 ASSERT_EQ(1, called_num
);
115 base::Callback
<void(int, const std::string
&)> cb2
;
118 cb2
= RunWhileLocked(base::Bind(TestCallback_2
));
120 cb2
.Run(123, std::string("yo"));
121 ASSERT_EQ(1, called_num
);
124 base::Callback
<void(int, const std::string
&, Param
)> cb3
;
127 cb3
= RunWhileLocked(base::Bind(TestCallback_3
));
129 cb3
.Run(123, std::string("yo"), Param());
130 ASSERT_EQ(1, called_num
);
133 base::Callback
<void(const std::string
&)> cb1_string
;
136 cb1_string
= RunWhileLocked(base::Bind(TestCallback_2
, 123));
138 cb1_string
.Run(std::string("yo"));
139 ASSERT_EQ(1, called_num
);
144 cb0
= RunWhileLocked(base::Bind(TestCallback_2
, 123, std::string("yo")));
147 ASSERT_EQ(1, called_num
);
151 TEST_F(PpapiProxyLockTest
, Unlocking
) {
153 expect_to_be_locked
= false;
154 // These calls should all try to _unlock_, so we must be locked before
156 ProxyAutoLock auto_lock
;
159 CallWhileUnlocked(TestCallback_0
);
160 ASSERT_EQ(1, called_num
);
164 CallWhileUnlocked(TestCallback_1
, 123);
165 ASSERT_EQ(1, called_num
);
169 // TODO(dmichael): Make const-ref arguments work properly with type
171 CallWhileUnlocked
<void, int, const std::string
&>(
172 TestCallback_2
, 123, std::string("yo"));
173 ASSERT_EQ(1, called_num
);
177 base::Callback
<void()> callback(base::Bind(TestCallback_0
));
178 CallWhileUnlocked(callback
);
179 ASSERT_EQ(1, called_num
);