Chromium Code Reviews| 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_types.h" | 10 #include "chrome/browser/lifetime/keep_alive_types.h" |
| 10 #include "chrome/browser/lifetime/scoped_keep_alive.h" | 11 #include "chrome/browser/lifetime/scoped_keep_alive.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 13 // Test the WillKeepAlive state and when we interact with the browser with | 14 class KeepAliveRegistryTest : public testing::Test, |
| 15 public KeepAliveStateObserver { | |
| 16 public: | |
| 17 KeepAliveRegistryTest() | |
| 18 : on_restart_allowed_call_count_(0), on_restart_forbidden_call_count_(0) { | |
| 19 registry_ = KeepAliveRegistry::GetInstance(); | |
|
sky
2016/02/26 22:13:25
nit: use member initialize.
dgn
2016/03/09 16:35:31
Done.
| |
| 20 registry_->AddObserver(this); | |
| 21 | |
| 22 EXPECT_FALSE(registry_->IsKeepingAlive()); | |
| 23 } | |
| 24 ~KeepAliveRegistryTest() override { | |
| 25 registry_->RemoveObserver(this); | |
| 26 | |
| 27 EXPECT_FALSE(registry_->IsKeepingAlive()); | |
| 28 } | |
| 29 | |
| 30 void OnRestartAllowed() override { | |
| 31 ++on_restart_allowed_call_count_; | |
| 32 }; | |
| 33 void OnRestartForbidden() override { | |
| 34 ++on_restart_forbidden_call_count_; | |
| 35 }; | |
| 36 | |
| 37 int on_restart_allowed_call_count_; | |
|
sky
2016/02/26 22:13:25
nit: protected.
dgn
2016/03/09 16:35:31
Done.
| |
| 38 int on_restart_forbidden_call_count_; | |
| 39 KeepAliveRegistry* registry_; | |
| 40 }; | |
| 41 | |
| 42 // Test the IsKeepingAlive state and when we interact with the browser with | |
| 14 // a KeepAlive registered. | 43 // a KeepAlive registered. |
| 15 TEST(KeepAliveRegistryTest, BasicKeepAliveTest) { | 44 TEST_F(KeepAliveRegistryTest, BasicKeepAliveTest) { |
| 16 const int base_keep_alive_count = chrome::GetKeepAliveCountForTesting(); | 45 const int base_keep_alive_count = chrome::GetKeepAliveCountForTesting(); |
| 17 KeepAliveRegistry* registry = KeepAliveRegistry::GetInstance(); | 46 KeepAliveRegistry* registry = KeepAliveRegistry::GetInstance(); |
| 18 | 47 |
| 19 EXPECT_FALSE(registry->WillKeepAlive()); | 48 EXPECT_FALSE(registry->IsKeepingAlive()); |
| 20 | 49 |
| 21 { | 50 { |
| 22 // Arbitrarily chosen Origin | 51 // Arbitrarily chosen Origin |
| 23 ScopedKeepAlive test_keep_alive(KeepAliveOrigin::CHROME_APP_DELEGATE); | 52 ScopedKeepAlive test_keep_alive(KeepAliveOrigin::CHROME_APP_DELEGATE, |
| 53 KeepAliveRestartOption::DISABLED); | |
| 24 | 54 |
| 25 // We should require the browser to stay alive | 55 // We should require the browser to stay alive |
| 26 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); | 56 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); |
| 27 EXPECT_TRUE(registry->WillKeepAlive()); | 57 EXPECT_TRUE(registry_->IsKeepingAlive()); |
| 28 } | 58 } |
| 29 | 59 |
| 30 // We should be back to normal now. | 60 // We should be back to normal now. |
| 31 EXPECT_EQ(base_keep_alive_count, chrome::GetKeepAliveCountForTesting()); | 61 EXPECT_EQ(base_keep_alive_count, chrome::GetKeepAliveCountForTesting()); |
| 32 EXPECT_FALSE(registry->WillKeepAlive()); | 62 EXPECT_FALSE(registry_->IsKeepingAlive()); |
| 33 } | 63 } |
| 34 | 64 |
| 35 // Test the WillKeepAlive state and when we interact with the browser with | 65 // Test the IsKeepingAlive state and when we interact with the browser with |
| 36 // more than one KeepAlive registered. | 66 // more than one KeepAlive registered. |
| 37 TEST(KeepAliveRegistryTest, DoubleKeepAliveTest) { | 67 TEST_F(KeepAliveRegistryTest, DoubleKeepAliveTest) { |
| 38 const int base_keep_alive_count = chrome::GetKeepAliveCountForTesting(); | 68 const int base_keep_alive_count = chrome::GetKeepAliveCountForTesting(); |
| 39 KeepAliveRegistry* registry = KeepAliveRegistry::GetInstance(); | |
| 40 scoped_ptr<ScopedKeepAlive> keep_alive_1, keep_alive_2; | 69 scoped_ptr<ScopedKeepAlive> keep_alive_1, keep_alive_2; |
| 41 | 70 |
| 42 keep_alive_1.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE)); | 71 keep_alive_1.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE, |
| 72 KeepAliveRestartOption::DISABLED)); | |
| 43 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); | 73 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); |
| 44 EXPECT_TRUE(registry->WillKeepAlive()); | 74 EXPECT_TRUE(registry_->IsKeepingAlive()); |
| 45 | 75 |
| 46 keep_alive_2.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE)); | 76 keep_alive_2.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE, |
| 77 KeepAliveRestartOption::DISABLED)); | |
| 47 // We should not increment the count twice | 78 // We should not increment the count twice |
| 48 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); | 79 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); |
| 49 EXPECT_TRUE(registry->WillKeepAlive()); | 80 EXPECT_TRUE(registry_->IsKeepingAlive()); |
| 50 | 81 |
| 51 keep_alive_1.reset(); | 82 keep_alive_1.reset(); |
| 52 // We should not decrement the count before the last keep alive is released. | 83 // We should not decrement the count before the last keep alive is released. |
| 53 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); | 84 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); |
| 54 EXPECT_TRUE(registry->WillKeepAlive()); | 85 EXPECT_TRUE(registry_->IsKeepingAlive()); |
| 55 | 86 |
| 56 keep_alive_2.reset(); | 87 keep_alive_2.reset(); |
| 57 EXPECT_EQ(base_keep_alive_count, chrome::GetKeepAliveCountForTesting()); | 88 EXPECT_EQ(base_keep_alive_count, chrome::GetKeepAliveCountForTesting()); |
| 58 EXPECT_FALSE(registry->WillKeepAlive()); | 89 EXPECT_FALSE(registry_->IsKeepingAlive()); |
| 59 } | 90 } |
| 91 | |
| 92 // Test the IsKeepingAlive state and when we interact with the browser with | |
| 93 // more than one KeepAlive registered. | |
| 94 TEST_F(KeepAliveRegistryTest, RestartOptionTest) { | |
| 95 scoped_ptr<ScopedKeepAlive> keep_alive, keep_alive_restart; | |
| 96 | |
| 97 EXPECT_EQ(0, on_restart_allowed_call_count_); | |
| 98 EXPECT_EQ(0, on_restart_forbidden_call_count_); | |
| 99 | |
| 100 // With a normal keep alive, restart should not be allowed | |
| 101 keep_alive.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE, | |
| 102 KeepAliveRestartOption::DISABLED)); | |
| 103 ASSERT_EQ(1, on_restart_forbidden_call_count_--); // decrement to ack | |
| 104 | |
| 105 // Restart should not be allowed if all KA don't allow it. | |
| 106 keep_alive_restart.reset(new ScopedKeepAlive( | |
| 107 KeepAliveOrigin::CHROME_APP_DELEGATE, KeepAliveRestartOption::ENABLED)); | |
| 108 EXPECT_EQ(0, on_restart_allowed_call_count_); | |
| 109 | |
| 110 // Now restart should be allowed, the only one left allows it. | |
| 111 keep_alive.reset(); | |
| 112 ASSERT_EQ(1, on_restart_allowed_call_count_--); | |
| 113 | |
| 114 // No keep alive, we should no prevent restarts. | |
| 115 keep_alive.reset(); | |
| 116 EXPECT_EQ(0, on_restart_forbidden_call_count_); | |
| 117 | |
| 118 // Make sure all calls were checked. | |
| 119 EXPECT_EQ(0, on_restart_allowed_call_count_); | |
| 120 EXPECT_EQ(0, on_restart_forbidden_call_count_); | |
| 121 } | |
| OLD | NEW |