| 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" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 class KeepAliveRegistryTest : public testing::Test, | 15 class KeepAliveRegistryTest : public testing::Test, |
| 15 public KeepAliveStateObserver { | 16 public KeepAliveStateObserver { |
| 16 public: | 17 public: |
| 17 KeepAliveRegistryTest() | 18 KeepAliveRegistryTest() |
| 18 : on_restart_allowed_call_count_(0), | 19 : on_restart_allowed_call_count_(0), |
| 19 on_restart_forbidden_call_count_(0), | 20 on_restart_forbidden_call_count_(0), |
| 20 registry_(KeepAliveRegistry::GetInstance()) { | 21 registry_(KeepAliveRegistry::GetInstance()) { |
| 21 registry_->AddObserver(this); | 22 registry_->AddObserver(this); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 38 | 39 |
| 39 protected: | 40 protected: |
| 40 int on_restart_allowed_call_count_; | 41 int on_restart_allowed_call_count_; |
| 41 int on_restart_forbidden_call_count_; | 42 int on_restart_forbidden_call_count_; |
| 42 KeepAliveRegistry* registry_; | 43 KeepAliveRegistry* registry_; |
| 43 }; | 44 }; |
| 44 | 45 |
| 45 // Test the IsKeepingAlive state and when we interact with the browser with | 46 // Test the IsKeepingAlive state and when we interact with the browser with |
| 46 // a KeepAlive registered. | 47 // a KeepAlive registered. |
| 47 TEST_F(KeepAliveRegistryTest, BasicKeepAliveTest) { | 48 TEST_F(KeepAliveRegistryTest, BasicKeepAliveTest) { |
| 48 const int base_keep_alive_count = chrome::GetKeepAliveCountForTesting(); | 49 TestingBrowserProcess* browser_process = TestingBrowserProcess::GetGlobal(); |
| 50 const unsigned int base_module_ref_count = |
| 51 browser_process->module_ref_count(); |
| 49 KeepAliveRegistry* registry = KeepAliveRegistry::GetInstance(); | 52 KeepAliveRegistry* registry = KeepAliveRegistry::GetInstance(); |
| 50 | 53 |
| 51 EXPECT_FALSE(registry->IsKeepingAlive()); | 54 EXPECT_FALSE(registry->IsKeepingAlive()); |
| 52 | 55 |
| 53 { | 56 { |
| 54 // Arbitrarily chosen Origin | 57 // Arbitrarily chosen Origin |
| 55 ScopedKeepAlive test_keep_alive(KeepAliveOrigin::CHROME_APP_DELEGATE, | 58 ScopedKeepAlive test_keep_alive(KeepAliveOrigin::CHROME_APP_DELEGATE, |
| 56 KeepAliveRestartOption::DISABLED); | 59 KeepAliveRestartOption::DISABLED); |
| 57 | 60 |
| 58 // We should require the browser to stay alive | 61 // We should require the browser to stay alive |
| 59 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); | 62 EXPECT_EQ(base_module_ref_count + 1, browser_process->module_ref_count()); |
| 60 EXPECT_TRUE(registry_->IsKeepingAlive()); | 63 EXPECT_TRUE(registry_->IsKeepingAlive()); |
| 61 } | 64 } |
| 62 | 65 |
| 63 // We should be back to normal now. | 66 // We should be back to normal now. |
| 64 EXPECT_EQ(base_keep_alive_count, chrome::GetKeepAliveCountForTesting()); | 67 EXPECT_EQ(base_module_ref_count, browser_process->module_ref_count()); |
| 65 EXPECT_FALSE(registry_->IsKeepingAlive()); | 68 EXPECT_FALSE(registry_->IsKeepingAlive()); |
| 66 } | 69 } |
| 67 | 70 |
| 68 // Test the IsKeepingAlive state and when we interact with the browser with | 71 // Test the IsKeepingAlive state and when we interact with the browser with |
| 69 // more than one KeepAlive registered. | 72 // more than one KeepAlive registered. |
| 70 TEST_F(KeepAliveRegistryTest, DoubleKeepAliveTest) { | 73 TEST_F(KeepAliveRegistryTest, DoubleKeepAliveTest) { |
| 71 const int base_keep_alive_count = chrome::GetKeepAliveCountForTesting(); | 74 TestingBrowserProcess* browser_process = TestingBrowserProcess::GetGlobal(); |
| 75 const unsigned int base_module_ref_count = |
| 76 browser_process->module_ref_count(); |
| 72 scoped_ptr<ScopedKeepAlive> keep_alive_1, keep_alive_2; | 77 scoped_ptr<ScopedKeepAlive> keep_alive_1, keep_alive_2; |
| 73 | 78 |
| 74 keep_alive_1.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE, | 79 keep_alive_1.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE, |
| 75 KeepAliveRestartOption::DISABLED)); | 80 KeepAliveRestartOption::DISABLED)); |
| 76 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); | 81 EXPECT_EQ(base_module_ref_count + 1, browser_process->module_ref_count()); |
| 77 EXPECT_TRUE(registry_->IsKeepingAlive()); | 82 EXPECT_TRUE(registry_->IsKeepingAlive()); |
| 78 | 83 |
| 79 keep_alive_2.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE, | 84 keep_alive_2.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE, |
| 80 KeepAliveRestartOption::DISABLED)); | 85 KeepAliveRestartOption::DISABLED)); |
| 81 // We should not increment the count twice | 86 // We should not increment the count twice |
| 82 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); | 87 EXPECT_EQ(base_module_ref_count + 1, browser_process->module_ref_count()); |
| 83 EXPECT_TRUE(registry_->IsKeepingAlive()); | 88 EXPECT_TRUE(registry_->IsKeepingAlive()); |
| 84 | 89 |
| 85 keep_alive_1.reset(); | 90 keep_alive_1.reset(); |
| 86 // We should not decrement the count before the last keep alive is released. | 91 // We should not decrement the count before the last keep alive is released. |
| 87 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); | 92 EXPECT_EQ(base_module_ref_count + 1, browser_process->module_ref_count()); |
| 88 EXPECT_TRUE(registry_->IsKeepingAlive()); | 93 EXPECT_TRUE(registry_->IsKeepingAlive()); |
| 89 | 94 |
| 90 keep_alive_2.reset(); | 95 keep_alive_2.reset(); |
| 91 EXPECT_EQ(base_keep_alive_count, chrome::GetKeepAliveCountForTesting()); | 96 EXPECT_EQ(base_module_ref_count, browser_process->module_ref_count()); |
| 92 EXPECT_FALSE(registry_->IsKeepingAlive()); | 97 EXPECT_FALSE(registry_->IsKeepingAlive()); |
| 93 } | 98 } |
| 94 | 99 |
| 95 // Test the IsKeepingAlive state and when we interact with the browser with | 100 // Test the IsKeepingAlive state and when we interact with the browser with |
| 96 // more than one KeepAlive registered. | 101 // more than one KeepAlive registered. |
| 97 TEST_F(KeepAliveRegistryTest, RestartOptionTest) { | 102 TEST_F(KeepAliveRegistryTest, RestartOptionTest) { |
| 98 scoped_ptr<ScopedKeepAlive> keep_alive, keep_alive_restart; | 103 scoped_ptr<ScopedKeepAlive> keep_alive, keep_alive_restart; |
| 99 | 104 |
| 100 EXPECT_EQ(0, on_restart_allowed_call_count_); | 105 EXPECT_EQ(0, on_restart_allowed_call_count_); |
| 101 EXPECT_EQ(0, on_restart_forbidden_call_count_); | 106 EXPECT_EQ(0, on_restart_forbidden_call_count_); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 115 ASSERT_EQ(1, on_restart_allowed_call_count_--); | 120 ASSERT_EQ(1, on_restart_allowed_call_count_--); |
| 116 | 121 |
| 117 // No keep alive, we should no prevent restarts. | 122 // No keep alive, we should no prevent restarts. |
| 118 keep_alive.reset(); | 123 keep_alive.reset(); |
| 119 EXPECT_EQ(0, on_restart_forbidden_call_count_); | 124 EXPECT_EQ(0, on_restart_forbidden_call_count_); |
| 120 | 125 |
| 121 // Make sure all calls were checked. | 126 // Make sure all calls were checked. |
| 122 EXPECT_EQ(0, on_restart_allowed_call_count_); | 127 EXPECT_EQ(0, on_restart_allowed_call_count_); |
| 123 EXPECT_EQ(0, on_restart_forbidden_call_count_); | 128 EXPECT_EQ(0, on_restart_forbidden_call_count_); |
| 124 } | 129 } |
| OLD | NEW |