Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ppapi / shared_impl / proxy_lock_unittest.cc
blob425a00d24adf41128466365ab506f0b0787991d6
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.
5 #include <string>
7 #include "base/bind.h"
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"
17 namespace ppapi {
19 namespace {
21 bool expect_to_be_locked = false;
22 void CheckLockState() {
23 if (expect_to_be_locked) {
24 ProxyLock::AssertAcquired();
25 } else {
26 // If we expect to be unlocked, try to lock. We rely on the checking inside
27 // base::Lock that prevents recursive locking.
28 ProxyAutoLock lock;
32 int called_num = 0;
34 class CheckLockStateInDestructor
35 : public base::RefCounted<CheckLockStateInDestructor> {
36 public:
37 CheckLockStateInDestructor() {}
38 void Method() { ++called_num; }
40 private:
41 friend class base::RefCounted<CheckLockStateInDestructor>;
42 ~CheckLockStateInDestructor() { CheckLockState(); }
43 DISALLOW_COPY_AND_ASSIGN(CheckLockStateInDestructor);
46 void TestCallback_0() {
47 CheckLockState();
48 ++called_num;
51 void TestCallback_1(int p1) {
52 CheckLockState();
53 ++called_num;
56 void TestCallback_2(int p1, const std::string& p2) {
57 CheckLockState();
58 ++called_num;
61 struct Param {};
62 void TestCallback_3(int p1, const std::string& p2, Param p3) {
63 CheckLockState();
64 ++called_num;
67 } // namespace
69 class PpapiProxyLockTest : public testing::Test {
70 base::MessageLoop message_loop_; // Required to receive callbacks.
73 TEST_F(PpapiProxyLockTest, Locking) {
74 TestGlobals globals;
75 expect_to_be_locked = true;
77 base::Callback<void()> cb0;
79 ProxyAutoLock lock;
80 cb0 = RunWhileLocked(base::Bind(TestCallback_0));
82 cb0.Run();
83 ASSERT_EQ(1, called_num);
84 called_num = 0;
87 ProxyAutoLock lock;
88 cb0 = RunWhileLocked(base::Bind(TestCallback_1, 123));
90 cb0.Run();
91 ASSERT_EQ(1, called_num);
92 called_num = 0;
95 ProxyAutoLock lock;
96 scoped_refptr<CheckLockStateInDestructor> object =
97 new CheckLockStateInDestructor();
98 cb0 =
99 RunWhileLocked(base::Bind(&CheckLockStateInDestructor::Method, object));
100 // Note after this scope, the Callback owns the only reference.
102 cb0.Run();
103 ASSERT_EQ(1, called_num);
104 called_num = 0;
106 base::Callback<void(int)> cb1;
108 ProxyAutoLock lock;
109 cb1 = RunWhileLocked(base::Bind(TestCallback_1));
111 cb1.Run(123);
112 ASSERT_EQ(1, called_num);
113 called_num = 0;
115 base::Callback<void(int, const std::string&)> cb2;
117 ProxyAutoLock lock;
118 cb2 = RunWhileLocked(base::Bind(TestCallback_2));
120 cb2.Run(123, std::string("yo"));
121 ASSERT_EQ(1, called_num);
122 called_num = 0;
124 base::Callback<void(int, const std::string&, Param)> cb3;
126 ProxyAutoLock lock;
127 cb3 = RunWhileLocked(base::Bind(TestCallback_3));
129 cb3.Run(123, std::string("yo"), Param());
130 ASSERT_EQ(1, called_num);
131 called_num = 0;
133 base::Callback<void(const std::string&)> cb1_string;
135 ProxyAutoLock lock;
136 cb1_string = RunWhileLocked(base::Bind(TestCallback_2, 123));
138 cb1_string.Run(std::string("yo"));
139 ASSERT_EQ(1, called_num);
140 called_num = 0;
143 ProxyAutoLock lock;
144 cb0 = RunWhileLocked(base::Bind(TestCallback_2, 123, std::string("yo")));
146 cb0.Run();
147 ASSERT_EQ(1, called_num);
148 called_num = 0;
151 TEST_F(PpapiProxyLockTest, Unlocking) {
152 TestGlobals globals;
153 expect_to_be_locked = false;
154 // These calls should all try to _unlock_, so we must be locked before
155 // entering them.
156 ProxyAutoLock auto_lock;
159 CallWhileUnlocked(TestCallback_0);
160 ASSERT_EQ(1, called_num);
161 called_num = 0;
164 CallWhileUnlocked(TestCallback_1, 123);
165 ASSERT_EQ(1, called_num);
166 called_num = 0;
169 // TODO(dmichael): Make const-ref arguments work properly with type
170 // deduction.
171 CallWhileUnlocked<void, int, const std::string&>(
172 TestCallback_2, 123, std::string("yo"));
173 ASSERT_EQ(1, called_num);
174 called_num = 0;
177 base::Callback<void()> callback(base::Bind(TestCallback_0));
178 CallWhileUnlocked(callback);
179 ASSERT_EQ(1, called_num);
180 called_num = 0;
184 } // namespace ppapi