blob: 27248e754744e99511d664de5b94cc1bfe3e94ce [file] [log] [blame]
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/event_tracer.h"
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/trace_event.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "test/gtest.h"
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000015
16namespace {
17
18class TestStatistics {
19 public:
Yves Gerey665174f2018-06-19 15:03:05 +020020 TestStatistics() : events_logged_(0) {}
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000021
Yves Gerey665174f2018-06-19 15:03:05 +020022 void Reset() { events_logged_ = 0; }
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000023
Yves Gerey665174f2018-06-19 15:03:05 +020024 void Increment() { ++events_logged_; }
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000025
26 int Count() const { return events_logged_; }
27
28 static TestStatistics* Get() {
deadbeef37f5ecf2017-02-27 14:06:41 -080029 static TestStatistics* test_stats = nullptr;
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000030 if (!test_stats)
31 test_stats = new TestStatistics();
32 return test_stats;
33 }
34
35 private:
36 int events_logged_;
37};
38
39static const unsigned char* GetCategoryEnabledHandler(const char* name) {
40 return reinterpret_cast<const unsigned char*>("test");
41}
42
43static void AddTraceEventHandler(char phase,
44 const unsigned char* category_enabled,
45 const char* name,
46 unsigned long long id,
47 int num_args,
48 const char** arg_names,
49 const unsigned char* arg_types,
50 const unsigned long long* arg_values,
51 unsigned char flags) {
52 TestStatistics::Get()->Increment();
53}
54
55} // namespace
56
57namespace webrtc {
58
59TEST(EventTracerTest, EventTracerDisabled) {
Yves Gerey665174f2018-06-19 15:03:05 +020060 { TRACE_EVENT0("test", "EventTracerDisabled"); }
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000061 EXPECT_FALSE(TestStatistics::Get()->Count());
62 TestStatistics::Get()->Reset();
63}
64
65TEST(EventTracerTest, ScopedTraceEvent) {
66 SetupEventTracer(&GetCategoryEnabledHandler, &AddTraceEventHandler);
Yves Gerey665174f2018-06-19 15:03:05 +020067 { TRACE_EVENT0("test", "ScopedTraceEvent"); }
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000068 EXPECT_EQ(2, TestStatistics::Get()->Count());
69 TestStatistics::Get()->Reset();
70}
71
72} // namespace webrtc