graph_registered_unittest.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2020 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. #include "components/performance_manager/public/graph/graph_registered.h"
  5. #include "base/test/gtest_util.h"
  6. #include "components/performance_manager/performance_manager_registry_impl.h"
  7. #include "components/performance_manager/public/performance_manager.h"
  8. #include "components/performance_manager/test_support/graph_test_harness.h"
  9. #include "components/performance_manager/test_support/performance_manager_test_harness.h"
  10. #include "testing/gmock/include/gmock/gmock.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace performance_manager {
  13. using GraphRegisteredTest = GraphTestHarness;
  14. class Foo : public GraphRegisteredImpl<Foo> {
  15. public:
  16. Foo() = default;
  17. ~Foo() override = default;
  18. };
  19. class Bar : public GraphRegisteredImpl<Bar> {
  20. public:
  21. Bar() = default;
  22. ~Bar() override = default;
  23. };
  24. TEST_F(GraphRegisteredTest, GraphRegistrationWorks) {
  25. // This ensures that the templated distinct TypeId generation works.
  26. ASSERT_NE(Foo::TypeId(), Bar::TypeId());
  27. EXPECT_FALSE(graph()->GetRegisteredObject(Foo::TypeId()));
  28. EXPECT_FALSE(graph()->GetRegisteredObjectAs<Foo>());
  29. EXPECT_FALSE(graph()->GetRegisteredObject(Bar::TypeId()));
  30. EXPECT_FALSE(graph()->GetRegisteredObjectAs<Bar>());
  31. // Insertion works.
  32. Foo foo;
  33. graph()->RegisterObject(&foo);
  34. EXPECT_EQ(&foo, graph()->GetRegisteredObject(Foo::TypeId()));
  35. EXPECT_EQ(&foo, graph()->GetRegisteredObjectAs<Foo>());
  36. EXPECT_FALSE(graph()->GetRegisteredObject(Bar::TypeId()));
  37. EXPECT_FALSE(graph()->GetRegisteredObjectAs<Bar>());
  38. // Inserting again fails.
  39. EXPECT_DCHECK_DEATH(graph()->RegisterObject(&foo));
  40. // Unregistered the wrong object fails.
  41. Foo foo2;
  42. EXPECT_DCHECK_DEATH(graph()->UnregisterObject(&foo2));
  43. // Unregistering works.
  44. graph()->UnregisterObject(&foo);
  45. EXPECT_FALSE(graph()->GetRegisteredObject(Foo::TypeId()));
  46. EXPECT_FALSE(graph()->GetRegisteredObjectAs<Foo>());
  47. EXPECT_FALSE(graph()->GetRegisteredObject(Bar::TypeId()));
  48. EXPECT_FALSE(graph()->GetRegisteredObjectAs<Bar>());
  49. // Unregistering again fails.
  50. EXPECT_DCHECK_DEATH(graph()->UnregisterObject(&foo));
  51. EXPECT_DCHECK_DEATH(graph()->UnregisterObject(&foo2));
  52. // Registering multiple objects works.
  53. Bar bar;
  54. graph()->RegisterObject(&foo);
  55. graph()->RegisterObject(&bar);
  56. EXPECT_EQ(&foo, graph()->GetRegisteredObject(Foo::TypeId()));
  57. EXPECT_EQ(&foo, graph()->GetRegisteredObjectAs<Foo>());
  58. EXPECT_EQ(&bar, graph()->GetRegisteredObject(Bar::TypeId()));
  59. EXPECT_EQ(&bar, graph()->GetRegisteredObjectAs<Bar>());
  60. // Check the various helper functions.
  61. EXPECT_EQ(&foo, Foo::GetFromGraph(graph()));
  62. EXPECT_EQ(&bar, Bar::GetFromGraph(graph()));
  63. EXPECT_TRUE(foo.IsRegistered(graph()));
  64. EXPECT_TRUE(bar.IsRegistered(graph()));
  65. EXPECT_FALSE(Foo::NothingRegistered(graph()));
  66. EXPECT_FALSE(Bar::NothingRegistered(graph()));
  67. graph()->UnregisterObject(&bar);
  68. EXPECT_EQ(&foo, Foo::GetFromGraph(graph()));
  69. EXPECT_FALSE(Bar::GetFromGraph(graph()));
  70. EXPECT_TRUE(foo.IsRegistered(graph()));
  71. EXPECT_FALSE(bar.IsRegistered(graph()));
  72. EXPECT_FALSE(Foo::NothingRegistered(graph()));
  73. EXPECT_TRUE(Bar::NothingRegistered(graph()));
  74. // At this point if the graph is torn down it should explode because foo
  75. // hasn't been unregistered.
  76. EXPECT_DCHECK_DEATH(TearDownAndDestroyGraph());
  77. graph()->UnregisterObject(&foo);
  78. }
  79. namespace {
  80. // This class is non-sensically both a GraphRegistered object and a
  81. // PerformanceManagerRegistered object. This is done to ensure that the
  82. // implementations use the appropriately typed "TypeId" functions, as there are
  83. // now two of them available!
  84. class Baz : public GraphRegisteredImpl<Baz>,
  85. public PerformanceManagerRegisteredImpl<Baz> {
  86. public:
  87. Baz() = default;
  88. ~Baz() override = default;
  89. };
  90. using GraphAndPerformanceManagerRegisteredTest = PerformanceManagerTestHarness;
  91. } // namespace
  92. TEST_F(GraphAndPerformanceManagerRegisteredTest, GraphAndPMRegistered) {
  93. // Each TypeId should be backed by a distinct "TypeId" implementation and
  94. // value.
  95. const uintptr_t kGraphId = GraphRegisteredImpl<Baz>::TypeId();
  96. const uintptr_t kPMId = PerformanceManagerRegisteredImpl<Baz>::TypeId();
  97. ASSERT_NE(kGraphId, kPMId);
  98. // Create a stand-alone graph that is bound to this sequence so we can test
  99. // both the PM and a graph on the same sequence.
  100. std::unique_ptr<GraphImpl> graph(new GraphImpl());
  101. PerformanceManagerRegistryImpl* registry =
  102. PerformanceManagerRegistryImpl::GetInstance();
  103. Baz baz;
  104. graph->RegisterObject(&baz);
  105. registry->RegisterObject(&baz);
  106. EXPECT_EQ(&baz, graph->GetRegisteredObject(kGraphId));
  107. EXPECT_EQ(&baz, registry->GetRegisteredObject(kPMId));
  108. EXPECT_EQ(nullptr, graph->GetRegisteredObject(kPMId));
  109. EXPECT_EQ(nullptr, registry->GetRegisteredObject(kGraphId));
  110. // This directly tests that the templated helper function uses the correct
  111. // instance of TypeId.
  112. EXPECT_EQ(&baz, graph->GetRegisteredObjectAs<Baz>());
  113. EXPECT_EQ(&baz, PerformanceManager::GetRegisteredObjectAs<Baz>());
  114. graph->UnregisterObject(&baz);
  115. registry->UnregisterObject(&baz);
  116. graph->TearDown();
  117. graph.reset();
  118. }
  119. } // namespace performance_manager