Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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/trace_event/memory_tracing_frontend.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <memory> | |
| 10 #include <utility> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/bind_helpers.h" | |
| 14 #include "base/callback.h" | |
| 15 #include "base/memory/ptr_util.h" | |
| 16 #include "base/memory/ref_counted_memory.h" | |
| 17 #include "base/message_loop/message_loop.h" | |
| 18 #include "base/run_loop.h" | |
| 19 #include "base/strings/stringprintf.h" | |
| 20 #include "base/synchronization/waitable_event.h" | |
| 21 #include "base/test/sequenced_worker_pool_owner.h" | |
| 22 #include "base/test/test_io_thread.h" | |
| 23 #include "base/test/trace_event_analyzer.h" | |
| 24 #include "base/threading/platform_thread.h" | |
| 25 #include "base/threading/sequenced_task_runner_handle.h" | |
| 26 #include "base/threading/sequenced_worker_pool.h" | |
| 27 #include "base/threading/thread.h" | |
| 28 #include "base/threading/thread_task_runner_handle.h" | |
| 29 #include "base/trace_event/memory_dump_manager.h" | |
| 30 #include "base/trace_event/memory_dump_provider.h" | |
| 31 #include "base/trace_event/memory_dump_scheduler.h" | |
| 32 #include "base/trace_event/memory_infra_background_whitelist.h" | |
| 33 #include "base/trace_event/process_memory_dump.h" | |
| 34 #include "base/trace_event/trace_buffer.h" | |
| 35 #include "base/trace_event/trace_config_memory_test_util.h" | |
| 36 #include "build/build_config.h" | |
| 37 #include "testing/gmock/include/gmock/gmock.h" | |
| 38 #include "testing/gtest/include/gtest/gtest.h" | |
|
hjd
2017/04/13 14:16:59
^I will remove the unused ones.
| |
| 39 | |
| 40 using testing::_; | |
| 41 using testing::AnyNumber; | |
| 42 using testing::AtMost; | |
| 43 using testing::Between; | |
| 44 using testing::Invoke; | |
| 45 using testing::Return; | |
| 46 using testing::NiceMock; | |
| 47 | |
| 48 namespace base { | |
| 49 namespace trace_event { | |
| 50 | |
| 51 namespace { | |
| 52 void EnableTracingWithLegacyCategories(const char* category) { | |
| 53 TraceLog::GetInstance()->SetEnabled(TraceConfig(category, ""), | |
| 54 TraceLog::RECORDING_MODE); | |
| 55 } | |
| 56 | |
| 57 // void EnableTracingWithTraceConfig(const std::string& trace_config) { | |
| 58 // TraceLog::GetInstance()->SetEnabled(TraceConfig(trace_config), | |
| 59 // TraceLog::RECORDING_MODE); | |
| 60 //} | |
| 61 | |
| 62 void DisableTracing() { | |
| 63 TraceLog::GetInstance()->SetDisabled(); | |
| 64 } | |
| 65 } // namespace | |
| 66 | |
| 67 class MockMemoryDumpManager : public MemoryDumpManager { | |
| 68 public: | |
| 69 MockMemoryDumpManager() { | |
| 70 // ON_CALL(*this, RequestGlobalDump(_, _)) | |
| 71 // .WillByDefault(Invoke( | |
| 72 // this, | |
| 73 // &MemoryDumpManagerDelegateForTesting::CreateProcessDump)); | |
| 74 } | |
| 75 | |
| 76 ~MockMemoryDumpManager() override {} | |
| 77 | |
| 78 MOCK_METHOD0(Enable, void()); | |
| 79 MOCK_METHOD0(Disable, void()); | |
| 80 | |
| 81 // MOCK_METHOD3(RequestGlobalDump, | |
| 82 // void(MemoryDumpType dump_type, | |
| 83 // MemoryDumpLevelOfDetail level_of_detail, | |
| 84 // const MemoryDumpCallback& callback)); | |
| 85 }; | |
| 86 | |
| 87 class MockTraceLog : public TraceLog { | |
| 88 public: | |
| 89 MOCK_METHOD0(IsEnabled, bool()); | |
| 90 MOCK_METHOD1(AddEnabledStateObserver, void(EnabledStateObserver*)); | |
| 91 MOCK_METHOD1(RemoveEnabledStateObserver, void(EnabledStateObserver*)); | |
| 92 }; | |
| 93 | |
| 94 class MemoryTracingFrontendTest : public testing::Test { | |
| 95 public: | |
| 96 MemoryTracingFrontendTest() : testing::Test() { | |
| 97 mock_trace_log_ = MakeUnique<NiceMock<MockTraceLog>>(); | |
| 98 mock_memory_dump_manager_ = MakeUnique<MockMemoryDumpManager>(); | |
| 99 memory_tracing_frontend_ = MakeUnique<MemoryTracingFrontend>( | |
| 100 mock_trace_log_.get(), mock_memory_dump_manager_.get()); | |
| 101 ON_CALL(*mock_trace_log_, IsEnabled()).WillByDefault(Return(false)); | |
| 102 } | |
| 103 | |
| 104 ~MemoryTracingFrontendTest() override {} | |
| 105 | |
| 106 protected: | |
| 107 std::unique_ptr<MockMemoryDumpManager> mock_memory_dump_manager_; | |
| 108 std::unique_ptr<MockTraceLog> mock_trace_log_; | |
| 109 std::unique_ptr<MemoryTracingFrontend> memory_tracing_frontend_; | |
| 110 }; | |
| 111 | |
| 112 TEST(MemoryTracingFrontendConstructionTest, RegistersAndUnregisters) { | |
| 113 std::unique_ptr<MockTraceLog> mock_log(new NiceMock<MockTraceLog>()); | |
| 114 std::unique_ptr<MockMemoryDumpManager> mock_manager( | |
| 115 new MockMemoryDumpManager()); | |
| 116 ON_CALL(*mock_log, IsEnabled()).WillByDefault(Return(false)); | |
| 117 EXPECT_CALL(*mock_log, AddEnabledStateObserver(_)).Times(1); | |
| 118 EXPECT_CALL(*mock_log, RemoveEnabledStateObserver(_)).Times(1); | |
| 119 auto* frontend = | |
| 120 new MemoryTracingFrontend(mock_log.get(), mock_manager.get()); | |
| 121 delete frontend; | |
| 122 } | |
| 123 | |
| 124 TEST(MemoryTracingFrontendConstructionTest, EnablesIfTracingAlreadyOn) { | |
| 125 std::unique_ptr<MockTraceLog> mock_log(new NiceMock<MockTraceLog>()); | |
| 126 std::unique_ptr<MockMemoryDumpManager> mock_manager( | |
| 127 new MockMemoryDumpManager()); | |
| 128 EnableTracingWithLegacyCategories(MemoryTracingFrontend::kTraceCategory); | |
| 129 ON_CALL(*mock_log, IsEnabled()).WillByDefault(Return(true)); | |
| 130 EXPECT_CALL(*mock_manager, Enable()).Times(1); | |
| 131 auto* frontend = | |
| 132 new MemoryTracingFrontend(mock_log.get(), mock_manager.get()); | |
| 133 delete frontend; | |
| 134 DisableTracing(); | |
| 135 } | |
| 136 | |
| 137 TEST_F(MemoryTracingFrontendTest, IgnoresEnableWhenMemoryTracingOff) { | |
| 138 EXPECT_CALL(*mock_memory_dump_manager_, Enable()).Times(0); | |
| 139 EXPECT_CALL(*mock_memory_dump_manager_, Disable()).Times(0); | |
| 140 EnableTracingWithLegacyCategories("foobar-but-not-memory"); | |
| 141 memory_tracing_frontend_->OnTraceLogEnabled(); | |
| 142 memory_tracing_frontend_->OnTraceLogDisabled(); | |
| 143 DisableTracing(); | |
| 144 } | |
| 145 | |
| 146 TEST_F(MemoryTracingFrontendTest, ForwardsEnableWhenMemoryTracingOn) { | |
| 147 EXPECT_CALL(*mock_memory_dump_manager_, Enable()).Times(1); | |
| 148 EXPECT_CALL(*mock_memory_dump_manager_, Disable()).Times(1); | |
| 149 EnableTracingWithLegacyCategories(MemoryTracingFrontend::kTraceCategory); | |
| 150 memory_tracing_frontend_->OnTraceLogEnabled(); | |
| 151 memory_tracing_frontend_->OnTraceLogDisabled(); | |
| 152 DisableTracing(); | |
| 153 } | |
| 154 | |
| 155 } // namespace trace_event | |
| 156 } // namespace base | |
| OLD | NEW |