OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/lifetime/keep_alive_registry.h" |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "chrome/browser/lifetime/application_lifetime.h" |
| 9 #include "chrome/browser/lifetime/keep_alive_types.h" |
| 10 #include "chrome/browser/lifetime/scoped_keep_alive.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 // Test the WillKeepAlive state and when we interact with the browser with |
| 14 // a KeepAlive registered. |
| 15 TEST(KeepAliveRegistryTest, BasicKeepAliveTest) { |
| 16 const int base_keep_alive_count = chrome::GetKeepAliveCountForTesting(); |
| 17 KeepAliveRegistry* registry = KeepAliveRegistry::GetInstance(); |
| 18 |
| 19 EXPECT_FALSE(registry->WillKeepAlive()); |
| 20 |
| 21 { |
| 22 // Arbitrarily chosen Origin |
| 23 ScopedKeepAlive test_keep_alive(KeepAliveOrigin::CHROME_APP_DELEGATE); |
| 24 |
| 25 // We should require the browser to stay alive |
| 26 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); |
| 27 EXPECT_TRUE(registry->WillKeepAlive()); |
| 28 } |
| 29 |
| 30 // We should be back to normal now. |
| 31 EXPECT_EQ(base_keep_alive_count, chrome::GetKeepAliveCountForTesting()); |
| 32 EXPECT_FALSE(registry->WillKeepAlive()); |
| 33 } |
| 34 |
| 35 // Test the WillKeepAlive state and when we interact with the browser with |
| 36 // more than one KeepAlive registered. |
| 37 TEST(KeepAliveRegistryTest, DoubleKeepAliveTest) { |
| 38 const int base_keep_alive_count = chrome::GetKeepAliveCountForTesting(); |
| 39 KeepAliveRegistry* registry = KeepAliveRegistry::GetInstance(); |
| 40 scoped_ptr<ScopedKeepAlive> keep_alive_1, keep_alive_2; |
| 41 |
| 42 keep_alive_1.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE)); |
| 43 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); |
| 44 EXPECT_TRUE(registry->WillKeepAlive()); |
| 45 |
| 46 keep_alive_2.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE)); |
| 47 // We should not increment the count twice |
| 48 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); |
| 49 EXPECT_TRUE(registry->WillKeepAlive()); |
| 50 |
| 51 keep_alive_1.reset(); |
| 52 // We should not decrement the count before the last keep alive is released. |
| 53 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); |
| 54 EXPECT_TRUE(registry->WillKeepAlive()); |
| 55 |
| 56 keep_alive_2.reset(); |
| 57 EXPECT_EQ(base_keep_alive_count, chrome::GetKeepAliveCountForTesting()); |
| 58 EXPECT_FALSE(registry->WillKeepAlive()); |
| 59 } |
OLD | NEW |