Chromium Code Reviews| Index: base/trace_event/memory_tracing_frontend_unittest.cc |
| diff --git a/base/trace_event/memory_tracing_frontend_unittest.cc b/base/trace_event/memory_tracing_frontend_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6354c5bc226edcb889ba5e9f53d78e3923a22134 |
| --- /dev/null |
| +++ b/base/trace_event/memory_tracing_frontend_unittest.cc |
| @@ -0,0 +1,156 @@ |
| +// Copyright 2017 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 "base/trace_event/memory_tracing_frontend.h" |
| + |
| +#include <stdint.h> |
| + |
| +#include <memory> |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "base/bind_helpers.h" |
| +#include "base/callback.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/memory/ref_counted_memory.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/run_loop.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "base/synchronization/waitable_event.h" |
| +#include "base/test/sequenced_worker_pool_owner.h" |
| +#include "base/test/test_io_thread.h" |
| +#include "base/test/trace_event_analyzer.h" |
| +#include "base/threading/platform_thread.h" |
| +#include "base/threading/sequenced_task_runner_handle.h" |
| +#include "base/threading/sequenced_worker_pool.h" |
| +#include "base/threading/thread.h" |
| +#include "base/threading/thread_task_runner_handle.h" |
| +#include "base/trace_event/memory_dump_manager.h" |
| +#include "base/trace_event/memory_dump_provider.h" |
| +#include "base/trace_event/memory_dump_scheduler.h" |
| +#include "base/trace_event/memory_infra_background_whitelist.h" |
| +#include "base/trace_event/process_memory_dump.h" |
| +#include "base/trace_event/trace_buffer.h" |
| +#include "base/trace_event/trace_config_memory_test_util.h" |
| +#include "build/build_config.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
|
hjd
2017/04/13 14:16:59
^I will remove the unused ones.
|
| + |
| +using testing::_; |
| +using testing::AnyNumber; |
| +using testing::AtMost; |
| +using testing::Between; |
| +using testing::Invoke; |
| +using testing::Return; |
| +using testing::NiceMock; |
| + |
| +namespace base { |
| +namespace trace_event { |
| + |
| +namespace { |
| +void EnableTracingWithLegacyCategories(const char* category) { |
| + TraceLog::GetInstance()->SetEnabled(TraceConfig(category, ""), |
| + TraceLog::RECORDING_MODE); |
| +} |
| + |
| +// void EnableTracingWithTraceConfig(const std::string& trace_config) { |
| +// TraceLog::GetInstance()->SetEnabled(TraceConfig(trace_config), |
| +// TraceLog::RECORDING_MODE); |
| +//} |
| + |
| +void DisableTracing() { |
| + TraceLog::GetInstance()->SetDisabled(); |
| +} |
| +} // namespace |
| + |
| +class MockMemoryDumpManager : public MemoryDumpManager { |
| + public: |
| + MockMemoryDumpManager() { |
| + // ON_CALL(*this, RequestGlobalDump(_, _)) |
| + // .WillByDefault(Invoke( |
| + // this, |
| + // &MemoryDumpManagerDelegateForTesting::CreateProcessDump)); |
| + } |
| + |
| + ~MockMemoryDumpManager() override {} |
| + |
| + MOCK_METHOD0(Enable, void()); |
| + MOCK_METHOD0(Disable, void()); |
| + |
| + // MOCK_METHOD3(RequestGlobalDump, |
| + // void(MemoryDumpType dump_type, |
| + // MemoryDumpLevelOfDetail level_of_detail, |
| + // const MemoryDumpCallback& callback)); |
| +}; |
| + |
| +class MockTraceLog : public TraceLog { |
| + public: |
| + MOCK_METHOD0(IsEnabled, bool()); |
| + MOCK_METHOD1(AddEnabledStateObserver, void(EnabledStateObserver*)); |
| + MOCK_METHOD1(RemoveEnabledStateObserver, void(EnabledStateObserver*)); |
| +}; |
| + |
| +class MemoryTracingFrontendTest : public testing::Test { |
| + public: |
| + MemoryTracingFrontendTest() : testing::Test() { |
| + mock_trace_log_ = MakeUnique<NiceMock<MockTraceLog>>(); |
| + mock_memory_dump_manager_ = MakeUnique<MockMemoryDumpManager>(); |
| + memory_tracing_frontend_ = MakeUnique<MemoryTracingFrontend>( |
| + mock_trace_log_.get(), mock_memory_dump_manager_.get()); |
| + ON_CALL(*mock_trace_log_, IsEnabled()).WillByDefault(Return(false)); |
| + } |
| + |
| + ~MemoryTracingFrontendTest() override {} |
| + |
| + protected: |
| + std::unique_ptr<MockMemoryDumpManager> mock_memory_dump_manager_; |
| + std::unique_ptr<MockTraceLog> mock_trace_log_; |
| + std::unique_ptr<MemoryTracingFrontend> memory_tracing_frontend_; |
| +}; |
| + |
| +TEST(MemoryTracingFrontendConstructionTest, RegistersAndUnregisters) { |
| + std::unique_ptr<MockTraceLog> mock_log(new NiceMock<MockTraceLog>()); |
| + std::unique_ptr<MockMemoryDumpManager> mock_manager( |
| + new MockMemoryDumpManager()); |
| + ON_CALL(*mock_log, IsEnabled()).WillByDefault(Return(false)); |
| + EXPECT_CALL(*mock_log, AddEnabledStateObserver(_)).Times(1); |
| + EXPECT_CALL(*mock_log, RemoveEnabledStateObserver(_)).Times(1); |
| + auto* frontend = |
| + new MemoryTracingFrontend(mock_log.get(), mock_manager.get()); |
| + delete frontend; |
| +} |
| + |
| +TEST(MemoryTracingFrontendConstructionTest, EnablesIfTracingAlreadyOn) { |
| + std::unique_ptr<MockTraceLog> mock_log(new NiceMock<MockTraceLog>()); |
| + std::unique_ptr<MockMemoryDumpManager> mock_manager( |
| + new MockMemoryDumpManager()); |
| + EnableTracingWithLegacyCategories(MemoryTracingFrontend::kTraceCategory); |
| + ON_CALL(*mock_log, IsEnabled()).WillByDefault(Return(true)); |
| + EXPECT_CALL(*mock_manager, Enable()).Times(1); |
| + auto* frontend = |
| + new MemoryTracingFrontend(mock_log.get(), mock_manager.get()); |
| + delete frontend; |
| + DisableTracing(); |
| +} |
| + |
| +TEST_F(MemoryTracingFrontendTest, IgnoresEnableWhenMemoryTracingOff) { |
| + EXPECT_CALL(*mock_memory_dump_manager_, Enable()).Times(0); |
| + EXPECT_CALL(*mock_memory_dump_manager_, Disable()).Times(0); |
| + EnableTracingWithLegacyCategories("foobar-but-not-memory"); |
| + memory_tracing_frontend_->OnTraceLogEnabled(); |
| + memory_tracing_frontend_->OnTraceLogDisabled(); |
| + DisableTracing(); |
| +} |
| + |
| +TEST_F(MemoryTracingFrontendTest, ForwardsEnableWhenMemoryTracingOn) { |
| + EXPECT_CALL(*mock_memory_dump_manager_, Enable()).Times(1); |
| + EXPECT_CALL(*mock_memory_dump_manager_, Disable()).Times(1); |
| + EnableTracingWithLegacyCategories(MemoryTracingFrontend::kTraceCategory); |
| + memory_tracing_frontend_->OnTraceLogEnabled(); |
| + memory_tracing_frontend_->OnTraceLogDisabled(); |
| + DisableTracing(); |
| +} |
| + |
| +} // namespace trace_event |
| +} // namespace base |