OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/debug/blame_context.h" |
| 6 |
| 7 #include "base/json/json_writer.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "base/test/trace_event_analyzer.h" |
| 10 #include "base/trace_event/trace_buffer.h" |
| 11 #include "base/trace_event/trace_event.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace base { |
| 15 namespace debug { |
| 16 namespace { |
| 17 |
| 18 const char kTestBlameCategory[] = "test"; |
| 19 const char kTestBlameName[] = "TestBlameContext"; |
| 20 const char kTestBlameScope[] = "TestBlameScope"; |
| 21 |
| 22 class TestBlameContext |
| 23 : public BlameContext<kTestBlameCategory, kTestBlameName, kTestBlameScope> { |
| 24 public: |
| 25 explicit TestBlameContext(int id) |
| 26 : BlameContext<kTestBlameCategory, kTestBlameName, kTestBlameScope>(id) {} |
| 27 |
| 28 TestBlameContext(int id, const TestBlameContext& parent) |
| 29 : BlameContext<kTestBlameCategory, kTestBlameName, kTestBlameScope>( |
| 30 id, |
| 31 parent) {} |
| 32 |
| 33 protected: |
| 34 void AsValueInto(trace_event::TracedValue* state) override { |
| 35 BlameContext<kTestBlameCategory, kTestBlameName, |
| 36 kTestBlameScope>::AsValueInto(state); |
| 37 state->SetBoolean("crossStreams", false); |
| 38 } |
| 39 }; |
| 40 |
| 41 void OnTraceDataCollected(Closure quit_closure, |
| 42 trace_event::TraceResultBuffer* buffer, |
| 43 const scoped_refptr<RefCountedString>& json, |
| 44 bool has_more_events) { |
| 45 buffer->AddFragment(json->data()); |
| 46 if (!has_more_events) |
| 47 quit_closure.Run(); |
| 48 } |
| 49 |
| 50 class BlameContextTest : public testing::Test { |
| 51 public: |
| 52 void StartTracing(); |
| 53 void StopTracing(); |
| 54 scoped_ptr<trace_analyzer::TraceAnalyzer> CreateTraceAnalyzer(); |
| 55 }; |
| 56 |
| 57 void BlameContextTest::StartTracing() { |
| 58 trace_event::TraceLog::GetInstance()->SetEnabled( |
| 59 trace_event::TraceConfig("*"), trace_event::TraceLog::RECORDING_MODE); |
| 60 } |
| 61 |
| 62 void BlameContextTest::StopTracing() { |
| 63 trace_event::TraceLog::GetInstance()->SetDisabled(); |
| 64 } |
| 65 |
| 66 scoped_ptr<trace_analyzer::TraceAnalyzer> |
| 67 BlameContextTest::CreateTraceAnalyzer() { |
| 68 trace_event::TraceResultBuffer buffer; |
| 69 trace_event::TraceResultBuffer::SimpleOutput trace_output; |
| 70 buffer.SetOutputCallback(trace_output.GetCallback()); |
| 71 RunLoop run_loop; |
| 72 buffer.Start(); |
| 73 trace_event::TraceLog::GetInstance()->Flush( |
| 74 Bind(&OnTraceDataCollected, run_loop.QuitClosure(), Unretained(&buffer))); |
| 75 run_loop.Run(); |
| 76 buffer.Finish(); |
| 77 |
| 78 return make_scoped_ptr( |
| 79 trace_analyzer::TraceAnalyzer::Create(trace_output.json_output)); |
| 80 } |
| 81 |
| 82 TEST_F(BlameContextTest, EnterAndLeave) { |
| 83 using trace_analyzer::Query; |
| 84 StartTracing(); |
| 85 { |
| 86 TestBlameContext blame_context(0x1234); |
| 87 blame_context.Enter(); |
| 88 blame_context.Leave(); |
| 89 } |
| 90 StopTracing(); |
| 91 scoped_ptr<trace_analyzer::TraceAnalyzer> analyzer = CreateTraceAnalyzer(); |
| 92 |
| 93 trace_analyzer::TraceEventVector events; |
| 94 Query q = Query::EventPhaseIs(TRACE_EVENT_PHASE_ENTER_CONTEXT) || |
| 95 Query::EventPhaseIs(TRACE_EVENT_PHASE_LEAVE_CONTEXT); |
| 96 analyzer->FindEvents(q, &events); |
| 97 |
| 98 EXPECT_EQ(2u, events.size()); |
| 99 EXPECT_EQ(TRACE_EVENT_PHASE_ENTER_CONTEXT, events[0]->phase); |
| 100 EXPECT_EQ(kTestBlameCategory, events[0]->category); |
| 101 EXPECT_EQ(kTestBlameName, events[0]->name); |
| 102 EXPECT_EQ("0x1234", events[0]->id); |
| 103 EXPECT_EQ(TRACE_EVENT_PHASE_LEAVE_CONTEXT, events[1]->phase); |
| 104 EXPECT_EQ(kTestBlameCategory, events[1]->category); |
| 105 EXPECT_EQ(kTestBlameName, events[1]->name); |
| 106 EXPECT_EQ("0x1234", events[1]->id); |
| 107 } |
| 108 |
| 109 TEST_F(BlameContextTest, TakeSnapshot) { |
| 110 using trace_analyzer::Query; |
| 111 StartTracing(); |
| 112 { |
| 113 TestBlameContext parent_blame_context(0x5678); |
| 114 TestBlameContext blame_context(0x1234, parent_blame_context); |
| 115 blame_context.TakeSnapshot(); |
| 116 } |
| 117 StopTracing(); |
| 118 scoped_ptr<trace_analyzer::TraceAnalyzer> analyzer = CreateTraceAnalyzer(); |
| 119 |
| 120 trace_analyzer::TraceEventVector events; |
| 121 Query q = Query::EventPhaseIs(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT); |
| 122 analyzer->FindEvents(q, &events); |
| 123 |
| 124 EXPECT_EQ(1u, events.size()); |
| 125 EXPECT_EQ(kTestBlameCategory, events[0]->category); |
| 126 EXPECT_EQ(kTestBlameName, events[0]->name); |
| 127 EXPECT_EQ("0x1234", events[0]->id); |
| 128 EXPECT_TRUE(events[0]->HasArg("snapshot")); |
| 129 |
| 130 const char kExpectedSnapshotJson[] = |
| 131 "{\n" |
| 132 " \"crossStreams\": false,\n" |
| 133 " \"parent\": {\n" |
| 134 " \"id_ref\": \"0x5678\",\n" |
| 135 " \"scope\": \"TestBlameScope\"\n" |
| 136 " }\n" |
| 137 "}\n"; |
| 138 |
| 139 std::string snapshot_json; |
| 140 JSONWriter::WriteWithOptions(*events[0]->GetKnownArgAsValue("snapshot"), |
| 141 JSONWriter::OPTIONS_PRETTY_PRINT, |
| 142 &snapshot_json); |
| 143 EXPECT_EQ(kExpectedSnapshotJson, snapshot_json); |
| 144 } |
| 145 |
| 146 } // namepace |
| 147 } // namespace debug |
| 148 } // namespace base |
OLD | NEW |