OLD | NEW |
| (Empty) |
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_UI_WEBUI_OPTIONS_PREFERENCES_BROWSERTEST_H_ | |
6 #define CHROME_BROWSER_UI_WEBUI_OPTIONS_PREFERENCES_BROWSERTEST_H_ | |
7 | |
8 #include <memory> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/compiler_specific.h" | |
13 #include "base/macros.h" | |
14 #include "chrome/test/base/in_process_browser_test.h" | |
15 #include "components/policy/core/common/mock_configuration_policy_provider.h" | |
16 #include "components/policy/core/common/policy_types.h" | |
17 #include "components/prefs/pref_change_registrar.h" | |
18 #include "components/prefs/pref_service.h" | |
19 #include "content/public/browser/notification_observer.h" | |
20 #include "testing/gmock/include/gmock/gmock.h" | |
21 | |
22 namespace base { | |
23 class DictionaryValue; | |
24 class Value; | |
25 } | |
26 | |
27 namespace content { | |
28 class RenderViewHost; | |
29 } | |
30 | |
31 // Tests verifying that the JavaScript Preferences class, the underlying C++ | |
32 // CoreOptionsHandler and the specialized classes handling Chrome OS device and | |
33 // proxy prefs behave correctly. | |
34 class PreferencesBrowserTest : public InProcessBrowserTest { | |
35 public: | |
36 PreferencesBrowserTest(); | |
37 ~PreferencesBrowserTest() override; | |
38 | |
39 // InProcessBrowserTest implementation: | |
40 void SetUpOnMainThread() override; | |
41 void TearDownOnMainThread() override; | |
42 | |
43 void OnPreferenceChanged(const std::string& pref_name); | |
44 | |
45 protected: | |
46 MOCK_METHOD1(OnCommit, void(const PrefService::Preference*)); | |
47 | |
48 // The pref service that holds the current pref values in the C++ backend. | |
49 PrefService* pref_service(); | |
50 | |
51 void SetUpPrefs(); | |
52 | |
53 // InProcessBrowserTest implementation: | |
54 void SetUpInProcessBrowserTestFixture() override; | |
55 | |
56 // Sets user policies through the mock policy provider. | |
57 void SetUserPolicies(const std::vector<std::string>& names, | |
58 const std::vector<std::unique_ptr<base::Value>>& values, | |
59 policy::PolicyLevel level); | |
60 // Clears user policies. | |
61 void ClearUserPolicies(); | |
62 // Set user-modified pref values directly in the C++ backend. | |
63 void SetUserValues(const std::vector<std::string>& names, | |
64 const std::vector<std::unique_ptr<base::Value>>& values); | |
65 | |
66 // Verifies that a dictionary contains a (key, value) pair. Takes ownership of | |
67 // |expected|. | |
68 void VerifyKeyValue(const base::DictionaryValue& dict, | |
69 const std::string& key, | |
70 const base::Value& expected); | |
71 // Verifies that a dictionary contains a given pref and that its value has | |
72 // been decorated correctly. | |
73 void VerifyPref(const base::DictionaryValue* prefs, | |
74 const std::string& name, | |
75 const std::unique_ptr<base::Value>& value, | |
76 const std::string& controlledBy, | |
77 bool disabled, | |
78 bool uncommitted); | |
79 // Verifies that a notification received from the JavaScript Preferences | |
80 // class contains a given pref and that its value has been decorated | |
81 // correctly. | |
82 void VerifyObservedPref(const std::string& observed_json, | |
83 const std::string& name, | |
84 const std::unique_ptr<base::Value>& value, | |
85 const std::string& controlledBy, | |
86 bool disabled, | |
87 bool uncommitted); | |
88 // Verifies that notifications received from the JavaScript Preferences class | |
89 // contain the given prefs and that their values have been decorated | |
90 // correctly. | |
91 void VerifyObservedPrefs( | |
92 const std::string& observed_json, | |
93 const std::vector<std::string>& names, | |
94 const std::vector<std::unique_ptr<base::Value>>& values, | |
95 const std::string& controlledBy, | |
96 bool disabled, | |
97 bool uncommitted); | |
98 | |
99 // Sets up the expectation that the JavaScript Preferences class will make no | |
100 // change to a user-modified pref value in the C++ backend. | |
101 void ExpectNoCommit(const std::string& name); | |
102 // Sets up the expectation that the JavaScript Preferences class will set a | |
103 // user-modified pref value in the C++ backend. | |
104 void ExpectSetCommit(const std::string& name, | |
105 const std::unique_ptr<base::Value>& value); | |
106 // Sets up the expectation that the JavaScript Preferences class will clear a | |
107 // user-modified pref value in the C++ backend. | |
108 void ExpectClearCommit(const std::string& name); | |
109 // Verifies that previously set expectations are met and clears them. | |
110 void VerifyAndClearExpectations(); | |
111 | |
112 // Sets up the JavaScript part of the test environment. | |
113 void SetupJavaScriptTestEnvironment( | |
114 const std::vector<std::string>& pref_names, | |
115 std::string* observed_json) const; | |
116 | |
117 // Sets a value through the JavaScript Preferences class as if the user had | |
118 // modified it. Returns the observation which can be verified using the | |
119 // VerifyObserved* methods. | |
120 void SetPref(const std::string& name, | |
121 const std::string& type, | |
122 const std::unique_ptr<base::Value>& value, | |
123 bool commit, | |
124 std::string* observed_json); | |
125 | |
126 // Verifies that setting a user-modified pref value through the JavaScript | |
127 // Preferences class fires the correct notification in JavaScript and commits | |
128 // the change to C++ if |commit| is true. | |
129 void VerifySetPref(const std::string& name, | |
130 const std::string& type, | |
131 const std::unique_ptr<base::Value>& value, | |
132 bool commit); | |
133 // Verifies that clearing a user-modified pref value through the JavaScript | |
134 // Preferences class fires the correct notification in JavaScript and does | |
135 // respectively does not cause the change to be committed to the C++ backend. | |
136 void VerifyClearPref(const std::string& name, | |
137 const std::unique_ptr<base::Value>& value, | |
138 bool commit); | |
139 // Verifies that committing a previously made change of a user-modified pref | |
140 // value through the JavaScript Preferences class fires the correct | |
141 // notification in JavaScript. | |
142 void VerifyCommit(const std::string& name, | |
143 const std::unique_ptr<base::Value>& value, | |
144 const std::string& controlledBy); | |
145 // Verifies that committing a previously set user-modified pref value through | |
146 // the JavaScript Preferences class fires the correct notification in | |
147 // JavaScript and causes the change to be committed to the C++ backend. | |
148 void VerifySetCommit(const std::string& name, | |
149 const std::unique_ptr<base::Value>& value); | |
150 // Verifies that committing the previously cleared user-modified pref value | |
151 // through the JavaScript Preferences class fires the correct notification in | |
152 // JavaScript and causes the change to be committed to the C++ backend. | |
153 void VerifyClearCommit(const std::string& name, | |
154 const std::unique_ptr<base::Value>& value); | |
155 // Verifies that rolling back a previously made change of a user-modified pref | |
156 // value through the JavaScript Preferences class fires the correct | |
157 // notification in JavaScript and does not cause the change to be committed to | |
158 // the C++ backend. | |
159 void VerifyRollback(const std::string& name, | |
160 const std::unique_ptr<base::Value>& value, | |
161 const std::string& controlledBy); | |
162 // Start observing notifications sent by the JavaScript Preferences class for | |
163 // pref values changes. | |
164 void StartObserving(); | |
165 // Change the value of a sentinel pref in the C++ backend and finish observing | |
166 // notifications sent by the JavaScript Preferences class when the | |
167 // notification for this pref is received. | |
168 void FinishObserving(std::string* observed_json); | |
169 | |
170 // Populate the lists of test prefs and corresponding policies with default | |
171 // values used by most tests. | |
172 void UseDefaultTestPrefs(bool includeListPref); | |
173 | |
174 // The current tab's render view host, required to inject JavaScript code into | |
175 // the tab. | |
176 content::RenderViewHost* render_view_host_; | |
177 | |
178 // Mock policy provider for both user and device policies. | |
179 policy::MockConfigurationPolicyProvider policy_provider_; | |
180 | |
181 // Pref change registrar that detects changes to user-modified pref values | |
182 // made in the C++ backend by the JavaScript Preferences class. | |
183 std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_; | |
184 | |
185 // The prefs and corresponding policies used by the current test. | |
186 std::vector<std::string> types_; | |
187 std::vector<std::string> pref_names_; | |
188 std::vector<std::string> policy_names_; | |
189 std::vector<std::unique_ptr<base::Value>> default_values_; | |
190 std::vector<std::unique_ptr<base::Value>> non_default_values_; | |
191 | |
192 private: | |
193 DISALLOW_COPY_AND_ASSIGN(PreferencesBrowserTest); | |
194 }; | |
195 | |
196 #endif // CHROME_BROWSER_UI_WEBUI_OPTIONS_PREFERENCES_BROWSERTEST_H_ | |
OLD | NEW |