| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2012 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 }; | 29 }; |
| 30 | 30 |
| 31 template<class slot_policy = sigslot::single_threaded, | 31 template<class slot_policy = sigslot::single_threaded, |
| 32 class signal_policy = sigslot::single_threaded> | 32 class signal_policy = sigslot::single_threaded> |
| 33 class SigslotReceiver : public sigslot::has_slots<slot_policy> { | 33 class SigslotReceiver : public sigslot::has_slots<slot_policy> { |
| 34 public: | 34 public: |
| 35 SigslotReceiver() : signal_(nullptr), signal_count_(0) {} | 35 SigslotReceiver() : signal_(nullptr), signal_count_(0) {} |
| 36 ~SigslotReceiver() { | 36 ~SigslotReceiver() { |
| 37 } | 37 } |
| 38 | 38 |
| 39 // Provide copy constructor so that tests can exercise the has_slots copy | |
| 40 // constructor. | |
| 41 SigslotReceiver(const SigslotReceiver&) = default; | |
| 42 | |
| 43 void Connect(sigslot::signal0<signal_policy>* signal) { | 39 void Connect(sigslot::signal0<signal_policy>* signal) { |
| 44 if (!signal) return; | 40 if (!signal) return; |
| 45 Disconnect(); | 41 Disconnect(); |
| 46 signal_ = signal; | 42 signal_ = signal; |
| 47 signal->connect(this, | 43 signal->connect(this, |
| 48 &SigslotReceiver<slot_policy, signal_policy>::OnSignal); | 44 &SigslotReceiver<slot_policy, signal_policy>::OnSignal); |
| 49 } | 45 } |
| 50 void Disconnect() { | 46 void Disconnect() { |
| 51 if (!signal_) return; | 47 if (!signal_) return; |
| 52 signal_->disconnect(this); | 48 signal_->disconnect(this); |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 // Test that locks are acquired and released correctly. | 215 // Test that locks are acquired and released correctly. |
| 220 TEST_F(SigslotMTLockTest, LockSanity) { | 216 TEST_F(SigslotMTLockTest, LockSanity) { |
| 221 const int lock_count = SignalLockCount(); | 217 const int lock_count = SignalLockCount(); |
| 222 Signal(); | 218 Signal(); |
| 223 EXPECT_FALSE(InCriticalSection()); | 219 EXPECT_FALSE(InCriticalSection()); |
| 224 EXPECT_EQ(lock_count + 1, SignalLockCount()); | 220 EXPECT_EQ(lock_count + 1, SignalLockCount()); |
| 225 EXPECT_EQ(1, signal_count()); | 221 EXPECT_EQ(1, signal_count()); |
| 226 } | 222 } |
| 227 | 223 |
| 228 // Destroy signal and slot in different orders. | 224 // Destroy signal and slot in different orders. |
| 229 TEST(SigslotDestructionOrder, SignalFirst) { | 225 TEST(DestructionOrder, SignalFirst) { |
| 230 sigslot::signal0<>* signal = new sigslot::signal0<>; | 226 sigslot::signal0<>* signal = new sigslot::signal0<>; |
| 231 SigslotReceiver<>* receiver = new SigslotReceiver<>(); | 227 SigslotReceiver<>* receiver = new SigslotReceiver<>(); |
| 232 receiver->Connect(signal); | 228 receiver->Connect(signal); |
| 233 (*signal)(); | 229 (*signal)(); |
| 234 EXPECT_EQ(1, receiver->signal_count()); | 230 EXPECT_EQ(1, receiver->signal_count()); |
| 235 delete signal; | 231 delete signal; |
| 236 delete receiver; | 232 delete receiver; |
| 237 } | 233 } |
| 238 | 234 |
| 239 TEST(SigslotDestructionOrder, SlotFirst) { | 235 TEST(DestructionOrder, SlotFirst) { |
| 240 sigslot::signal0<>* signal = new sigslot::signal0<>; | 236 sigslot::signal0<>* signal = new sigslot::signal0<>; |
| 241 SigslotReceiver<>* receiver = new SigslotReceiver<>(); | 237 SigslotReceiver<>* receiver = new SigslotReceiver<>(); |
| 242 receiver->Connect(signal); | 238 receiver->Connect(signal); |
| 243 (*signal)(); | 239 (*signal)(); |
| 244 EXPECT_EQ(1, receiver->signal_count()); | 240 EXPECT_EQ(1, receiver->signal_count()); |
| 245 | 241 |
| 246 delete receiver; | 242 delete receiver; |
| 247 (*signal)(); | 243 (*signal)(); |
| 248 delete signal; | 244 delete signal; |
| 249 } | 245 } |
| 250 | |
| 251 // Test that if a signal is copied, its slot connections are copied as well. | |
| 252 TEST(SigslotTest, CopyConnectedSignal) { | |
| 253 sigslot::signal<> signal; | |
| 254 SigslotReceiver<> receiver; | |
| 255 receiver.Connect(&signal); | |
| 256 | |
| 257 // Fire the copied signal, expecting the receiver to be notified. | |
| 258 sigslot::signal<> copied_signal(signal); | |
| 259 copied_signal(); | |
| 260 EXPECT_EQ(1, receiver.signal_count()); | |
| 261 } | |
| 262 | |
| 263 // Test that if a slot is copied, its signal connections are copied as well. | |
| 264 TEST(SigslotTest, CopyConnectedSlot) { | |
| 265 sigslot::signal<> signal; | |
| 266 SigslotReceiver<> receiver; | |
| 267 receiver.Connect(&signal); | |
| 268 | |
| 269 // Fire the signal after copying the receiver, expecting the copied receiver | |
| 270 // to be notified. | |
| 271 SigslotReceiver<> copied_receiver(receiver); | |
| 272 signal(); | |
| 273 EXPECT_EQ(1, copied_receiver.signal_count()); | |
| 274 } | |
| OLD | NEW |