1 // Copyright (c) 2012 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_SYNC_TEST_INTEGRATION_PREFERENCES_HELPER_H_
6 #define CHROME_BROWSER_SYNC_TEST_INTEGRATION_PREFERENCES_HELPER_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <string>
12 #include <vector>
13 
14 #include "base/files/file_path.h"
15 #include "base/memory/scoped_refptr.h"
16 #include "base/values.h"
17 #include "chrome/browser/sync/test/integration/status_change_checker.h"
18 #include "components/prefs/json_pref_store.h"
19 
20 class PrefChangeRegistrar;
21 class PrefService;
22 class Profile;
23 
24 namespace user_prefs {
25 class PrefRegistrySyncable;
26 }
27 
28 namespace preferences_helper {
29 
30 // Used to access the preferences within a particular sync profile.
31 PrefService* GetPrefs(int index);
32 
33 // Provides access to the syncable pref registy of a profile.
34 user_prefs::PrefRegistrySyncable* GetRegistry(Profile* profile);
35 
36 // Inverts the value of the boolean preference with name |pref_name| in the
37 // profile with index |index|.
38 void ChangeBooleanPref(int index, const char* pref_name);
39 
40 // Changes the value of the integer preference with name |pref_name| in the
41 // profile with index |index| to |new_value|.
42 void ChangeIntegerPref(int index, const char* pref_name, int new_value);
43 
44 // Changes the value of the string preference with name |pref_name| in the
45 // profile with index |index| to |new_value|.
46 void ChangeStringPref(int index,
47                       const char* pref_name,
48                       const std::string& new_value);
49 
50 // Clears the value of the preference with name |pref_name| in the profile with
51 // index |index|.
52 void ClearPref(int index, const char* pref_name);
53 
54 // Changes the value of the list preference with name |pref_name| in the
55 // profile with index |index| to |new_value|.
56 void ChangeListPref(int index,
57                     const char* pref_name,
58                     const base::ListValue& new_value);
59 
60 // Reads preferences from a given profile's pref file (after flushing) and loads
61 // them into a new created pref store.
62 scoped_refptr<PrefStore> BuildPrefStoreFromPrefsFile(Profile* profile);
63 
64 // Used to verify that the boolean preference with name |pref_name| has the
65 // same value across all profiles.
66 bool BooleanPrefMatches(const char* pref_name) WARN_UNUSED_RESULT;
67 
68 // Used to verify that the integer preference with name |pref_name| has the
69 // same value across all profiles.
70 bool IntegerPrefMatches(const char* pref_name) WARN_UNUSED_RESULT;
71 
72 // Used to verify that the string preference with name |pref_name| has the
73 // same value across all profiles.
74 bool StringPrefMatches(const char* pref_name) WARN_UNUSED_RESULT;
75 
76 // Used to verify that the list preference with name |pref_name| has the
77 // same value across all profiles.
78 bool ListPrefMatches(const char* pref_name) WARN_UNUSED_RESULT;
79 
80 }  // namespace preferences_helper
81 
82 // Abstract checker that takes care of registering for preference changes.
83 class PrefMatchChecker : public StatusChangeChecker {
84  public:
85   explicit PrefMatchChecker(const char* path);
86   ~PrefMatchChecker() override;
87 
88   // StatusChangeChecker implementation.
89   bool IsExitConditionSatisfied(std::ostream* os) override = 0;
90 
91  protected:
92   const char* GetPath() const;
93 
94  private:
95   void RegisterPrefListener(PrefService* pref_service);
96 
97   std::vector<std::unique_ptr<PrefChangeRegistrar>> pref_change_registrars_;
98   const char* path_;
99 };
100 
101 // Matcher that blocks until the specified list pref matches on all clients.
102 class ListPrefMatchChecker : public PrefMatchChecker {
103  public:
104   explicit ListPrefMatchChecker(const char* path);
105 
106   // PrefMatchChecker implementation.
107   bool IsExitConditionSatisfied(std::ostream* os) override;
108 };
109 
110 // Matcher that blocks until the specified boolean pref matches on all clients.
111 class BooleanPrefMatchChecker : public PrefMatchChecker {
112  public:
113   explicit BooleanPrefMatchChecker(const char* path);
114 
115   // PrefMatchChecker implementation.
116   bool IsExitConditionSatisfied(std::ostream* os) override;
117 };
118 
119 // Matcher that blocks until the specified integer pref matches on all clients.
120 class IntegerPrefMatchChecker : public PrefMatchChecker {
121  public:
122   explicit IntegerPrefMatchChecker(const char* path);
123 
124   // PrefMatchChecker implementation.
125   bool IsExitConditionSatisfied(std::ostream* os) override;
126 };
127 
128 // Matcher that blocks until the specified string pref matches on all clients.
129 class StringPrefMatchChecker : public PrefMatchChecker {
130  public:
131   explicit StringPrefMatchChecker(const char* path);
132 
133   // PrefMatchChecker implementation.
134   bool IsExitConditionSatisfied(std::ostream* os) override;
135 };
136 
137 // Matcher that blocks until the specified pref is cleared on all clients.
138 class ClearedPrefMatchChecker : public PrefMatchChecker {
139  public:
140   explicit ClearedPrefMatchChecker(const char* path);
141 
142   // PrefMatchChecker implementation.
143   bool IsExitConditionSatisfied(std::ostream* os) override;
144 };
145 
146 #endif  // CHROME_BROWSER_SYNC_TEST_INTEGRATION_PREFERENCES_HELPER_H_
147