1// Copyright 2014 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_MOCK_VALIDATION_DELEGATE_H_
6#define CHROME_BROWSER_PREFS_MOCK_VALIDATION_DELEGATE_H_
7
8#include <string>
9#include <vector>
10
11#include "base/compiler_specific.h"
12#include "base/macros.h"
13#include "chrome/browser/prefs/pref_hash_filter.h"
14#include "chrome/browser/prefs/pref_hash_store_transaction.h"
15#include "chrome/browser/prefs/tracked/tracked_preference_helper.h"
16#include "chrome/browser/prefs/tracked/tracked_preference_validation_delegate.h"
17
18// A mock tracked preference validation delegate for use by tests.
19class MockValidationDelegate : public TrackedPreferenceValidationDelegate {
20 public:
21  struct ValidationEvent {
22    ValidationEvent(const std::string& path,
23                    PrefHashStoreTransaction::ValueState state,
24                    TrackedPreferenceHelper::ResetAction action,
25                    PrefHashFilter::PrefTrackingStrategy tracking_strategy)
26        : pref_path(path),
27          value_state(state),
28          reset_action(action),
29          strategy(tracking_strategy) {}
30
31    std::string pref_path;
32    PrefHashStoreTransaction::ValueState value_state;
33    TrackedPreferenceHelper::ResetAction reset_action;
34    PrefHashFilter::PrefTrackingStrategy strategy;
35  };
36
37  MockValidationDelegate();
38  virtual ~MockValidationDelegate();
39
40  // Returns the number of recorded validations.
41  size_t recorded_validations_count() const { return validations_.size(); }
42
43  // Returns the number of validations of a given value state.
44  size_t CountValidationsOfState(
45      PrefHashStoreTransaction::ValueState value_state) const;
46
47  // Returns the event for the preference with a given path.
48  const ValidationEvent* GetEventForPath(const std::string& pref_path) const;
49
50  // TrackedPreferenceValidationDelegate implementation.
51  virtual void OnAtomicPreferenceValidation(
52      const std::string& pref_path,
53      const base::Value* value,
54      PrefHashStoreTransaction::ValueState value_state,
55      TrackedPreferenceHelper::ResetAction reset_action) OVERRIDE;
56  virtual void OnSplitPreferenceValidation(
57      const std::string& pref_path,
58      const base::DictionaryValue* dict_value,
59      const std::vector<std::string>& invalid_keys,
60      PrefHashStoreTransaction::ValueState value_state,
61      TrackedPreferenceHelper::ResetAction reset_action) OVERRIDE;
62
63 private:
64  // Adds a new validation event.
65  void RecordValidation(const std::string& pref_path,
66                        PrefHashStoreTransaction::ValueState value_state,
67                        TrackedPreferenceHelper::ResetAction reset_action,
68                        PrefHashFilter::PrefTrackingStrategy strategy);
69
70  std::vector<ValidationEvent> validations_;
71
72  DISALLOW_COPY_AND_ASSIGN(MockValidationDelegate);
73};
74
75#endif  // CHROME_BROWSER_PREFS_MOCK_VALIDATION_DELEGATE_H_
76