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 <string> | 8 #include <string> |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
10 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
11 #include "base/file_path.h" | 12 #include "base/file_path.h" |
12 #include "base/file_util.h" | 13 #include "base/file_util.h" |
13 #include "base/json/json_reader.h" | 14 #include "base/json/json_reader.h" |
14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/scoped_vector.h" |
15 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
16 #include "base/values.h" | 18 #include "base/values.h" |
17 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
18 #include "chrome/browser/policy/browser_policy_connector.h" | 20 #include "chrome/browser/policy/browser_policy_connector.h" |
19 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 21 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
20 #include "chrome/browser/policy/policy_map.h" | 22 #include "chrome/browser/policy/policy_map.h" |
21 #include "chrome/browser/prefs/pref_service.h" | 23 #include "chrome/browser/prefs/pref_service.h" |
22 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
23 #include "chrome/browser/ui/browser.h" | 25 #include "chrome/browser/ui/browser.h" |
24 #include "chrome/browser/ui/browser_tabstrip.h" | 26 #include "chrome/browser/ui/browser_tabstrip.h" |
(...skipping 18 matching lines...) Expand all Loading... |
43 "chrome://settings-frame/passwords", | 45 "chrome://settings-frame/passwords", |
44 "chrome://settings-frame/autofill", | 46 "chrome://settings-frame/autofill", |
45 "chrome://settings-frame/content", | 47 "chrome://settings-frame/content", |
46 "chrome://settings-frame/homePageOverlay", | 48 "chrome://settings-frame/homePageOverlay", |
47 "chrome://settings-frame/languages", | 49 "chrome://settings-frame/languages", |
48 #if defined(OS_CHROMEOS) | 50 #if defined(OS_CHROMEOS) |
49 "chrome://settings-frame/accounts", | 51 "chrome://settings-frame/accounts", |
50 #endif | 52 #endif |
51 }; | 53 }; |
52 | 54 |
| 55 // Contains the details of one test case verifying the behavior of controlled |
| 56 // setting indicators in the settings UI for a policy, part of the data loaded |
| 57 // from chrome/test/data/policy/policy_test_cases.json. |
| 58 class IndicatorTestCase { |
| 59 public: |
| 60 IndicatorTestCase(const base::DictionaryValue& policy, |
| 61 const std::string& value, |
| 62 bool readonly) |
| 63 : policy_(policy.DeepCopy()), value_(value), readonly_(readonly) {} |
| 64 ~IndicatorTestCase() {} |
| 65 |
| 66 const base::DictionaryValue& policy() const { return *policy_; } |
| 67 const std::string& value() const { return value_; } |
| 68 bool readonly() const { return readonly_; } |
| 69 |
| 70 private: |
| 71 scoped_ptr<base::DictionaryValue> policy_; |
| 72 std::string value_; |
| 73 bool readonly_; |
| 74 |
| 75 DISALLOW_COPY_AND_ASSIGN(IndicatorTestCase); |
| 76 }; |
| 77 |
53 // Contains the testing details for a single policy, loaded from | 78 // Contains the testing details for a single policy, loaded from |
54 // chrome/test/data/policy/policy_test_cases.json. | 79 // chrome/test/data/policy/policy_test_cases.json. |
55 class PolicyTestCase { | 80 class PolicyTestCase { |
56 public: | 81 public: |
57 explicit PolicyTestCase(const std::string& name) | 82 explicit PolicyTestCase(const std::string& name) |
58 : name_(name), | 83 : name_(name), |
| 84 can_be_recommended_(false), |
59 is_local_state_(false), | 85 is_local_state_(false), |
60 official_only_(false) {} | 86 official_only_(false) {} |
61 ~PolicyTestCase() {} | 87 ~PolicyTestCase() {} |
62 | 88 |
63 const std::string& name() const { return name_; } | 89 const std::string& name() const { return name_; } |
64 | 90 |
65 void set_pref(const std::string& pref) { pref_ = pref; } | 91 void set_pref(const std::string& pref) { pref_ = pref; } |
66 const std::string& pref() const { return pref_; } | 92 const std::string& pref() const { return pref_; } |
67 const char* pref_name() const { return pref_.c_str(); } | 93 const char* pref_name() const { return pref_.c_str(); } |
68 | 94 |
| 95 bool can_be_recommended() const { return can_be_recommended_; } |
| 96 void set_can_be_recommended(bool can_be_recommended) { |
| 97 can_be_recommended_ = can_be_recommended; |
| 98 } |
| 99 |
69 const PolicyMap& test_policy() const { return test_policy_; } | 100 const PolicyMap& test_policy() const { return test_policy_; } |
70 void set_test_policy(const PolicyMap& policy) { | 101 void set_test_policy(const PolicyMap& policy) { |
71 test_policy_.CopyFrom(policy); | 102 test_policy_.CopyFrom(policy); |
72 } | 103 } |
73 | 104 |
| 105 const ScopedVector<IndicatorTestCase>& indicator_test_cases() const { |
| 106 return indicator_test_cases_; |
| 107 } |
| 108 void AddIndicatorTestCase(IndicatorTestCase* test_case) { |
| 109 indicator_test_cases_.push_back(test_case); |
| 110 } |
| 111 |
74 const std::vector<GURL>& settings_pages() const { return settings_pages_; } | 112 const std::vector<GURL>& settings_pages() const { return settings_pages_; } |
75 void AddSettingsPage(const GURL& url) { settings_pages_.push_back(url); } | 113 void AddSettingsPage(const GURL& url) { settings_pages_.push_back(url); } |
76 | 114 |
77 bool IsOsSupported() const { | 115 bool IsOsSupported() const { |
78 #if defined(OS_WIN) | 116 #if defined(OS_WIN) |
79 const std::string os("win"); | 117 const std::string os("win"); |
80 #elif defined(OS_MACOSX) | 118 #elif defined(OS_MACOSX) |
81 const std::string os("mac"); | 119 const std::string os("mac"); |
82 #elif defined(OS_CHROMEOS) | 120 #elif defined(OS_CHROMEOS) |
83 const std::string os("chromeos"); | 121 const std::string os("chromeos"); |
(...skipping 18 matching lines...) Expand all Loading... |
102 #if !defined(OFFICIAL_BUILD) | 140 #if !defined(OFFICIAL_BUILD) |
103 if (is_official_only()) | 141 if (is_official_only()) |
104 return false; | 142 return false; |
105 #endif | 143 #endif |
106 return IsOsSupported(); | 144 return IsOsSupported(); |
107 } | 145 } |
108 | 146 |
109 private: | 147 private: |
110 std::string name_; | 148 std::string name_; |
111 std::string pref_; | 149 std::string pref_; |
| 150 bool can_be_recommended_; |
112 PolicyMap test_policy_; | 151 PolicyMap test_policy_; |
| 152 ScopedVector<IndicatorTestCase> indicator_test_cases_; |
113 std::vector<GURL> settings_pages_; | 153 std::vector<GURL> settings_pages_; |
114 std::vector<std::string> supported_os_; | 154 std::vector<std::string> supported_os_; |
115 bool is_local_state_; | 155 bool is_local_state_; |
116 bool official_only_; | 156 bool official_only_; |
117 | 157 |
118 DISALLOW_COPY_AND_ASSIGN(PolicyTestCase); | 158 DISALLOW_COPY_AND_ASSIGN(PolicyTestCase); |
119 }; | 159 }; |
120 | 160 |
121 // Parses all the test cases and makes then available in a map. | 161 // Parses all the test cases and makes then available in a map. |
122 class TestCases { | 162 class TestCases { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 private: | 210 private: |
171 PolicyTestCase* GetTestCase(const base::DictionaryValue* tests, | 211 PolicyTestCase* GetTestCase(const base::DictionaryValue* tests, |
172 const std::string& name) { | 212 const std::string& name) { |
173 const base::DictionaryValue* dict = NULL; | 213 const base::DictionaryValue* dict = NULL; |
174 if (!tests->GetDictionary(name, &dict)) | 214 if (!tests->GetDictionary(name, &dict)) |
175 return NULL; | 215 return NULL; |
176 PolicyTestCase* test_case = new PolicyTestCase(name); | 216 PolicyTestCase* test_case = new PolicyTestCase(name); |
177 std::string pref; | 217 std::string pref; |
178 if (dict->GetString("pref", &pref)) | 218 if (dict->GetString("pref", &pref)) |
179 test_case->set_pref(pref); | 219 test_case->set_pref(pref); |
| 220 bool flag = false; |
| 221 if (dict->GetBoolean("can_be_recommended", &flag)) |
| 222 test_case->set_can_be_recommended(flag); |
180 const base::DictionaryValue* policy_dict = NULL; | 223 const base::DictionaryValue* policy_dict = NULL; |
181 if (dict->GetDictionary("test_policy", &policy_dict)) { | 224 if (dict->GetDictionary("test_policy", &policy_dict)) { |
182 PolicyMap policies; | 225 PolicyMap policies; |
183 policies.LoadFrom(policy_dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); | 226 policies.LoadFrom(policy_dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); |
184 test_case->set_test_policy(policies); | 227 test_case->set_test_policy(policies); |
185 } | 228 } |
| 229 const base::ListValue* indicator_tests = NULL; |
| 230 if (dict->GetList("indicator_tests", &indicator_tests)) { |
| 231 for (size_t i = 0; i < indicator_tests->GetSize(); ++i) { |
| 232 const base::DictionaryValue* indicator_test_dict = NULL; |
| 233 const base::DictionaryValue* policy = NULL; |
| 234 if (!indicator_tests->GetDictionary(i, &indicator_test_dict) || |
| 235 !indicator_test_dict->GetDictionary("policy", &policy)) { |
| 236 continue; |
| 237 } |
| 238 std::string value; |
| 239 indicator_test_dict->GetString("value", &value); |
| 240 bool readonly = false; |
| 241 indicator_test_dict->GetBoolean("readonly", &readonly); |
| 242 test_case->AddIndicatorTestCase( |
| 243 new IndicatorTestCase(*policy, value, readonly)); |
| 244 } |
| 245 } |
186 const base::ListValue* settings_pages = NULL; | 246 const base::ListValue* settings_pages = NULL; |
187 if (dict->GetList("settings_pages", &settings_pages)) { | 247 if (dict->GetList("settings_pages", &settings_pages)) { |
188 for (size_t i = 0; i < settings_pages->GetSize(); ++i) { | 248 for (size_t i = 0; i < settings_pages->GetSize(); ++i) { |
189 std::string page; | 249 std::string page; |
190 if (settings_pages->GetString(i, &page)) | 250 if (settings_pages->GetString(i, &page)) |
191 test_case->AddSettingsPage(GURL(page)); | 251 test_case->AddSettingsPage(GURL(page)); |
192 } | 252 } |
193 } | 253 } |
194 const base::ListValue* supported_os = NULL; | 254 const base::ListValue* supported_os = NULL; |
195 if (dict->GetList("os", &supported_os)) { | 255 if (dict->GetList("os", &supported_os)) { |
196 for (size_t i = 0; i < supported_os->GetSize(); ++i) { | 256 for (size_t i = 0; i < supported_os->GetSize(); ++i) { |
197 std::string os; | 257 std::string os; |
198 if (supported_os->GetString(i, &os)) | 258 if (supported_os->GetString(i, &os)) |
199 test_case->AddSupportedOs(os); | 259 test_case->AddSupportedOs(os); |
200 } | 260 } |
201 } | 261 } |
202 bool flag = false; | |
203 if (dict->GetBoolean("local_state", &flag)) | 262 if (dict->GetBoolean("local_state", &flag)) |
204 test_case->set_local_state(flag); | 263 test_case->set_local_state(flag); |
205 if (dict->GetBoolean("official_only", &flag)) | 264 if (dict->GetBoolean("official_only", &flag)) |
206 test_case->set_official_only(flag); | 265 test_case->set_official_only(flag); |
207 return test_case; | 266 return test_case; |
208 } | 267 } |
209 | 268 |
210 TestCaseMap* test_cases_; | 269 TestCaseMap* test_cases_; |
211 | 270 |
212 DISALLOW_COPY_AND_ASSIGN(TestCases); | 271 DISALLOW_COPY_AND_ASSIGN(TestCases); |
(...skipping 11 matching lines...) Expand all Loading... |
224 L" if (banners[i].parentElement.id == 'templates')" | 283 L" if (banners[i].parentElement.id == 'templates')" |
225 L" continue;" | 284 L" continue;" |
226 L" if (window.getComputedStyle(banners[i]).display != 'none')" | 285 L" if (window.getComputedStyle(banners[i]).display != 'none')" |
227 L" visible = true;" | 286 L" visible = true;" |
228 L"}" | 287 L"}" |
229 L"domAutomationController.send(visible);", | 288 L"domAutomationController.send(visible);", |
230 &result)); | 289 &result)); |
231 return result; | 290 return result; |
232 } | 291 } |
233 | 292 |
| 293 void VerifyControlledSettingIndicators(Browser* browser, |
| 294 const std::string& pref, |
| 295 const std::string& value, |
| 296 const std::string& controlled_by, |
| 297 bool readonly) { |
| 298 std::wstringstream javascript; |
| 299 javascript << "var nodes = document.querySelectorAll(" |
| 300 << " 'span.controlled-setting-indicator[" |
| 301 << " pref=" << pref.c_str() << "]');" |
| 302 << "var indicators = [];" |
| 303 << "for (var i = 0; i < nodes.length; i++) {" |
| 304 << " var node = nodes[i];" |
| 305 << " var indicator = {};" |
| 306 << " indicator.value = node.value || '';" |
| 307 << " indicator.controlledBy = node.controlledBy || '';" |
| 308 << " indicator.readOnly = node.readOnly || false;" |
| 309 << " indicator.visible =" |
| 310 << " window.getComputedStyle(node).display != 'none';" |
| 311 << " indicators.push(indicator)" |
| 312 << "}" |
| 313 << "domAutomationController.send(JSON.stringify(indicators));"; |
| 314 content::WebContents* contents = chrome::GetActiveWebContents(browser); |
| 315 std::string json; |
| 316 // Retrieve the state of all controlled setting indicators for |pref| as JSON. |
| 317 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
| 318 contents->GetRenderViewHost(), L"", javascript.str(), &json)); |
| 319 scoped_ptr<base::Value> value_ptr(base::JSONReader::Read(json)); |
| 320 const base::ListValue* indicators = NULL; |
| 321 ASSERT_TRUE(value_ptr.get()); |
| 322 ASSERT_TRUE(value_ptr->GetAsList(&indicators)); |
| 323 // Verify that controlled setting indicators representing |value| are visible |
| 324 // and have the correct state while those not representing |value| are |
| 325 // invisible. |
| 326 for (base::ListValue::const_iterator indicator = indicators->begin(); |
| 327 indicator != indicators->end(); ++indicator) { |
| 328 const base::DictionaryValue* properties = NULL; |
| 329 ASSERT_TRUE((*indicator)->GetAsDictionary(&properties)); |
| 330 std::string indicator_value; |
| 331 std::string indicator_controlled_by; |
| 332 bool indicator_readonly; |
| 333 bool indicator_visible; |
| 334 EXPECT_TRUE(properties->GetString("value", &indicator_value)); |
| 335 EXPECT_TRUE(properties->GetString("controlledBy", |
| 336 &indicator_controlled_by)); |
| 337 EXPECT_TRUE(properties->GetBoolean("readOnly", &indicator_readonly)); |
| 338 EXPECT_TRUE(properties->GetBoolean("visible", &indicator_visible)); |
| 339 if (!controlled_by.empty() && (indicator_value == value)) { |
| 340 EXPECT_EQ(controlled_by, indicator_controlled_by); |
| 341 EXPECT_EQ(readonly, indicator_readonly); |
| 342 EXPECT_TRUE(indicator_visible); |
| 343 } else { |
| 344 EXPECT_FALSE(indicator_visible); |
| 345 } |
| 346 } |
| 347 } |
| 348 |
234 } // namespace | 349 } // namespace |
235 | 350 |
236 // A class of tests parameterized by a settings page URL. | 351 // A class of tests parameterized by a settings page URL. |
237 class PolicyPrefsSettingsBannerTest | 352 class PolicyPrefsSettingsBannerTest |
238 : public InProcessBrowserTest, | 353 : public InProcessBrowserTest, |
239 public testing::WithParamInterface<const char*> {}; | 354 public testing::WithParamInterface<const char*> {}; |
240 | 355 |
241 // Base class for tests that change policies. | 356 // Base class for tests that change policies. |
242 class PolicyBaseTest : public InProcessBrowserTest { | 357 class PolicyBaseTest : public InProcessBrowserTest { |
243 protected: | 358 protected: |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 ui_test_utils::NavigateToURL(browser(), pages[i]); | 468 ui_test_utils::NavigateToURL(browser(), pages[i]); |
354 EXPECT_FALSE(IsBannerVisible(browser())); | 469 EXPECT_FALSE(IsBannerVisible(browser())); |
355 provider_.UpdateChromePolicy(test_case->test_policy()); | 470 provider_.UpdateChromePolicy(test_case->test_policy()); |
356 EXPECT_TRUE(IsBannerVisible(browser())); | 471 EXPECT_TRUE(IsBannerVisible(browser())); |
357 const PolicyMap kNoPolicies; | 472 const PolicyMap kNoPolicies; |
358 provider_.UpdateChromePolicy(kNoPolicies); | 473 provider_.UpdateChromePolicy(kNoPolicies); |
359 EXPECT_FALSE(IsBannerVisible(browser())); | 474 EXPECT_FALSE(IsBannerVisible(browser())); |
360 } | 475 } |
361 } | 476 } |
362 | 477 |
| 478 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, CheckPolicyIndicators) { |
| 479 // Verifies that controlled setting indicators correctly show whether a pref's |
| 480 // value is recommended or enforced by a corresponding policy. |
| 481 const PolicyTestCase* policy_test_case = test_cases_.Get(GetParam().name); |
| 482 ASSERT_TRUE(policy_test_case); |
| 483 const ScopedVector<IndicatorTestCase>& indicator_test_cases = |
| 484 policy_test_case->indicator_test_cases(); |
| 485 if (!policy_test_case->IsSupported() || indicator_test_cases.empty()) |
| 486 return; |
| 487 LOG(INFO) << "Testing policy: " << policy_test_case->name(); |
| 488 |
| 489 PrefService* prefs = policy_test_case->is_local_state() ? |
| 490 g_browser_process->local_state() : browser()->profile()->GetPrefs(); |
| 491 // The preference must have been registered. |
| 492 const PrefService::Preference* pref = |
| 493 prefs->FindPreference(policy_test_case->pref_name()); |
| 494 ASSERT_TRUE(pref); |
| 495 ui_test_utils::NavigateToURL(browser(), GURL(kSettingsPages[0])); |
| 496 |
| 497 for (ScopedVector<IndicatorTestCase>::const_iterator |
| 498 indicator_test_case = indicator_test_cases.begin(); |
| 499 indicator_test_case != indicator_test_cases.end(); |
| 500 ++indicator_test_case) { |
| 501 // Check that no controlled setting indicator is visible when no value is |
| 502 // set by policy. |
| 503 PolicyMap policies; |
| 504 provider_.UpdateChromePolicy(policies); |
| 505 VerifyControlledSettingIndicators(browser(), policy_test_case->pref(), |
| 506 "", "", false); |
| 507 // Check that the appropriate controlled setting indicator is shown when a |
| 508 // value is enforced by policy. |
| 509 policies.LoadFrom(&(*indicator_test_case)->policy(), |
| 510 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); |
| 511 provider_.UpdateChromePolicy(policies); |
| 512 VerifyControlledSettingIndicators(browser(), policy_test_case->pref(), |
| 513 (*indicator_test_case)->value(), |
| 514 "policy", |
| 515 (*indicator_test_case)->readonly()); |
| 516 if (!policy_test_case->can_be_recommended()) |
| 517 return; |
| 518 // Check that the appropriate controlled setting indicator is shown when a |
| 519 // value is recommended by policy and the user has not overridden the |
| 520 // recommendation. |
| 521 policies.LoadFrom(&(*indicator_test_case)->policy(), |
| 522 POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER); |
| 523 provider_.UpdateChromePolicy(policies); |
| 524 VerifyControlledSettingIndicators(browser(), policy_test_case->pref(), |
| 525 (*indicator_test_case)->value(), |
| 526 "recommended", |
| 527 (*indicator_test_case)->readonly()); |
| 528 // Check that the appropriate controlled setting indicator is shown when a |
| 529 // value is recommended by policy and the user has overriddent the |
| 530 // recommendation. |
| 531 prefs->Set(policy_test_case->pref_name(), *pref->GetValue()); |
| 532 VerifyControlledSettingIndicators(browser(), policy_test_case->pref(), |
| 533 (*indicator_test_case)->value(), |
| 534 "hasRecommendation", |
| 535 (*indicator_test_case)->readonly()); |
| 536 prefs->ClearPref(policy_test_case->pref_name()); |
| 537 } |
| 538 } |
| 539 |
363 INSTANTIATE_TEST_CASE_P( | 540 INSTANTIATE_TEST_CASE_P( |
364 PolicyPrefsTestInstance, | 541 PolicyPrefsTestInstance, |
365 PolicyPrefsTest, | 542 PolicyPrefsTest, |
366 testing::ValuesIn(GetChromePolicyDefinitionList()->begin, | 543 testing::ValuesIn(GetChromePolicyDefinitionList()->begin, |
367 GetChromePolicyDefinitionList()->end)); | 544 GetChromePolicyDefinitionList()->end)); |
368 | 545 |
369 } // namespace policy | 546 } // namespace policy |
OLD | NEW |