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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstdlib> | 8 #include <cstdlib> |
9 #include <map> | 9 #include <map> |
10 #include <memory> | 10 #include <memory> |
11 #include <sstream> | 11 #include <sstream> |
12 #include <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
16 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
17 #include "base/json/json_reader.h" | 17 #include "base/json/json_reader.h" |
18 #include "base/logging.h" | 18 #include "base/logging.h" |
19 #include "base/macros.h" | 19 #include "base/macros.h" |
20 #include "base/memory/ptr_util.h" | 20 #include "base/memory/ptr_util.h" |
21 #include "base/memory/scoped_vector.h" | |
22 #include "base/memory/weak_ptr.h" | 21 #include "base/memory/weak_ptr.h" |
23 #include "base/run_loop.h" | 22 #include "base/run_loop.h" |
24 #include "base/stl_util.h" | 23 #include "base/stl_util.h" |
25 #include "base/strings/string_util.h" | 24 #include "base/strings/string_util.h" |
26 #include "base/strings/utf_string_conversions.h" | 25 #include "base/strings/utf_string_conversions.h" |
27 #include "base/values.h" | 26 #include "base/values.h" |
28 #include "build/build_config.h" | 27 #include "build/build_config.h" |
29 #include "chrome/browser/browser_process.h" | 28 #include "chrome/browser/browser_process.h" |
30 #include "chrome/browser/profiles/profile.h" | 29 #include "chrome/browser/profiles/profile.h" |
31 #include "chrome/browser/search_engines/template_url_service_factory.h" | 30 #include "chrome/browser/search_engines/template_url_service_factory.h" |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
124 const std::string& indicator_test_url() const { return indicator_test_url_; } | 123 const std::string& indicator_test_url() const { return indicator_test_url_; } |
125 | 124 |
126 const std::string& indicator_test_setup_js() const { | 125 const std::string& indicator_test_setup_js() const { |
127 return indicator_test_setup_js_; | 126 return indicator_test_setup_js_; |
128 } | 127 } |
129 | 128 |
130 const std::string& indicator_selector() const { | 129 const std::string& indicator_selector() const { |
131 return indicator_selector_; | 130 return indicator_selector_; |
132 } | 131 } |
133 | 132 |
134 const ScopedVector<IndicatorTestCase>& indicator_test_cases() const { | 133 const std::vector<std::unique_ptr<IndicatorTestCase>>& indicator_test_cases() |
134 const { | |
135 return indicator_test_cases_; | 135 return indicator_test_cases_; |
136 } | 136 } |
137 void AddIndicatorTestCase(IndicatorTestCase* test_case) { | 137 void AddIndicatorTestCase(std::unique_ptr<IndicatorTestCase> test_case) { |
138 indicator_test_cases_.push_back(test_case); | 138 indicator_test_cases_.push_back(std::move(test_case)); |
Lei Zhang
2017/03/23 02:59:26
#include <utility>
leonhsl(Using Gerrit)
2017/03/23 15:03:17
Done.
| |
139 } | 139 } |
140 | 140 |
141 private: | 141 private: |
142 const std::string pref_; | 142 const std::string pref_; |
143 const bool is_local_state_; | 143 const bool is_local_state_; |
144 const bool check_for_mandatory_; | 144 const bool check_for_mandatory_; |
145 const bool check_for_recommended_; | 145 const bool check_for_recommended_; |
146 const std::string indicator_test_url_; | 146 const std::string indicator_test_url_; |
147 const std::string indicator_test_setup_js_; | 147 const std::string indicator_test_setup_js_; |
148 const std::string indicator_selector_; | 148 const std::string indicator_selector_; |
149 ScopedVector<IndicatorTestCase> indicator_test_cases_; | 149 std::vector<std::unique_ptr<IndicatorTestCase>> indicator_test_cases_; |
150 | 150 |
151 DISALLOW_COPY_AND_ASSIGN(PrefMapping); | 151 DISALLOW_COPY_AND_ASSIGN(PrefMapping); |
152 }; | 152 }; |
153 | 153 |
154 // Contains the testing details for a single policy. This is part of the data | 154 // Contains the testing details for a single policy. This is part of the data |
155 // loaded from chrome/test/data/policy/policy_test_cases.json. | 155 // loaded from chrome/test/data/policy/policy_test_cases.json. |
156 class PolicyTestCase { | 156 class PolicyTestCase { |
157 public: | 157 public: |
158 PolicyTestCase(const std::string& name, | 158 PolicyTestCase(const std::string& name, |
159 bool is_official_only, | 159 bool is_official_only, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 #endif | 195 #endif |
196 return IsOsSupported(); | 196 return IsOsSupported(); |
197 } | 197 } |
198 | 198 |
199 const base::DictionaryValue& test_policy() const { return test_policy_; } | 199 const base::DictionaryValue& test_policy() const { return test_policy_; } |
200 void SetTestPolicy(const base::DictionaryValue& policy) { | 200 void SetTestPolicy(const base::DictionaryValue& policy) { |
201 test_policy_.Clear(); | 201 test_policy_.Clear(); |
202 test_policy_.MergeDictionary(&policy); | 202 test_policy_.MergeDictionary(&policy); |
203 } | 203 } |
204 | 204 |
205 const ScopedVector<PrefMapping>& pref_mappings() const { | 205 const std::vector<std::unique_ptr<PrefMapping>>& pref_mappings() const { |
206 return pref_mappings_; | 206 return pref_mappings_; |
207 } | 207 } |
208 void AddPrefMapping(PrefMapping* pref_mapping) { | 208 void AddPrefMapping(std::unique_ptr<PrefMapping> pref_mapping) { |
209 pref_mappings_.push_back(pref_mapping); | 209 pref_mappings_.push_back(std::move(pref_mapping)); |
210 } | 210 } |
211 | 211 |
212 const std::string& indicator_selector() const { return indicator_selector_; } | 212 const std::string& indicator_selector() const { return indicator_selector_; } |
213 | 213 |
214 private: | 214 private: |
215 std::string name_; | 215 std::string name_; |
216 bool is_official_only_; | 216 bool is_official_only_; |
217 bool can_be_recommended_; | 217 bool can_be_recommended_; |
218 std::vector<std::string> supported_os_; | 218 std::vector<std::string> supported_os_; |
219 base::DictionaryValue test_policy_; | 219 base::DictionaryValue test_policy_; |
220 ScopedVector<PrefMapping> pref_mappings_; | 220 std::vector<std::unique_ptr<PrefMapping>> pref_mappings_; |
221 std::string indicator_selector_; | 221 std::string indicator_selector_; |
222 | 222 |
223 DISALLOW_COPY_AND_ASSIGN(PolicyTestCase); | 223 DISALLOW_COPY_AND_ASSIGN(PolicyTestCase); |
224 }; | 224 }; |
225 | 225 |
226 // Parses all policy test cases and makes them available in a map. | 226 // Parses all policy test cases and makes them available in a map. |
227 class PolicyTestCases { | 227 class PolicyTestCases { |
228 public: | 228 public: |
229 typedef std::vector<PolicyTestCase*> PolicyTestCaseVector; | 229 typedef std::vector<PolicyTestCase*> PolicyTestCaseVector; |
230 typedef std::map<std::string, PolicyTestCaseVector> PolicyTestCaseMap; | 230 typedef std::map<std::string, PolicyTestCaseVector> PolicyTestCaseMap; |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
332 bool check_for_recommended = true; | 332 bool check_for_recommended = true; |
333 pref_mapping_dict->GetBoolean("check_for_recommended", | 333 pref_mapping_dict->GetBoolean("check_for_recommended", |
334 &check_for_recommended); | 334 &check_for_recommended); |
335 std::string indicator_test_url; | 335 std::string indicator_test_url; |
336 pref_mapping_dict->GetString("indicator_test_url", &indicator_test_url); | 336 pref_mapping_dict->GetString("indicator_test_url", &indicator_test_url); |
337 std::string indicator_test_setup_js; | 337 std::string indicator_test_setup_js; |
338 pref_mapping_dict->GetString("indicator_test_setup_js", | 338 pref_mapping_dict->GetString("indicator_test_setup_js", |
339 &indicator_test_setup_js); | 339 &indicator_test_setup_js); |
340 std::string indicator_selector; | 340 std::string indicator_selector; |
341 pref_mapping_dict->GetString("indicator_selector", &indicator_selector); | 341 pref_mapping_dict->GetString("indicator_selector", &indicator_selector); |
342 PrefMapping* pref_mapping = new PrefMapping(pref, | 342 auto pref_mapping = base::MakeUnique<PrefMapping>( |
343 is_local_state, | 343 pref, is_local_state, check_for_mandatory, check_for_recommended, |
344 check_for_mandatory, | 344 indicator_test_url, indicator_test_setup_js, indicator_selector); |
345 check_for_recommended, | |
346 indicator_test_url, | |
347 indicator_test_setup_js, | |
348 indicator_selector); | |
349 const base::ListValue* indicator_tests = NULL; | 345 const base::ListValue* indicator_tests = NULL; |
350 if (pref_mapping_dict->GetList("indicator_tests", &indicator_tests)) { | 346 if (pref_mapping_dict->GetList("indicator_tests", &indicator_tests)) { |
351 for (size_t i = 0; i < indicator_tests->GetSize(); ++i) { | 347 for (size_t i = 0; i < indicator_tests->GetSize(); ++i) { |
352 const base::DictionaryValue* indicator_test_dict = NULL; | 348 const base::DictionaryValue* indicator_test_dict = NULL; |
353 const base::DictionaryValue* policy = NULL; | 349 const base::DictionaryValue* policy = NULL; |
354 if (!indicator_tests->GetDictionary(i, &indicator_test_dict) || | 350 if (!indicator_tests->GetDictionary(i, &indicator_test_dict) || |
355 !indicator_test_dict->GetDictionary("policy", &policy)) { | 351 !indicator_test_dict->GetDictionary("policy", &policy)) { |
356 ADD_FAILURE() << "Malformed indicator_tests entry in " | 352 ADD_FAILURE() << "Malformed indicator_tests entry in " |
357 << "policy_test_cases.json."; | 353 << "policy_test_cases.json."; |
358 continue; | 354 continue; |
359 } | 355 } |
360 std::string value; | 356 std::string value; |
361 indicator_test_dict->GetString("value", &value); | 357 indicator_test_dict->GetString("value", &value); |
362 bool readonly = false; | 358 bool readonly = false; |
363 indicator_test_dict->GetBoolean("readonly", &readonly); | 359 indicator_test_dict->GetBoolean("readonly", &readonly); |
364 pref_mapping->AddIndicatorTestCase( | 360 pref_mapping->AddIndicatorTestCase( |
365 new IndicatorTestCase(*policy, value, readonly)); | 361 base::MakeUnique<IndicatorTestCase>(*policy, value, readonly)); |
366 } | 362 } |
367 } | 363 } |
368 policy_test_case->AddPrefMapping(pref_mapping); | 364 policy_test_case->AddPrefMapping(std::move(pref_mapping)); |
369 } | 365 } |
370 } | 366 } |
371 return policy_test_case; | 367 return policy_test_case; |
372 } | 368 } |
373 | 369 |
374 PolicyTestCaseMap policy_test_cases_; | 370 PolicyTestCaseMap policy_test_cases_; |
375 | 371 |
376 DISALLOW_COPY_AND_ASSIGN(PolicyTestCases); | 372 DISALLOW_COPY_AND_ASSIGN(PolicyTestCases); |
377 }; | 373 }; |
378 | 374 |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
550 PrefService* user_prefs = browser()->profile()->GetPrefs(); | 546 PrefService* user_prefs = browser()->profile()->GetPrefs(); |
551 | 547 |
552 const PolicyTestCases test_cases; | 548 const PolicyTestCases test_cases; |
553 for (PolicyTestCases::iterator policy = test_cases.begin(); | 549 for (PolicyTestCases::iterator policy = test_cases.begin(); |
554 policy != test_cases.end(); | 550 policy != test_cases.end(); |
555 ++policy) { | 551 ++policy) { |
556 for (PolicyTestCases::PolicyTestCaseVector::const_iterator test_case = | 552 for (PolicyTestCases::PolicyTestCaseVector::const_iterator test_case = |
557 policy->second.begin(); | 553 policy->second.begin(); |
558 test_case != policy->second.end(); | 554 test_case != policy->second.end(); |
559 ++test_case) { | 555 ++test_case) { |
560 const ScopedVector<PrefMapping>& pref_mappings = | 556 const auto& pref_mappings = (*test_case)->pref_mappings(); |
561 (*test_case)->pref_mappings(); | |
562 if (!(*test_case)->IsSupported() || pref_mappings.empty()) | 557 if (!(*test_case)->IsSupported() || pref_mappings.empty()) |
563 continue; | 558 continue; |
564 | 559 |
565 LOG(INFO) << "Testing policy: " << policy->first; | 560 LOG(INFO) << "Testing policy: " << policy->first; |
566 | 561 |
567 for (ScopedVector<PrefMapping>::const_iterator pref_mapping = | 562 for (auto pref_mapping = pref_mappings.begin(); |
Lei Zhang
2017/03/23 02:59:26
Can this be a range-based for-loop instead? Ditto
leonhsl(Using Gerrit)
2017/03/23 15:03:17
Done. And also for below 2X.
| |
568 pref_mappings.begin(); | 563 pref_mapping != pref_mappings.end(); ++pref_mapping) { |
569 pref_mapping != pref_mappings.end(); | |
570 ++pref_mapping) { | |
571 // Skip Chrome OS preferences that use a different backend and cannot be | 564 // Skip Chrome OS preferences that use a different backend and cannot be |
572 // retrieved through the prefs mechanism. | 565 // retrieved through the prefs mechanism. |
573 if (base::StartsWith((*pref_mapping)->pref(), kCrosSettingsPrefix, | 566 if (base::StartsWith((*pref_mapping)->pref(), kCrosSettingsPrefix, |
574 base::CompareCase::SENSITIVE)) | 567 base::CompareCase::SENSITIVE)) |
575 continue; | 568 continue; |
576 | 569 |
577 // Skip preferences that should not be checked when the policy is set to | 570 // Skip preferences that should not be checked when the policy is set to |
578 // a mandatory value. | 571 // a mandatory value. |
579 if (!(*pref_mapping)->check_for_mandatory()) | 572 if (!(*pref_mapping)->check_for_mandatory()) |
580 continue; | 573 continue; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
625 test_cases.Get(*policy); | 618 test_cases.Get(*policy); |
626 ASSERT_TRUE(policy_test_cases) << "PolicyTestCase not found for " | 619 ASSERT_TRUE(policy_test_cases) << "PolicyTestCase not found for " |
627 << *policy; | 620 << *policy; |
628 for (std::vector<PolicyTestCase*>::const_iterator test_case = | 621 for (std::vector<PolicyTestCase*>::const_iterator test_case = |
629 policy_test_cases->begin(); | 622 policy_test_cases->begin(); |
630 test_case != policy_test_cases->end(); | 623 test_case != policy_test_cases->end(); |
631 ++test_case) { | 624 ++test_case) { |
632 PolicyTestCase* policy_test_case = *test_case; | 625 PolicyTestCase* policy_test_case = *test_case; |
633 if (!policy_test_case->IsSupported()) | 626 if (!policy_test_case->IsSupported()) |
634 continue; | 627 continue; |
635 const ScopedVector<PrefMapping>& pref_mappings = | 628 const auto& pref_mappings = policy_test_case->pref_mappings(); |
636 policy_test_case->pref_mappings(); | |
637 if (policy_test_case->indicator_selector().empty()) { | 629 if (policy_test_case->indicator_selector().empty()) { |
638 bool has_pref_indicator_tests = false; | 630 bool has_pref_indicator_tests = false; |
639 for (ScopedVector<PrefMapping>::const_iterator pref_mapping = | 631 for (auto pref_mapping = pref_mappings.begin(); |
640 pref_mappings.begin(); | 632 pref_mapping != pref_mappings.end(); ++pref_mapping) { |
641 pref_mapping != pref_mappings.end(); | |
642 ++pref_mapping) { | |
643 PrefService* prefs = | 633 PrefService* prefs = |
644 (*pref_mapping)->is_local_state() ? local_state : user_prefs; | 634 (*pref_mapping)->is_local_state() ? local_state : user_prefs; |
645 if (prefs->FindPreference((*pref_mapping)->pref())) | 635 if (prefs->FindPreference((*pref_mapping)->pref())) |
646 prefs->ClearPref((*pref_mapping)->pref()); | 636 prefs->ClearPref((*pref_mapping)->pref()); |
647 if (!(*pref_mapping)->indicator_test_cases().empty()) { | 637 if (!(*pref_mapping)->indicator_test_cases().empty()) { |
648 has_pref_indicator_tests = true; | 638 has_pref_indicator_tests = true; |
649 break; | 639 break; |
650 } | 640 } |
651 } | 641 } |
652 if (!has_pref_indicator_tests) | 642 if (!has_pref_indicator_tests) |
(...skipping 26 matching lines...) Expand all Loading... | |
679 // enforced value is removed. | 669 // enforced value is removed. |
680 ClearProviderPolicy(); | 670 ClearProviderPolicy(); |
681 VerifyControlledSettingIndicators( | 671 VerifyControlledSettingIndicators( |
682 browser(), | 672 browser(), |
683 policy_test_case->indicator_selector(), | 673 policy_test_case->indicator_selector(), |
684 std::string(), | 674 std::string(), |
685 std::string(), | 675 std::string(), |
686 false); | 676 false); |
687 } | 677 } |
688 | 678 |
689 for (ScopedVector<PrefMapping>::const_iterator | 679 for (auto pref_mapping = pref_mappings.begin(); |
690 pref_mapping = pref_mappings.begin(); | 680 pref_mapping != pref_mappings.end(); ++pref_mapping) { |
691 pref_mapping != pref_mappings.end(); | 681 const auto& indicator_test_cases = |
692 ++pref_mapping) { | |
693 const ScopedVector<IndicatorTestCase>& indicator_test_cases = | |
694 (*pref_mapping)->indicator_test_cases(); | 682 (*pref_mapping)->indicator_test_cases(); |
695 if (indicator_test_cases.empty()) | 683 if (indicator_test_cases.empty()) |
696 continue; | 684 continue; |
697 | 685 |
698 if (!(*pref_mapping)->indicator_test_setup_js().empty()) { | 686 if (!(*pref_mapping)->indicator_test_setup_js().empty()) { |
699 ASSERT_TRUE(content::ExecuteScript( | 687 ASSERT_TRUE(content::ExecuteScript( |
700 browser()->tab_strip_model()->GetActiveWebContents(), | 688 browser()->tab_strip_model()->GetActiveWebContents(), |
701 (*pref_mapping)->indicator_test_setup_js())); | 689 (*pref_mapping)->indicator_test_setup_js())); |
702 } | 690 } |
703 | 691 |
704 // A non-empty indicator_test_url is expected to be used in very | 692 // A non-empty indicator_test_url is expected to be used in very |
705 // few cases, so it's currently implemented by navigating to the URL | 693 // few cases, so it's currently implemented by navigating to the URL |
706 // right before the test and navigating back afterwards. | 694 // right before the test and navigating back afterwards. |
707 // If you introduce many test cases with the same non-empty | 695 // If you introduce many test cases with the same non-empty |
708 // indicator_test_url, this would be inefficient. We could consider | 696 // indicator_test_url, this would be inefficient. We could consider |
709 // navigting to a specific indicator_test_url once for many test cases | 697 // navigting to a specific indicator_test_url once for many test cases |
710 // instead. | 698 // instead. |
711 if (!(*pref_mapping)->indicator_test_url().empty()) { | 699 if (!(*pref_mapping)->indicator_test_url().empty()) { |
712 ui_test_utils::NavigateToURL( | 700 ui_test_utils::NavigateToURL( |
713 browser(), GURL((*pref_mapping)->indicator_test_url())); | 701 browser(), GURL((*pref_mapping)->indicator_test_url())); |
714 } | 702 } |
715 | 703 |
716 std::string indicator_selector = (*pref_mapping)->indicator_selector(); | 704 std::string indicator_selector = (*pref_mapping)->indicator_selector(); |
717 if (indicator_selector.empty()) | 705 if (indicator_selector.empty()) |
718 indicator_selector = "[pref=\"" + (*pref_mapping)->pref() + "\"]"; | 706 indicator_selector = "[pref=\"" + (*pref_mapping)->pref() + "\"]"; |
719 for (ScopedVector<IndicatorTestCase>::const_iterator | 707 for (auto indicator_test_case = indicator_test_cases.begin(); |
720 indicator_test_case = indicator_test_cases.begin(); | |
721 indicator_test_case != indicator_test_cases.end(); | 708 indicator_test_case != indicator_test_cases.end(); |
722 ++indicator_test_case) { | 709 ++indicator_test_case) { |
723 // Check that no controlled setting indicator is visible when no value | 710 // Check that no controlled setting indicator is visible when no value |
724 // is set by policy. | 711 // is set by policy. |
725 ClearProviderPolicy(); | 712 ClearProviderPolicy(); |
726 VerifyControlledSettingIndicators(browser(), | 713 VerifyControlledSettingIndicators(browser(), |
727 indicator_selector, | 714 indicator_selector, |
728 std::string(), | 715 std::string(), |
729 std::string(), | 716 std::string(), |
730 false); | 717 false); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
782 } | 769 } |
783 } | 770 } |
784 } | 771 } |
785 } | 772 } |
786 | 773 |
787 INSTANTIATE_TEST_CASE_P(PolicyPrefIndicatorTestInstance, | 774 INSTANTIATE_TEST_CASE_P(PolicyPrefIndicatorTestInstance, |
788 PolicyPrefIndicatorTest, | 775 PolicyPrefIndicatorTest, |
789 testing::ValuesIn(SplitPoliciesIntoChunks(10))); | 776 testing::ValuesIn(SplitPoliciesIntoChunks(10))); |
790 | 777 |
791 } // namespace policy | 778 } // namespace policy |
OLD | NEW |