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 |
(...skipping 23 matching lines...) Expand all Loading... | |
34 #include "policy/policy_constants.h" | 34 #include "policy/policy_constants.h" |
35 #include "testing/gmock/include/gmock/gmock.h" | 35 #include "testing/gmock/include/gmock/gmock.h" |
36 #include "testing/gtest/include/gtest/gtest.h" | 36 #include "testing/gtest/include/gtest/gtest.h" |
37 | 37 |
38 using testing::Return; | 38 using testing::Return; |
39 | 39 |
40 namespace policy { | 40 namespace policy { |
41 | 41 |
42 namespace { | 42 namespace { |
43 | 43 |
44 const char* kSettingsPages[] = { | 44 const char kMainSettingsPage[] = "chrome://settings-frame"; |
45 "chrome://settings-frame", | |
46 "chrome://settings-frame/searchEngines", | |
47 "chrome://settings-frame/passwords", | |
48 "chrome://settings-frame/autofill", | |
49 "chrome://settings-frame/content", | |
50 "chrome://settings-frame/homePageOverlay", | |
51 "chrome://settings-frame/languages", | |
52 #if defined(OS_CHROMEOS) | |
53 "chrome://settings-frame/accounts", | |
54 #endif | |
55 }; | |
56 | 45 |
57 const char kCrosSettingsPrefix[] = "cros."; | 46 const char kCrosSettingsPrefix[] = "cros."; |
58 | 47 |
59 // Contains the details of a single test case verifying that the controlled | 48 // Contains the details of a single test case verifying that the controlled |
60 // setting indicators for a pref affected by a policy work correctly. This is | 49 // setting indicators for a pref affected by a policy work correctly. This is |
61 // part of the data loaded from chrome/test/data/policy/policy_test_cases.json. | 50 // part of the data loaded from chrome/test/data/policy/policy_test_cases.json. |
62 class IndicatorTestCase { | 51 class IndicatorTestCase { |
63 public: | 52 public: |
64 IndicatorTestCase(const base::DictionaryValue& policy, | 53 IndicatorTestCase(const base::DictionaryValue& policy, |
65 const std::string& value, | 54 const std::string& value, |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 return false; | 155 return false; |
167 #endif | 156 #endif |
168 return IsOsSupported(); | 157 return IsOsSupported(); |
169 } | 158 } |
170 | 159 |
171 const PolicyMap& test_policy() const { return test_policy_; } | 160 const PolicyMap& test_policy() const { return test_policy_; } |
172 void SetTestPolicy(const PolicyMap& policy) { | 161 void SetTestPolicy(const PolicyMap& policy) { |
173 test_policy_.CopyFrom(policy); | 162 test_policy_.CopyFrom(policy); |
174 } | 163 } |
175 | 164 |
176 const std::vector<GURL>& settings_pages() const { return settings_pages_; } | |
177 void AddSettingsPage(const GURL& url) { settings_pages_.push_back(url); } | |
178 | |
179 const ScopedVector<PrefMapping>& pref_mappings() const { | 165 const ScopedVector<PrefMapping>& pref_mappings() const { |
180 return pref_mappings_; | 166 return pref_mappings_; |
181 } | 167 } |
182 void AddPrefMapping(PrefMapping* pref_mapping) { | 168 void AddPrefMapping(PrefMapping* pref_mapping) { |
183 pref_mappings_.push_back(pref_mapping); | 169 pref_mappings_.push_back(pref_mapping); |
184 } | 170 } |
185 | 171 |
186 private: | 172 private: |
187 std::string name_; | 173 std::string name_; |
188 bool is_official_only_; | 174 bool is_official_only_; |
189 bool can_be_recommended_; | 175 bool can_be_recommended_; |
190 std::vector<std::string> supported_os_; | 176 std::vector<std::string> supported_os_; |
191 PolicyMap test_policy_; | 177 PolicyMap test_policy_; |
192 std::vector<GURL> settings_pages_; | |
193 ScopedVector<PrefMapping> pref_mappings_; | 178 ScopedVector<PrefMapping> pref_mappings_; |
194 | 179 |
195 DISALLOW_COPY_AND_ASSIGN(PolicyTestCase); | 180 DISALLOW_COPY_AND_ASSIGN(PolicyTestCase); |
196 }; | 181 }; |
197 | 182 |
198 // Parses all policy test cases and makes then available in a map. | 183 // Parses all policy test cases and makes then available in a map. |
199 class PolicyTestCases { | 184 class PolicyTestCases { |
200 public: | 185 public: |
201 typedef std::map<std::string, PolicyTestCase*> PolicyTestCaseMap; | 186 typedef std::map<std::string, PolicyTestCase*> PolicyTestCaseMap; |
202 typedef PolicyTestCaseMap::const_iterator iterator; | 187 typedef PolicyTestCaseMap::const_iterator iterator; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
263 if (os_list->GetString(i, &os)) | 248 if (os_list->GetString(i, &os)) |
264 policy_test_case->AddSupportedOs(os); | 249 policy_test_case->AddSupportedOs(os); |
265 } | 250 } |
266 } | 251 } |
267 const base::DictionaryValue* policy_dict = NULL; | 252 const base::DictionaryValue* policy_dict = NULL; |
268 if (policy_test_dict->GetDictionary("test_policy", &policy_dict)) { | 253 if (policy_test_dict->GetDictionary("test_policy", &policy_dict)) { |
269 PolicyMap policy; | 254 PolicyMap policy; |
270 policy.LoadFrom(policy_dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); | 255 policy.LoadFrom(policy_dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); |
271 policy_test_case->SetTestPolicy(policy); | 256 policy_test_case->SetTestPolicy(policy); |
272 } | 257 } |
273 const base::ListValue* settings_pages = NULL; | |
274 if (policy_test_dict->GetList("settings_pages", &settings_pages)) { | |
275 for (size_t i = 0; i < settings_pages->GetSize(); ++i) { | |
276 std::string page; | |
277 if (settings_pages->GetString(i, &page)) | |
278 policy_test_case->AddSettingsPage(GURL(page)); | |
279 } | |
280 } | |
281 const base::ListValue* pref_mappings = NULL; | 258 const base::ListValue* pref_mappings = NULL; |
282 if (policy_test_dict->GetList("pref_mappings", &pref_mappings)) { | 259 if (policy_test_dict->GetList("pref_mappings", &pref_mappings)) { |
283 for (size_t i = 0; i < pref_mappings->GetSize(); ++i) { | 260 for (size_t i = 0; i < pref_mappings->GetSize(); ++i) { |
284 const base::DictionaryValue* pref_mapping_dict = NULL; | 261 const base::DictionaryValue* pref_mapping_dict = NULL; |
285 std::string pref; | 262 std::string pref; |
286 if (!pref_mappings->GetDictionary(i, &pref_mapping_dict) || | 263 if (!pref_mappings->GetDictionary(i, &pref_mapping_dict) || |
287 !pref_mapping_dict->GetString("pref", &pref)) { | 264 !pref_mapping_dict->GetString("pref", &pref)) { |
288 ADD_FAILURE() << "Malformed pref_mappings entry in " | 265 ADD_FAILURE() << "Malformed pref_mappings entry in " |
289 << "policy_test_cases.json."; | 266 << "policy_test_cases.json."; |
290 continue; | 267 continue; |
(...skipping 30 matching lines...) Expand all Loading... | |
321 } | 298 } |
322 } | 299 } |
323 return policy_test_case; | 300 return policy_test_case; |
324 } | 301 } |
325 | 302 |
326 PolicyTestCaseMap* policy_test_cases_; | 303 PolicyTestCaseMap* policy_test_cases_; |
327 | 304 |
328 DISALLOW_COPY_AND_ASSIGN(PolicyTestCases); | 305 DISALLOW_COPY_AND_ASSIGN(PolicyTestCases); |
329 }; | 306 }; |
330 | 307 |
331 bool IsBannerVisible(Browser* browser) { | |
332 content::WebContents* contents = chrome::GetActiveWebContents(browser); | |
333 bool result = false; | |
334 EXPECT_TRUE(content::ExecuteJavaScriptAndExtractBool( | |
335 contents->GetRenderViewHost(), | |
336 std::wstring(), | |
337 L"var visible = false;" | |
338 L"var banners = document.querySelectorAll('.page-banner');" | |
339 L"for (var i = 0; i < banners.length; i++) {" | |
340 L" if (banners[i].parentElement.id == 'templates')" | |
341 L" continue;" | |
342 L" if (window.getComputedStyle(banners[i]).display != 'none')" | |
343 L" visible = true;" | |
344 L"}" | |
345 L"domAutomationController.send(visible);", | |
346 &result)); | |
347 return result; | |
348 } | |
349 | |
350 void VerifyControlledSettingIndicators(Browser* browser, | 308 void VerifyControlledSettingIndicators(Browser* browser, |
351 const std::string& selector, | 309 const std::string& selector, |
352 const std::string& value, | 310 const std::string& value, |
353 const std::string& controlled_by, | 311 const std::string& controlled_by, |
354 bool readonly) { | 312 bool readonly) { |
355 std::wstringstream javascript; | 313 std::wstringstream javascript; |
356 javascript << "var nodes = document.querySelectorAll(" | 314 javascript << "var nodes = document.querySelectorAll(" |
357 << " 'span.controlled-setting-indicator" | 315 << " 'span.controlled-setting-indicator" |
358 << selector.c_str() << "');" | 316 << selector.c_str() << "');" |
359 << "var indicators = [];" | 317 << "var indicators = [];" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
410 } | 368 } |
411 if (!controlled_by.empty()) { | 369 if (!controlled_by.empty()) { |
412 EXPECT_TRUE(have_visible_indicators) | 370 EXPECT_TRUE(have_visible_indicators) |
413 << "Expected to find at least one visible controlled setting " | 371 << "Expected to find at least one visible controlled setting " |
414 << "indicator."; | 372 << "indicator."; |
415 } | 373 } |
416 } | 374 } |
417 | 375 |
418 } // namespace | 376 } // namespace |
419 | 377 |
420 // A class of tests parameterized by a settings page URL. | 378 // Base class for tests that change policy and are parameterized with a policy |
421 class PolicyPrefsSettingsBannerTest | 379 // definition. |
380 class PolicyPrefsTest | |
422 : public InProcessBrowserTest, | 381 : public InProcessBrowserTest, |
423 public testing::WithParamInterface<const char*> {}; | 382 public testing::WithParamInterface<PolicyDefinitionList::Entry> { |
424 | |
425 // Base class for tests that change policies. | |
426 class PolicyBaseTest : public InProcessBrowserTest { | |
427 protected: | 383 protected: |
428 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | 384 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { |
429 EXPECT_CALL(provider_, IsInitializationComplete()) | 385 EXPECT_CALL(provider_, IsInitializationComplete()) |
430 .WillRepeatedly(Return(true)); | 386 .WillRepeatedly(Return(true)); |
431 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); | 387 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); |
432 } | 388 } |
433 | 389 |
434 PolicyTestCases policy_test_cases_; | 390 PolicyTestCases policy_test_cases_; |
435 MockConfigurationPolicyProvider provider_; | 391 MockConfigurationPolicyProvider provider_; |
436 }; | 392 }; |
437 | 393 |
438 // A class of tests that change policy and don't need parameters. | |
439 class PolicyPrefsBannerTest : public PolicyBaseTest {}; | |
440 | |
441 // A class of tests that change policy and are parameterized with a policy | |
442 // definition. | |
443 class PolicyPrefsTest | |
444 : public PolicyBaseTest, | |
445 public testing::WithParamInterface<PolicyDefinitionList::Entry> {}; | |
446 | |
447 TEST(PolicyPrefsTest, AllPoliciesHaveATestCase) { | 394 TEST(PolicyPrefsTest, AllPoliciesHaveATestCase) { |
Joao da Silva
2012/10/25 11:07:52
TEST() expand the arguments into a class name, and
bartfab (slow)
2012/10/25 13:13:13
Done.
| |
448 // Verifies that all known policies have a test case in the JSON file. | 395 // Verifies that all known policies have a test case in the JSON file. |
449 // This test fails when a policy is added to | 396 // This test fails when a policy is added to |
450 // chrome/app/policy/policy_templates.json but a test case is not added to | 397 // chrome/app/policy/policy_templates.json but a test case is not added to |
451 // chrome/test/data/policy/policy_test_cases.json. | 398 // chrome/test/data/policy/policy_test_cases.json. |
452 PolicyTestCases policy_test_cases; | 399 PolicyTestCases policy_test_cases; |
453 const PolicyDefinitionList* list = GetChromePolicyDefinitionList(); | 400 const PolicyDefinitionList* list = GetChromePolicyDefinitionList(); |
454 for (const PolicyDefinitionList::Entry* policy = list->begin; | 401 for (const PolicyDefinitionList::Entry* policy = list->begin; |
455 policy != list->end; ++policy) { | 402 policy != list->end; ++policy) { |
456 EXPECT_TRUE(ContainsKey(policy_test_cases.map(), policy->name)) | 403 EXPECT_TRUE(ContainsKey(policy_test_cases.map(), policy->name)) |
457 << "Missing policy test case for: " << policy->name; | 404 << "Missing policy test case for: " << policy->name; |
458 } | 405 } |
459 } | 406 } |
460 | 407 |
461 IN_PROC_BROWSER_TEST_P(PolicyPrefsSettingsBannerTest, NoPoliciesNoBanner) { | |
462 // Verifies that the banner isn't shown in the settings UI when no policies | |
463 // are set. | |
464 ui_test_utils::NavigateToURL(browser(), GURL(GetParam())); | |
465 EXPECT_FALSE(IsBannerVisible(browser())); | |
466 } | |
467 | |
468 INSTANTIATE_TEST_CASE_P(PolicyPrefsSettingsBannerTestInstance, | |
469 PolicyPrefsSettingsBannerTest, | |
470 testing::ValuesIn(kSettingsPages)); | |
471 | |
472 IN_PROC_BROWSER_TEST_F(PolicyPrefsBannerTest, TogglePolicyTogglesBanner) { | |
473 // Verifies that the banner appears and disappears as policies are added and | |
474 // removed. | |
475 // |test_case| is just a particular policy that should trigger the banner | |
476 // on the main settings page. | |
477 const PolicyTestCase* test_case = policy_test_cases_.Get("ShowHomeButton"); | |
478 ASSERT_TRUE(test_case); | |
479 // No banner by default. | |
480 ui_test_utils::NavigateToURL(browser(), GURL(kSettingsPages[0])); | |
481 EXPECT_FALSE(IsBannerVisible(browser())); | |
482 // Adding a policy makes the banner show up. | |
483 provider_.UpdateChromePolicy(test_case->test_policy()); | |
484 EXPECT_TRUE(IsBannerVisible(browser())); | |
485 // And removing it makes the banner go away. | |
486 const PolicyMap kNoPolicies; | |
487 provider_.UpdateChromePolicy(kNoPolicies); | |
488 EXPECT_FALSE(IsBannerVisible(browser())); | |
489 // Do it again, just in case. | |
490 provider_.UpdateChromePolicy(test_case->test_policy()); | |
491 EXPECT_TRUE(IsBannerVisible(browser())); | |
492 provider_.UpdateChromePolicy(kNoPolicies); | |
493 EXPECT_FALSE(IsBannerVisible(browser())); | |
494 } | |
495 | |
496 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, PolicyToPrefsMapping) { | 408 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, PolicyToPrefsMapping) { |
497 // Verifies that policies make their corresponding preferences become managed, | 409 // Verifies that policies make their corresponding preferences become managed, |
498 // and that the user can't override that setting. | 410 // and that the user can't override that setting. |
499 const PolicyTestCase* test_case = policy_test_cases_.Get(GetParam().name); | 411 const PolicyTestCase* test_case = policy_test_cases_.Get(GetParam().name); |
500 ASSERT_TRUE(test_case); | 412 ASSERT_TRUE(test_case); |
501 const ScopedVector<PrefMapping>& pref_mappings = test_case->pref_mappings(); | 413 const ScopedVector<PrefMapping>& pref_mappings = test_case->pref_mappings(); |
502 if (!test_case->IsSupported() || pref_mappings.empty()) | 414 if (!test_case->IsSupported() || pref_mappings.empty()) |
503 return; | 415 return; |
504 LOG(INFO) << "Testing policy: " << test_case->name(); | 416 LOG(INFO) << "Testing policy: " << test_case->name(); |
505 | 417 |
(...skipping 23 matching lines...) Expand all Loading... | |
529 EXPECT_FALSE(pref->IsManaged()); | 441 EXPECT_FALSE(pref->IsManaged()); |
530 | 442 |
531 provider_.UpdateChromePolicy(test_case->test_policy()); | 443 provider_.UpdateChromePolicy(test_case->test_policy()); |
532 EXPECT_FALSE(pref->IsDefaultValue()); | 444 EXPECT_FALSE(pref->IsDefaultValue()); |
533 EXPECT_FALSE(pref->IsUserModifiable()); | 445 EXPECT_FALSE(pref->IsUserModifiable()); |
534 EXPECT_FALSE(pref->IsUserControlled()); | 446 EXPECT_FALSE(pref->IsUserControlled()); |
535 EXPECT_TRUE(pref->IsManaged()); | 447 EXPECT_TRUE(pref->IsManaged()); |
536 } | 448 } |
537 } | 449 } |
538 | 450 |
539 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, CheckAllPoliciesThatShowTheBanner) { | |
540 // Verifies that the banner appears for each policy that affects a control | |
541 // in the settings UI. | |
542 const PolicyTestCase* test_case = policy_test_cases_.Get(GetParam().name); | |
543 ASSERT_TRUE(test_case); | |
544 if (!test_case->IsSupported() || test_case->settings_pages().empty()) | |
545 return; | |
546 LOG(INFO) << "Testing policy: " << test_case->name(); | |
547 | |
548 const std::vector<GURL>& pages = test_case->settings_pages(); | |
549 for (size_t i = 0; i < pages.size(); ++i) { | |
550 ui_test_utils::NavigateToURL(browser(), pages[i]); | |
551 EXPECT_FALSE(IsBannerVisible(browser())); | |
552 provider_.UpdateChromePolicy(test_case->test_policy()); | |
553 EXPECT_TRUE(IsBannerVisible(browser())); | |
554 const PolicyMap kNoPolicies; | |
555 provider_.UpdateChromePolicy(kNoPolicies); | |
556 EXPECT_FALSE(IsBannerVisible(browser())); | |
557 } | |
558 } | |
559 | |
560 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, CheckPolicyIndicators) { | 451 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, CheckPolicyIndicators) { |
561 // Verifies that controlled setting indicators correctly show whether a pref's | 452 // Verifies that controlled setting indicators correctly show whether a pref's |
562 // value is recommended or enforced by a corresponding policy. | 453 // value is recommended or enforced by a corresponding policy. |
563 const PolicyTestCase* policy_test_case = | 454 const PolicyTestCase* policy_test_case = |
564 policy_test_cases_.Get(GetParam().name); | 455 policy_test_cases_.Get(GetParam().name); |
565 ASSERT_TRUE(policy_test_case); | 456 ASSERT_TRUE(policy_test_case); |
566 const ScopedVector<PrefMapping>& pref_mappings = | 457 const ScopedVector<PrefMapping>& pref_mappings = |
567 policy_test_case->pref_mappings(); | 458 policy_test_case->pref_mappings(); |
568 if (!policy_test_case->IsSupported() || pref_mappings.empty()) | 459 if (!policy_test_case->IsSupported() || pref_mappings.empty()) |
569 return; | 460 return; |
(...skipping 13 matching lines...) Expand all Loading... | |
583 | 474 |
584 for (ScopedVector<PrefMapping>::const_iterator | 475 for (ScopedVector<PrefMapping>::const_iterator |
585 pref_mapping = pref_mappings.begin(); | 476 pref_mapping = pref_mappings.begin(); |
586 pref_mapping != pref_mappings.end(); | 477 pref_mapping != pref_mappings.end(); |
587 ++pref_mapping) { | 478 ++pref_mapping) { |
588 const ScopedVector<IndicatorTestCase>& | 479 const ScopedVector<IndicatorTestCase>& |
589 indicator_test_cases = (*pref_mapping)->indicator_test_cases(); | 480 indicator_test_cases = (*pref_mapping)->indicator_test_cases(); |
590 if (indicator_test_cases.empty()) | 481 if (indicator_test_cases.empty()) |
591 continue; | 482 continue; |
592 | 483 |
593 ui_test_utils::NavigateToURL(browser(), GURL(kSettingsPages[0])); | 484 ui_test_utils::NavigateToURL(browser(), GURL(kMainSettingsPage)); |
594 if (!(*pref_mapping)->indicator_test_setup_js().empty()) { | 485 if (!(*pref_mapping)->indicator_test_setup_js().empty()) { |
595 ASSERT_TRUE(content::ExecuteJavaScript( | 486 ASSERT_TRUE(content::ExecuteJavaScript( |
596 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), L"", | 487 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), L"", |
597 (*pref_mapping)->indicator_test_setup_js())); | 488 (*pref_mapping)->indicator_test_setup_js())); |
598 } | 489 } |
599 | 490 |
600 std::string indicator_selector = (*pref_mapping)->indicator_selector(); | 491 std::string indicator_selector = (*pref_mapping)->indicator_selector(); |
601 if (indicator_selector.empty()) | 492 if (indicator_selector.empty()) |
602 indicator_selector = "[pref=\"" + (*pref_mapping)->pref() + "\"]"; | 493 indicator_selector = "[pref=\"" + (*pref_mapping)->pref() + "\"]"; |
603 for (ScopedVector<IndicatorTestCase>::const_iterator | 494 for (ScopedVector<IndicatorTestCase>::const_iterator |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
653 } | 544 } |
654 } | 545 } |
655 | 546 |
656 INSTANTIATE_TEST_CASE_P( | 547 INSTANTIATE_TEST_CASE_P( |
657 PolicyPrefsTestInstance, | 548 PolicyPrefsTestInstance, |
658 PolicyPrefsTest, | 549 PolicyPrefsTest, |
659 testing::ValuesIn(GetChromePolicyDefinitionList()->begin, | 550 testing::ValuesIn(GetChromePolicyDefinitionList()->begin, |
660 GetChromePolicyDefinitionList()->end)); | 551 GetChromePolicyDefinitionList()->end)); |
661 | 552 |
662 } // namespace policy | 553 } // namespace policy |
OLD | NEW |