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