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