1 // Copyright 2015 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.
5 #include "base/trace_event/memory_dump_manager.h"
7 #include "base/trace_event/memory_dump_provider.h"
8 #include "base/trace_event/process_memory_dump.h"
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
15 namespace trace_event
{
17 class MemoryDumpManagerTest
: public testing::Test
{
19 void SetUp() override
{
20 mdm_
.reset(new MemoryDumpManager());
21 MemoryDumpManager::SetInstanceForTesting(mdm_
.get());
22 ASSERT_EQ(mdm_
, MemoryDumpManager::GetInstance());
23 MemoryDumpManager::GetInstance()->Initialize();
26 void TearDown() override
{
27 MemoryDumpManager::SetInstanceForTesting(nullptr);
29 TraceLog::DeleteForTesting();
33 const char* const kTraceCategory
= MemoryDumpManager::kTraceCategory
;
35 void EnableTracing(const char* category
) {
36 TraceLog::GetInstance()->SetEnabled(
37 CategoryFilter(category
), TraceLog::RECORDING_MODE
, TraceOptions());
40 void DisableTracing() { TraceLog::GetInstance()->SetDisabled(); }
42 scoped_ptr
<MemoryDumpManager
> mdm_
;
45 // We want our singleton torn down after each test.
46 ShadowingAtExitManager at_exit_manager_
;
49 class MockDumpProvider
: public MemoryDumpProvider
{
51 MOCK_METHOD1(DumpInto
, void(ProcessMemoryDump
* pmd
));
54 TEST_F(MemoryDumpManagerTest
, SingleDumper
) {
56 mdm_
->RegisterDumpProvider(&mdp
);
58 // Check that the dumper is not called if the memory category is not enabled.
59 EnableTracing("foo-and-bar-but-not-memory");
60 EXPECT_CALL(mdp
, DumpInto(_
)).Times(0);
61 mdm_
->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED
);
64 // Now repeat enabling the memory category and check that the dumper is
66 EnableTracing(kTraceCategory
);
67 EXPECT_CALL(mdp
, DumpInto(_
)).Times(3);
68 for (int i
= 0; i
< 3; ++i
)
69 mdm_
->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED
);
72 mdm_
->UnregisterDumpProvider(&mdp
);
74 // Finally check the unregister logic (no calls to the mdp after unregister).
75 EnableTracing(kTraceCategory
);
76 EXPECT_CALL(mdp
, DumpInto(_
)).Times(0);
77 mdm_
->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED
);
78 TraceLog::GetInstance()->SetDisabled();
81 TEST_F(MemoryDumpManagerTest
, UnregisterDumperWhileTracing
) {
83 mdm_
->RegisterDumpProvider(&mdp
);
85 EnableTracing(kTraceCategory
);
86 EXPECT_CALL(mdp
, DumpInto(_
)).Times(1);
87 mdm_
->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED
);
89 mdm_
->UnregisterDumpProvider(&mdp
);
90 EXPECT_CALL(mdp
, DumpInto(_
)).Times(0);
91 mdm_
->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED
);
96 TEST_F(MemoryDumpManagerTest
, MultipleDumpers
) {
97 MockDumpProvider mdp1
;
98 MockDumpProvider mdp2
;
101 mdm_
->RegisterDumpProvider(&mdp1
);
102 EnableTracing(kTraceCategory
);
103 EXPECT_CALL(mdp1
, DumpInto(_
)).Times(1);
104 EXPECT_CALL(mdp2
, DumpInto(_
)).Times(0);
105 mdm_
->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED
);
108 // Invert: enable mdp1 and disable mdp2.
109 mdm_
->UnregisterDumpProvider(&mdp1
);
110 mdm_
->RegisterDumpProvider(&mdp2
);
111 EnableTracing(kTraceCategory
);
112 EXPECT_CALL(mdp1
, DumpInto(_
)).Times(0);
113 EXPECT_CALL(mdp2
, DumpInto(_
)).Times(1);
114 mdm_
->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED
);
117 // Enable both mdp1 and mdp2.
118 mdm_
->RegisterDumpProvider(&mdp1
);
119 EnableTracing(kTraceCategory
);
120 EXPECT_CALL(mdp1
, DumpInto(_
)).Times(1);
121 EXPECT_CALL(mdp2
, DumpInto(_
)).Times(1);
122 mdm_
->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED
);
126 } // namespace trace_Event