Index: chrome/browser/lifetime/keep_alive_registry_unittest.cc |
diff --git a/chrome/browser/lifetime/keep_alive_registry_unittest.cc b/chrome/browser/lifetime/keep_alive_registry_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..178b8ee0de1afa5262f3ff8d2a3329d0922d3970 |
--- /dev/null |
+++ b/chrome/browser/lifetime/keep_alive_registry_unittest.cc |
@@ -0,0 +1,89 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/lifetime/keep_alive_registry.h" |
+ |
+#include "base/memory/scoped_ptr.h" |
+#include "chrome/browser/lifetime/keep_alive_types.h" |
+#include "chrome/browser/lifetime/scoped_keep_alive.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using testing::Mock; |
+ |
+namespace { |
+ |
+class MockKeepAliveRegistry : public KeepAliveRegistry { |
+ public: |
+ MOCK_METHOD0(HugBrowser, void()); |
+ MOCK_METHOD0(ReleaseBrowser, void()); |
+}; |
+ |
+} // namespace |
+ |
+class KeepAliveRegistryTest : public testing::Test { |
+ public: |
+ KeepAliveRegistryTest() {} |
+ ~KeepAliveRegistryTest() override {} |
+ |
+ void SetUp() override { |
+ registry_.reset(new MockKeepAliveRegistry()); |
+ KeepAliveRegistry::SetInstanceForTesting(registry_.get()); |
+ } |
+ |
+ void TearDown() override { |
+ KeepAliveRegistry::SetInstanceForTesting(nullptr); |
+ registry_.reset(); |
+ } |
+ |
+ protected: |
+ scoped_ptr<MockKeepAliveRegistry> registry_; |
+}; |
+ |
+// Test the WillKeepAlive state and when we interact with the browser with |
+// a KeepAlive registered. |
+TEST_F(KeepAliveRegistryTest, BasicKeepAliveTest) { |
+ EXPECT_FALSE(registry_->WillKeepAlive()); |
+ |
+ { |
+ EXPECT_CALL(*registry_.get(), HugBrowser()); |
+ ScopedKeepAlive test_keep_alive(KeepAliveOrigin::TEST); |
+ Mock::VerifyAndClearExpectations(registry_.get()); |
+ |
+ EXPECT_TRUE(registry_->WillKeepAlive()); |
+ |
+ // Expect ReleaseBrowser to be called when |test_keep_alive| goes out of |
+ // scope and is discarded. |
+ EXPECT_CALL(*registry_.get(), ReleaseBrowser()); |
+ } |
+ Mock::VerifyAndClearExpectations(registry_.get()); |
+ |
+ EXPECT_FALSE(registry_->WillKeepAlive()); |
+} |
+ |
+// Test the WillKeepAlive state and when we interact with the browser with |
+// more than one KeepAlive registered. |
+TEST_F(KeepAliveRegistryTest, DoubleKeepAliveTest) { |
+ scoped_ptr<ScopedKeepAlive> keep_alive_1, keep_alive_2; |
+ |
+ EXPECT_CALL(*registry_.get(), HugBrowser()); |
+ keep_alive_1.reset(new ScopedKeepAlive(KeepAliveOrigin::TEST)); |
+ Mock::VerifyAndClearExpectations(registry_.get()); |
+ EXPECT_TRUE(registry_->WillKeepAlive()); |
+ |
+ EXPECT_CALL(*registry_.get(), HugBrowser()).Times(0); |
+ keep_alive_2.reset(new ScopedKeepAlive(KeepAliveOrigin::TEST)); |
+ Mock::VerifyAndClearExpectations(registry_.get()); |
+ EXPECT_TRUE(registry_->WillKeepAlive()); |
+ |
+ EXPECT_CALL(*registry_.get(), ReleaseBrowser()).Times(0); |
+ keep_alive_1.reset(); |
+ Mock::VerifyAndClearExpectations(registry_.get()); |
+ EXPECT_TRUE(registry_->WillKeepAlive()); |
+ |
+ EXPECT_CALL(*registry_.get(), ReleaseBrowser()).Times(1); |
+ keep_alive_2.reset(); |
+ Mock::VerifyAndClearExpectations(registry_.get()); |
+ EXPECT_FALSE(registry_->WillKeepAlive()); |
+} |