registered_objects_unittest.cc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2019 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/registered_objects.h"
  5. #include "base/memory/ptr_util.h"
  6. #include "base/test/gtest_util.h"
  7. #include "testing/gmock/include/gmock/gmock.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace performance_manager {
  10. namespace {
  11. class Registered {
  12. public:
  13. Registered() = default;
  14. virtual ~Registered() = default;
  15. virtual uintptr_t GetTypeId() const = 0;
  16. };
  17. class Foo : public Registered {
  18. public:
  19. Foo() = default;
  20. ~Foo() override = default;
  21. static uintptr_t TypeId() { return 0; }
  22. uintptr_t GetTypeId() const override { return TypeId(); }
  23. };
  24. class Bar : public Registered {
  25. public:
  26. Bar() = default;
  27. ~Bar() override = default;
  28. static uintptr_t TypeId() { return 1; }
  29. uintptr_t GetTypeId() const override { return TypeId(); }
  30. };
  31. } // namespace
  32. TEST(RegisteredObjectsTest, ContainerWorksAsAdvertised) {
  33. std::unique_ptr<RegisteredObjects<Registered>> registry(
  34. new RegisteredObjects<Registered>());
  35. ASSERT_NE(Foo::TypeId(), Bar::TypeId());
  36. EXPECT_FALSE(registry->GetRegisteredObject(Foo::TypeId()));
  37. EXPECT_FALSE(registry->GetRegisteredObject(Bar::TypeId()));
  38. // Insertion works.
  39. Foo foo;
  40. EXPECT_EQ(0u, registry->size());
  41. registry->RegisterObject(&foo);
  42. EXPECT_EQ(1u, registry->size());
  43. EXPECT_EQ(&foo, registry->GetRegisteredObject(Foo::TypeId()));
  44. EXPECT_FALSE(registry->GetRegisteredObject(Bar::TypeId()));
  45. // Inserting again fails.
  46. EXPECT_DCHECK_DEATH(registry->RegisterObject(&foo));
  47. // Unregistered the wrong object fails.
  48. Foo foo2;
  49. EXPECT_DCHECK_DEATH(registry->UnregisterObject(&foo2));
  50. // Unregistering works.
  51. registry->UnregisterObject(&foo);
  52. EXPECT_EQ(0u, registry->size());
  53. EXPECT_FALSE(registry->GetRegisteredObject(Foo::TypeId()));
  54. EXPECT_FALSE(registry->GetRegisteredObject(Bar::TypeId()));
  55. // Unregistering again fails.
  56. EXPECT_DCHECK_DEATH(registry->UnregisterObject(&foo));
  57. EXPECT_DCHECK_DEATH(registry->UnregisterObject(&foo2));
  58. // Registering multiple objects works.
  59. Bar bar;
  60. registry->RegisterObject(&foo);
  61. EXPECT_EQ(1u, registry->size());
  62. registry->RegisterObject(&bar);
  63. EXPECT_EQ(2u, registry->size());
  64. EXPECT_EQ(&foo, registry->GetRegisteredObject(Foo::TypeId()));
  65. EXPECT_EQ(&bar, registry->GetRegisteredObject(Bar::TypeId()));
  66. // Expect the container to explode if deleted with objects.
  67. EXPECT_DCHECK_DEATH(registry.reset());
  68. // Empty the registry.
  69. registry->UnregisterObject(&bar);
  70. EXPECT_EQ(1u, registry->size());
  71. registry->UnregisterObject(&foo);
  72. EXPECT_EQ(0u, registry->size());
  73. }
  74. } // namespace performance_manager