OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include <string> |
| 6 |
| 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 "ppapi/shared_impl/proxy_lock.h" |
| 13 #include "ppapi/shared_impl/test_globals.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace ppapi { |
| 17 |
| 18 namespace { |
| 19 |
| 20 bool expect_to_be_locked = false; |
| 21 void CheckLockState() { |
| 22 if (expect_to_be_locked) { |
| 23 ProxyLock::AssertAcquired(); |
| 24 } else { |
| 25 // If we expect to be unlocked, try to lock. We rely on the checking inside |
| 26 // base::Lock that prevents recursive locking. |
| 27 ProxyAutoLock lock; |
| 28 } |
| 29 } |
| 30 |
| 31 int called_num = 0; |
| 32 |
| 33 class CheckLockStateInDestructor |
| 34 : public base::RefCounted<CheckLockStateInDestructor> { |
| 35 public: |
| 36 CheckLockStateInDestructor() {} |
| 37 void Method() { |
| 38 ++called_num; |
| 39 } |
| 40 private: |
| 41 friend class base::RefCounted<CheckLockStateInDestructor>; |
| 42 ~CheckLockStateInDestructor() { |
| 43 CheckLockState(); |
| 44 } |
| 45 DISALLOW_COPY_AND_ASSIGN(CheckLockStateInDestructor); |
| 46 }; |
| 47 |
| 48 void TestCallback_0() { |
| 49 CheckLockState(); |
| 50 ++called_num; |
| 51 } |
| 52 |
| 53 void TestCallback_1(int p1) { |
| 54 CheckLockState(); |
| 55 ++called_num; |
| 56 } |
| 57 |
| 58 void TestCallback_2(int p1, const std::string& p2) { |
| 59 CheckLockState(); |
| 60 ++called_num; |
| 61 } |
| 62 |
| 63 struct Param {}; |
| 64 void TestCallback_3(int p1, const std::string& p2, Param p3) { |
| 65 CheckLockState(); |
| 66 ++called_num; |
| 67 } |
| 68 |
| 69 } // namespace |
| 70 |
| 71 TEST(PpapiProxyLockTest, Locking) { |
| 72 TestGlobals globals; |
| 73 base::Lock lock; |
| 74 globals.set_proxy_lock(&lock); |
| 75 expect_to_be_locked = true; |
| 76 |
| 77 { |
| 78 base::Callback<void()> cb(RunWhileLocked(base::Bind(TestCallback_0))); |
| 79 cb.Run(); |
| 80 ASSERT_EQ(1, called_num); |
| 81 called_num = 0; |
| 82 } { |
| 83 base::Callback<void()> cb(RunWhileLocked(base::Bind(TestCallback_1, 123))); |
| 84 cb.Run(); |
| 85 ASSERT_EQ(1, called_num); |
| 86 called_num = 0; |
| 87 } { |
| 88 scoped_refptr<CheckLockStateInDestructor> object = |
| 89 new CheckLockStateInDestructor(); |
| 90 base::Callback<void()> cb( |
| 91 RunWhileLocked( |
| 92 base::Bind(&CheckLockStateInDestructor::Method, |
| 93 object))); |
| 94 // Clear the scoped_refptr so that the Callback owns the only reference. |
| 95 object = NULL; |
| 96 cb.Run(); |
| 97 ASSERT_EQ(1, called_num); |
| 98 called_num = 0; |
| 99 } { |
| 100 base::Callback<void(int)> cb(RunWhileLocked(base::Bind(TestCallback_1))); |
| 101 cb.Run(123); |
| 102 ASSERT_EQ(1, called_num); |
| 103 called_num = 0; |
| 104 } { |
| 105 base::Callback<void(int, const std::string&)> cb( |
| 106 RunWhileLocked(base::Bind(TestCallback_2))); |
| 107 cb.Run(123, std::string("yo")); |
| 108 ASSERT_EQ(1, called_num); |
| 109 called_num = 0; |
| 110 } { |
| 111 base::Callback<void(int, const std::string&, Param)> cb( |
| 112 RunWhileLocked(base::Bind(TestCallback_3))); |
| 113 cb.Run(123, std::string("yo"), Param()); |
| 114 ASSERT_EQ(1, called_num); |
| 115 called_num = 0; |
| 116 } { |
| 117 base::Callback<void(const std::string&)> cb( |
| 118 RunWhileLocked(base::Bind(TestCallback_2, 123))); |
| 119 cb.Run(std::string("yo")); |
| 120 ASSERT_EQ(1, called_num); |
| 121 called_num = 0; |
| 122 } { |
| 123 base::Callback<void()> cb( |
| 124 RunWhileLocked(base::Bind(TestCallback_2, 123, std::string("yo")))); |
| 125 cb.Run(); |
| 126 ASSERT_EQ(1, called_num); |
| 127 called_num = 0; |
| 128 } |
| 129 } |
| 130 |
| 131 TEST(PpapiProxyLockTest, Unlocking) { |
| 132 TestGlobals globals; |
| 133 base::Lock lock; |
| 134 globals.set_proxy_lock(&lock); |
| 135 expect_to_be_locked = false; |
| 136 // These calls should all try to _unlock_, so we must be locked before |
| 137 // entering them. |
| 138 ProxyAutoLock auto_lock; |
| 139 |
| 140 { |
| 141 CallWhileUnlocked(TestCallback_0); |
| 142 ASSERT_EQ(1, called_num); |
| 143 called_num = 0; |
| 144 } { |
| 145 CallWhileUnlocked(TestCallback_1, 123); |
| 146 ASSERT_EQ(1, called_num); |
| 147 called_num = 0; |
| 148 } { |
| 149 // TODO(dmichael): Make const-ref arguments work properly with type |
| 150 // deduction. |
| 151 CallWhileUnlocked<void, int, const std::string&>( |
| 152 TestCallback_2, 123, std::string("yo")); |
| 153 ASSERT_EQ(1, called_num); |
| 154 called_num = 0; |
| 155 } { |
| 156 base::Callback<void()> callback(base::Bind(TestCallback_0)); |
| 157 CallWhileUnlocked(callback); |
| 158 ASSERT_EQ(1, called_num); |
| 159 called_num = 0; |
| 160 } |
| 161 } |
| 162 |
| 163 } // namespace ppapi |
OLD | NEW |