keep_alive_registry_unittest.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Copyright 2016 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/keep_alive_registry/keep_alive_registry.h"
  5. #include <memory>
  6. #include "base/memory/raw_ptr.h"
  7. #include "components/keep_alive_registry/keep_alive_state_observer.h"
  8. #include "components/keep_alive_registry/keep_alive_types.h"
  9. #include "components/keep_alive_registry/scoped_keep_alive.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. class KeepAliveRegistryTest : public testing::Test,
  12. public KeepAliveStateObserver {
  13. public:
  14. KeepAliveRegistryTest()
  15. : on_restart_allowed_call_count_(0),
  16. on_restart_forbidden_call_count_(0),
  17. start_keep_alive_call_count_(0),
  18. stop_keep_alive_call_count_(0),
  19. registry_(KeepAliveRegistry::GetInstance()) {
  20. registry_->AddObserver(this);
  21. EXPECT_FALSE(registry_->IsKeepingAlive());
  22. }
  23. ~KeepAliveRegistryTest() override {
  24. registry_->RemoveObserver(this);
  25. EXPECT_FALSE(registry_->IsKeepingAlive());
  26. }
  27. void OnKeepAliveStateChanged(bool is_keeping_alive) override {
  28. if (is_keeping_alive)
  29. ++start_keep_alive_call_count_;
  30. else
  31. ++stop_keep_alive_call_count_;
  32. }
  33. void OnKeepAliveRestartStateChanged(bool can_restart) override {
  34. if (can_restart)
  35. ++on_restart_allowed_call_count_;
  36. else
  37. ++on_restart_forbidden_call_count_;
  38. }
  39. protected:
  40. int on_restart_allowed_call_count_;
  41. int on_restart_forbidden_call_count_;
  42. int start_keep_alive_call_count_;
  43. int stop_keep_alive_call_count_;
  44. raw_ptr<KeepAliveRegistry> registry_;
  45. };
  46. // Test the IsKeepingAlive state and when we interact with the browser with
  47. // a KeepAlive registered.
  48. TEST_F(KeepAliveRegistryTest, BasicKeepAliveTest) {
  49. EXPECT_EQ(0, start_keep_alive_call_count_);
  50. EXPECT_EQ(0, stop_keep_alive_call_count_);
  51. {
  52. // Arbitrarily chosen Origin
  53. ScopedKeepAlive test_keep_alive(KeepAliveOrigin::CHROME_APP_DELEGATE,
  54. KeepAliveRestartOption::DISABLED);
  55. // We should require the browser to stay alive
  56. ASSERT_EQ(1, start_keep_alive_call_count_--); // decrement to ack
  57. EXPECT_TRUE(registry_->IsKeepingAlive());
  58. }
  59. // We should be back to normal now, notifying of the state change.
  60. ASSERT_EQ(1, stop_keep_alive_call_count_--);
  61. EXPECT_FALSE(registry_->IsKeepingAlive());
  62. // This should not have changed.
  63. ASSERT_EQ(0, start_keep_alive_call_count_);
  64. }
  65. // Test the IsKeepingAlive state and when we interact with the browser with
  66. // more than one KeepAlive registered.
  67. TEST_F(KeepAliveRegistryTest, DoubleKeepAliveTest) {
  68. EXPECT_EQ(0, start_keep_alive_call_count_);
  69. EXPECT_EQ(0, stop_keep_alive_call_count_);
  70. std::unique_ptr<ScopedKeepAlive> keep_alive_1, keep_alive_2;
  71. keep_alive_1 = std::make_unique<ScopedKeepAlive>(
  72. KeepAliveOrigin::CHROME_APP_DELEGATE, KeepAliveRestartOption::DISABLED);
  73. ASSERT_EQ(1, start_keep_alive_call_count_--); // decrement to ack
  74. EXPECT_TRUE(registry_->IsKeepingAlive());
  75. keep_alive_2 = std::make_unique<ScopedKeepAlive>(
  76. KeepAliveOrigin::CHROME_APP_DELEGATE, KeepAliveRestartOption::DISABLED);
  77. // We should not increment the count twice
  78. EXPECT_EQ(0, start_keep_alive_call_count_);
  79. EXPECT_TRUE(registry_->IsKeepingAlive());
  80. keep_alive_1.reset();
  81. // We should not decrement the count before the last keep alive is released.
  82. EXPECT_EQ(0, stop_keep_alive_call_count_);
  83. EXPECT_TRUE(registry_->IsKeepingAlive());
  84. keep_alive_2.reset();
  85. ASSERT_EQ(1, stop_keep_alive_call_count_--);
  86. EXPECT_EQ(0, start_keep_alive_call_count_);
  87. EXPECT_FALSE(registry_->IsKeepingAlive());
  88. }
  89. // Test the IsKeepingAlive state and when we interact with the browser with
  90. // more than one KeepAlive registered.
  91. TEST_F(KeepAliveRegistryTest, RestartOptionTest) {
  92. std::unique_ptr<ScopedKeepAlive> keep_alive, keep_alive_restart;
  93. EXPECT_EQ(0, on_restart_allowed_call_count_);
  94. EXPECT_EQ(0, on_restart_forbidden_call_count_);
  95. // With a normal keep alive, restart should not be allowed
  96. keep_alive = std::make_unique<ScopedKeepAlive>(
  97. KeepAliveOrigin::CHROME_APP_DELEGATE, KeepAliveRestartOption::DISABLED);
  98. ASSERT_EQ(1, on_restart_forbidden_call_count_--); // decrement to ack
  99. // Restart should not be allowed if all KA don't allow it.
  100. keep_alive_restart = std::make_unique<ScopedKeepAlive>(
  101. KeepAliveOrigin::CHROME_APP_DELEGATE, KeepAliveRestartOption::ENABLED);
  102. EXPECT_EQ(0, on_restart_allowed_call_count_);
  103. // Now restart should be allowed, the only one left allows it.
  104. keep_alive.reset();
  105. ASSERT_EQ(1, on_restart_allowed_call_count_--);
  106. // No keep alive, we should no prevent restarts.
  107. keep_alive.reset();
  108. EXPECT_EQ(0, on_restart_forbidden_call_count_);
  109. // Make sure all calls were checked.
  110. EXPECT_EQ(0, on_restart_allowed_call_count_);
  111. EXPECT_EQ(0, on_restart_forbidden_call_count_);
  112. }
  113. // Check that KeepAliveState is changed on attempting restarting,
  114. // if the remaining keepalive is only RestartOption::ENABLED.
  115. TEST_F(KeepAliveRegistryTest, AttemptRestarting) {
  116. std::unique_ptr<ScopedKeepAlive> keep_alive, keep_alive_restart;
  117. EXPECT_EQ(0, on_restart_allowed_call_count_);
  118. EXPECT_EQ(0, on_restart_forbidden_call_count_);
  119. // With a normal keep alive, restart should not be allowed
  120. keep_alive = std::make_unique<ScopedKeepAlive>(
  121. KeepAliveOrigin::CHROME_APP_DELEGATE, KeepAliveRestartOption::DISABLED);
  122. ASSERT_EQ(1, start_keep_alive_call_count_--); // decrement to ack
  123. ASSERT_EQ(1, on_restart_forbidden_call_count_--);
  124. // Restart should not be allowed if all KA don't allow it.
  125. keep_alive_restart = std::make_unique<ScopedKeepAlive>(
  126. KeepAliveOrigin::CHROME_APP_DELEGATE, KeepAliveRestartOption::ENABLED);
  127. // No state change.
  128. EXPECT_EQ(0, start_keep_alive_call_count_);
  129. EXPECT_EQ(0, on_restart_allowed_call_count_);
  130. // Now restart should be allowed, the only one left allows it.
  131. keep_alive.reset();
  132. EXPECT_EQ(0, start_keep_alive_call_count_);
  133. ASSERT_EQ(1, on_restart_allowed_call_count_--);
  134. EXPECT_TRUE(registry_->IsRestartAllowed());
  135. // Trigger the restarting procedure.
  136. registry_->SetRestarting();
  137. ASSERT_EQ(1, stop_keep_alive_call_count_);
  138. }
  139. TEST_F(KeepAliveRegistryTest,
  140. AttemptRestartingBeforeDestroyingDisabledKeepAlive) {
  141. std::unique_ptr<ScopedKeepAlive> keep_alive, keep_alive_restart;
  142. EXPECT_EQ(0, on_restart_allowed_call_count_);
  143. EXPECT_EQ(0, on_restart_forbidden_call_count_);
  144. // With a normal keep alive, restart should not be allowed
  145. keep_alive = std::make_unique<ScopedKeepAlive>(
  146. KeepAliveOrigin::CHROME_APP_DELEGATE, KeepAliveRestartOption::DISABLED);
  147. ASSERT_EQ(1, start_keep_alive_call_count_--); // decrement to ack
  148. ASSERT_EQ(1, on_restart_forbidden_call_count_--);
  149. // Restart should not be allowed if all KA don't allow it.
  150. keep_alive_restart = std::make_unique<ScopedKeepAlive>(
  151. KeepAliveOrigin::CHROME_APP_DELEGATE, KeepAliveRestartOption::ENABLED);
  152. // No state change.
  153. EXPECT_EQ(0, start_keep_alive_call_count_);
  154. EXPECT_EQ(0, on_restart_allowed_call_count_);
  155. // Trigger the restarting procedure, during normal keep alive is still
  156. // active.
  157. registry_->SetRestarting();
  158. EXPECT_EQ(0, stop_keep_alive_call_count_);
  159. // Now restart should be allowed, the only one left allows it.
  160. // This also updates KeepAliveState.
  161. keep_alive.reset();
  162. ASSERT_EQ(1, stop_keep_alive_call_count_--);
  163. ASSERT_EQ(1, on_restart_allowed_call_count_--);
  164. }
  165. TEST_F(KeepAliveRegistryTest, WouldRestartWithoutTest) {
  166. // WouldRestartWithout() should have the same results as IsRestartAllowed()
  167. // when called with an empty vector.
  168. std::vector<KeepAliveOrigin> empty_vector;
  169. // Init and sanity checks.
  170. ScopedKeepAlive kar(KeepAliveOrigin::BACKGROUND_MODE_MANAGER,
  171. KeepAliveRestartOption::ENABLED);
  172. ASSERT_TRUE(registry_->IsRestartAllowed());
  173. EXPECT_EQ(registry_->IsRestartAllowed(),
  174. registry_->WouldRestartWithout(empty_vector));
  175. ScopedKeepAlive ka1(KeepAliveOrigin::CHROME_APP_DELEGATE,
  176. KeepAliveRestartOption::DISABLED);
  177. ASSERT_FALSE(registry_->IsRestartAllowed());
  178. // Basic case: exclude one KeepAlive.
  179. EXPECT_TRUE(
  180. registry_->WouldRestartWithout({KeepAliveOrigin::CHROME_APP_DELEGATE}));
  181. EXPECT_FALSE(registry_->WouldRestartWithout(
  182. {KeepAliveOrigin::BACKGROUND_MODE_MANAGER}));
  183. // Check it works properly with multiple KeepAlives of the same type
  184. ScopedKeepAlive ka2(KeepAliveOrigin::CHROME_APP_DELEGATE,
  185. KeepAliveRestartOption::DISABLED);
  186. EXPECT_TRUE(
  187. registry_->WouldRestartWithout({KeepAliveOrigin::CHROME_APP_DELEGATE}));
  188. // Check it works properly with different KeepAlive types
  189. ScopedKeepAlive ka3(KeepAliveOrigin::PANEL, KeepAliveRestartOption::DISABLED);
  190. EXPECT_FALSE(
  191. registry_->WouldRestartWithout({KeepAliveOrigin::CHROME_APP_DELEGATE}));
  192. EXPECT_FALSE(registry_->WouldRestartWithout(
  193. {KeepAliveOrigin::BACKGROUND_MODE_MANAGER}));
  194. EXPECT_TRUE(registry_->WouldRestartWithout(
  195. {KeepAliveOrigin::CHROME_APP_DELEGATE, KeepAliveOrigin::PANEL}));
  196. EXPECT_EQ(registry_->IsRestartAllowed(),
  197. registry_->WouldRestartWithout(empty_vector));
  198. }