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: |
| 20 | TestStatistics() : events_logged_(0) { |
| 21 | } |
| 22 | |
| 23 | void Reset() { |
| 24 | events_logged_ = 0; |
| 25 | } |
| 26 | |
| 27 | void Increment() { |
| 28 | ++events_logged_; |
| 29 | } |
| 30 | |
| 31 | int Count() const { return events_logged_; } |
| 32 | |
| 33 | static TestStatistics* Get() { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 34 | static TestStatistics* test_stats = nullptr; |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 35 | if (!test_stats) |
| 36 | test_stats = new TestStatistics(); |
| 37 | return test_stats; |
| 38 | } |
| 39 | |
| 40 | private: |
| 41 | int events_logged_; |
| 42 | }; |
| 43 | |
| 44 | static const unsigned char* GetCategoryEnabledHandler(const char* name) { |
| 45 | return reinterpret_cast<const unsigned char*>("test"); |
| 46 | } |
| 47 | |
| 48 | static void AddTraceEventHandler(char phase, |
| 49 | const unsigned char* category_enabled, |
| 50 | const char* name, |
| 51 | unsigned long long id, |
| 52 | int num_args, |
| 53 | const char** arg_names, |
| 54 | const unsigned char* arg_types, |
| 55 | const unsigned long long* arg_values, |
| 56 | unsigned char flags) { |
| 57 | TestStatistics::Get()->Increment(); |
| 58 | } |
| 59 | |
| 60 | } // namespace |
| 61 | |
| 62 | namespace webrtc { |
| 63 | |
| 64 | TEST(EventTracerTest, EventTracerDisabled) { |
| 65 | { |
| 66 | TRACE_EVENT0("test", "EventTracerDisabled"); |
| 67 | } |
| 68 | EXPECT_FALSE(TestStatistics::Get()->Count()); |
| 69 | TestStatistics::Get()->Reset(); |
| 70 | } |
| 71 | |
| 72 | TEST(EventTracerTest, ScopedTraceEvent) { |
| 73 | SetupEventTracer(&GetCategoryEnabledHandler, &AddTraceEventHandler); |
| 74 | { |
| 75 | TRACE_EVENT0("test", "ScopedTraceEvent"); |
| 76 | } |
| 77 | EXPECT_EQ(2, TestStatistics::Get()->Count()); |
| 78 | TestStatistics::Get()->Reset(); |
| 79 | } |
| 80 | |
| 81 | } // namespace webrtc |