performance_manager_registry_impl_unittest.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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/performance_manager_registry_impl.h"
  5. #include "base/memory/ptr_util.h"
  6. #include "base/test/gtest_util.h"
  7. #include "components/performance_manager/public/performance_manager_main_thread_mechanism.h"
  8. #include "components/performance_manager/public/performance_manager_main_thread_observer.h"
  9. #include "components/performance_manager/public/performance_manager_owned.h"
  10. #include "components/performance_manager/public/performance_manager_registered.h"
  11. #include "components/performance_manager/test_support/performance_manager_test_harness.h"
  12. #include "content/public/browser/navigation_handle.h"
  13. #include "content/public/test/mock_navigation_handle.h"
  14. #include "content/public/test/test_navigation_throttle.h"
  15. #include "testing/gmock/include/gmock/gmock.h"
  16. #include "testing/gtest/include/gtest/gtest.h"
  17. namespace performance_manager {
  18. namespace {
  19. class PerformanceManagerRegistryImplTest
  20. : public PerformanceManagerTestHarness {
  21. public:
  22. PerformanceManagerRegistryImplTest() = default;
  23. };
  24. class LenientMockObserver : public PerformanceManagerMainThreadObserver {
  25. public:
  26. LenientMockObserver() = default;
  27. ~LenientMockObserver() override = default;
  28. MOCK_METHOD1(OnPageNodeCreatedForWebContents, void(content::WebContents*));
  29. MOCK_METHOD0(OnBeforePerformanceManagerDestroyed, void());
  30. };
  31. using MockObserver = ::testing::StrictMock<LenientMockObserver>;
  32. class LenientMockMechanism : public PerformanceManagerMainThreadMechanism {
  33. public:
  34. LenientMockMechanism() = default;
  35. ~LenientMockMechanism() override = default;
  36. void ExpectCallToCreateThrottlesForNavigation(
  37. content::NavigationHandle* handle,
  38. Throttles throttles_to_return) {
  39. throttles_to_return_ = std::move(throttles_to_return);
  40. EXPECT_CALL(*this, OnCreateThrottlesForNavigation(handle));
  41. }
  42. private:
  43. MOCK_METHOD1(OnCreateThrottlesForNavigation,
  44. void(content::NavigationHandle*));
  45. // PerformanceManagerMainThreadMechanism implementation:
  46. // GMock doesn't support move-only types, so we use a custom wrapper to work
  47. // around this.
  48. Throttles CreateThrottlesForNavigation(
  49. content::NavigationHandle* handle) override {
  50. OnCreateThrottlesForNavigation(handle);
  51. return std::move(throttles_to_return_);
  52. }
  53. Throttles throttles_to_return_;
  54. };
  55. using MockMechanism = ::testing::StrictMock<LenientMockMechanism>;
  56. } // namespace
  57. TEST_F(PerformanceManagerRegistryImplTest, ObserverWorks) {
  58. MockObserver observer;
  59. PerformanceManagerRegistryImpl* registry =
  60. PerformanceManagerRegistryImpl::GetInstance();
  61. registry->AddObserver(&observer);
  62. std::unique_ptr<content::WebContents> contents =
  63. content::RenderViewHostTestHarness::CreateTestWebContents();
  64. EXPECT_CALL(observer, OnPageNodeCreatedForWebContents(contents.get()));
  65. registry->CreatePageNodeForWebContents(contents.get());
  66. testing::Mock::VerifyAndClear(&observer);
  67. // Expect a tear down notification, and use it to unregister ourselves.
  68. EXPECT_CALL(observer, OnBeforePerformanceManagerDestroyed())
  69. .WillOnce(testing::Invoke(
  70. [&registry, &observer]() { registry->RemoveObserver(&observer); }));
  71. TearDownNow();
  72. }
  73. TEST_F(PerformanceManagerRegistryImplTest,
  74. MechanismCreateThrottlesForNavigation) {
  75. MockMechanism mechanism1, mechanism2;
  76. PerformanceManagerRegistryImpl* registry =
  77. PerformanceManagerRegistryImpl::GetInstance();
  78. registry->AddMechanism(&mechanism1);
  79. registry->AddMechanism(&mechanism2);
  80. std::unique_ptr<content::WebContents> contents =
  81. content::RenderViewHostTestHarness::CreateTestWebContents();
  82. std::unique_ptr<content::NavigationHandle> handle =
  83. std::make_unique<content::MockNavigationHandle>(contents.get());
  84. std::unique_ptr<content::NavigationThrottle> throttle1 =
  85. std::make_unique<content::TestNavigationThrottle>(handle.get());
  86. std::unique_ptr<content::NavigationThrottle> throttle2 =
  87. std::make_unique<content::TestNavigationThrottle>(handle.get());
  88. std::unique_ptr<content::NavigationThrottle> throttle3 =
  89. std::make_unique<content::TestNavigationThrottle>(handle.get());
  90. auto* raw_throttle1 = throttle1.get();
  91. auto* raw_throttle2 = throttle2.get();
  92. auto* raw_throttle3 = throttle3.get();
  93. MockMechanism::Throttles throttles1, throttles2;
  94. throttles1.push_back(std::move(throttle1));
  95. throttles2.push_back(std::move(throttle2));
  96. throttles2.push_back(std::move(throttle3));
  97. mechanism1.ExpectCallToCreateThrottlesForNavigation(handle.get(),
  98. std::move(throttles1));
  99. mechanism2.ExpectCallToCreateThrottlesForNavigation(handle.get(),
  100. std::move(throttles2));
  101. auto throttles = registry->CreateThrottlesForNavigation(handle.get());
  102. testing::Mock::VerifyAndClear(&mechanism1);
  103. testing::Mock::VerifyAndClear(&mechanism2);
  104. // Expect that the throttles from both mechanisms were combined into one
  105. // list.
  106. ASSERT_EQ(3u, throttles.size());
  107. EXPECT_EQ(raw_throttle1, throttles[0].get());
  108. EXPECT_EQ(raw_throttle2, throttles[1].get());
  109. EXPECT_EQ(raw_throttle3, throttles[2].get());
  110. registry->RemoveMechanism(&mechanism1);
  111. registry->RemoveMechanism(&mechanism2);
  112. }
  113. namespace {
  114. class LenientOwned : public PerformanceManagerOwned {
  115. public:
  116. LenientOwned() = default;
  117. ~LenientOwned() override { OnDestructor(); }
  118. LenientOwned(const LenientOwned&) = delete;
  119. LenientOwned& operator=(const LenientOwned) = delete;
  120. // PerformanceManagerOwned implementation:
  121. MOCK_METHOD0(OnPassedToPM, void());
  122. MOCK_METHOD0(OnTakenFromPM, void());
  123. MOCK_METHOD0(OnDestructor, void());
  124. };
  125. using Owned = testing::StrictMock<LenientOwned>;
  126. } // namespace
  127. TEST_F(PerformanceManagerRegistryImplTest, PerformanceManagerOwned) {
  128. PerformanceManagerRegistryImpl* registry =
  129. PerformanceManagerRegistryImpl::GetInstance();
  130. std::unique_ptr<Owned> owned1 = std::make_unique<Owned>();
  131. std::unique_ptr<Owned> owned2 = std::make_unique<Owned>();
  132. auto* raw1 = owned1.get();
  133. auto* raw2 = owned2.get();
  134. // Pass both objects to the PM.
  135. EXPECT_EQ(0u, registry->GetOwnedCountForTesting());
  136. EXPECT_CALL(*raw1, OnPassedToPM());
  137. PerformanceManager::PassToPM(std::move(owned1));
  138. EXPECT_EQ(1u, registry->GetOwnedCountForTesting());
  139. EXPECT_CALL(*raw2, OnPassedToPM());
  140. PerformanceManager::PassToPM(std::move(owned2));
  141. EXPECT_EQ(2u, registry->GetOwnedCountForTesting());
  142. // Take one back.
  143. EXPECT_CALL(*raw1, OnTakenFromPM());
  144. owned1 = PerformanceManager::TakeFromPMAs<Owned>(raw1);
  145. EXPECT_EQ(1u, registry->GetOwnedCountForTesting());
  146. // Destroy that object and expect its destructor to have been invoked.
  147. EXPECT_CALL(*raw1, OnDestructor());
  148. owned1.reset();
  149. raw1 = nullptr;
  150. // Tear down the PM and expect the other object to die with it.
  151. EXPECT_CALL(*raw2, OnTakenFromPM());
  152. EXPECT_CALL(*raw2, OnDestructor());
  153. TearDownNow();
  154. }
  155. namespace {
  156. class Foo : public PerformanceManagerRegisteredImpl<Foo> {
  157. public:
  158. Foo() = default;
  159. ~Foo() override = default;
  160. };
  161. class Bar : public PerformanceManagerRegisteredImpl<Bar> {
  162. public:
  163. Bar() = default;
  164. ~Bar() override = default;
  165. };
  166. } // namespace
  167. TEST_F(PerformanceManagerRegistryImplTest, RegistrationWorks) {
  168. // The bulk of the tests for the registry are done in the RegisteredObjects
  169. // templated base class. This simply checks the additional functionality
  170. // endowed by the PM wrapper.
  171. PerformanceManagerRegistryImpl* registry =
  172. PerformanceManagerRegistryImpl::GetInstance();
  173. // This ensures that the templated distinct TypeId generation works.
  174. ASSERT_NE(Foo::TypeId(), Bar::TypeId());
  175. EXPECT_EQ(0u, registry->GetRegisteredCountForTesting());
  176. EXPECT_FALSE(PerformanceManager::GetRegisteredObjectAs<Foo>());
  177. EXPECT_FALSE(PerformanceManager::GetRegisteredObjectAs<Bar>());
  178. // Insertion works.
  179. Foo foo;
  180. Bar bar;
  181. PerformanceManager::RegisterObject(&foo);
  182. EXPECT_EQ(1u, registry->GetRegisteredCountForTesting());
  183. PerformanceManager::RegisterObject(&bar);
  184. EXPECT_EQ(2u, registry->GetRegisteredCountForTesting());
  185. EXPECT_EQ(&foo, PerformanceManager::GetRegisteredObjectAs<Foo>());
  186. EXPECT_EQ(&bar, PerformanceManager::GetRegisteredObjectAs<Bar>());
  187. // Check the various helper functions.
  188. EXPECT_EQ(&foo, Foo::GetFromPM());
  189. EXPECT_EQ(&bar, Bar::GetFromPM());
  190. EXPECT_TRUE(foo.IsRegisteredInPM());
  191. EXPECT_TRUE(bar.IsRegisteredInPM());
  192. EXPECT_FALSE(Foo::NothingRegisteredInPM());
  193. EXPECT_FALSE(Bar::NothingRegisteredInPM());
  194. PerformanceManager::UnregisterObject(&bar);
  195. EXPECT_EQ(1u, registry->GetRegisteredCountForTesting());
  196. EXPECT_EQ(&foo, Foo::GetFromPM());
  197. EXPECT_FALSE(Bar::GetFromPM());
  198. EXPECT_TRUE(foo.IsRegisteredInPM());
  199. EXPECT_FALSE(bar.IsRegisteredInPM());
  200. EXPECT_FALSE(Foo::NothingRegisteredInPM());
  201. EXPECT_TRUE(Bar::NothingRegisteredInPM());
  202. PerformanceManager::UnregisterObject(&foo);
  203. EXPECT_EQ(0u, registry->GetRegisteredCountForTesting());
  204. }
  205. } // namespace performance_manager