| 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <map> | 6 #include <map> |
| 7 #include <sstream> | 7 #include <sstream> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
| 13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 #include "base/json/json_reader.h" | 14 #include "base/json/json_reader.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/scoped_vector.h" | 16 #include "base/memory/scoped_vector.h" |
| 17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 18 #include "base/utf_string_conversions.h" |
| 18 #include "base/values.h" | 19 #include "base/values.h" |
| 19 #include "chrome/browser/browser_process.h" | 20 #include "chrome/browser/browser_process.h" |
| 20 #include "chrome/browser/policy/browser_policy_connector.h" | 21 #include "chrome/browser/policy/browser_policy_connector.h" |
| 21 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 22 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 22 #include "chrome/browser/policy/policy_map.h" | 23 #include "chrome/browser/policy/policy_map.h" |
| 23 #include "chrome/browser/prefs/pref_service.h" | 24 #include "chrome/browser/prefs/pref_service.h" |
| 24 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
| 25 #include "chrome/browser/ui/browser.h" | 26 #include "chrome/browser/ui/browser.h" |
| 26 #include "chrome/browser/ui/browser_tabstrip.h" | 27 #include "chrome/browser/ui/browser_tabstrip.h" |
| 27 #include "chrome/test/base/in_process_browser_test.h" | 28 #include "chrome/test/base/in_process_browser_test.h" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 bool can_be_recommended() const { return can_be_recommended_; } | 96 bool can_be_recommended() const { return can_be_recommended_; } |
| 96 void set_can_be_recommended(bool can_be_recommended) { | 97 void set_can_be_recommended(bool can_be_recommended) { |
| 97 can_be_recommended_ = can_be_recommended; | 98 can_be_recommended_ = can_be_recommended; |
| 98 } | 99 } |
| 99 | 100 |
| 100 const PolicyMap& test_policy() const { return test_policy_; } | 101 const PolicyMap& test_policy() const { return test_policy_; } |
| 101 void set_test_policy(const PolicyMap& policy) { | 102 void set_test_policy(const PolicyMap& policy) { |
| 102 test_policy_.CopyFrom(policy); | 103 test_policy_.CopyFrom(policy); |
| 103 } | 104 } |
| 104 | 105 |
| 106 const std::wstring& indicator_test_setup_js() const { |
| 107 return indicator_test_setup_js_; |
| 108 } |
| 109 void set_indicator_test_setup_js(const std::string& indicator_test_setup_js) { |
| 110 indicator_test_setup_js_ = ASCIIToWide(indicator_test_setup_js); |
| 111 } |
| 112 |
| 113 const std::string& indicator_selector() const { return indicator_selector_; } |
| 114 void set_indicator_selector(const std::string& indicator_selector) { |
| 115 indicator_selector_ = indicator_selector; |
| 116 } |
| 117 |
| 105 const ScopedVector<IndicatorTestCase>& indicator_test_cases() const { | 118 const ScopedVector<IndicatorTestCase>& indicator_test_cases() const { |
| 106 return indicator_test_cases_; | 119 return indicator_test_cases_; |
| 107 } | 120 } |
| 108 void AddIndicatorTestCase(IndicatorTestCase* test_case) { | 121 void AddIndicatorTestCase(IndicatorTestCase* test_case) { |
| 109 indicator_test_cases_.push_back(test_case); | 122 indicator_test_cases_.push_back(test_case); |
| 110 } | 123 } |
| 111 | 124 |
| 112 const std::vector<GURL>& settings_pages() const { return settings_pages_; } | 125 const std::vector<GURL>& settings_pages() const { return settings_pages_; } |
| 113 void AddSettingsPage(const GURL& url) { settings_pages_.push_back(url); } | 126 void AddSettingsPage(const GURL& url) { settings_pages_.push_back(url); } |
| 114 | 127 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 142 return false; | 155 return false; |
| 143 #endif | 156 #endif |
| 144 return IsOsSupported(); | 157 return IsOsSupported(); |
| 145 } | 158 } |
| 146 | 159 |
| 147 private: | 160 private: |
| 148 std::string name_; | 161 std::string name_; |
| 149 std::string pref_; | 162 std::string pref_; |
| 150 bool can_be_recommended_; | 163 bool can_be_recommended_; |
| 151 PolicyMap test_policy_; | 164 PolicyMap test_policy_; |
| 165 std::wstring indicator_test_setup_js_; |
| 166 std::string indicator_selector_; |
| 152 ScopedVector<IndicatorTestCase> indicator_test_cases_; | 167 ScopedVector<IndicatorTestCase> indicator_test_cases_; |
| 153 std::vector<GURL> settings_pages_; | 168 std::vector<GURL> settings_pages_; |
| 154 std::vector<std::string> supported_os_; | 169 std::vector<std::string> supported_os_; |
| 155 bool is_local_state_; | 170 bool is_local_state_; |
| 156 bool official_only_; | 171 bool official_only_; |
| 157 | 172 |
| 158 DISALLOW_COPY_AND_ASSIGN(PolicyTestCase); | 173 DISALLOW_COPY_AND_ASSIGN(PolicyTestCase); |
| 159 }; | 174 }; |
| 160 | 175 |
| 161 // Parses all the test cases and makes then available in a map. | 176 // Parses all the test cases and makes then available in a map. |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 test_case->set_pref(pref); | 234 test_case->set_pref(pref); |
| 220 bool flag = false; | 235 bool flag = false; |
| 221 if (dict->GetBoolean("can_be_recommended", &flag)) | 236 if (dict->GetBoolean("can_be_recommended", &flag)) |
| 222 test_case->set_can_be_recommended(flag); | 237 test_case->set_can_be_recommended(flag); |
| 223 const base::DictionaryValue* policy_dict = NULL; | 238 const base::DictionaryValue* policy_dict = NULL; |
| 224 if (dict->GetDictionary("test_policy", &policy_dict)) { | 239 if (dict->GetDictionary("test_policy", &policy_dict)) { |
| 225 PolicyMap policies; | 240 PolicyMap policies; |
| 226 policies.LoadFrom(policy_dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); | 241 policies.LoadFrom(policy_dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); |
| 227 test_case->set_test_policy(policies); | 242 test_case->set_test_policy(policies); |
| 228 } | 243 } |
| 244 std::string indicator_test_setup_js; |
| 245 if (dict->GetString("indicator_test_setup_js", &indicator_test_setup_js)) |
| 246 test_case->set_indicator_test_setup_js(indicator_test_setup_js); |
| 247 std::string indicator_selector; |
| 248 if (dict->GetString("indicator_selector", &indicator_selector)) |
| 249 test_case->set_indicator_selector(indicator_selector); |
| 229 const base::ListValue* indicator_tests = NULL; | 250 const base::ListValue* indicator_tests = NULL; |
| 230 if (dict->GetList("indicator_tests", &indicator_tests)) { | 251 if (dict->GetList("indicator_tests", &indicator_tests)) { |
| 231 for (size_t i = 0; i < indicator_tests->GetSize(); ++i) { | 252 for (size_t i = 0; i < indicator_tests->GetSize(); ++i) { |
| 232 const base::DictionaryValue* indicator_test_dict = NULL; | 253 const base::DictionaryValue* indicator_test_dict = NULL; |
| 233 const base::DictionaryValue* policy = NULL; | 254 const base::DictionaryValue* policy = NULL; |
| 234 if (!indicator_tests->GetDictionary(i, &indicator_test_dict) || | 255 if (!indicator_tests->GetDictionary(i, &indicator_test_dict) || |
| 235 !indicator_test_dict->GetDictionary("policy", &policy)) { | 256 !indicator_test_dict->GetDictionary("policy", &policy)) { |
| 236 continue; | 257 continue; |
| 237 } | 258 } |
| 238 std::string value; | 259 std::string value; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 L" continue;" | 305 L" continue;" |
| 285 L" if (window.getComputedStyle(banners[i]).display != 'none')" | 306 L" if (window.getComputedStyle(banners[i]).display != 'none')" |
| 286 L" visible = true;" | 307 L" visible = true;" |
| 287 L"}" | 308 L"}" |
| 288 L"domAutomationController.send(visible);", | 309 L"domAutomationController.send(visible);", |
| 289 &result)); | 310 &result)); |
| 290 return result; | 311 return result; |
| 291 } | 312 } |
| 292 | 313 |
| 293 void VerifyControlledSettingIndicators(Browser* browser, | 314 void VerifyControlledSettingIndicators(Browser* browser, |
| 294 const std::string& pref, | 315 const std::string& selector, |
| 295 const std::string& value, | 316 const std::string& value, |
| 296 const std::string& controlled_by, | 317 const std::string& controlled_by, |
| 297 bool readonly) { | 318 bool readonly) { |
| 298 std::wstringstream javascript; | 319 std::wstringstream javascript; |
| 299 javascript << "var nodes = document.querySelectorAll(" | 320 javascript << "var nodes = document.querySelectorAll(" |
| 300 << " 'span.controlled-setting-indicator[" | 321 << " 'span.controlled-setting-indicator" |
| 301 << " pref=\"" << pref.c_str() << "\"]');" | 322 << selector.c_str() << "');" |
| 302 << "var indicators = [];" | 323 << "var indicators = [];" |
| 303 << "for (var i = 0; i < nodes.length; i++) {" | 324 << "for (var i = 0; i < nodes.length; i++) {" |
| 304 << " var node = nodes[i];" | 325 << " var node = nodes[i];" |
| 305 << " var indicator = {};" | 326 << " var indicator = {};" |
| 306 << " indicator.value = node.value || '';" | 327 << " indicator.value = node.value || '';" |
| 307 << " indicator.controlledBy = node.controlledBy || '';" | 328 << " indicator.controlledBy = node.controlledBy || '';" |
| 308 << " indicator.readOnly = node.readOnly || false;" | 329 << " indicator.readOnly = node.readOnly || false;" |
| 309 << " indicator.visible =" | 330 << " indicator.visible =" |
| 310 << " window.getComputedStyle(node).display != 'none';" | 331 << " window.getComputedStyle(node).display != 'none';" |
| 311 << " indicators.push(indicator)" | 332 << " indicators.push(indicator)" |
| 312 << "}" | 333 << "}" |
| 313 << "domAutomationController.send(JSON.stringify(indicators));"; | 334 << "domAutomationController.send(JSON.stringify(indicators));"; |
| 314 content::WebContents* contents = chrome::GetActiveWebContents(browser); | 335 content::WebContents* contents = chrome::GetActiveWebContents(browser); |
| 315 std::string json; | 336 std::string json; |
| 316 // Retrieve the state of all controlled setting indicators for |pref| as JSON. | 337 // Retrieve the state of all controlled setting indicators matching the |
| 338 // |selector| as JSON. |
| 317 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 339 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
| 318 contents->GetRenderViewHost(), L"", javascript.str(), &json)); | 340 contents->GetRenderViewHost(), L"", javascript.str(), &json)); |
| 319 scoped_ptr<base::Value> value_ptr(base::JSONReader::Read(json)); | 341 scoped_ptr<base::Value> value_ptr(base::JSONReader::Read(json)); |
| 320 const base::ListValue* indicators = NULL; | 342 const base::ListValue* indicators = NULL; |
| 321 ASSERT_TRUE(value_ptr.get()); | 343 ASSERT_TRUE(value_ptr.get()); |
| 322 ASSERT_TRUE(value_ptr->GetAsList(&indicators)); | 344 ASSERT_TRUE(value_ptr->GetAsList(&indicators)); |
| 323 // Verify that controlled setting indicators representing |value| are visible | 345 // Verify that controlled setting indicators representing |value| are visible |
| 324 // and have the correct state while those not representing |value| are | 346 // and have the correct state while those not representing |value| are |
| 325 // invisible. | 347 // invisible. |
| 348 if (!controlled_by.empty()) { |
| 349 EXPECT_GT(indicators->GetSize(), 0u) |
| 350 << "Expected to find at least one controlled setting indicator."; |
| 351 } |
| 326 for (base::ListValue::const_iterator indicator = indicators->begin(); | 352 for (base::ListValue::const_iterator indicator = indicators->begin(); |
| 327 indicator != indicators->end(); ++indicator) { | 353 indicator != indicators->end(); ++indicator) { |
| 328 const base::DictionaryValue* properties = NULL; | 354 const base::DictionaryValue* properties = NULL; |
| 329 ASSERT_TRUE((*indicator)->GetAsDictionary(&properties)); | 355 ASSERT_TRUE((*indicator)->GetAsDictionary(&properties)); |
| 330 std::string indicator_value; | 356 std::string indicator_value; |
| 331 std::string indicator_controlled_by; | 357 std::string indicator_controlled_by; |
| 332 bool indicator_readonly; | 358 bool indicator_readonly; |
| 333 bool indicator_visible; | 359 bool indicator_visible; |
| 334 EXPECT_TRUE(properties->GetString("value", &indicator_value)); | 360 EXPECT_TRUE(properties->GetString("value", &indicator_value)); |
| 335 EXPECT_TRUE(properties->GetString("controlledBy", | 361 EXPECT_TRUE(properties->GetString("controlledBy", |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 485 if (!policy_test_case->IsSupported() || indicator_test_cases.empty()) | 511 if (!policy_test_case->IsSupported() || indicator_test_cases.empty()) |
| 486 return; | 512 return; |
| 487 LOG(INFO) << "Testing policy: " << policy_test_case->name(); | 513 LOG(INFO) << "Testing policy: " << policy_test_case->name(); |
| 488 | 514 |
| 489 PrefService* prefs = policy_test_case->is_local_state() ? | 515 PrefService* prefs = policy_test_case->is_local_state() ? |
| 490 g_browser_process->local_state() : browser()->profile()->GetPrefs(); | 516 g_browser_process->local_state() : browser()->profile()->GetPrefs(); |
| 491 // The preference must have been registered. | 517 // The preference must have been registered. |
| 492 const PrefService::Preference* pref = | 518 const PrefService::Preference* pref = |
| 493 prefs->FindPreference(policy_test_case->pref_name()); | 519 prefs->FindPreference(policy_test_case->pref_name()); |
| 494 ASSERT_TRUE(pref); | 520 ASSERT_TRUE(pref); |
| 521 |
| 495 ui_test_utils::NavigateToURL(browser(), GURL(kSettingsPages[0])); | 522 ui_test_utils::NavigateToURL(browser(), GURL(kSettingsPages[0])); |
| 523 if (!policy_test_case->indicator_test_setup_js().empty()) { |
| 524 ASSERT_TRUE(content::ExecuteJavaScript( |
| 525 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), L"", |
| 526 policy_test_case->indicator_test_setup_js())); |
| 527 } |
| 496 | 528 |
| 529 std::string indicator_selector = policy_test_case->indicator_selector(); |
| 530 if (indicator_selector.empty()) |
| 531 indicator_selector = "[pref=\"" + policy_test_case->pref() + "\"]"; |
| 497 for (ScopedVector<IndicatorTestCase>::const_iterator | 532 for (ScopedVector<IndicatorTestCase>::const_iterator |
| 498 indicator_test_case = indicator_test_cases.begin(); | 533 indicator_test_case = indicator_test_cases.begin(); |
| 499 indicator_test_case != indicator_test_cases.end(); | 534 indicator_test_case != indicator_test_cases.end(); |
| 500 ++indicator_test_case) { | 535 ++indicator_test_case) { |
| 501 // Check that no controlled setting indicator is visible when no value is | 536 // Check that no controlled setting indicator is visible when no value is |
| 502 // set by policy. | 537 // set by policy. |
| 503 PolicyMap policies; | 538 PolicyMap policies; |
| 504 provider_.UpdateChromePolicy(policies); | 539 provider_.UpdateChromePolicy(policies); |
| 505 VerifyControlledSettingIndicators(browser(), policy_test_case->pref(), | 540 VerifyControlledSettingIndicators(browser(), indicator_selector, |
| 506 "", "", false); | 541 "", "", false); |
| 507 // Check that the appropriate controlled setting indicator is shown when a | 542 // Check that the appropriate controlled setting indicator is shown when a |
| 508 // value is enforced by policy. | 543 // value is enforced by policy. |
| 509 policies.LoadFrom(&(*indicator_test_case)->policy(), | 544 policies.LoadFrom(&(*indicator_test_case)->policy(), |
| 510 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); | 545 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); |
| 511 provider_.UpdateChromePolicy(policies); | 546 provider_.UpdateChromePolicy(policies); |
| 512 VerifyControlledSettingIndicators(browser(), policy_test_case->pref(), | 547 VerifyControlledSettingIndicators(browser(), indicator_selector, |
| 513 (*indicator_test_case)->value(), | 548 (*indicator_test_case)->value(), |
| 514 "policy", | 549 "policy", |
| 515 (*indicator_test_case)->readonly()); | 550 (*indicator_test_case)->readonly()); |
| 516 if (!policy_test_case->can_be_recommended()) | 551 if (!policy_test_case->can_be_recommended()) |
| 517 return; | 552 continue; |
| 518 // Check that the appropriate controlled setting indicator is shown when a | 553 // Check that the appropriate controlled setting indicator is shown when a |
| 519 // value is recommended by policy and the user has not overridden the | 554 // value is recommended by policy and the user has not overridden the |
| 520 // recommendation. | 555 // recommendation. |
| 521 policies.LoadFrom(&(*indicator_test_case)->policy(), | 556 policies.LoadFrom(&(*indicator_test_case)->policy(), |
| 522 POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER); | 557 POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER); |
| 523 provider_.UpdateChromePolicy(policies); | 558 provider_.UpdateChromePolicy(policies); |
| 524 VerifyControlledSettingIndicators(browser(), policy_test_case->pref(), | 559 VerifyControlledSettingIndicators(browser(), indicator_selector, |
| 525 (*indicator_test_case)->value(), | 560 (*indicator_test_case)->value(), |
| 526 "recommended", | 561 "recommended", |
| 527 (*indicator_test_case)->readonly()); | 562 (*indicator_test_case)->readonly()); |
| 528 // Check that the appropriate controlled setting indicator is shown when a | 563 // Check that the appropriate controlled setting indicator is shown when a |
| 529 // value is recommended by policy and the user has overriddent the | 564 // value is recommended by policy and the user has overriddent the |
| 530 // recommendation. | 565 // recommendation. |
| 531 prefs->Set(policy_test_case->pref_name(), *pref->GetValue()); | 566 prefs->Set(policy_test_case->pref_name(), *pref->GetValue()); |
| 532 VerifyControlledSettingIndicators(browser(), policy_test_case->pref(), | 567 VerifyControlledSettingIndicators(browser(), indicator_selector, |
| 533 (*indicator_test_case)->value(), | 568 (*indicator_test_case)->value(), |
| 534 "hasRecommendation", | 569 "hasRecommendation", |
| 535 (*indicator_test_case)->readonly()); | 570 (*indicator_test_case)->readonly()); |
| 536 prefs->ClearPref(policy_test_case->pref_name()); | 571 prefs->ClearPref(policy_test_case->pref_name()); |
| 537 } | 572 } |
| 538 } | 573 } |
| 539 | 574 |
| 540 INSTANTIATE_TEST_CASE_P( | 575 INSTANTIATE_TEST_CASE_P( |
| 541 PolicyPrefsTestInstance, | 576 PolicyPrefsTestInstance, |
| 542 PolicyPrefsTest, | 577 PolicyPrefsTest, |
| 543 testing::ValuesIn(GetChromePolicyDefinitionList()->begin, | 578 testing::ValuesIn(GetChromePolicyDefinitionList()->begin, |
| 544 GetChromePolicyDefinitionList()->end)); | 579 GetChromePolicyDefinitionList()->end)); |
| 545 | 580 |
| 546 } // namespace policy | 581 } // namespace policy |
| OLD | NEW |