From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- third_party/libwebrtc/rtc_base/test_utils.h | 99 +++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 third_party/libwebrtc/rtc_base/test_utils.h (limited to 'third_party/libwebrtc/rtc_base/test_utils.h') diff --git a/third_party/libwebrtc/rtc_base/test_utils.h b/third_party/libwebrtc/rtc_base/test_utils.h new file mode 100644 index 0000000000..3f877fc9af --- /dev/null +++ b/third_party/libwebrtc/rtc_base/test_utils.h @@ -0,0 +1,99 @@ +/* + * 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. + */ + +#ifndef RTC_BASE_TEST_UTILS_H_ +#define RTC_BASE_TEST_UTILS_H_ + +// Utilities for testing rtc infrastructure in unittests + +#include +#include + +#include "rtc_base/socket.h" +#include "rtc_base/third_party/sigslot/sigslot.h" + +namespace webrtc { +namespace testing { + +/////////////////////////////////////////////////////////////////////////////// +// StreamSink - Monitor asynchronously signalled events from Socket. +/////////////////////////////////////////////////////////////////////////////// + +// Note: Any event that is an error is treated as SSE_ERROR instead of that +// event. + +enum StreamSinkEvent { + SSE_OPEN = 1, + SSE_READ = 2, + SSE_WRITE = 4, + SSE_CLOSE = 8, + SSE_ERROR = 16 +}; + +class StreamSink : public sigslot::has_slots<> { + public: + StreamSink(); + ~StreamSink() override; + + void Monitor(rtc::Socket* socket) { + socket->SignalConnectEvent.connect(this, &StreamSink::OnConnectEvent); + socket->SignalReadEvent.connect(this, &StreamSink::OnReadEvent); + socket->SignalWriteEvent.connect(this, &StreamSink::OnWriteEvent); + socket->SignalCloseEvent.connect(this, &StreamSink::OnCloseEvent); + // In case you forgot to unmonitor a previous object with this address + events_.erase(socket); + } + void Unmonitor(rtc::Socket* socket) { + socket->SignalConnectEvent.disconnect(this); + socket->SignalReadEvent.disconnect(this); + socket->SignalWriteEvent.disconnect(this); + socket->SignalCloseEvent.disconnect(this); + events_.erase(socket); + } + bool Check(rtc::Socket* socket, StreamSinkEvent event, bool reset = true) { + return DoCheck(socket, event, reset); + } + + private: + typedef std::map EventMap; + + void OnConnectEvent(rtc::Socket* socket) { AddEvents(socket, SSE_OPEN); } + void OnReadEvent(rtc::Socket* socket) { AddEvents(socket, SSE_READ); } + void OnWriteEvent(rtc::Socket* socket) { AddEvents(socket, SSE_WRITE); } + void OnCloseEvent(rtc::Socket* socket, int error) { + AddEvents(socket, (0 == error) ? SSE_CLOSE : SSE_ERROR); + } + + void AddEvents(rtc::Socket* obj, int events) { + EventMap::iterator it = events_.find(obj); + if (events_.end() == it) { + events_.insert(EventMap::value_type(obj, events)); + } else { + it->second |= events; + } + } + bool DoCheck(rtc::Socket* obj, StreamSinkEvent event, bool reset) { + EventMap::iterator it = events_.find(obj); + if ((events_.end() == it) || (0 == (it->second & event))) { + return false; + } + if (reset) { + it->second &= ~event; + } + return true; + } + + EventMap events_; +}; + +} // namespace testing +} // namespace webrtc + +#endif // RTC_BASE_TEST_UTILS_H_ -- cgit v1.2.3