summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/options/preferences_browsertest.h
blob: eeb13b58a48a8602ec980a72258e84e5835fea8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_WEBUI_OPTIONS_PREFERENCES_BROWSERTEST_H_
#define CHROME_BROWSER_UI_WEBUI_OPTIONS_PREFERENCES_BROWSERTEST_H_

#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_vector.h"
#include "base/prefs/pref_change_registrar.h"
#include "base/prefs/pref_service.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_types.h"
#include "content/public/browser/notification_observer.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace base {
class DictionaryValue;
class Value;
}

namespace content {
class NotificationDetails;
class NotificationSource;
class RenderViewHost;
}

// Tests verifying that the JavaScript Preferences class, the underlying C++
// CoreOptionsHandler and the specialized classes handling Chrome OS device and
// proxy prefs behave correctly.
class PreferencesBrowserTest : public InProcessBrowserTest {
 public:
  PreferencesBrowserTest();
  ~PreferencesBrowserTest() override;

  // InProcessBrowserTest implementation:
  void SetUpOnMainThread() override;

  void OnPreferenceChanged(const std::string& pref_name);

 protected:
  MOCK_METHOD1(OnCommit, void(const PrefService::Preference*));

  void SetUpPrefs();

  // InProcessBrowserTest implementation:
  void SetUpInProcessBrowserTestFixture() override;

  // Sets user policies through the mock policy provider.
  void SetUserPolicies(const std::vector<std::string>& names,
                       const std::vector<base::Value*>& values,
                       policy::PolicyLevel level);
  // Clears user policies.
  void ClearUserPolicies();
  // Set user-modified pref values directly in the C++ backend.
  void SetUserValues(const std::vector<std::string>& names,
                     const std::vector<base::Value*>& values);

  // Verifies that a dictionary contains a (key, value) pair. Takes ownership of
  // |expected|.
  void VerifyKeyValue(const base::DictionaryValue& dict,
                      const std::string& key,
                      const base::Value& expected);
  // Verifies that a dictionary contains a given pref and that its value has
  // been decorated correctly.
  void VerifyPref(const base::DictionaryValue* prefs,
                  const std::string& name,
                  const base::Value* value,
                  const std::string& controlledBy,
                  bool disabled,
                  bool uncommitted);
  // Verifies that a notification received from the JavaScript Preferences
  // class contains a given pref and that its value has been decorated
  // correctly.
  void VerifyObservedPref(const std::string& observed_json,
                          const std::string& name,
                          const base::Value* value,
                          const std::string& controlledBy,
                          bool disabled,
                          bool uncommitted);
  // Verifies that notifications received from the JavaScript Preferences class
  // contain the given prefs and that their values have been decorated
  // correctly.
  void VerifyObservedPrefs(const std::string& observed_json,
                           const std::vector<std::string>& names,
                           const std::vector<base::Value*>& values,
                           const std::string& controlledBy,
                           bool disabled,
                           bool uncommitted);

  // Sets up the expectation that the JavaScript Preferences class will make no
  // change to a user-modified pref value in the C++ backend.
  void ExpectNoCommit(const std::string& name);
  // Sets up the expectation that the JavaScript Preferences class will set a
  // user-modified pref value in the C++ backend.
  void ExpectSetCommit(const std::string& name,
                       const base::Value* value);
  // Sets up the expectation that the JavaScript Preferences class will clear a
  // user-modified pref value in the C++ backend.
  void ExpectClearCommit(const std::string& name);
  // Verifies that previously set expectations are met and clears them.
  void VerifyAndClearExpectations();

  // Sets up the JavaScript part of the test environment.
  void SetupJavaScriptTestEnvironment(
      const std::vector<std::string>& pref_names,
      std::string* observed_json) const;

  // Sets a value through the JavaScript Preferences class as if the user had
  // modified it. Returns the observation which can be verified using the
  // VerifyObserved* methods.
  void SetPref(const std::string& name,
               const std::string& type,
               const base::Value* value,
               bool commit,
               std::string* observed_json);

  // Verifies that setting a user-modified pref value through the JavaScript
  // Preferences class fires the correct notification in JavaScript and commits
  // the change to C++ if |commit| is true.
  void VerifySetPref(const std::string& name,
                     const std::string& type,
                     const base::Value* value,
                     bool commit);
  // Verifies that clearing a user-modified pref value through the JavaScript
  // Preferences class fires the correct notification in JavaScript and does
  // respectively does not cause the change to be committed to the C++ backend.
  void VerifyClearPref(const std::string& name,
                       const base::Value* value,
                       bool commit);
  // Verifies that committing a previously made change of a user-modified pref
  // value through the JavaScript Preferences class fires the correct
  // notification in JavaScript.
  void VerifyCommit(const std::string& name,
                    const base::Value* value,
                    const std::string& controlledBy);
  // Verifies that committing a previously set user-modified pref value through
  // the JavaScript Preferences class fires the correct notification in
  // JavaScript and causes the change to be committed to the C++ backend.
  void VerifySetCommit(const std::string& name,
                       const base::Value* value);
  // Verifies that committing the previously cleared user-modified pref value
  // through the JavaScript Preferences class fires the correct notification in
  // JavaScript and causes the change to be committed to the C++ backend.
  void VerifyClearCommit(const std::string& name,
                         const base::Value* value);
  // Verifies that rolling back a previously made change of a user-modified pref
  // value through the JavaScript Preferences class fires the correct
  // notification in JavaScript and does not cause the change to be committed to
  // the C++ backend.
  void VerifyRollback(const std::string& name,
                      const base::Value* value,
                      const std::string& controlledBy);
  // Start observing notifications sent by the JavaScript Preferences class for
  // pref values changes.
  void StartObserving();
  // Change the value of a sentinel pref in the C++ backend and finish observing
  // notifications sent by the JavaScript Preferences class when the
  // notification for this pref is received.
  void FinishObserving(std::string* observed_json);

  // Populate the lists of test prefs and corresponding policies with default
  // values used by most tests.
  void UseDefaultTestPrefs(bool includeListPref);

  // The current tab's render view host, required to inject JavaScript code into
  // the tab.
  content::RenderViewHost* render_view_host_;

  // Mock policy provider for both user and device policies.
  policy::MockConfigurationPolicyProvider policy_provider_;

  // Pref change registrar that detects changes to user-modified pref values
  // made in the C++ backend by the JavaScript Preferences class.
  PrefChangeRegistrar pref_change_registrar_;

  // The pref service that holds the current pref values in the C++ backend.
  PrefService* pref_service_;

  // The prefs and corresponding policies used by the current test.
  std::vector<std::string> types_;
  std::vector<std::string> pref_names_;
  std::vector<std::string> policy_names_;
  ScopedVector<base::Value> default_values_;
  ScopedVector<base::Value> non_default_values_;

 private:
  DISALLOW_COPY_AND_ASSIGN(PreferencesBrowserTest);
};

#endif  // CHROME_BROWSER_UI_WEBUI_OPTIONS_PREFERENCES_BROWSERTEST_H_