mock_pref_change_callback.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright (c) 2011 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. #ifndef COMPONENTS_PREFS_MOCK_PREF_CHANGE_CALLBACK_H_
  5. #define COMPONENTS_PREFS_MOCK_PREF_CHANGE_CALLBACK_H_
  6. #include <string>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/values_equivalent.h"
  9. #include "components/prefs/pref_change_registrar.h"
  10. #include "components/prefs/pref_service.h"
  11. #include "testing/gmock/include/gmock/gmock.h"
  12. using testing::Pointee;
  13. using testing::Property;
  14. using testing::Truly;
  15. // Matcher that checks whether the current value of the preference named
  16. // |pref_name| in |prefs| matches |value|. If |value| is NULL, the matcher
  17. // checks that the value is not set.
  18. MATCHER_P3(PrefValueMatches, prefs, pref_name, value, "") {
  19. const PrefService::Preference* pref = prefs->FindPreference(pref_name);
  20. if (!pref)
  21. return false;
  22. return base::ValuesEquivalent(value, pref->GetValue());
  23. }
  24. // A mock for testing preference notifications and easy setup of expectations.
  25. class MockPrefChangeCallback {
  26. public:
  27. explicit MockPrefChangeCallback(PrefService* prefs);
  28. virtual ~MockPrefChangeCallback();
  29. PrefChangeRegistrar::NamedChangeCallback GetCallback();
  30. MOCK_METHOD1(OnPreferenceChanged, void(const std::string&));
  31. void Expect(const std::string& pref_name,
  32. const base::Value* value);
  33. private:
  34. raw_ptr<PrefService> prefs_;
  35. };
  36. #endif // COMPONENTS_PREFS_MOCK_PREF_CHANGE_CALLBACK_H_