| OLD | NEW | 
|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "chrome/browser/ui/webui/options/preferences_browsertest.h" | 5 #include "chrome/browser/ui/webui/options/preferences_browsertest.h" | 
| 6 | 6 | 
| 7 #include <iostream> | 7 #include <iostream> | 
| 8 #include <sstream> | 8 #include <sstream> | 
| 9 | 9 | 
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" | 
| 11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" | 
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" | 
| 13 #include "base/values.h" | 13 #include "base/values.h" | 
|  | 14 #include "chrome/browser/api/prefs/pref_service_base.h" | 
| 14 #include "chrome/browser/policy/browser_policy_connector.h" | 15 #include "chrome/browser/policy/browser_policy_connector.h" | 
| 15 #include "chrome/browser/policy/policy_map.h" | 16 #include "chrome/browser/policy/policy_map.h" | 
| 16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" | 
| 17 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" | 
| 18 #include "chrome/browser/ui/browser_tabstrip.h" | 19 #include "chrome/browser/ui/browser_tabstrip.h" | 
|  | 20 #include "chrome/common/chrome_notification_types.h" | 
| 19 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" | 
| 20 #include "chrome/common/url_constants.h" | 22 #include "chrome/common/url_constants.h" | 
| 21 #include "chrome/test/base/ui_test_utils.h" | 23 #include "chrome/test/base/ui_test_utils.h" | 
|  | 24 #include "content/public/browser/notification_details.h" | 
|  | 25 #include "content/public/browser/notification_source.h" | 
| 22 #include "content/public/browser/render_view_host.h" | 26 #include "content/public/browser/render_view_host.h" | 
| 23 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" | 
| 24 #include "content/public/common/content_client.h" |  | 
| 25 #include "content/public/test/browser_test_utils.h" | 28 #include "content/public/test/browser_test_utils.h" | 
| 26 #include "googleurl/src/gurl.h" | 29 #include "googleurl/src/gurl.h" | 
| 27 #include "policy/policy_constants.h" | 30 #include "policy/policy_constants.h" | 
| 28 #include "testing/gmock/include/gmock/gmock.h" |  | 
| 29 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" | 
| 30 | 32 | 
| 31 #if defined(OS_CHROMEOS) | 33 #if defined(OS_CHROMEOS) | 
| 32 #include "chrome/browser/chromeos/settings/cros_settings_names.h" | 34 #include "chrome/browser/chromeos/settings/cros_settings_names.h" | 
| 33 #include "chrome/browser/chromeos/settings/cros_settings.h" | 35 #include "chrome/browser/chromeos/settings/cros_settings.h" | 
| 34 #include "chrome/browser/chromeos/proxy_cros_settings_parser.h" | 36 #include "chrome/browser/chromeos/proxy_cros_settings_parser.h" | 
| 35 #endif | 37 #endif | 
| 36 | 38 | 
|  | 39 using testing::AllOf; | 
|  | 40 using testing::Mock; | 
|  | 41 using testing::Property; | 
| 37 using testing::Return; | 42 using testing::Return; | 
| 38 | 43 | 
| 39 namespace base { | 44 namespace base { | 
| 40 | 45 | 
| 41 // Helper for using EXPECT_EQ() with base::Value. | 46 // Helper for using EXPECT_EQ() with base::Value. | 
| 42 bool operator==(const base::Value& first, const base::Value& second) { | 47 bool operator==(const Value& first, const Value& second) { | 
| 43   return first.Equals(&second); | 48   return first.Equals(&second); | 
| 44 } | 49 } | 
| 45 | 50 | 
| 46 // Helper for pretty-printing the contents of base::Value in case of failures. | 51 // Helper for pretty-printing the contents of base::Value in case of failures. | 
| 47 void PrintTo(const Value& value, std::ostream* stream) { | 52 void PrintTo(const Value& value, std::ostream* stream) { | 
| 48   std::string json; | 53   std::string json; | 
| 49   JSONWriter::Write(&value, &json); | 54   JSONWriter::Write(&value, &json); | 
| 50   *stream << json; | 55   *stream << json; | 
| 51 } | 56 } | 
| 52 | 57 | 
| 53 }  // namespace base | 58 }  // namespace base | 
| 54 | 59 | 
|  | 60 // Googlemock matcher for base::Value. | 
|  | 61 MATCHER_P(EqualsValue, expected, "") { | 
|  | 62   return arg && arg->Equals(expected); | 
|  | 63 } | 
|  | 64 | 
| 55 PreferencesBrowserTest::PreferencesBrowserTest() { | 65 PreferencesBrowserTest::PreferencesBrowserTest() { | 
| 56 } | 66 } | 
| 57 | 67 | 
| 58 // Sets up a mock user policy provider. | 68 PreferencesBrowserTest::~PreferencesBrowserTest() { | 
| 59 void PreferencesBrowserTest::SetUpInProcessBrowserTestFixture() { | 69 } | 
| 60   EXPECT_CALL(policy_provider_, IsInitializationComplete()) |  | 
| 61       .WillRepeatedly(Return(true)); |  | 
| 62   policy::BrowserPolicyConnector::SetPolicyProviderForTesting( |  | 
| 63       &policy_provider_); |  | 
| 64 }; |  | 
| 65 | 70 | 
| 66 // Navigates to the settings page, causing the JS preference handling classes to | 71 // Navigates to the settings page, causing the JavaScript pref handling code to | 
| 67 // be loaded and initialized. | 72 // load and injects JavaScript testing code. | 
| 68 void PreferencesBrowserTest::SetUpOnMainThread() { | 73 void PreferencesBrowserTest::SetUpOnMainThread() { | 
| 69   ui_test_utils::NavigateToURL(browser(), | 74   ui_test_utils::NavigateToURL(browser(), | 
| 70                                GURL(chrome::kChromeUISettingsFrameURL)); | 75                                GURL(chrome::kChromeUISettingsFrameURL)); | 
| 71   content::WebContents* web_contents = chrome::GetActiveWebContents(browser()); | 76   content::WebContents* web_contents = chrome::GetActiveWebContents(browser()); | 
| 72   ASSERT_TRUE(web_contents); | 77   ASSERT_TRUE(web_contents); | 
| 73   render_view_host_ = web_contents->GetRenderViewHost(); | 78   render_view_host_ = web_contents->GetRenderViewHost(); | 
| 74   ASSERT_TRUE(render_view_host_); | 79   ASSERT_TRUE(render_view_host_); | 
| 75 } | 80   pref_change_registrar_.Init( | 
| 76 | 81       PrefServiceBase::ForProfile(browser()->profile())); | 
| 77 void PreferencesBrowserTest::VerifyValue(const base::DictionaryValue* dict, | 82   pref_service_ = browser()->profile()->GetPrefs(); | 
| 78                                          const std::string& key, | 83   ASSERT_TRUE(content::ExecuteJavaScript(render_view_host_, L"", | 
| 79                                          base::Value* expected) { | 84       L"function TestEnv() {" | 
|  | 85       L"  this.sentinelName_ = 'profile.exited_cleanly';" | 
|  | 86       L"  this.prefs_ = [];" | 
|  | 87       L"  TestEnv.instance_ = this;" | 
|  | 88       L"}" | 
|  | 89       L"" | 
|  | 90       L"TestEnv.handleEvent = function(event) {" | 
|  | 91       L"  var env = TestEnv.instance_;" | 
|  | 92       L"  var name = event.type;" | 
|  | 93       L"  env.removePrefListener_(name);" | 
|  | 94       L"  if (name == TestEnv.sentinelName_)" | 
|  | 95       L"    env.sentinelValue_ = event.value.value;" | 
|  | 96       L"  else" | 
|  | 97       L"    env.reply_[name] = event.value;" | 
|  | 98       L"  if (env.fetching_ && !--env.fetching_ ||" | 
|  | 99       L"      !env.fetching_ && name == env.sentinelName_) {" | 
|  | 100       L"    env.removePrefListeners_();" | 
|  | 101       L"    window.domAutomationController.send(JSON.stringify(env.reply_));" | 
|  | 102       L"    delete env.reply_;" | 
|  | 103       L"  }" | 
|  | 104       L"};" | 
|  | 105       L"" | 
|  | 106       L"TestEnv.prototype = {" | 
|  | 107       L"  addPrefListener_: function(name) {" | 
|  | 108       L"    Preferences.getInstance().addEventListener(name," | 
|  | 109       L"                                               TestEnv.handleEvent);" | 
|  | 110       L"  }," | 
|  | 111       L"" | 
|  | 112       L"  addPrefListeners_: function() {" | 
|  | 113       L"    for (var i in this.prefs_)" | 
|  | 114       L"      this.addPrefListener_(this.prefs_[i]);" | 
|  | 115       L"  }," | 
|  | 116       L"" | 
|  | 117       L"  removePrefListener_: function(name) {" | 
|  | 118       L"    Preferences.getInstance().removeEventListener(name," | 
|  | 119       L"                                                  TestEnv.handleEvent);" | 
|  | 120       L"  }," | 
|  | 121       L"" | 
|  | 122       L"  removePrefListeners_: function() {" | 
|  | 123       L"    for (var i in this.prefs_)" | 
|  | 124       L"      this.removePrefListener_(this.prefs_[i]);" | 
|  | 125       L"  }," | 
|  | 126       L"" | 
|  | 127       L"" | 
|  | 128       L"  addPref: function(name) {" | 
|  | 129       L"    this.prefs_.push(name);" | 
|  | 130       L"  }," | 
|  | 131       L"" | 
|  | 132       L"  setupAndReply: function() {" | 
|  | 133       L"    this.reply_ = {};" | 
|  | 134       L"    Preferences.instance_ = new Preferences();" | 
|  | 135       L"    this.addPref(this.sentinelName_);" | 
|  | 136       L"    this.fetching_ = this.prefs_.length;" | 
|  | 137       L"    this.addPrefListeners_();" | 
|  | 138       L"    Preferences.getInstance().initialize();" | 
|  | 139       L"  }," | 
|  | 140       L"" | 
|  | 141       L"  runAndReply: function(test) {" | 
|  | 142       L"    this.reply_ = {};" | 
|  | 143       L"    this.addPrefListeners_();" | 
|  | 144       L"    test();" | 
|  | 145       L"    this.sentinelValue_ = !this.sentinelValue_;" | 
|  | 146       L"    Preferences.setBooleanPref(this.sentinelName_, this.sentinelValue_," | 
|  | 147       L"                               true);" | 
|  | 148       L"  }," | 
|  | 149       L"" | 
|  | 150       L"  startObserving: function() {" | 
|  | 151       L"    this.reply_ = {};" | 
|  | 152       L"    this.addPrefListeners_();" | 
|  | 153       L"  }," | 
|  | 154       L"" | 
|  | 155       L"  finishObservingAndReply: function() {" | 
|  | 156       L"    this.sentinelValue_ = !this.sentinelValue_;" | 
|  | 157       L"    Preferences.setBooleanPref(this.sentinelName_, this.sentinelValue_," | 
|  | 158       L"                               true);" | 
|  | 159       L"  }" | 
|  | 160       L"};")); | 
|  | 161 } | 
|  | 162 | 
|  | 163 // Forwards notifications received when pref values change in the backend. | 
|  | 164 void PreferencesBrowserTest::Observe( | 
|  | 165     int type, | 
|  | 166     const content::NotificationSource& source, | 
|  | 167     const content::NotificationDetails& details) { | 
|  | 168   ASSERT_EQ(chrome::NOTIFICATION_PREF_CHANGED, type); | 
|  | 169   ASSERT_EQ(pref_service_, content::Source<PrefService>(source).ptr()); | 
|  | 170   std::string* name = content::Details<std::string>(details).ptr(); | 
|  | 171   ASSERT_TRUE(name); | 
|  | 172   OnCommit(pref_service_->FindPreference(name->c_str())); | 
|  | 173 } | 
|  | 174 | 
|  | 175 // Sets up a mock user policy provider. | 
|  | 176 void PreferencesBrowserTest::SetUpInProcessBrowserTestFixture() { | 
|  | 177   EXPECT_CALL(policy_provider_, IsInitializationComplete()) | 
|  | 178       .WillRepeatedly(Return(true)); | 
|  | 179   policy::BrowserPolicyConnector::SetPolicyProviderForTesting( | 
|  | 180       &policy_provider_); | 
|  | 181 }; | 
|  | 182 | 
|  | 183 void PreferencesBrowserTest::TearDownInProcessBrowserTestFixture() { | 
|  | 184   DeleteValues(default_values_); | 
|  | 185   DeleteValues(non_default_values_); | 
|  | 186 } | 
|  | 187 | 
|  | 188 void PreferencesBrowserTest::SetUserPolicies( | 
|  | 189     const std::vector<std::string>& names, | 
|  | 190     const std::vector<base::Value*>& values, | 
|  | 191     policy::PolicyLevel level) { | 
|  | 192   policy::PolicyMap map; | 
|  | 193   for (size_t i = 0; i < names.size(); ++i) | 
|  | 194     map.Set(names[i], level, policy::POLICY_SCOPE_USER, values[i]->DeepCopy()); | 
|  | 195   policy_provider_.UpdateChromePolicy(map); | 
|  | 196 } | 
|  | 197 | 
|  | 198 void PreferencesBrowserTest::ClearUserPolicies() { | 
|  | 199   policy::PolicyMap empty_policy_map; | 
|  | 200   policy_provider_.UpdateChromePolicy(empty_policy_map); | 
|  | 201 } | 
|  | 202 | 
|  | 203 void PreferencesBrowserTest::SetUserValues( | 
|  | 204     const std::vector<std::string>& names, | 
|  | 205     const std::vector<base::Value*>& values) { | 
|  | 206   for (size_t i = 0; i < names.size(); ++i) | 
|  | 207     pref_service_->Set(names[i].c_str(), *values[i]); | 
|  | 208 } | 
|  | 209 | 
|  | 210 void PreferencesBrowserTest::DeleteValues(std::vector<base::Value*>& values){ | 
|  | 211   for (std::vector<base::Value*>::iterator value = values.begin(); | 
|  | 212        value != values.end(); ++value) | 
|  | 213     delete *value; | 
|  | 214   values.clear(); | 
|  | 215 } | 
|  | 216 | 
|  | 217 void PreferencesBrowserTest::VerifyKeyValue(const base::DictionaryValue* dict, | 
|  | 218                                             const std::string& key, | 
|  | 219                                             base::Value* expected) { | 
| 80   const base::Value* actual = NULL; | 220   const base::Value* actual = NULL; | 
| 81   EXPECT_TRUE(dict->Get(key, &actual)) << "Was checking key: " << key; | 221   EXPECT_TRUE(dict->Get(key, &actual)) << "Was checking key: " << key; | 
| 82   EXPECT_EQ(*expected, *actual) << "Was checking key: " << key; | 222   EXPECT_EQ(*expected, *actual) << "Was checking key: " << key; | 
| 83   delete expected; | 223   delete expected; | 
| 84 } | 224 } | 
| 85 | 225 | 
| 86 void PreferencesBrowserTest::VerifyDict(const base::DictionaryValue* dict, |  | 
| 87                                         const base::Value* value, |  | 
| 88                                         const std::string& controlledBy, |  | 
| 89                                         bool disabled) { |  | 
| 90   VerifyValue(dict, "value", value->DeepCopy()); |  | 
| 91   if (!controlledBy.empty()) { |  | 
| 92     VerifyValue(dict, "controlledBy", |  | 
| 93                 base::Value::CreateStringValue(controlledBy)); |  | 
| 94   } else { |  | 
| 95     EXPECT_FALSE(dict->HasKey("controlledBy")); |  | 
| 96   } |  | 
| 97 |  | 
| 98   if (disabled) |  | 
| 99     VerifyValue(dict, "disabled", base::Value::CreateBooleanValue(true)); |  | 
| 100   else if (dict->HasKey("disabled")) |  | 
| 101     VerifyValue(dict, "disabled", base::Value::CreateBooleanValue(false)); |  | 
| 102 } |  | 
| 103 |  | 
| 104 void PreferencesBrowserTest::VerifyPref(const base::DictionaryValue* prefs, | 226 void PreferencesBrowserTest::VerifyPref(const base::DictionaryValue* prefs, | 
| 105                                         const std::string& name, | 227                                         const std::string& name, | 
| 106                                         const base::Value* value, | 228                                         const base::Value* value, | 
| 107                                         const std::string& controlledBy, | 229                                         const std::string& controlledBy, | 
| 108                                         bool disabled) { | 230                                         bool disabled) { | 
| 109   const base::Value* pref; | 231   const base::Value* pref; | 
| 110   const base::DictionaryValue* dict; | 232   const base::DictionaryValue* dict; | 
| 111   ASSERT_TRUE(prefs->Get(name, &pref)); | 233   ASSERT_TRUE(prefs->GetWithoutPathExpansion(name, &pref)); | 
| 112   ASSERT_TRUE(pref->GetAsDictionary(&dict)); | 234   ASSERT_TRUE(pref->GetAsDictionary(&dict)); | 
| 113   VerifyDict(dict, value, controlledBy, disabled); | 235   VerifyKeyValue(dict, "value", value->DeepCopy()); | 
| 114 } | 236   if (!controlledBy.empty()) { | 
| 115 | 237     VerifyKeyValue(dict, "controlledBy", | 
| 116 void PreferencesBrowserTest::VerifyPrefs( | 238                    base::Value::CreateStringValue(controlledBy)); | 
| 117     const base::DictionaryValue* prefs, | 239   } else { | 
|  | 240     EXPECT_FALSE(dict->HasKey("controlledBy")); | 
|  | 241   } | 
|  | 242   if (disabled) | 
|  | 243     VerifyKeyValue(dict, "disabled", base::Value::CreateBooleanValue(true)); | 
|  | 244   else if (dict->HasKey("disabled")) | 
|  | 245     VerifyKeyValue(dict, "disabled", base::Value::CreateBooleanValue(false)); | 
|  | 246 } | 
|  | 247 | 
|  | 248 void PreferencesBrowserTest::VerifyObservedPref(const std::string& json, | 
|  | 249                                                 const std::string& name, | 
|  | 250                                                 const base::Value* value, | 
|  | 251                                                 const std::string& controlledBy, | 
|  | 252                                                 bool disabled) { | 
|  | 253   scoped_ptr<base::Value> observed_value_ptr(base::JSONReader::Read(json)); | 
|  | 254   const base::DictionaryValue* observed_dict; | 
|  | 255   ASSERT_TRUE(observed_value_ptr.get()); | 
|  | 256   ASSERT_TRUE(observed_value_ptr->GetAsDictionary(&observed_dict)); | 
|  | 257   VerifyPref(observed_dict, name, value, controlledBy, disabled); | 
|  | 258 } | 
|  | 259 | 
|  | 260 void PreferencesBrowserTest::VerifyObservedPrefs( | 
|  | 261     const std::string& json, | 
| 118     const std::vector<std::string>& names, | 262     const std::vector<std::string>& names, | 
| 119     const std::vector<base::Value*>& values, | 263     const std::vector<base::Value*>& values, | 
| 120     const std::string& controlledBy, | 264     const std::string& controlledBy, | 
| 121     bool disabled) { | 265     bool disabled) { | 
| 122   ASSERT_EQ(names.size(), values.size()); | 266   scoped_ptr<base::Value> observed_value_ptr(base::JSONReader::Read(json)); | 
|  | 267   const base::DictionaryValue* observed_dict; | 
|  | 268   ASSERT_TRUE(observed_value_ptr.get()); | 
|  | 269   ASSERT_TRUE(observed_value_ptr->GetAsDictionary(&observed_dict)); | 
| 123   for (size_t i = 0; i < names.size(); ++i) | 270   for (size_t i = 0; i < names.size(); ++i) | 
| 124     VerifyPref(prefs, names[i], values[i], controlledBy, disabled); | 271     VerifyPref(observed_dict, names[i], values[i], controlledBy, disabled); | 
|  | 272 } | 
|  | 273 | 
|  | 274 void PreferencesBrowserTest::ExpectNoCommit(const std::string& name) { | 
|  | 275   pref_change_registrar_.Add(name.c_str(), this); | 
|  | 276   EXPECT_CALL(*this, OnCommit(Property(&PrefService::Preference::name, name))) | 
|  | 277       .Times(0); | 
|  | 278 } | 
|  | 279 | 
|  | 280 void PreferencesBrowserTest::ExpectSetCommit(const std::string& name, | 
|  | 281                                              const base::Value* value) { | 
|  | 282   pref_change_registrar_.Add(name.c_str(), this); | 
|  | 283   EXPECT_CALL(*this, OnCommit(AllOf( | 
|  | 284       Property(&PrefService::Preference::name, name), | 
|  | 285       Property(&PrefService::Preference::IsUserControlled, true), | 
|  | 286       Property(&PrefService::Preference::GetValue, EqualsValue(value))))); | 
|  | 287 } | 
|  | 288 | 
|  | 289 void PreferencesBrowserTest::ExpectClearCommit(const std::string& name) { | 
|  | 290   pref_change_registrar_.Add(name.c_str(), this); | 
|  | 291   EXPECT_CALL(*this, OnCommit(AllOf( | 
|  | 292       Property(&PrefService::Preference::name, name), | 
|  | 293       Property(&PrefService::Preference::IsUserControlled, false)))); | 
|  | 294 } | 
|  | 295 | 
|  | 296 void PreferencesBrowserTest::VerifyAndClearExpectations() { | 
|  | 297   Mock::VerifyAndClearExpectations(this); | 
|  | 298   pref_change_registrar_.RemoveAll(); | 
|  | 299 } | 
|  | 300 | 
|  | 301 void PreferencesBrowserTest::SetupJavaScriptTestEnvironment( | 
|  | 302     const std::vector<std::string>& pref_names, | 
|  | 303     std::string* observed_json) const { | 
|  | 304   std::wstringstream javascript; | 
|  | 305   javascript << "var testEnv = new TestEnv();"; | 
|  | 306   for (std::vector<std::string>::const_iterator name = pref_names.begin(); | 
|  | 307        name != pref_names.end(); ++name) | 
|  | 308     javascript << "testEnv.addPref('" << name->c_str() << "');"; | 
|  | 309   javascript << "testEnv.setupAndReply();"; | 
|  | 310   std::string temp_observed_json; | 
|  | 311   if (!observed_json) | 
|  | 312     observed_json = &temp_observed_json; | 
|  | 313   ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 
|  | 314       render_view_host_, L"", javascript.str(), observed_json)); | 
| 125 } | 315 } | 
| 126 | 316 | 
| 127 void PreferencesBrowserTest::VerifySetPref(const std::string& name, | 317 void PreferencesBrowserTest::VerifySetPref(const std::string& name, | 
| 128                                            const std::string& type, | 318                                            const std::string& type, | 
| 129                                            base::Value* set_value, | 319                                            const base::Value* value, | 
| 130                                            base::Value* expected_value) { | 320                                            bool commit) { | 
| 131   scoped_ptr<base::Value> set_value_ptr(set_value); | 321   if (commit) | 
| 132   scoped_ptr<base::Value> expected_value_ptr(expected_value); | 322     ExpectSetCommit(name, value); | 
| 133   std::string set_value_json; | 323   else | 
| 134   base::JSONWriter::Write(set_value, &set_value_json); | 324     ExpectNoCommit(name); | 
|  | 325   scoped_ptr<base::Value> commit_ptr(base::Value::CreateBooleanValue(commit)); | 
|  | 326   std::string value_json; | 
|  | 327   std::string commit_json; | 
|  | 328   base::JSONWriter::Write(value, &value_json); | 
|  | 329   base::JSONWriter::Write(commit_ptr.get(), &commit_json); | 
| 135   std::wstringstream javascript; | 330   std::wstringstream javascript; | 
| 136   javascript << "pref_changed_callback = function(notification) {" | 331   javascript << "testEnv.runAndReply(function() {" | 
| 137              << "  if (notification[0] == '" << name.c_str() << "') {" | 332              << "    Preferences.set" << type.c_str() << "Pref(" | 
| 138              << "    pref_changed_callback = function() {};" | 333              << "      '" << name.c_str() << "'," | 
| 139              << "    window.domAutomationController.send(" | 334              << "      " << value_json.c_str() << "," | 
| 140              << "        JSON.stringify(notification[1]));" | 335              << "      " << commit_json.c_str() << ");});"; | 
| 141              << "  }" | 336   std::string observed_json; | 
| 142              << "};" |  | 
| 143              << "chrome.send('observePrefs', ['pref_changed_callback'," |  | 
| 144              << "    '" << name.c_str() << "']);" |  | 
| 145              << "Preferences.set" << type.c_str() << "Pref(" |  | 
| 146              << "    '" << name.c_str() << "'," |  | 
| 147              << "    " << set_value_json.c_str() << ");"; |  | 
| 148   std::string actual_json; |  | 
| 149   ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 337   ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 
| 150       render_view_host_, L"", javascript.str(), &actual_json)); | 338       render_view_host_, L"", javascript.str(), &observed_json)); | 
| 151 | 339   VerifyObservedPref(observed_json, name, value, "", false); | 
| 152   base::Value* actual_value = base::JSONReader::Read(actual_json); | 340   VerifyAndClearExpectations(); | 
| 153   const base::DictionaryValue* actual_dict; | 341 } | 
| 154   ASSERT_TRUE(actual_value); | 342 | 
| 155   ASSERT_TRUE(actual_value->GetAsDictionary(&actual_dict)); | 343 void PreferencesBrowserTest::VerifyClearPref(const std::string& name, | 
| 156   VerifyDict(actual_dict, expected_value ? expected_value : set_value, | 344                                              const base::Value* value, | 
| 157              "", false); | 345                                              bool commit) { | 
| 158 } | 346   if (commit) | 
| 159 | 347     ExpectClearCommit(name); | 
| 160 void PreferencesBrowserTest::FetchPrefs(const std::vector<std::string>& names, | 348   else | 
| 161                                         base::DictionaryValue** prefs) { | 349     ExpectNoCommit(name); | 
| 162   *prefs = NULL; | 350   scoped_ptr<base::Value> commit_ptr(base::Value::CreateBooleanValue(commit)); | 
| 163   base::ListValue args_list; | 351   std::string commit_json; | 
| 164   args_list.Append(base::Value::CreateStringValue("prefs_fetched_callback")); | 352   base::JSONWriter::Write(commit_ptr.get(), &commit_json); | 
| 165   for (std::vector<std::string>::const_iterator name = names.begin(); |  | 
| 166        name != names.end(); ++name) |  | 
| 167     args_list.Append(base::Value::CreateStringValue(*name)); |  | 
| 168   std::string args_json; |  | 
| 169   base::JSONWriter::Write(&args_list, &args_json); |  | 
| 170   std::wstringstream javascript; | 353   std::wstringstream javascript; | 
| 171   javascript << "prefs_fetched_callback = function(dict) {" | 354   javascript << "testEnv.runAndReply(function() {" | 
| 172              << "  window.domAutomationController.send(JSON.stringify(dict));" | 355              << "    Preferences.clearPref(" | 
| 173              << "};" | 356              << "      '" << name.c_str() << "'," | 
| 174              << "chrome.send('fetchPrefs', " << args_json.c_str() << ");"; | 357              << "      " << commit_json.c_str() << ");});"; | 
| 175 | 358   std::string observed_json; | 
| 176   std::string fetched_json; |  | 
| 177   ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 359   ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 
| 178       render_view_host_, L"", javascript.str(), &fetched_json)); | 360       render_view_host_, L"", javascript.str(), &observed_json)); | 
| 179   base::Value* fetched_value = base::JSONReader::Read(fetched_json); | 361   VerifyObservedPref(observed_json, name, value, "recommended", false); | 
| 180   base::DictionaryValue* fetched_dict; | 362   VerifyAndClearExpectations(); | 
| 181   ASSERT_TRUE(fetched_value); | 363 } | 
| 182   ASSERT_TRUE(fetched_value->GetAsDictionary(&fetched_dict)); | 364 | 
| 183   *prefs = fetched_dict; | 365 void PreferencesBrowserTest::VerifyCommit(const std::string& name, | 
| 184 } | 366                                           const base::Value* value, | 
| 185 | 367                                           const std::string& controlledBy) { | 
| 186 void PreferencesBrowserTest::SetUserPolicies( | 368   std::wstringstream javascript; | 
| 187     const std::vector<std::string>& names, | 369   javascript << "testEnv.runAndReply(function() {" | 
| 188     const std::vector<base::Value*>& values, | 370              << "    Preferences.getInstance().commitPref(" | 
| 189     policy::PolicyLevel level) { | 371              << "        '" << name.c_str() << "');});"; | 
| 190   ASSERT_TRUE(names.size() == values.size()); | 372   std::string observed_json; | 
| 191   policy::PolicyMap map; | 373   ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 
| 192   for (size_t i = 0; i < names.size(); ++i) | 374       render_view_host_, L"", javascript.str(), &observed_json)); | 
| 193     map.Set(names[i], level, policy::POLICY_SCOPE_USER, values[i]->DeepCopy()); | 375   VerifyObservedPref(observed_json, name, value, controlledBy, false); | 
| 194   policy_provider_.UpdateChromePolicy(map); | 376 } | 
| 195 } | 377 | 
| 196 | 378 void PreferencesBrowserTest::VerifySetCommit(const std::string& name, | 
| 197 void PreferencesBrowserTest::DeleteValues(std::vector<base::Value*>& values){ | 379                                              const base::Value* value) { | 
| 198   for (std::vector<base::Value*>::iterator value = values.begin(); | 380   ExpectSetCommit(name, value); | 
| 199        value != values.end(); ++value) | 381   VerifyCommit(name, value, ""); | 
| 200     delete *value; | 382   VerifyAndClearExpectations(); | 
| 201   values.clear(); | 383 } | 
| 202 } | 384 | 
| 203 | 385 void PreferencesBrowserTest::VerifyClearCommit(const std::string& name, | 
| 204 // Verifies that pref values received by observer callbacks are decorated | 386                                                const base::Value* value) { | 
| 205 // correctly when the prefs are set from JavaScript. | 387   ExpectClearCommit(name); | 
|  | 388   VerifyCommit(name, value, "recommended"); | 
|  | 389   VerifyAndClearExpectations(); | 
|  | 390 } | 
|  | 391 | 
|  | 392 void PreferencesBrowserTest::VerifyRollback(const std::string& name, | 
|  | 393                                             const base::Value* value, | 
|  | 394                                             const std::string& controlledBy) { | 
|  | 395   ExpectNoCommit(name); | 
|  | 396   std::wstringstream javascript; | 
|  | 397   javascript << "testEnv.runAndReply(function() {" | 
|  | 398              << "    Preferences.getInstance().rollbackPref(" | 
|  | 399              << "        '" << name.c_str() << "');});"; | 
|  | 400   std::string observed_json; | 
|  | 401   ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 
|  | 402       render_view_host_, L"", javascript.str(), &observed_json)); | 
|  | 403   VerifyObservedPref(observed_json, name, value, controlledBy, false); | 
|  | 404   VerifyAndClearExpectations(); | 
|  | 405 } | 
|  | 406 | 
|  | 407 void PreferencesBrowserTest::StartObserving() { | 
|  | 408   ASSERT_TRUE(content::ExecuteJavaScript( | 
|  | 409       render_view_host_, L"", L"testEnv.startObserving();")); | 
|  | 410 } | 
|  | 411 | 
|  | 412 void PreferencesBrowserTest::FinishObserving(std::string* observed_json) { | 
|  | 413   ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 
|  | 414       render_view_host_, L"", L"testEnv.finishObservingAndReply();", | 
|  | 415       observed_json)); | 
|  | 416 } | 
|  | 417 | 
|  | 418 void PreferencesBrowserTest::UseDefaultTestPrefs(bool includeListPref) { | 
|  | 419   // Boolean pref. | 
|  | 420   types_.push_back("Boolean"); | 
|  | 421   pref_names_.push_back(prefs::kAlternateErrorPagesEnabled); | 
|  | 422   policy_names_.push_back(policy::key::kAlternateErrorPagesEnabled); | 
|  | 423   non_default_values_.push_back(base::Value::CreateBooleanValue(false)); | 
|  | 424 | 
|  | 425   // Integer pref. | 
|  | 426   types_.push_back("Integer"); | 
|  | 427   pref_names_.push_back(prefs::kRestoreOnStartup); | 
|  | 428   policy_names_.push_back(policy::key::kRestoreOnStartup); | 
|  | 429   non_default_values_.push_back(base::Value::CreateIntegerValue(4)); | 
|  | 430 | 
|  | 431   // String pref. | 
|  | 432   types_.push_back("String"); | 
|  | 433   pref_names_.push_back(prefs::kEnterpriseWebStoreName); | 
|  | 434   policy_names_.push_back(policy::key::kEnterpriseWebStoreName); | 
|  | 435   non_default_values_.push_back(base::Value::CreateStringValue("Store")); | 
|  | 436 | 
|  | 437   // URL pref. | 
|  | 438   types_.push_back("URL"); | 
|  | 439   pref_names_.push_back(prefs::kEnterpriseWebStoreURL); | 
|  | 440   policy_names_.push_back(policy::key::kEnterpriseWebStoreURL); | 
|  | 441   non_default_values_.push_back( | 
|  | 442       base::Value::CreateStringValue("http://www.google.com/")); | 
|  | 443 | 
|  | 444   // List pref. | 
|  | 445   if (includeListPref) { | 
|  | 446     types_.push_back("List"); | 
|  | 447     pref_names_.push_back(prefs::kURLsToRestoreOnStartup); | 
|  | 448     policy_names_.push_back(policy::key::kRestoreOnStartupURLs); | 
|  | 449     base::ListValue* list = new base::ListValue; | 
|  | 450     list->Append(base::Value::CreateStringValue("http://www.google.com")); | 
|  | 451     list->Append(base::Value::CreateStringValue("http://example.com")); | 
|  | 452     non_default_values_.push_back(list); | 
|  | 453   } | 
|  | 454 | 
|  | 455   // Retrieve default values. | 
|  | 456   for (std::vector<std::string>::const_iterator name = pref_names_.begin(); | 
|  | 457         name != pref_names_.end(); ++name) { | 
|  | 458     default_values_.push_back( | 
|  | 459         pref_service_->GetDefaultPrefValue(name->c_str())->DeepCopy()); | 
|  | 460   } | 
|  | 461 } | 
|  | 462 | 
|  | 463 // Verifies that initializing the JavaScript Preferences class fires the correct | 
|  | 464 // notifications in JavaScript. | 
|  | 465 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, FetchPrefs) { | 
|  | 466   UseDefaultTestPrefs(true); | 
|  | 467   std::string observed_json; | 
|  | 468 | 
|  | 469   // Verify notifications when default values are in effect. | 
|  | 470   SetupJavaScriptTestEnvironment(pref_names_, &observed_json); | 
|  | 471   VerifyObservedPrefs(observed_json, pref_names_, default_values_, "", false); | 
|  | 472 | 
|  | 473   // Verify notifications when recommended values are in effect. | 
|  | 474   SetUserPolicies(policy_names_, non_default_values_, | 
|  | 475                   policy::POLICY_LEVEL_RECOMMENDED); | 
|  | 476   SetupJavaScriptTestEnvironment(pref_names_, &observed_json); | 
|  | 477   VerifyObservedPrefs(observed_json, pref_names_, non_default_values_, | 
|  | 478                       "recommended", false); | 
|  | 479 | 
|  | 480   // Verify notifications when mandatory values are in effect. | 
|  | 481   SetUserPolicies(policy_names_, non_default_values_, | 
|  | 482                   policy::POLICY_LEVEL_MANDATORY); | 
|  | 483   SetupJavaScriptTestEnvironment(pref_names_, &observed_json); | 
|  | 484   VerifyObservedPrefs(observed_json, pref_names_, non_default_values_, | 
|  | 485                       "policy", true); | 
|  | 486 | 
|  | 487   // Verify notifications when user-modified values are in effect. | 
|  | 488   ClearUserPolicies(); | 
|  | 489   SetUserValues(pref_names_, non_default_values_); | 
|  | 490   SetupJavaScriptTestEnvironment(pref_names_, &observed_json); | 
|  | 491   VerifyObservedPrefs(observed_json, pref_names_, non_default_values_, | 
|  | 492                       "", false); | 
|  | 493 } | 
|  | 494 | 
|  | 495 // Verifies that setting a user-modified pref value through the JavaScript | 
|  | 496 // Preferences class fires the correct notification in JavaScript and causes the | 
|  | 497 // change to be committed to the C++ backend. | 
| 206 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, SetPrefs) { | 498 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, SetPrefs) { | 
| 207   // Prefs handled by CoreOptionsHandler. | 499   UseDefaultTestPrefs(false); | 
| 208   VerifySetPref(prefs::kAlternateErrorPagesEnabled, "Boolean", | 500 | 
| 209                 base::Value::CreateBooleanValue(false), NULL); | 501   ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_, NULL)); | 
| 210   VerifySetPref(prefs::kRestoreOnStartup, "Integer", | 502   for (size_t i = 0; i < pref_names_.size(); ++i) | 
| 211                 base::Value::CreateIntegerValue(4), NULL); | 503     VerifySetPref(pref_names_[i], types_[i], non_default_values_[i], true); | 
| 212   VerifySetPref(prefs::kEnterpriseWebStoreName, "String", | 504 } | 
| 213                 base::Value::CreateStringValue("Store"), NULL); | 505 | 
| 214   VerifySetPref(prefs::kEnterpriseWebStoreURL, "URL", | 506 // Verifies that clearing a user-modified pref value through the JavaScript | 
| 215                 base::Value::CreateStringValue("http://www.google.com"), | 507 // Preferences class fires the correct notification in JavaScript and causes the | 
| 216                 base::Value::CreateStringValue("http://www.google.com/")); | 508 // change to be committed to the C++ backend. | 
| 217 } | 509 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, ClearPrefs) { | 
| 218 | 510   UseDefaultTestPrefs(false); | 
| 219 // Verifies that pref values are decorated correctly when fetched from | 511 | 
| 220 // JavaScript. | 512   SetUserPolicies(policy_names_, default_values_, | 
| 221 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, FetchPrefs) { | 513                   policy::POLICY_LEVEL_RECOMMENDED); | 
| 222   // Pref names. | 514   SetUserValues(pref_names_, non_default_values_); | 
| 223   std::vector<std::string> pref_names; | 515   ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_, NULL)); | 
|  | 516   for (size_t i = 0; i < pref_names_.size(); ++i) | 
|  | 517     VerifyClearPref(pref_names_[i], default_values_[i], true); | 
|  | 518 } | 
|  | 519 | 
|  | 520 // Verifies that when the user-modified value of a dialog pref is set and the | 
|  | 521 // change then committed through the JavaScript Preferences class, the correct | 
|  | 522 // notifications fire and a commit to the C++ backend occurs in the latter step | 
|  | 523 // only. | 
|  | 524 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, DialogPrefsSetCommit) { | 
|  | 525   UseDefaultTestPrefs(false); | 
|  | 526 | 
|  | 527   ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_, NULL)); | 
|  | 528   for (size_t i = 0; i < pref_names_.size(); ++i) { | 
|  | 529     VerifySetPref(pref_names_[i], types_[i], non_default_values_[i], false); | 
|  | 530     VerifySetCommit(pref_names_[i], non_default_values_[i]); | 
|  | 531   } | 
|  | 532 } | 
|  | 533 | 
|  | 534 // Verifies that when the user-modified value of a dialog pref is set and the | 
|  | 535 // change then rolled back through the JavaScript Preferences class, the correct | 
|  | 536 // notifications fire and no commit to the C++ backend occurs. | 
|  | 537 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, DialogPrefsSetRollback) { | 
|  | 538   UseDefaultTestPrefs(false); | 
|  | 539 | 
|  | 540   // Verify behavior when default values are in effect. | 
|  | 541   ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_, NULL)); | 
|  | 542   for (size_t i = 0; i < pref_names_.size(); ++i) { | 
|  | 543     VerifySetPref(pref_names_[i], types_[i], non_default_values_[i], false); | 
|  | 544     VerifyRollback(pref_names_[i], default_values_[i], ""); | 
|  | 545   } | 
|  | 546 | 
|  | 547   // Verify behavior when recommended values are in effect. | 
|  | 548   SetUserPolicies(policy_names_, default_values_, | 
|  | 549                   policy::POLICY_LEVEL_RECOMMENDED); | 
|  | 550   ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_, NULL)); | 
|  | 551   for (size_t i = 0; i < pref_names_.size(); ++i) { | 
|  | 552     VerifySetPref(pref_names_[i], types_[i], non_default_values_[i], false); | 
|  | 553     VerifyRollback(pref_names_[i], default_values_[i], "recommended"); | 
|  | 554   } | 
|  | 555 } | 
|  | 556 | 
|  | 557 // Verifies that when the user-modified value of a dialog pref is cleared and | 
|  | 558 // the change then committed through the JavaScript Preferences class, the | 
|  | 559 // correct notifications fire and a commit to the C++ backend occurs in the | 
|  | 560 // latter step only. | 
|  | 561 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, DialogPrefsClearCommit) { | 
|  | 562   UseDefaultTestPrefs(false); | 
|  | 563 | 
|  | 564   SetUserPolicies(policy_names_, default_values_, | 
|  | 565                   policy::POLICY_LEVEL_RECOMMENDED); | 
|  | 566   SetUserValues(pref_names_, non_default_values_); | 
|  | 567   ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_, NULL)); | 
|  | 568   for (size_t i = 0; i < pref_names_.size(); ++i) { | 
|  | 569     VerifyClearPref(pref_names_[i], default_values_[i], false); | 
|  | 570     VerifyClearCommit(pref_names_[i], default_values_[i]); | 
|  | 571   } | 
|  | 572 } | 
|  | 573 | 
|  | 574 // Verifies that when the user-modified value of a dialog pref is cleared and | 
|  | 575 // the change then rolled back through the JavaScript Preferences class, the | 
|  | 576 // correct notifications fire and no commit to the C++ backend occurs. | 
|  | 577 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, DialogPrefsClearRollback) { | 
|  | 578   UseDefaultTestPrefs(false); | 
|  | 579 | 
|  | 580   SetUserPolicies(policy_names_, default_values_, | 
|  | 581                   policy::POLICY_LEVEL_RECOMMENDED); | 
|  | 582   SetUserValues(pref_names_, non_default_values_); | 
|  | 583   ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_, NULL)); | 
|  | 584   for (size_t i = 0; i < pref_names_.size(); ++i) { | 
|  | 585     VerifyClearPref(pref_names_[i], default_values_[i], false); | 
|  | 586     VerifyRollback(pref_names_[i], non_default_values_[i], ""); | 
|  | 587   } | 
|  | 588 } | 
|  | 589 | 
|  | 590 // Verifies that when preference values change in the C++ backend, the correct | 
|  | 591 // notifications fire in JavaScript. | 
|  | 592 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, NotificationsOnBackendChanges) { | 
|  | 593   UseDefaultTestPrefs(false); | 
|  | 594   std::string observed_json; | 
|  | 595 | 
|  | 596   ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_, NULL)); | 
|  | 597 | 
|  | 598   // Verify notifications when recommended values come into effect. | 
|  | 599   StartObserving(); | 
|  | 600   SetUserPolicies(policy_names_, non_default_values_, | 
|  | 601                   policy::POLICY_LEVEL_RECOMMENDED); | 
|  | 602   FinishObserving(&observed_json); | 
|  | 603   VerifyObservedPrefs(observed_json, pref_names_, non_default_values_, | 
|  | 604                       "recommended", false); | 
|  | 605 | 
|  | 606   // Verify notifications when mandatory values come into effect. | 
|  | 607   StartObserving(); | 
|  | 608   SetUserPolicies(policy_names_, non_default_values_, | 
|  | 609                   policy::POLICY_LEVEL_MANDATORY); | 
|  | 610   FinishObserving(&observed_json); | 
|  | 611   VerifyObservedPrefs(observed_json, pref_names_, non_default_values_, | 
|  | 612                       "policy", true); | 
|  | 613 | 
|  | 614   // Verify notifications when default values come into effect. | 
|  | 615   StartObserving(); | 
|  | 616   ClearUserPolicies(); | 
|  | 617   FinishObserving(&observed_json); | 
|  | 618   VerifyObservedPrefs(observed_json, pref_names_, default_values_, "", false); | 
|  | 619 | 
|  | 620   // Verify notifications when user-modified values come into effect. | 
|  | 621   StartObserving(); | 
|  | 622   SetUserValues(pref_names_, non_default_values_); | 
|  | 623   FinishObserving(&observed_json); | 
|  | 624   VerifyObservedPrefs(observed_json, pref_names_, non_default_values_, | 
|  | 625                       "", false); | 
|  | 626 } | 
|  | 627 | 
|  | 628 #if defined(OS_CHROMEOS) | 
|  | 629 | 
|  | 630 // Verifies that initializing the JavaScript Preferences class fires the correct | 
|  | 631 // notifications in JavaScript for pref values handled by the | 
|  | 632 // CoreChromeOSOptionsHandler class. | 
|  | 633 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, ChromeOSDeviceFetchPrefs) { | 
|  | 634   std::vector<base::Value*> decorated_non_default_values; | 
|  | 635   std::string observed_json; | 
|  | 636 | 
| 224   // Boolean pref. | 637   // Boolean pref. | 
| 225   pref_names.push_back(prefs::kAlternateErrorPagesEnabled); | 638   pref_names_.push_back(chromeos::kAccountsPrefAllowGuest); | 
| 226   // Integer pref. | 639   default_values_.push_back(base::Value::CreateBooleanValue(true)); | 
| 227   pref_names.push_back(prefs::kRestoreOnStartup); | 640   non_default_values_.push_back(base::Value::CreateBooleanValue(false)); | 
|  | 641   decorated_non_default_values.push_back( | 
|  | 642       non_default_values_.back()->DeepCopy()); | 
|  | 643 | 
| 228   // String pref. | 644   // String pref. | 
| 229   pref_names.push_back(prefs::kEnterpriseWebStoreName); | 645   pref_names_.push_back(chromeos::kReleaseChannel); | 
| 230   // URL pref. | 646   default_values_.push_back(base::Value::CreateStringValue("")); | 
| 231   pref_names.push_back(prefs::kEnterpriseWebStoreURL); | 647   non_default_values_.push_back( | 
|  | 648       base::Value::CreateStringValue("stable-channel")); | 
|  | 649   decorated_non_default_values.push_back( | 
|  | 650       non_default_values_.back()->DeepCopy()); | 
|  | 651 | 
| 232   // List pref. | 652   // List pref. | 
| 233   pref_names.push_back(prefs::kURLsToRestoreOnStartup); | 653   pref_names_.push_back(chromeos::kAccountsPrefUsers); | 
| 234 | 654   default_values_.push_back(new base::ListValue); | 
| 235   // Corresponding policy names. |  | 
| 236   std::vector<std::string> policy_names; |  | 
| 237   policy_names.push_back(policy::key::kAlternateErrorPagesEnabled); |  | 
| 238   policy_names.push_back(policy::key::kRestoreOnStartup); |  | 
| 239   policy_names.push_back(policy::key::kEnterpriseWebStoreName); |  | 
| 240   policy_names.push_back(policy::key::kEnterpriseWebStoreURL); |  | 
| 241   policy_names.push_back(policy::key::kRestoreOnStartupURLs); |  | 
| 242 |  | 
| 243   // Default values. |  | 
| 244   std::vector<base::Value*> values; |  | 
| 245   values.push_back(base::Value::CreateBooleanValue(true)); |  | 
| 246 #if defined(OS_CHROMEOS) |  | 
| 247   values.push_back(base::Value::CreateIntegerValue(1)); |  | 
| 248 #else |  | 
| 249   values.push_back(base::Value::CreateIntegerValue(5)); |  | 
| 250 #endif |  | 
| 251   values.push_back(base::Value::CreateStringValue("")); |  | 
| 252   values.push_back(base::Value::CreateStringValue("")); |  | 
| 253   values.push_back(new base::ListValue); |  | 
| 254 |  | 
| 255   // Verify default values are fetched and decorated correctly. |  | 
| 256   base::DictionaryValue* fetched; |  | 
| 257   FetchPrefs(pref_names, &fetched); |  | 
| 258   if (fetched) |  | 
| 259     VerifyPrefs(fetched, pref_names, values, "", false); |  | 
| 260   delete fetched; |  | 
| 261 |  | 
| 262   // Set recommended values. |  | 
| 263   DeleteValues(values); |  | 
| 264   values.push_back(base::Value::CreateBooleanValue(false)); |  | 
| 265   values.push_back(base::Value::CreateIntegerValue(4)); |  | 
| 266   values.push_back(base::Value::CreateStringValue("Store")); |  | 
| 267   values.push_back(base::Value::CreateStringValue("http://www.google.com")); |  | 
| 268   base::ListValue* list = new base::ListValue; |  | 
| 269   list->Append(base::Value::CreateStringValue("http://www.google.com")); |  | 
| 270   list->Append(base::Value::CreateStringValue("http://example.com")); |  | 
| 271   values.push_back(list); |  | 
| 272   SetUserPolicies(policy_names, values, policy::POLICY_LEVEL_RECOMMENDED); |  | 
| 273 |  | 
| 274   // Verify recommended values are fetched and decorated correctly. |  | 
| 275   FetchPrefs(pref_names, &fetched); |  | 
| 276   if (fetched) |  | 
| 277     VerifyPrefs(fetched, pref_names, values, "recommended", false); |  | 
| 278   delete fetched; |  | 
| 279 |  | 
| 280   // Set mandatory values. |  | 
| 281   SetUserPolicies(policy_names, values, policy::POLICY_LEVEL_MANDATORY); |  | 
| 282 |  | 
| 283   // Verify mandatory values are fetched and decorated correctly. |  | 
| 284   FetchPrefs(pref_names, &fetched); |  | 
| 285   if (fetched) |  | 
| 286     VerifyPrefs(fetched, pref_names, values, "policy", true); |  | 
| 287   delete fetched; |  | 
| 288 |  | 
| 289   DeleteValues(values); |  | 
| 290 } |  | 
| 291 |  | 
| 292 #if defined(OS_CHROMEOS) |  | 
| 293 // Verifies that pref values handled by the CoreChromeOSOptionsHandler class |  | 
| 294 // are decorated correctly when requested from JavaScript. |  | 
| 295 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, ChromeOSDeviceFetchPrefs) { |  | 
| 296   // Pref names. |  | 
| 297   std::vector<std::string> names; |  | 
| 298   // Boolean pref. |  | 
| 299   names.push_back(chromeos::kAccountsPrefAllowGuest); |  | 
| 300   // String pref. |  | 
| 301   names.push_back(chromeos::kReleaseChannel); |  | 
| 302   // List pref. |  | 
| 303   names.push_back(chromeos::kAccountsPrefUsers); |  | 
| 304 |  | 
| 305   // Default pref values. |  | 
| 306   std::vector<base::Value*> values; |  | 
| 307   values.push_back(base::Value::CreateBooleanValue(true)); |  | 
| 308   values.push_back(base::Value::CreateStringValue("")); |  | 
| 309   values.push_back(new base::ListValue); |  | 
| 310 |  | 
| 311   // Verify default values are fetched and decorated correctly. |  | 
| 312   base::DictionaryValue* fetched; |  | 
| 313   FetchPrefs(names, &fetched); |  | 
| 314   if (fetched) |  | 
| 315     VerifyPrefs(fetched, names, values, "", true); |  | 
| 316   delete fetched; |  | 
| 317 |  | 
| 318   // Set mandatory values. |  | 
| 319   DeleteValues(values); |  | 
| 320   values.push_back(base::Value::CreateBooleanValue(false)); |  | 
| 321   values.push_back(base::Value::CreateStringValue("stable-channel")); |  | 
| 322   base::ListValue* list = new base::ListValue; | 655   base::ListValue* list = new base::ListValue; | 
| 323   list->Append(base::Value::CreateStringValue("me@google.com")); | 656   list->Append(base::Value::CreateStringValue("me@google.com")); | 
| 324   list->Append(base::Value::CreateStringValue("you@google.com")); | 657   list->Append(base::Value::CreateStringValue("you@google.com")); | 
| 325   values.push_back(list); | 658   non_default_values_.push_back(list); | 
| 326   ASSERT_EQ(names.size(), values.size()); |  | 
| 327   chromeos::CrosSettings* cros_settings = chromeos::CrosSettings::Get(); |  | 
| 328   for (size_t i = 0; i < names.size(); ++i) |  | 
| 329     cros_settings->Set(names[i], *values[i]); |  | 
| 330 |  | 
| 331   // Verify mandatory values are fetched and decorated correctly. |  | 
| 332   DeleteValues(values); |  | 
| 333   values.push_back(base::Value::CreateBooleanValue(false)); |  | 
| 334   values.push_back(base::Value::CreateStringValue("stable-channel")); |  | 
| 335   list = new base::ListValue; | 659   list = new base::ListValue; | 
| 336   base::DictionaryValue* dict = new base::DictionaryValue; | 660   base::DictionaryValue* dict = new base::DictionaryValue; | 
| 337   dict->SetString("username", "me@google.com"); | 661   dict->SetString("username", "me@google.com"); | 
| 338   dict->SetString("name", "me@google.com"); | 662   dict->SetString("name", "me@google.com"); | 
| 339   dict->SetString("email", ""); | 663   dict->SetString("email", ""); | 
| 340   dict->SetBoolean("owner", false); | 664   dict->SetBoolean("owner", false); | 
| 341   list->Append(dict); | 665   list->Append(dict); | 
| 342   dict = new base::DictionaryValue; | 666   dict = new base::DictionaryValue; | 
| 343   dict->SetString("username", "you@google.com"); | 667   dict->SetString("username", "you@google.com"); | 
| 344   dict->SetString("name", "you@google.com"); | 668   dict->SetString("name", "you@google.com"); | 
| 345   dict->SetString("email", ""); | 669   dict->SetString("email", ""); | 
| 346   dict->SetBoolean("owner", false); | 670   dict->SetBoolean("owner", false); | 
| 347   list->Append(dict); | 671   list->Append(dict); | 
| 348   values.push_back(list); | 672   decorated_non_default_values.push_back(list); | 
| 349   FetchPrefs(names, &fetched); | 673 | 
|  | 674   // Verify notifications when default values are in effect. | 
|  | 675   SetupJavaScriptTestEnvironment(pref_names_, &observed_json); | 
|  | 676   VerifyObservedPrefs(observed_json, pref_names_, default_values_, "", true); | 
|  | 677 | 
|  | 678   // Verify notifications when mandatory values are in effect. | 
|  | 679   chromeos::CrosSettings* cros_settings = chromeos::CrosSettings::Get(); | 
|  | 680   for (size_t i = 0; i < pref_names_.size(); ++i) | 
|  | 681     cros_settings->Set(pref_names_[i], *non_default_values_[i]); | 
| 350   // FIXME(bartfab): Find a way to simulate enterprise enrollment in browser | 682   // FIXME(bartfab): Find a way to simulate enterprise enrollment in browser | 
| 351   // tests. Only if Chrome thinks that it is enrolled will the device prefs be | 683   // tests. Only if Chrome thinks that it is enrolled will the device prefs be | 
| 352   // decorated with "controlledBy: policy". | 684   // decorated with "controlledBy: policy". | 
| 353   if (fetched) | 685   SetupJavaScriptTestEnvironment(pref_names_, &observed_json); | 
| 354     VerifyPrefs(fetched, names, values, "", true); | 686   VerifyObservedPrefs(observed_json, pref_names_, decorated_non_default_values, | 
| 355   delete fetched; | 687                       "", true); | 
| 356 | 688 | 
| 357   DeleteValues(values); | 689   DeleteValues(decorated_non_default_values); | 
| 358 } | 690 } | 
| 359 | 691 | 
| 360 // Verifies that pref values handled by the Chrome OS proxy settings parser are | 692 // Verifies that initializing the JavaScript Preferences class fires the correct | 
| 361 // decorated correctly when requested from JavaScript. | 693 // notifications in JavaScript for pref values handled by the Chrome OS proxy | 
|  | 694 // settings parser. | 
| 362 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, ChromeOSProxyFetchPrefs) { | 695 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, ChromeOSProxyFetchPrefs) { | 
| 363   // Pref names. | 696   std::string observed_json; | 
| 364   std::vector<std::string> names; | 697 | 
| 365   // Boolean pref. | 698   // Boolean pref. | 
| 366   names.push_back(chromeos::kProxySingle); | 699   pref_names_.push_back(chromeos::kProxySingle); | 
|  | 700   default_values_.push_back(base::Value::CreateBooleanValue(false)); | 
|  | 701   non_default_values_.push_back(base::Value::CreateBooleanValue(true)); | 
|  | 702 | 
| 367   // Integer pref. | 703   // Integer pref. | 
| 368   names.push_back(chromeos::kProxySingleHttpPort); | 704   pref_names_.push_back(chromeos::kProxySingleHttpPort); | 
|  | 705   default_values_.push_back(base::Value::CreateStringValue("")); | 
|  | 706   non_default_values_.push_back(base::Value::CreateIntegerValue(8080)); | 
|  | 707 | 
| 369   // String pref. | 708   // String pref. | 
| 370   names.push_back(chromeos::kProxySingleHttp); | 709   pref_names_.push_back(chromeos::kProxySingleHttp); | 
|  | 710   default_values_.push_back(base::Value::CreateStringValue("")); | 
|  | 711   non_default_values_.push_back(base::Value::CreateStringValue("127.0.0.1")); | 
|  | 712 | 
| 371   // List pref. | 713   // List pref. | 
| 372   names.push_back(chromeos::kProxyIgnoreList); | 714   pref_names_.push_back(chromeos::kProxyIgnoreList); | 
| 373 | 715   default_values_.push_back(new base::ListValue()); | 
| 374   // Default values. |  | 
| 375   std::vector<base::Value*> values; |  | 
| 376   values.push_back(base::Value::CreateBooleanValue(false)); |  | 
| 377   values.push_back(base::Value::CreateStringValue("")); |  | 
| 378   values.push_back(base::Value::CreateStringValue("")); |  | 
| 379   values.push_back(new base::ListValue()); |  | 
| 380 |  | 
| 381   // Verify default values are fetched and decorated correctly. |  | 
| 382   base::DictionaryValue* fetched; |  | 
| 383   FetchPrefs(names, &fetched); |  | 
| 384   if (fetched) |  | 
| 385     VerifyPrefs(fetched, names, values, "", false); |  | 
| 386   delete fetched; |  | 
| 387 |  | 
| 388   // Set user-modified values. |  | 
| 389   DeleteValues(values); |  | 
| 390   values.push_back(base::Value::CreateBooleanValue(true)); |  | 
| 391   values.push_back(base::Value::CreateIntegerValue(8080)); |  | 
| 392   values.push_back(base::Value::CreateStringValue("127.0.0.1")); |  | 
| 393   base::ListValue* list = new base::ListValue(); | 716   base::ListValue* list = new base::ListValue(); | 
| 394   list->Append(base::Value::CreateStringValue("www.google.com")); | 717   list->Append(base::Value::CreateStringValue("www.google.com")); | 
| 395   list->Append(base::Value::CreateStringValue("example.com")); | 718   list->Append(base::Value::CreateStringValue("example.com")); | 
| 396   values.push_back(list); | 719   non_default_values_.push_back(list); | 
| 397   ASSERT_EQ(names.size(), values.size()); | 720 | 
|  | 721   // Verify notifications when default values are in effect. | 
|  | 722   SetupJavaScriptTestEnvironment(pref_names_, &observed_json); | 
|  | 723   VerifyObservedPrefs(observed_json, pref_names_, default_values_, "", false); | 
|  | 724 | 
|  | 725   // Verify notifications when user-modified values are in effect. | 
| 398   Profile* profile = browser()->profile(); | 726   Profile* profile = browser()->profile(); | 
| 399   // Do not set the Boolean pref. It will toogle automatically. | 727   // Do not set the Boolean pref. It will toogle automatically. | 
| 400   for (size_t i = 1; i < names.size(); ++i) | 728   for (size_t i = 1; i < pref_names_.size(); ++i) | 
| 401     chromeos::proxy_cros_settings_parser::SetProxyPrefValue( | 729     chromeos::proxy_cros_settings_parser::SetProxyPrefValue( | 
| 402         profile, names[i], values[i]->DeepCopy()); | 730         profile, pref_names_[i], non_default_values_[i]->DeepCopy()); | 
|  | 731   SetupJavaScriptTestEnvironment(pref_names_, &observed_json); | 
|  | 732   VerifyObservedPrefs(observed_json, pref_names_, non_default_values_, | 
|  | 733                       "", false); | 
|  | 734 } | 
| 403 | 735 | 
| 404   // Verify user-modified values are fetched and decorated correctly. |  | 
| 405   FetchPrefs(names, &fetched); |  | 
| 406   if (fetched) |  | 
| 407     VerifyPrefs(fetched, names, values, "", false); |  | 
| 408   delete fetched; |  | 
| 409 |  | 
| 410   DeleteValues(values); |  | 
| 411 } |  | 
| 412 #endif | 736 #endif | 
| OLD | NEW | 
|---|