| Index: base/trace_event/pseudo_stack_unittest.cc
|
| diff --git a/base/trace_event/pseudo_stack_unittest.cc b/base/trace_event/pseudo_stack_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..2fae86a99db3c0db5bcb6c6b43c04920d11ae7da
|
| --- /dev/null
|
| +++ b/base/trace_event/pseudo_stack_unittest.cc
|
| @@ -0,0 +1,138 @@
|
| +// Copyright (c) 2015 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 <cstring>
|
| +#include <iterator>
|
| +
|
| +#include "base/trace_event/pseudo_stack.h"
|
| +#include "base/trace_event/trace_event.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace base {
|
| +namespace trace_event {
|
| +
|
| +bool operator==(const PseudoStackFrame& lhs, const PseudoStackFrame& rhs) {
|
| + return strcmp(lhs.name, rhs.name) == 0;
|
| +}
|
| +
|
| +void CheckPseudoStackEquals(const PseudoStackFrame* expected_top,
|
| + const PseudoStackFrame* expected_bottom) {
|
| + auto actual = PseudoStack::GetInstance()->Top();
|
| + auto actual_bottom = PseudoStack::GetInstance()->Bottom();
|
| + auto expected = expected_top;
|
| +
|
| + for (; actual != actual_bottom && expected != expected_bottom;
|
| + actual++, expected++) {
|
| + ASSERT_EQ(*expected, *actual);
|
| + }
|
| +
|
| + // Ensure that the height of the stacks is the same.
|
| + ASSERT_TRUE(actual == actual_bottom && expected == expected_bottom);
|
| +}
|
| +
|
| +void CheckPseudoStackEmpty() {
|
| + CheckPseudoStackEquals(nullptr, nullptr);
|
| +}
|
| +
|
| +class PseudoStackTest : public testing::Test {
|
| + public:
|
| + void EnableTracing() {
|
| + TraceConfig config(
|
| + "{\"included_categories\": [\"disabled-by-default-memory-infra\"]}");
|
| + TraceLog::GetInstance()->SetEnabled(config, TraceLog::RECORDING_MODE);
|
| + }
|
| +
|
| + void DisableTracing() { TraceLog::GetInstance()->SetDisabled(); }
|
| +
|
| + private:
|
| + PseudoStackController controller_;
|
| +};
|
| +
|
| +TEST_F(PseudoStackTest, ScopedTrace) {
|
| + PseudoStackFrame c = {"Cupcake"};
|
| + PseudoStackFrame d = {"Donut"};
|
| + PseudoStackFrame e = {"Eclair"};
|
| + PseudoStackFrame f = {"Froyo"};
|
| +
|
| + EnableTracing();
|
| + CheckPseudoStackEmpty();
|
| +
|
| + {
|
| + TRACE_EVENT0("Testing", "Cupcake");
|
| + PseudoStackFrame frame_c[] = {c};
|
| + CheckPseudoStackEquals(std::begin(frame_c), std::end(frame_c));
|
| +
|
| + {
|
| + TRACE_EVENT0("Testing", "Donut");
|
| + PseudoStackFrame frame_dc[] = {d, c};
|
| + CheckPseudoStackEquals(std::begin(frame_dc), std::end(frame_dc));
|
| + }
|
| +
|
| + CheckPseudoStackEquals(std::begin(frame_c), std::end(frame_c));
|
| +
|
| + {
|
| + TRACE_EVENT0("Testing", "Eclair");
|
| + PseudoStackFrame frame_ec[] = {e, c};
|
| + CheckPseudoStackEquals(std::begin(frame_ec), std::end(frame_ec));
|
| + }
|
| +
|
| + CheckPseudoStackEquals(std::begin(frame_c), std::end(frame_c));
|
| + }
|
| +
|
| + CheckPseudoStackEmpty();
|
| +
|
| + {
|
| + TRACE_EVENT0("Testing", "Froyo");
|
| + PseudoStackFrame frame_f[] = {f};
|
| + CheckPseudoStackEquals(std::begin(frame_f), std::end(frame_f));
|
| + }
|
| +
|
| + CheckPseudoStackEmpty();
|
| + DisableTracing();
|
| +}
|
| +
|
| +TEST_F(PseudoStackTest, EnableWithEventInScope) {
|
| + PseudoStackFrame h = {"Honeycomb"};
|
| +
|
| + {
|
| + TRACE_EVENT0("Testing", "Gingerbread");
|
| + EnableTracing();
|
| + CheckPseudoStackEmpty();
|
| +
|
| + {
|
| + TRACE_EVENT0("Testing", "Honeycomb");
|
| + PseudoStackFrame frame_h[] = {h};
|
| + CheckPseudoStackEquals(std::begin(frame_h), std::end(frame_h));
|
| + }
|
| +
|
| + CheckPseudoStackEmpty();
|
| +
|
| + // The pop at the end of this scope for the 'Gingerbread' frame must not
|
| + // cause a stack underflow.
|
| + }
|
| + CheckPseudoStackEmpty();
|
| + DisableTracing();
|
| +}
|
| +
|
| +TEST_F(PseudoStackTest, GenerationsAreIndependent) {
|
| + PseudoStackFrame i = {"Ice Cream Sandwich"};
|
| +
|
| + EnableTracing();
|
| + CheckPseudoStackEmpty();
|
| + {
|
| + TRACE_EVENT0("Testing", "Ice Cream Sandwich");
|
| + PseudoStackFrame frame_i[] = {i};
|
| + CheckPseudoStackEquals(std::begin(frame_i), std::end(frame_i));
|
| +
|
| + // Disable tracing before popping the frame, so it never gets popped.
|
| + DisableTracing();
|
| + }
|
| +
|
| + EnableTracing();
|
| + CheckPseudoStackEmpty();
|
| + DisableTracing();
|
| +}
|
| +
|
| +} // namespace trace_event
|
| +} // namespace base
|
|
|