tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/event_tracer.h" |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/trace_event.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "test/gtest.h" |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 15 | |
| 16 | namespace { |
| 17 | |
| 18 | class TestStatistics { |
| 19 | public: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 20 | TestStatistics() : events_logged_(0) {} |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 21 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 22 | void Reset() { events_logged_ = 0; } |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 23 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 24 | void Increment() { ++events_logged_; } |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 25 | |
| 26 | int Count() const { return events_logged_; } |
| 27 | |
| 28 | static TestStatistics* Get() { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 29 | static TestStatistics* test_stats = nullptr; |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 30 | if (!test_stats) |
| 31 | test_stats = new TestStatistics(); |
| 32 | return test_stats; |
| 33 | } |
| 34 | |
| 35 | private: |
| 36 | int events_logged_; |
| 37 | }; |
| 38 | |
| 39 | static const unsigned char* GetCategoryEnabledHandler(const char* name) { |
| 40 | return reinterpret_cast<const unsigned char*>("test"); |
| 41 | } |
| 42 | |
| 43 | static 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 | |
| 57 | namespace webrtc { |
| 58 | |
| 59 | TEST(EventTracerTest, EventTracerDisabled) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 60 | { TRACE_EVENT0("test", "EventTracerDisabled"); } |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 61 | EXPECT_FALSE(TestStatistics::Get()->Count()); |
| 62 | TestStatistics::Get()->Reset(); |
| 63 | } |
| 64 | |
| 65 | TEST(EventTracerTest, ScopedTraceEvent) { |
| 66 | SetupEventTracer(&GetCategoryEnabledHandler, &AddTraceEventHandler); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 67 | { TRACE_EVENT0("test", "ScopedTraceEvent"); } |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 68 | EXPECT_EQ(2, TestStatistics::Get()->Count()); |
| 69 | TestStatistics::Get()->Reset(); |
| 70 | } |
| 71 | |
| 72 | } // namespace webrtc |