| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/lifetime/keep_alive_registry.h" | 5 #include "chrome/browser/lifetime/keep_alive_registry.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "chrome/browser/lifetime/application_lifetime.h" | 8 #include "chrome/browser/lifetime/application_lifetime.h" |
| 9 #include "chrome/browser/lifetime/keep_alive_state_observer.h" | 9 #include "chrome/browser/lifetime/keep_alive_state_observer.h" |
| 10 #include "chrome/browser/lifetime/keep_alive_types.h" | 10 #include "chrome/browser/lifetime/keep_alive_types.h" |
| 11 #include "chrome/browser/lifetime/scoped_keep_alive.h" | 11 #include "chrome/browser/lifetime/scoped_keep_alive.h" |
| 12 #include "chrome/test/base/testing_browser_process.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 13 |
| 15 class KeepAliveRegistryTest : public testing::Test, | 14 class KeepAliveRegistryTest : public testing::Test, |
| 16 public KeepAliveStateObserver { | 15 public KeepAliveStateObserver { |
| 17 public: | 16 public: |
| 18 KeepAliveRegistryTest() | 17 KeepAliveRegistryTest() |
| 19 : on_restart_allowed_call_count_(0), | 18 : on_restart_allowed_call_count_(0), |
| 20 on_restart_forbidden_call_count_(0), | 19 on_restart_forbidden_call_count_(0), |
| 20 start_keep_alive_call_count_(0), |
| 21 stop_keep_alive_call_count_(0), |
| 21 registry_(KeepAliveRegistry::GetInstance()) { | 22 registry_(KeepAliveRegistry::GetInstance()) { |
| 22 registry_->AddObserver(this); | 23 registry_->AddObserver(this); |
| 23 | 24 |
| 24 EXPECT_FALSE(registry_->IsKeepingAlive()); | 25 EXPECT_FALSE(registry_->IsKeepingAlive()); |
| 25 } | 26 } |
| 26 | 27 |
| 27 ~KeepAliveRegistryTest() override { | 28 ~KeepAliveRegistryTest() override { |
| 28 registry_->RemoveObserver(this); | 29 registry_->RemoveObserver(this); |
| 29 | 30 |
| 30 EXPECT_FALSE(registry_->IsKeepingAlive()); | 31 EXPECT_FALSE(registry_->IsKeepingAlive()); |
| 31 } | 32 } |
| 32 | 33 |
| 34 void OnKeepAliveStateChanged(bool is_keeping_alive) override { |
| 35 if (is_keeping_alive) |
| 36 ++start_keep_alive_call_count_; |
| 37 else |
| 38 ++stop_keep_alive_call_count_; |
| 39 } |
| 40 |
| 33 void OnKeepAliveRestartStateChanged(bool can_restart) override { | 41 void OnKeepAliveRestartStateChanged(bool can_restart) override { |
| 34 if (can_restart) | 42 if (can_restart) |
| 35 ++on_restart_allowed_call_count_; | 43 ++on_restart_allowed_call_count_; |
| 36 else | 44 else |
| 37 ++on_restart_forbidden_call_count_; | 45 ++on_restart_forbidden_call_count_; |
| 38 } | 46 } |
| 39 | 47 |
| 40 protected: | 48 protected: |
| 41 int on_restart_allowed_call_count_; | 49 int on_restart_allowed_call_count_; |
| 42 int on_restart_forbidden_call_count_; | 50 int on_restart_forbidden_call_count_; |
| 51 int start_keep_alive_call_count_; |
| 52 int stop_keep_alive_call_count_; |
| 43 KeepAliveRegistry* registry_; | 53 KeepAliveRegistry* registry_; |
| 44 }; | 54 }; |
| 45 | 55 |
| 46 // Test the IsKeepingAlive state and when we interact with the browser with | 56 // Test the IsKeepingAlive state and when we interact with the browser with |
| 47 // a KeepAlive registered. | 57 // a KeepAlive registered. |
| 48 TEST_F(KeepAliveRegistryTest, BasicKeepAliveTest) { | 58 TEST_F(KeepAliveRegistryTest, BasicKeepAliveTest) { |
| 49 TestingBrowserProcess* browser_process = TestingBrowserProcess::GetGlobal(); | 59 EXPECT_EQ(0, start_keep_alive_call_count_); |
| 50 const unsigned int base_module_ref_count = | 60 EXPECT_EQ(0, stop_keep_alive_call_count_); |
| 51 browser_process->module_ref_count(); | |
| 52 KeepAliveRegistry* registry = KeepAliveRegistry::GetInstance(); | |
| 53 | |
| 54 EXPECT_FALSE(registry->IsKeepingAlive()); | |
| 55 | 61 |
| 56 { | 62 { |
| 57 // Arbitrarily chosen Origin | 63 // Arbitrarily chosen Origin |
| 58 ScopedKeepAlive test_keep_alive(KeepAliveOrigin::CHROME_APP_DELEGATE, | 64 ScopedKeepAlive test_keep_alive(KeepAliveOrigin::CHROME_APP_DELEGATE, |
| 59 KeepAliveRestartOption::DISABLED); | 65 KeepAliveRestartOption::DISABLED); |
| 60 | 66 |
| 61 // We should require the browser to stay alive | 67 // We should require the browser to stay alive |
| 62 EXPECT_EQ(base_module_ref_count + 1, browser_process->module_ref_count()); | 68 ASSERT_EQ(1, start_keep_alive_call_count_--); // decrement to ack |
| 63 EXPECT_TRUE(registry_->IsKeepingAlive()); | 69 EXPECT_TRUE(registry_->IsKeepingAlive()); |
| 64 } | 70 } |
| 65 | 71 |
| 66 // We should be back to normal now. | 72 // We should be back to normal now, notifying of the state change. |
| 67 EXPECT_EQ(base_module_ref_count, browser_process->module_ref_count()); | 73 ASSERT_EQ(1, stop_keep_alive_call_count_--); |
| 68 EXPECT_FALSE(registry_->IsKeepingAlive()); | 74 EXPECT_FALSE(registry_->IsKeepingAlive()); |
| 75 |
| 76 // This should not have changed. |
| 77 ASSERT_EQ(0, start_keep_alive_call_count_); |
| 69 } | 78 } |
| 70 | 79 |
| 71 // Test the IsKeepingAlive state and when we interact with the browser with | 80 // Test the IsKeepingAlive state and when we interact with the browser with |
| 72 // more than one KeepAlive registered. | 81 // more than one KeepAlive registered. |
| 73 TEST_F(KeepAliveRegistryTest, DoubleKeepAliveTest) { | 82 TEST_F(KeepAliveRegistryTest, DoubleKeepAliveTest) { |
| 74 TestingBrowserProcess* browser_process = TestingBrowserProcess::GetGlobal(); | 83 EXPECT_EQ(0, start_keep_alive_call_count_); |
| 75 const unsigned int base_module_ref_count = | 84 EXPECT_EQ(0, stop_keep_alive_call_count_); |
| 76 browser_process->module_ref_count(); | |
| 77 scoped_ptr<ScopedKeepAlive> keep_alive_1, keep_alive_2; | 85 scoped_ptr<ScopedKeepAlive> keep_alive_1, keep_alive_2; |
| 78 | 86 |
| 79 keep_alive_1.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE, | 87 keep_alive_1.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE, |
| 80 KeepAliveRestartOption::DISABLED)); | 88 KeepAliveRestartOption::DISABLED)); |
| 81 EXPECT_EQ(base_module_ref_count + 1, browser_process->module_ref_count()); | 89 ASSERT_EQ(1, start_keep_alive_call_count_--); // decrement to ack |
| 82 EXPECT_TRUE(registry_->IsKeepingAlive()); | 90 EXPECT_TRUE(registry_->IsKeepingAlive()); |
| 83 | 91 |
| 84 keep_alive_2.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE, | 92 keep_alive_2.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE, |
| 85 KeepAliveRestartOption::DISABLED)); | 93 KeepAliveRestartOption::DISABLED)); |
| 86 // We should not increment the count twice | 94 // We should not increment the count twice |
| 87 EXPECT_EQ(base_module_ref_count + 1, browser_process->module_ref_count()); | 95 EXPECT_EQ(0, start_keep_alive_call_count_); |
| 88 EXPECT_TRUE(registry_->IsKeepingAlive()); | 96 EXPECT_TRUE(registry_->IsKeepingAlive()); |
| 89 | 97 |
| 90 keep_alive_1.reset(); | 98 keep_alive_1.reset(); |
| 91 // We should not decrement the count before the last keep alive is released. | 99 // We should not decrement the count before the last keep alive is released. |
| 92 EXPECT_EQ(base_module_ref_count + 1, browser_process->module_ref_count()); | 100 EXPECT_EQ(0, stop_keep_alive_call_count_); |
| 93 EXPECT_TRUE(registry_->IsKeepingAlive()); | 101 EXPECT_TRUE(registry_->IsKeepingAlive()); |
| 94 | 102 |
| 95 keep_alive_2.reset(); | 103 keep_alive_2.reset(); |
| 96 EXPECT_EQ(base_module_ref_count, browser_process->module_ref_count()); | 104 ASSERT_EQ(1, stop_keep_alive_call_count_--); |
| 105 EXPECT_EQ(0, start_keep_alive_call_count_); |
| 97 EXPECT_FALSE(registry_->IsKeepingAlive()); | 106 EXPECT_FALSE(registry_->IsKeepingAlive()); |
| 98 } | 107 } |
| 99 | 108 |
| 100 // Test the IsKeepingAlive state and when we interact with the browser with | 109 // Test the IsKeepingAlive state and when we interact with the browser with |
| 101 // more than one KeepAlive registered. | 110 // more than one KeepAlive registered. |
| 102 TEST_F(KeepAliveRegistryTest, RestartOptionTest) { | 111 TEST_F(KeepAliveRegistryTest, RestartOptionTest) { |
| 103 scoped_ptr<ScopedKeepAlive> keep_alive, keep_alive_restart; | 112 scoped_ptr<ScopedKeepAlive> keep_alive, keep_alive_restart; |
| 104 | 113 |
| 105 EXPECT_EQ(0, on_restart_allowed_call_count_); | 114 EXPECT_EQ(0, on_restart_allowed_call_count_); |
| 106 EXPECT_EQ(0, on_restart_forbidden_call_count_); | 115 EXPECT_EQ(0, on_restart_forbidden_call_count_); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 120 ASSERT_EQ(1, on_restart_allowed_call_count_--); | 129 ASSERT_EQ(1, on_restart_allowed_call_count_--); |
| 121 | 130 |
| 122 // No keep alive, we should no prevent restarts. | 131 // No keep alive, we should no prevent restarts. |
| 123 keep_alive.reset(); | 132 keep_alive.reset(); |
| 124 EXPECT_EQ(0, on_restart_forbidden_call_count_); | 133 EXPECT_EQ(0, on_restart_forbidden_call_count_); |
| 125 | 134 |
| 126 // Make sure all calls were checked. | 135 // Make sure all calls were checked. |
| 127 EXPECT_EQ(0, on_restart_allowed_call_count_); | 136 EXPECT_EQ(0, on_restart_allowed_call_count_); |
| 128 EXPECT_EQ(0, on_restart_forbidden_call_count_); | 137 EXPECT_EQ(0, on_restart_forbidden_call_count_); |
| 129 } | 138 } |
| OLD | NEW |