From 2aa4a82499d4becd2284cdb482213d541b8804dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 16:29:10 +0200 Subject: Adding upstream version 86.0.1. Signed-off-by: Daniel Baumann --- .../libwebrtc/webrtc/rtc_base/callback_unittest.cc | 140 +++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 third_party/libwebrtc/webrtc/rtc_base/callback_unittest.cc (limited to 'third_party/libwebrtc/webrtc/rtc_base/callback_unittest.cc') diff --git a/third_party/libwebrtc/webrtc/rtc_base/callback_unittest.cc b/third_party/libwebrtc/webrtc/rtc_base/callback_unittest.cc new file mode 100644 index 0000000000..26bfd5daf4 --- /dev/null +++ b/third_party/libwebrtc/webrtc/rtc_base/callback_unittest.cc @@ -0,0 +1,140 @@ +/* + * Copyright 2004 The WebRTC Project Authors. All rights reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "rtc_base/callback.h" +#include "rtc_base/bind.h" +#include "rtc_base/gunit.h" +#include "rtc_base/keep_ref_until_done.h" +#include "rtc_base/refcount.h" + +namespace rtc { + +namespace { + +void f() {} +int g() { return 42; } +int h(int x) { return x * x; } +void i(int& x) { x *= x; } // NOLINT: Testing refs + +struct BindTester { + int a() { return 24; } + int b(int x) const { return x * x; } +}; + +class RefCountedBindTester : public RefCountInterface { + public: + RefCountedBindTester() : count_(0) {} + void AddRef() const override { ++count_; } + RefCountReleaseStatus Release() const override { + --count_; + return count_ == 0 ? RefCountReleaseStatus::kDroppedLastRef + : RefCountReleaseStatus::kOtherRefsRemained; + } + int RefCount() const { return count_; } + + private: + mutable int count_; +}; + +} // namespace + +TEST(CallbackTest, VoidReturn) { + Callback0 cb; + EXPECT_TRUE(cb.empty()); + cb(); // Executing an empty callback should not crash. + cb = Callback0(&f); + EXPECT_FALSE(cb.empty()); + cb(); +} + +TEST(CallbackTest, IntReturn) { + Callback0 cb; + EXPECT_TRUE(cb.empty()); + cb = Callback0(&g); + EXPECT_FALSE(cb.empty()); + EXPECT_EQ(42, cb()); + EXPECT_EQ(42, cb()); +} + +TEST(CallbackTest, OneParam) { + Callback1 cb1(&h); + EXPECT_FALSE(cb1.empty()); + EXPECT_EQ(9, cb1(-3)); + EXPECT_EQ(100, cb1(10)); + + // Try clearing a callback. + cb1 = Callback1(); + EXPECT_TRUE(cb1.empty()); + + // Try a callback with a ref parameter. + Callback1 cb2(&i); + int x = 3; + cb2(x); + EXPECT_EQ(9, x); + cb2(x); + EXPECT_EQ(81, x); +} + +TEST(CallbackTest, WithBind) { + BindTester t; + Callback0 cb1 = Bind(&BindTester::a, &t); + EXPECT_EQ(24, cb1()); + EXPECT_EQ(24, cb1()); + cb1 = Bind(&BindTester::b, &t, 10); + EXPECT_EQ(100, cb1()); + EXPECT_EQ(100, cb1()); + cb1 = Bind(&BindTester::b, &t, 5); + EXPECT_EQ(25, cb1()); + EXPECT_EQ(25, cb1()); +} + +TEST(KeepRefUntilDoneTest, simple) { + RefCountedBindTester t; + EXPECT_EQ(0, t.RefCount()); + { + Callback0 cb = KeepRefUntilDone(&t); + EXPECT_EQ(1, t.RefCount()); + cb(); + EXPECT_EQ(1, t.RefCount()); + cb(); + EXPECT_EQ(1, t.RefCount()); + } + EXPECT_EQ(0, t.RefCount()); +} + +TEST(KeepRefUntilDoneTest, copy) { + RefCountedBindTester t; + EXPECT_EQ(0, t.RefCount()); + Callback0 cb2; + { + Callback0 cb = KeepRefUntilDone(&t); + EXPECT_EQ(1, t.RefCount()); + cb2 = cb; + } + EXPECT_EQ(1, t.RefCount()); + cb2 = Callback0(); + EXPECT_EQ(0, t.RefCount()); +} + +TEST(KeepRefUntilDoneTest, scopedref) { + RefCountedBindTester t; + EXPECT_EQ(0, t.RefCount()); + { + scoped_refptr t_scoped_ref(&t); + Callback0 cb = KeepRefUntilDone(t_scoped_ref); + t_scoped_ref = nullptr; + EXPECT_EQ(1, t.RefCount()); + cb(); + EXPECT_EQ(1, t.RefCount()); + } + EXPECT_EQ(0, t.RefCount()); +} + +} // namespace rtc -- cgit v1.2.3