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. 4 5 #include "chrome_frame/scoped_initialization_manager.h" 6 #include "gmock/gmock.h" 7 #include "gtest/gtest.h" 8 9 using ::testing::InSequence; 10 11 // Initialization traits that delegate to a registered delegate instance. 12 class TestInitializationTraits { 13 public: 14 class Delegate { 15 public: 16 virtual void Initialize() = 0; 17 virtual void Shutdown() = 0; 18 }; 19 20 static void SetDelegate(Delegate* delegate) { 21 delegate_ = delegate; 22 } 23 24 static void Initialize() { 25 ASSERT_TRUE(delegate_ != NULL); 26 delegate_->Initialize(); 27 } 28 29 static void Shutdown() { 30 ASSERT_TRUE(delegate_ != NULL); 31 delegate_->Shutdown(); 32 } 33 34 private: 35 static Delegate* delegate_; 36 }; 37 38 TestInitializationTraits::Delegate* TestInitializationTraits::delegate_ = NULL; 39 40 // A mock delegate for use in tests. 41 class MockInitializationTraitsDelegate 42 : public TestInitializationTraits::Delegate { 43 public: 44 MOCK_METHOD0(Initialize, void()); 45 MOCK_METHOD0(Shutdown, void()); 46 }; 47 48 // A test fixture that sets up a mock traits delegate for use in tests. 49 class ScopedInitializationManagerTest : public ::testing::Test { 50 protected: 51 // A manager type that will invoke methods 52 typedef chrome_frame::ScopedInitializationManager<TestInitializationTraits> 53 TestScopedInitializationManager; 54 55 virtual void SetUp() OVERRIDE { 56 TestInitializationTraits::SetDelegate(&mock_traits_delegate_); 57 } 58 virtual void TearDown() OVERRIDE { 59 TestInitializationTraits::SetDelegate(NULL); 60 } 61 62 MockInitializationTraitsDelegate mock_traits_delegate_; 63 }; 64 65 // Test that Initialize and Shutdown are called in order for the simple case. 66 TEST_F(ScopedInitializationManagerTest, InitializeAndShutdown) { 67 { 68 InSequence dummy; 69 EXPECT_CALL(mock_traits_delegate_, Initialize()); 70 EXPECT_CALL(mock_traits_delegate_, Shutdown()); 71 } 72 TestScopedInitializationManager test_manager; 73 } 74 75 // Test that Initialize and Shutdown are called in order only once despite 76 // multiple instances. 77 TEST_F(ScopedInitializationManagerTest, InitializeAndShutdownOnlyOnce) { 78 { 79 InSequence dummy; 80 EXPECT_CALL(mock_traits_delegate_, Initialize()); 81 EXPECT_CALL(mock_traits_delegate_, Shutdown()); 82 } 83 TestScopedInitializationManager test_manager_1; 84 TestScopedInitializationManager test_manager_2; 85 TestScopedInitializationManager test_manager_3; 86 } 87