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/keep_alive_types.h" |
| 9 #include "chrome/browser/lifetime/scoped_keep_alive.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 using testing::Mock; |
| 14 |
| 15 namespace { |
| 16 |
| 17 class MockKeepAliveRegistry : public KeepAliveRegistry { |
| 18 public: |
| 19 MOCK_METHOD0(HugBrowser, void()); |
| 20 MOCK_METHOD0(ReleaseBrowser, void()); |
| 21 }; |
| 22 |
| 23 } // namespace |
| 24 |
| 25 class KeepAliveRegistryTest : public testing::Test { |
| 26 public: |
| 27 KeepAliveRegistryTest() {} |
| 28 ~KeepAliveRegistryTest() override {} |
| 29 |
| 30 void SetUp() override { |
| 31 registry_.reset(new MockKeepAliveRegistry()); |
| 32 KeepAliveRegistry::SetInstanceForTesting(registry_.get()); |
| 33 } |
| 34 |
| 35 void TearDown() override { |
| 36 KeepAliveRegistry::SetInstanceForTesting(nullptr); |
| 37 registry_.reset(); |
| 38 } |
| 39 |
| 40 protected: |
| 41 scoped_ptr<MockKeepAliveRegistry> registry_; |
| 42 }; |
| 43 |
| 44 // Test the WillKeepAlive state and when we interact with the browser with |
| 45 // a KeepAlive registered. |
| 46 TEST_F(KeepAliveRegistryTest, BasicKeepAliveTest) { |
| 47 EXPECT_FALSE(registry_->WillKeepAlive()); |
| 48 |
| 49 { |
| 50 EXPECT_CALL(*registry_.get(), HugBrowser()); |
| 51 ScopedKeepAlive test_keep_alive(KeepAliveOrigin::TEST); |
| 52 Mock::VerifyAndClearExpectations(registry_.get()); |
| 53 |
| 54 EXPECT_TRUE(registry_->WillKeepAlive()); |
| 55 |
| 56 // Expect ReleaseBrowser to be called when |test_keep_alive| goes out of |
| 57 // scope and is discarded. |
| 58 EXPECT_CALL(*registry_.get(), ReleaseBrowser()); |
| 59 } |
| 60 Mock::VerifyAndClearExpectations(registry_.get()); |
| 61 |
| 62 EXPECT_FALSE(registry_->WillKeepAlive()); |
| 63 } |
| 64 |
| 65 // Test the WillKeepAlive state and when we interact with the browser with |
| 66 // more than one KeepAlive registered. |
| 67 TEST_F(KeepAliveRegistryTest, DoubleKeepAliveTest) { |
| 68 scoped_ptr<ScopedKeepAlive> keep_alive_1, keep_alive_2; |
| 69 |
| 70 EXPECT_CALL(*registry_.get(), HugBrowser()); |
| 71 keep_alive_1.reset(new ScopedKeepAlive(KeepAliveOrigin::TEST)); |
| 72 Mock::VerifyAndClearExpectations(registry_.get()); |
| 73 EXPECT_TRUE(registry_->WillKeepAlive()); |
| 74 |
| 75 EXPECT_CALL(*registry_.get(), HugBrowser()).Times(0); |
| 76 keep_alive_2.reset(new ScopedKeepAlive(KeepAliveOrigin::TEST)); |
| 77 Mock::VerifyAndClearExpectations(registry_.get()); |
| 78 EXPECT_TRUE(registry_->WillKeepAlive()); |
| 79 |
| 80 EXPECT_CALL(*registry_.get(), ReleaseBrowser()).Times(0); |
| 81 keep_alive_1.reset(); |
| 82 Mock::VerifyAndClearExpectations(registry_.get()); |
| 83 EXPECT_TRUE(registry_->WillKeepAlive()); |
| 84 |
| 85 EXPECT_CALL(*registry_.get(), ReleaseBrowser()).Times(1); |
| 86 keep_alive_2.reset(); |
| 87 Mock::VerifyAndClearExpectations(registry_.get()); |
| 88 EXPECT_FALSE(registry_->WillKeepAlive()); |
| 89 } |
OLD | NEW |