| Index: base/debug/blame_context_unittest.cc
|
| diff --git a/base/debug/blame_context_unittest.cc b/base/debug/blame_context_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..190288b02f6acaf294de8314450ef746a3ae9f71
|
| --- /dev/null
|
| +++ b/base/debug/blame_context_unittest.cc
|
| @@ -0,0 +1,148 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "base/debug/blame_context.h"
|
| +
|
| +#include "base/json/json_writer.h"
|
| +#include "base/run_loop.h"
|
| +#include "base/test/trace_event_analyzer.h"
|
| +#include "base/trace_event/trace_buffer.h"
|
| +#include "base/trace_event/trace_event.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace base {
|
| +namespace debug {
|
| +namespace {
|
| +
|
| +const char kTestBlameCategory[] = "test";
|
| +const char kTestBlameName[] = "TestBlameContext";
|
| +const char kTestBlameScope[] = "TestBlameScope";
|
| +
|
| +class TestBlameContext
|
| + : public BlameContext<kTestBlameCategory, kTestBlameName, kTestBlameScope> {
|
| + public:
|
| + explicit TestBlameContext(int id)
|
| + : BlameContext<kTestBlameCategory, kTestBlameName, kTestBlameScope>(id) {}
|
| +
|
| + TestBlameContext(int id, const TestBlameContext& parent)
|
| + : BlameContext<kTestBlameCategory, kTestBlameName, kTestBlameScope>(
|
| + id,
|
| + parent) {}
|
| +
|
| + protected:
|
| + void AsValueInto(trace_event::TracedValue* state) override {
|
| + BlameContext<kTestBlameCategory, kTestBlameName,
|
| + kTestBlameScope>::AsValueInto(state);
|
| + state->SetBoolean("crossStreams", false);
|
| + }
|
| +};
|
| +
|
| +void OnTraceDataCollected(Closure quit_closure,
|
| + trace_event::TraceResultBuffer* buffer,
|
| + const scoped_refptr<RefCountedString>& json,
|
| + bool has_more_events) {
|
| + buffer->AddFragment(json->data());
|
| + if (!has_more_events)
|
| + quit_closure.Run();
|
| +}
|
| +
|
| +class BlameContextTest : public testing::Test {
|
| + public:
|
| + void StartTracing();
|
| + void StopTracing();
|
| + scoped_ptr<trace_analyzer::TraceAnalyzer> CreateTraceAnalyzer();
|
| +};
|
| +
|
| +void BlameContextTest::StartTracing() {
|
| + trace_event::TraceLog::GetInstance()->SetEnabled(
|
| + trace_event::TraceConfig("*"), trace_event::TraceLog::RECORDING_MODE);
|
| +}
|
| +
|
| +void BlameContextTest::StopTracing() {
|
| + trace_event::TraceLog::GetInstance()->SetDisabled();
|
| +}
|
| +
|
| +scoped_ptr<trace_analyzer::TraceAnalyzer>
|
| +BlameContextTest::CreateTraceAnalyzer() {
|
| + trace_event::TraceResultBuffer buffer;
|
| + trace_event::TraceResultBuffer::SimpleOutput trace_output;
|
| + buffer.SetOutputCallback(trace_output.GetCallback());
|
| + RunLoop run_loop;
|
| + buffer.Start();
|
| + trace_event::TraceLog::GetInstance()->Flush(
|
| + Bind(&OnTraceDataCollected, run_loop.QuitClosure(), Unretained(&buffer)));
|
| + run_loop.Run();
|
| + buffer.Finish();
|
| +
|
| + return make_scoped_ptr(
|
| + trace_analyzer::TraceAnalyzer::Create(trace_output.json_output));
|
| +}
|
| +
|
| +TEST_F(BlameContextTest, EnterAndLeave) {
|
| + using trace_analyzer::Query;
|
| + StartTracing();
|
| + {
|
| + TestBlameContext blame_context(0x1234);
|
| + blame_context.Enter();
|
| + blame_context.Leave();
|
| + }
|
| + StopTracing();
|
| + scoped_ptr<trace_analyzer::TraceAnalyzer> analyzer = CreateTraceAnalyzer();
|
| +
|
| + trace_analyzer::TraceEventVector events;
|
| + Query q = Query::EventPhaseIs(TRACE_EVENT_PHASE_ENTER_CONTEXT) ||
|
| + Query::EventPhaseIs(TRACE_EVENT_PHASE_LEAVE_CONTEXT);
|
| + analyzer->FindEvents(q, &events);
|
| +
|
| + EXPECT_EQ(2u, events.size());
|
| + EXPECT_EQ(TRACE_EVENT_PHASE_ENTER_CONTEXT, events[0]->phase);
|
| + EXPECT_EQ(kTestBlameCategory, events[0]->category);
|
| + EXPECT_EQ(kTestBlameName, events[0]->name);
|
| + EXPECT_EQ("0x1234", events[0]->id);
|
| + EXPECT_EQ(TRACE_EVENT_PHASE_LEAVE_CONTEXT, events[1]->phase);
|
| + EXPECT_EQ(kTestBlameCategory, events[1]->category);
|
| + EXPECT_EQ(kTestBlameName, events[1]->name);
|
| + EXPECT_EQ("0x1234", events[1]->id);
|
| +}
|
| +
|
| +TEST_F(BlameContextTest, TakeSnapshot) {
|
| + using trace_analyzer::Query;
|
| + StartTracing();
|
| + {
|
| + TestBlameContext parent_blame_context(0x5678);
|
| + TestBlameContext blame_context(0x1234, parent_blame_context);
|
| + blame_context.TakeSnapshot();
|
| + }
|
| + StopTracing();
|
| + scoped_ptr<trace_analyzer::TraceAnalyzer> analyzer = CreateTraceAnalyzer();
|
| +
|
| + trace_analyzer::TraceEventVector events;
|
| + Query q = Query::EventPhaseIs(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT);
|
| + analyzer->FindEvents(q, &events);
|
| +
|
| + EXPECT_EQ(1u, events.size());
|
| + EXPECT_EQ(kTestBlameCategory, events[0]->category);
|
| + EXPECT_EQ(kTestBlameName, events[0]->name);
|
| + EXPECT_EQ("0x1234", events[0]->id);
|
| + EXPECT_TRUE(events[0]->HasArg("snapshot"));
|
| +
|
| + const char kExpectedSnapshotJson[] =
|
| + "{\n"
|
| + " \"crossStreams\": false,\n"
|
| + " \"parent\": {\n"
|
| + " \"id_ref\": \"0x5678\",\n"
|
| + " \"scope\": \"TestBlameScope\"\n"
|
| + " }\n"
|
| + "}\n";
|
| +
|
| + std::string snapshot_json;
|
| + JSONWriter::WriteWithOptions(*events[0]->GetKnownArgAsValue("snapshot"),
|
| + JSONWriter::OPTIONS_PRETTY_PRINT,
|
| + &snapshot_json);
|
| + EXPECT_EQ(kExpectedSnapshotJson, snapshot_json);
|
| +}
|
| +
|
| +} // namepace
|
| +} // namespace debug
|
| +} // namespace base
|
|
|