1// Copyright 2013 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/trace_event_system_stats_monitor.h"
6
7#include <sstream>
8#include <string>
9
10#include "base/debug/trace_event_impl.h"
11#include "base/message_loop/message_loop.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14namespace base {
15namespace debug {
16
17#if !defined(OS_IOS)
18// Tests for the system stats monitor.
19// Exists as a class so it can be a friend of TraceEventSystemStatsMonitor.
20class TraceSystemStatsMonitorTest : public testing::Test {
21 public:
22  TraceSystemStatsMonitorTest() {}
23  virtual ~TraceSystemStatsMonitorTest() {}
24
25 private:
26  DISALLOW_COPY_AND_ASSIGN(TraceSystemStatsMonitorTest);
27};
28
29//////////////////////////////////////////////////////////////////////////////
30
31TEST_F(TraceSystemStatsMonitorTest, TraceEventSystemStatsMonitor) {
32  MessageLoop message_loop;
33
34  // Start with no observers of the TraceLog.
35  EXPECT_EQ(0u, TraceLog::GetInstance()->GetObserverCountForTest());
36
37  // Creating a system stats monitor adds it to the TraceLog observer list.
38  scoped_ptr<TraceEventSystemStatsMonitor> system_stats_monitor(
39      new TraceEventSystemStatsMonitor(
40          message_loop.message_loop_proxy()));
41  EXPECT_EQ(1u, TraceLog::GetInstance()->GetObserverCountForTest());
42  EXPECT_TRUE(
43      TraceLog::GetInstance()->HasEnabledStateObserver(
44          system_stats_monitor.get()));
45
46  // By default the observer isn't dumping memory profiles.
47  EXPECT_FALSE(system_stats_monitor->IsTimerRunningForTest());
48
49  // Simulate enabling tracing.
50  system_stats_monitor->StartProfiling();
51  message_loop.RunUntilIdle();
52  EXPECT_TRUE(system_stats_monitor->IsTimerRunningForTest());
53
54  // Simulate disabling tracing.
55  system_stats_monitor->StopProfiling();
56  message_loop.RunUntilIdle();
57  EXPECT_FALSE(system_stats_monitor->IsTimerRunningForTest());
58
59  // Deleting the observer removes it from the TraceLog observer list.
60  system_stats_monitor.reset();
61  EXPECT_EQ(0u, TraceLog::GetInstance()->GetObserverCountForTest());
62}
63#endif  // !defined(OS_IOS)
64
65}  // namespace debug
66}  // namespace base
67