1 // Copyright 2013 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.
5 #include "chrome_frame/scoped_initialization_manager.h"
6 #include "gmock/gmock.h"
7 #include "gtest/gtest.h"
9 using ::testing::InSequence
;
11 // Initialization traits that delegate to a registered delegate instance.
12 class TestInitializationTraits
{
16 virtual void Initialize() = 0;
17 virtual void Shutdown() = 0;
20 static void SetDelegate(Delegate
* delegate
) {
24 static void Initialize() {
25 ASSERT_TRUE(delegate_
!= NULL
);
26 delegate_
->Initialize();
29 static void Shutdown() {
30 ASSERT_TRUE(delegate_
!= NULL
);
31 delegate_
->Shutdown();
35 static Delegate
* delegate_
;
38 TestInitializationTraits::Delegate
* TestInitializationTraits::delegate_
= NULL
;
40 // A mock delegate for use in tests.
41 class MockInitializationTraitsDelegate
42 : public TestInitializationTraits::Delegate
{
44 MOCK_METHOD0(Initialize
, void());
45 MOCK_METHOD0(Shutdown
, void());
48 // A test fixture that sets up a mock traits delegate for use in tests.
49 class ScopedInitializationManagerTest
: public ::testing::Test
{
51 // A manager type that will invoke methods
52 typedef chrome_frame::ScopedInitializationManager
<TestInitializationTraits
>
53 TestScopedInitializationManager
;
55 virtual void SetUp() OVERRIDE
{
56 TestInitializationTraits::SetDelegate(&mock_traits_delegate_
);
58 virtual void TearDown() OVERRIDE
{
59 TestInitializationTraits::SetDelegate(NULL
);
62 MockInitializationTraitsDelegate mock_traits_delegate_
;
65 // Test that Initialize and Shutdown are called in order for the simple case.
66 TEST_F(ScopedInitializationManagerTest
, InitializeAndShutdown
) {
69 EXPECT_CALL(mock_traits_delegate_
, Initialize());
70 EXPECT_CALL(mock_traits_delegate_
, Shutdown());
72 TestScopedInitializationManager test_manager
;
75 // Test that Initialize and Shutdown are called in order only once despite
76 // multiple instances.
77 TEST_F(ScopedInitializationManagerTest
, InitializeAndShutdownOnlyOnce
) {
80 EXPECT_CALL(mock_traits_delegate_
, Initialize());
81 EXPECT_CALL(mock_traits_delegate_
, Shutdown());
83 TestScopedInitializationManager test_manager_1
;
84 TestScopedInitializationManager test_manager_2
;
85 TestScopedInitializationManager test_manager_3
;