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 "mojo/core/handle_table.h"
6
7 #include <memory>
8
9 #include "base/synchronization/lock.h"
10 #include "base/trace_event/memory_allocator_dump.h"
11 #include "base/trace_event/memory_dump_request_args.h"
12 #include "base/trace_event/process_memory_dump.h"
13 #include "base/trace_event/trace_event_argument.h"
14 #include "mojo/core/dispatcher.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 using base::trace_event::MemoryAllocatorDump;
19 using testing::Contains;
20 using testing::Eq;
21 using testing::Contains;
22 using testing::ByRef;
23
24 namespace mojo {
25 namespace core {
26 namespace {
27
28 class FakeMessagePipeDispatcher : public Dispatcher {
29 public:
FakeMessagePipeDispatcher()30 FakeMessagePipeDispatcher() {}
31
GetType() const32 Type GetType() const override { return Type::MESSAGE_PIPE; }
33
Close()34 MojoResult Close() override { return MOJO_RESULT_OK; }
35
36 private:
~FakeMessagePipeDispatcher()37 ~FakeMessagePipeDispatcher() override {}
38 DISALLOW_COPY_AND_ASSIGN(FakeMessagePipeDispatcher);
39 };
40
CheckNameAndValue(base::trace_event::ProcessMemoryDump * pmd,const std::string & name,uint64_t value)41 void CheckNameAndValue(base::trace_event::ProcessMemoryDump* pmd,
42 const std::string& name,
43 uint64_t value) {
44 base::trace_event::MemoryAllocatorDump* mad = pmd->GetAllocatorDump(name);
45 ASSERT_TRUE(mad);
46
47 MemoryAllocatorDump::Entry expected(
48 "object_count", MemoryAllocatorDump::kUnitsObjects, value);
49 EXPECT_THAT(mad->entries(), Contains(Eq(ByRef(expected))));
50 }
51
52 } // namespace
53
TEST(HandleTableTest,OnMemoryDump)54 TEST(HandleTableTest, OnMemoryDump) {
55 HandleTable ht;
56
57 {
58 base::AutoLock auto_lock(ht.GetLock());
59 scoped_refptr<Dispatcher> dispatcher(new FakeMessagePipeDispatcher);
60 ht.AddDispatcher(dispatcher);
61 }
62
63 base::trace_event::MemoryDumpArgs args = {
64 base::trace_event::MemoryDumpLevelOfDetail::DETAILED};
65 base::trace_event::ProcessMemoryDump pmd(args);
66 ht.OnMemoryDump(args, &pmd);
67
68 CheckNameAndValue(&pmd, "mojo/message_pipe", 1);
69 CheckNameAndValue(&pmd, "mojo/data_pipe_consumer", 0);
70 }
71
72 } // namespace core
73 } // namespace mojo
74