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