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"
13 using testing::Return
;
16 namespace trace_event
{
18 class MemoryDumpManagerTest
: public testing::Test
{
20 void SetUp() override
{
21 mdm_
.reset(new MemoryDumpManager());
22 MemoryDumpManager::SetInstanceForTesting(mdm_
.get());
23 ASSERT_EQ(mdm_
, MemoryDumpManager::GetInstance());
24 MemoryDumpManager::GetInstance()->Initialize();
27 void TearDown() override
{
28 MemoryDumpManager::SetInstanceForTesting(nullptr);
30 TraceLog::DeleteForTesting();
34 const char* const kTraceCategory
= MemoryDumpManager::kTraceCategory
;
36 void EnableTracing(const char* category
) {
37 TraceLog::GetInstance()->SetEnabled(
38 CategoryFilter(category
), TraceLog::RECORDING_MODE
, TraceOptions());
41 void DisableTracing() { TraceLog::GetInstance()->SetDisabled(); }
43 scoped_ptr
<MemoryDumpManager
> mdm_
;
46 // We want our singleton torn down after each test.
47 ShadowingAtExitManager at_exit_manager_
;
50 class MockDumpProvider
: public MemoryDumpProvider
{
52 MOCK_METHOD1(DumpInto
, bool(ProcessMemoryDump
* pmd
));
54 const char* GetFriendlyName() const override
{ return "MockDumpProvider"; }
57 TEST_F(MemoryDumpManagerTest
, SingleDumper
) {
59 mdm_
->RegisterDumpProvider(&mdp
);
61 // Check that the dumper is not called if the memory category is not enabled.
62 EnableTracing("foo-and-bar-but-not-memory");
63 EXPECT_CALL(mdp
, DumpInto(_
)).Times(0);
64 mdm_
->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED
);
67 // Now repeat enabling the memory category and check that the dumper is
69 EnableTracing(kTraceCategory
);
70 EXPECT_CALL(mdp
, DumpInto(_
)).Times(3).WillRepeatedly(Return(true));
71 for (int i
= 0; i
< 3; ++i
)
72 mdm_
->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED
);
75 mdm_
->UnregisterDumpProvider(&mdp
);
77 // Finally check the unregister logic (no calls to the mdp after unregister).
78 EnableTracing(kTraceCategory
);
79 EXPECT_CALL(mdp
, DumpInto(_
)).Times(0);
80 mdm_
->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED
);
81 TraceLog::GetInstance()->SetDisabled();
84 TEST_F(MemoryDumpManagerTest
, UnregisterDumperWhileTracing
) {
86 mdm_
->RegisterDumpProvider(&mdp
);
88 EnableTracing(kTraceCategory
);
89 EXPECT_CALL(mdp
, DumpInto(_
)).Times(1).WillRepeatedly(Return(true));
90 mdm_
->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED
);
92 mdm_
->UnregisterDumpProvider(&mdp
);
93 EXPECT_CALL(mdp
, DumpInto(_
)).Times(0);
94 mdm_
->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED
);
99 TEST_F(MemoryDumpManagerTest
, MultipleDumpers
) {
100 MockDumpProvider mdp1
;
101 MockDumpProvider mdp2
;
104 mdm_
->RegisterDumpProvider(&mdp1
);
105 EnableTracing(kTraceCategory
);
106 EXPECT_CALL(mdp1
, DumpInto(_
)).Times(1).WillRepeatedly(Return(true));
107 EXPECT_CALL(mdp2
, DumpInto(_
)).Times(0);
108 mdm_
->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED
);
111 // Invert: enable mdp1 and disable mdp2.
112 mdm_
->UnregisterDumpProvider(&mdp1
);
113 mdm_
->RegisterDumpProvider(&mdp2
);
114 EnableTracing(kTraceCategory
);
115 EXPECT_CALL(mdp1
, DumpInto(_
)).Times(0);
116 EXPECT_CALL(mdp2
, DumpInto(_
)).Times(1).WillRepeatedly(Return(true));
117 mdm_
->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED
);
120 // Enable both mdp1 and mdp2.
121 mdm_
->RegisterDumpProvider(&mdp1
);
122 EnableTracing(kTraceCategory
);
123 EXPECT_CALL(mdp1
, DumpInto(_
)).Times(1).WillRepeatedly(Return(true));
124 EXPECT_CALL(mdp2
, DumpInto(_
)).Times(1).WillRepeatedly(Return(true));
125 mdm_
->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED
);
129 // Enable both dump providers, make mdp1 fail and assert that only mdp2 is
130 // invoked the 2nd time.
131 // FIXME(primiano): remove once crbug.com/461788 gets fixed.
132 TEST_F(MemoryDumpManagerTest
, DisableFailingDumpers
) {
133 MockDumpProvider mdp1
;
134 MockDumpProvider mdp2
;
136 mdm_
->RegisterDumpProvider(&mdp1
);
137 mdm_
->RegisterDumpProvider(&mdp2
);
138 EnableTracing(kTraceCategory
);
140 EXPECT_CALL(mdp1
, DumpInto(_
)).Times(1).WillRepeatedly(Return(false));
141 EXPECT_CALL(mdp2
, DumpInto(_
)).Times(1).WillRepeatedly(Return(true));
142 mdm_
->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED
);
144 EXPECT_CALL(mdp1
, DumpInto(_
)).Times(0);
145 EXPECT_CALL(mdp2
, DumpInto(_
)).Times(1).WillRepeatedly(Return(false));
146 mdm_
->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED
);
151 } // namespace trace_Event