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_PREFS_PREF_OBSERVER_MOCK_H_ |
| 6 #define CHROME_BROWSER_PREFS_PREF_OBSERVER_MOCK_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <string> |
| 10 |
| 11 #include "chrome/browser/prefs/pref_service.h" |
| 12 #include "chrome/common/notification_details.h" |
| 13 #include "chrome/common/notification_observer.h" |
| 14 #include "chrome/common/notification_source.h" |
| 15 #include "chrome/common/notification_type.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 |
| 18 using testing::Pointee; |
| 19 using testing::Property; |
| 20 using testing::Truly; |
| 21 |
| 22 // Matcher that checks whether the current value of the preference named |
| 23 // |pref_name| in |prefs| matches |value|. If |value| is NULL, the matcher |
| 24 // checks that the value is not set. |
| 25 MATCHER_P3(PrefValueMatches, prefs, pref_name, value, "") { |
| 26 const PrefService::Preference* pref = |
| 27 prefs->FindPreference(pref_name.c_str()); |
| 28 if (!pref) |
| 29 return false; |
| 30 |
| 31 const Value* actual_value = pref->GetValue(); |
| 32 if (!actual_value) |
| 33 return value == NULL; |
| 34 if (!value) |
| 35 return actual_value == NULL; |
| 36 return value->Equals(actual_value); |
| 37 } |
| 38 |
| 39 // A mock for testing preference notifications and easy setup of expectations. |
| 40 class PrefObserverMock : public NotificationObserver { |
| 41 public: |
| 42 PrefObserverMock() {} |
| 43 virtual ~PrefObserverMock() {} |
| 44 |
| 45 MOCK_METHOD3(Observe, void(NotificationType type, |
| 46 const NotificationSource& source, |
| 47 const NotificationDetails& details)); |
| 48 |
| 49 void Expect(const PrefService* prefs, |
| 50 const std::string& pref_name, |
| 51 const Value* value) { |
| 52 EXPECT_CALL(*this, Observe(NotificationType(NotificationType::PREF_CHANGED), |
| 53 Source<PrefService>(prefs), |
| 54 Property(&Details<std::string>::ptr, |
| 55 Pointee(pref_name)))) |
| 56 .With(PrefValueMatches(prefs, pref_name, value)); |
| 57 } |
| 58 }; |
| 59 |
| 60 #endif // CHROME_BROWSER_PREFS_PREF_OBSERVER_MOCK_H_ |
OLD | NEW |