OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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 #include "base/observer_list.h" | |
6 #include "base/prefs/writeable_pref_store.h" | |
7 #include "base/values.h" | |
8 #include "chrome/browser/ui/prefs_migrator.h" | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 | |
11 namespace { | |
12 | |
13 class DictionaryPrefStore : public WriteablePrefStore { | |
14 public: | |
15 DictionaryPrefStore() : WriteablePrefStore() {} | |
16 // Overrides from PrefStore. | |
17 virtual void AddObserver(Observer* observer) OVERRIDE { | |
18 observers_.AddObserver(observer); | |
19 } | |
20 | |
21 virtual void RemoveObserver(Observer* observer) OVERRIDE { | |
22 observers_.RemoveObserver(observer); | |
23 } | |
24 | |
25 // Get the value for a given preference |key| and stores it in |*result|. | |
Bernhard Bauer
2014/09/10 09:15:47
Nit: I don't think these comments are necessary.
gab
2014/09/10 14:40:04
+1, no need to copy method comments from derived A
dgrogan
2014/09/10 23:10:00
Done.
dgrogan
2014/09/10 23:10:00
Done.
| |
26 // |*result| is only modified if the return value is true and if |result| | |
27 // is not NULL. Ownership of the |*result| value remains with the PrefStore. | |
28 virtual bool GetValue(const std::string& key, | |
29 const base::Value** result) const OVERRIDE { | |
30 const base::Value* tmp = NULL; | |
31 if (!prefs_.Get(key, &tmp)) | |
32 return false; | |
33 | |
34 if (result) | |
35 *result = tmp; | |
36 return true; | |
gab
2014/09/10 14:40:05
Doesn't
return prefs_.Get(key, result);
do the s
dgrogan
2014/09/10 23:10:00
I copied this from JsonPrefStore and was wondering
| |
37 } | |
38 | |
39 // Overrides from WriteablePrefStore. | |
40 // Sets a |value| for |key| in the store. Assumes ownership of |value|, which | |
41 // must be non-NULL. | |
42 virtual void SetValue(const std::string& key, base::Value* value) OVERRIDE { | |
43 DCHECK(value); | |
44 scoped_ptr<base::Value> new_value(value); | |
45 base::Value* old_value = NULL; | |
46 prefs_.Get(key, &old_value); | |
47 if (!old_value || !value->Equals(old_value)) { | |
gab
2014/09/10 14:40:04
Do you really need these checks? Seems overkill to
dgrogan
2014/09/10 23:10:00
Done. Again, I had just copy/pasted from JsonPrefS
| |
48 prefs_.Set(key, new_value.release()); | |
49 ReportValueChanged(key); | |
50 } | |
51 } | |
52 | |
53 // Removes the value for |key|. | |
54 virtual void RemoveValue(const std::string& key) OVERRIDE { | |
55 if (prefs_.RemovePath(key, NULL)) | |
56 ReportValueChanged(key); | |
57 } | |
58 | |
59 // Equivalent to PrefStore::GetValue but returns a mutable value. | |
60 virtual bool GetMutableValue(const std::string& key, | |
61 base::Value** result) OVERRIDE { | |
62 return prefs_.Get(key, result); | |
63 } | |
64 | |
65 // Triggers a value changed notification. This function needs to be called | |
66 // if one retrieves a list or dictionary with GetMutableValue and change its | |
67 // value. SetValue takes care of notifications itself. Note that | |
68 // ReportValueChanged will trigger notifications even if nothing has changed. | |
69 virtual void ReportValueChanged(const std::string& key) OVERRIDE { | |
70 NOTIMPLEMENTED(); | |
71 } | |
72 | |
73 // Same as SetValue, but doesn't generate notifications. This is used by | |
74 // PrefService::GetMutableUserPref() in order to put empty entries | |
75 // into the user pref store. Using SetValue is not an option since existing | |
76 // tests rely on the number of notifications generated. | |
77 virtual void SetValueSilently(const std::string& key, | |
78 base::Value* value) OVERRIDE { | |
79 NOTIMPLEMENTED(); | |
80 } | |
81 | |
82 void SignalObservers() { | |
83 FOR_EACH_OBSERVER( | |
84 PrefStore::Observer, observers_, OnInitializationCompleted(true)); | |
85 } | |
86 | |
87 private: | |
88 virtual ~DictionaryPrefStore() {} | |
89 | |
90 base::DictionaryValue prefs_; | |
91 ObserverList<PrefStore::Observer, true> observers_; | |
92 | |
93 DISALLOW_COPY_AND_ASSIGN(DictionaryPrefStore); | |
94 }; | |
95 | |
96 } // unnamed namespace | |
Bernhard Bauer
2014/09/10 09:15:47
Nit: Usually the closing comment for a namespace i
dgrogan
2014/09/10 23:10:00
Done.
| |
97 | |
98 TEST(UIPrefsMigratorTest, First) { | |
99 scoped_refptr<DictionaryPrefStore> pref_store(new DictionaryPrefStore); | |
100 } | |
OLD | NEW |