OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 #ifndef CHROME_BROWSER_POLICY_ASYNCHRONOUS_POLICY_TEST_BASE_H_ |
| 6 #define CHROME_BROWSER_POLICY_ASYNCHRONOUS_POLICY_TEST_BASE_H_ |
| 7 #pragma once |
| 8 |
| 9 #include "base/message_loop.h" |
| 10 #include "base/time.h" |
| 11 #include "chrome/browser/browser_thread.h" |
| 12 #include "chrome/browser/policy/asynchronous_policy_provider.h" |
| 13 #include "chrome/browser/policy/mock_configuration_policy_store.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace policy { |
| 18 |
| 19 // A delegate for testing that can feed arbitrary information to the loader. |
| 20 class ProviderDelegateMock : public AsynchronousPolicyProvider::Delegate { |
| 21 public: |
| 22 ProviderDelegateMock() : AsynchronousPolicyProvider::Delegate() {} |
| 23 virtual ~ProviderDelegateMock() {} |
| 24 |
| 25 MOCK_METHOD1(Init, void(AsynchronousPolicyProvider::PolicyChangeObserver*)); |
| 26 MOCK_METHOD0(Stop, void()); |
| 27 MOCK_METHOD0(Load, DictionaryValue*()); |
| 28 MOCK_METHOD2(IsSafeToReloadPolicy, bool(const base::Time&, base::TimeDelta*)); |
| 29 |
| 30 private: |
| 31 DISALLOW_COPY_AND_ASSIGN(ProviderDelegateMock); |
| 32 }; |
| 33 |
| 34 class AsynchronousPolicyTestBase : public testing::Test { |
| 35 public: |
| 36 AsynchronousPolicyTestBase() |
| 37 : run_pending_during_tear_down_(true), |
| 38 ui_thread_(BrowserThread::UI, &loop_), |
| 39 file_thread_(BrowserThread::FILE, &loop_) {} |
| 40 |
| 41 virtual ~AsynchronousPolicyTestBase() {} |
| 42 |
| 43 virtual void SetUp() { |
| 44 delegate_.reset(new ProviderDelegateMock()); |
| 45 store_.reset(new MockConfigurationPolicyStore); |
| 46 } |
| 47 |
| 48 virtual void TearDown() { |
| 49 provider_.reset(NULL); |
| 50 if (run_pending_during_tear_down_) |
| 51 loop_.RunAllPending(); |
| 52 } |
| 53 |
| 54 void DontRunAllPendingDuringTearDown() { |
| 55 run_pending_during_tear_down_ = false; |
| 56 } |
| 57 |
| 58 protected: |
| 59 MessageLoop loop_; |
| 60 scoped_ptr<AsynchronousPolicyProvider> provider_; |
| 61 |
| 62 // The mocks that are used in the test must outlive the scope of the test |
| 63 // because they still get accessed in the RunAllPending of the TearDown. |
| 64 scoped_ptr<MockConfigurationPolicyStore> store_; |
| 65 scoped_ptr<ProviderDelegateMock> delegate_; |
| 66 |
| 67 private: |
| 68 // True if a test guarantees that it's handled all tasks pending in the |
| 69 // message loop and doesn't want RunAllPending run during TearDown. |
| 70 bool run_pending_during_tear_down_; |
| 71 BrowserThread ui_thread_; |
| 72 BrowserThread file_thread_; |
| 73 |
| 74 DISALLOW_COPY_AND_ASSIGN(AsynchronousPolicyTestBase); |
| 75 }; |
| 76 |
| 77 inline static void MessageLoopQuitNow() { |
| 78 MessageLoop::current()->QuitNow(); |
| 79 } |
| 80 |
| 81 } // namespace policy |
| 82 |
| 83 #endif // CHROME_BROWSER_POLICY_ASYNCHRONOUS_POLICY_TEST_BASE_H_ |
OLD | NEW |