Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(984)

Unified Diff: chrome/browser/lifetime/keep_alive_registry_unittest.cc

Issue 1708343002: Add ScopedKeepAlive to c/b/lifetime (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use a map and counts Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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());
+}

Powered by Google App Engine
This is Rietveld 408576698