Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(516)

Side by Side Diff: chrome/browser/policy/policy_prefs_browsertest.cc

Issue 11096020: Add first batch of controlled setting indicators (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/resources/options/home_page_overlay.css » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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),
59 is_local_state_(false), 84 is_local_state_(false),
60 official_only_(false) {} 85 official_only_(false) {}
61 ~PolicyTestCase() {} 86 ~PolicyTestCase() {}
62 87
63 const std::string& name() const { return name_; } 88 const std::string& name() const { return name_; }
64 89
65 void set_pref(const std::string& pref) { pref_ = pref; } 90 void set_pref(const std::string& pref) { pref_ = pref; }
66 const std::string& pref() const { return pref_; } 91 const std::string& pref() const { return pref_; }
67 const char* pref_name() const { return pref_.c_str(); } 92 const char* pref_name() const { return pref_.c_str(); }
68 93
94 bool can_be_recommended() const { return can_be_recommended_; }
Joao da Silva 2012/10/09 14:09:14 Set a default value in the ctor
bartfab (slow) 2012/10/09 14:33:02 Done.
95 void set_can_be_recommended(bool can_be_recommended) {
96 can_be_recommended_ = can_be_recommended;
97 }
98
69 const PolicyMap& test_policy() const { return test_policy_; } 99 const PolicyMap& test_policy() const { return test_policy_; }
70 void set_test_policy(const PolicyMap& policy) { 100 void set_test_policy(const PolicyMap& policy) {
71 test_policy_.CopyFrom(policy); 101 test_policy_.CopyFrom(policy);
72 } 102 }
103 const ScopedVector<IndicatorTestCase>& indicator_test_cases() const {
Joao da Silva 2012/10/09 14:09:14 nit: newline before this line
bartfab (slow) 2012/10/09 14:33:02 Done.
104 return indicator_test_cases_;
105 }
106 void AddIndicatorTestCase(IndicatorTestCase* test_case) {
107 indicator_test_cases_.push_back(test_case);
108 }
73 109
74 const std::vector<GURL>& settings_pages() const { return settings_pages_; } 110 const std::vector<GURL>& settings_pages() const { return settings_pages_; }
75 void AddSettingsPage(const GURL& url) { settings_pages_.push_back(url); } 111 void AddSettingsPage(const GURL& url) { settings_pages_.push_back(url); }
76 112
77 bool IsOsSupported() const { 113 bool IsOsSupported() const {
78 #if defined(OS_WIN) 114 #if defined(OS_WIN)
79 const std::string os("win"); 115 const std::string os("win");
80 #elif defined(OS_MACOSX) 116 #elif defined(OS_MACOSX)
81 const std::string os("mac"); 117 const std::string os("mac");
82 #elif defined(OS_CHROMEOS) 118 #elif defined(OS_CHROMEOS)
(...skipping 19 matching lines...) Expand all
102 #if !defined(OFFICIAL_BUILD) 138 #if !defined(OFFICIAL_BUILD)
103 if (is_official_only()) 139 if (is_official_only())
104 return false; 140 return false;
105 #endif 141 #endif
106 return IsOsSupported(); 142 return IsOsSupported();
107 } 143 }
108 144
109 private: 145 private:
110 std::string name_; 146 std::string name_;
111 std::string pref_; 147 std::string pref_;
148 bool can_be_recommended_;
112 PolicyMap test_policy_; 149 PolicyMap test_policy_;
150 ScopedVector<IndicatorTestCase> indicator_test_cases_;
113 std::vector<GURL> settings_pages_; 151 std::vector<GURL> settings_pages_;
114 std::vector<std::string> supported_os_; 152 std::vector<std::string> supported_os_;
115 bool is_local_state_; 153 bool is_local_state_;
116 bool official_only_; 154 bool official_only_;
117 155
118 DISALLOW_COPY_AND_ASSIGN(PolicyTestCase); 156 DISALLOW_COPY_AND_ASSIGN(PolicyTestCase);
119 }; 157 };
120 158
121 // Parses all the test cases and makes then available in a map. 159 // Parses all the test cases and makes then available in a map.
122 class TestCases { 160 class TestCases {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 private: 208 private:
171 PolicyTestCase* GetTestCase(const base::DictionaryValue* tests, 209 PolicyTestCase* GetTestCase(const base::DictionaryValue* tests,
172 const std::string& name) { 210 const std::string& name) {
173 const base::DictionaryValue* dict = NULL; 211 const base::DictionaryValue* dict = NULL;
174 if (!tests->GetDictionary(name, &dict)) 212 if (!tests->GetDictionary(name, &dict))
175 return NULL; 213 return NULL;
176 PolicyTestCase* test_case = new PolicyTestCase(name); 214 PolicyTestCase* test_case = new PolicyTestCase(name);
177 std::string pref; 215 std::string pref;
178 if (dict->GetString("pref", &pref)) 216 if (dict->GetString("pref", &pref))
179 test_case->set_pref(pref); 217 test_case->set_pref(pref);
218 bool flag = false;
219 if (dict->GetBoolean("can_be_recommended", &flag))
220 test_case->set_can_be_recommended(flag);
180 const base::DictionaryValue* policy_dict = NULL; 221 const base::DictionaryValue* policy_dict = NULL;
181 if (dict->GetDictionary("test_policy", &policy_dict)) { 222 if (dict->GetDictionary("test_policy", &policy_dict)) {
182 PolicyMap policies; 223 PolicyMap policies;
183 policies.LoadFrom(policy_dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); 224 policies.LoadFrom(policy_dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER);
184 test_case->set_test_policy(policies); 225 test_case->set_test_policy(policies);
185 } 226 }
227 const base::ListValue* indicator_tests = NULL;
228 if (dict->GetList("indicator_tests", &indicator_tests)) {
229 for (size_t i = 0; i < indicator_tests->GetSize(); ++i) {
230 const base::DictionaryValue* indicator_test_dict = NULL;
231 const base::DictionaryValue* policy = NULL;
232 if (!indicator_tests->GetDictionary(i, &indicator_test_dict) ||
233 !indicator_test_dict->GetDictionary("policy", &policy))
Joao da Silva 2012/10/09 14:09:14 nit: braces
bartfab (slow) 2012/10/09 14:33:02 Done.
234 continue;
235 std::string value;
236 indicator_test_dict->GetString("value", &value);
237 bool readonly = false;
238 indicator_test_dict->GetBoolean("readonly", &readonly);
239 test_case->AddIndicatorTestCase(new IndicatorTestCase(*policy, value,
240 readonly));
Joao da Silva 2012/10/09 14:09:14 nitty nit/matter of preference: I find it more rea
bartfab (slow) 2012/10/09 14:33:02 Done.
241 }
242 }
186 const base::ListValue* settings_pages = NULL; 243 const base::ListValue* settings_pages = NULL;
187 if (dict->GetList("settings_pages", &settings_pages)) { 244 if (dict->GetList("settings_pages", &settings_pages)) {
188 for (size_t i = 0; i < settings_pages->GetSize(); ++i) { 245 for (size_t i = 0; i < settings_pages->GetSize(); ++i) {
189 std::string page; 246 std::string page;
190 if (settings_pages->GetString(i, &page)) 247 if (settings_pages->GetString(i, &page))
191 test_case->AddSettingsPage(GURL(page)); 248 test_case->AddSettingsPage(GURL(page));
192 } 249 }
193 } 250 }
194 const base::ListValue* supported_os = NULL; 251 const base::ListValue* supported_os = NULL;
195 if (dict->GetList("os", &supported_os)) { 252 if (dict->GetList("os", &supported_os)) {
196 for (size_t i = 0; i < supported_os->GetSize(); ++i) { 253 for (size_t i = 0; i < supported_os->GetSize(); ++i) {
197 std::string os; 254 std::string os;
198 if (supported_os->GetString(i, &os)) 255 if (supported_os->GetString(i, &os))
199 test_case->AddSupportedOs(os); 256 test_case->AddSupportedOs(os);
200 } 257 }
201 } 258 }
202 bool flag = false; 259 flag = false;
Joao da Silva 2012/10/09 14:09:14 nit: this line can be removed
bartfab (slow) 2012/10/09 14:33:02 Done.
203 if (dict->GetBoolean("local_state", &flag)) 260 if (dict->GetBoolean("local_state", &flag))
204 test_case->set_local_state(flag); 261 test_case->set_local_state(flag);
205 if (dict->GetBoolean("official_only", &flag)) 262 if (dict->GetBoolean("official_only", &flag))
206 test_case->set_official_only(flag); 263 test_case->set_official_only(flag);
207 return test_case; 264 return test_case;
208 } 265 }
209 266
210 TestCaseMap* test_cases_; 267 TestCaseMap* test_cases_;
211 268
212 DISALLOW_COPY_AND_ASSIGN(TestCases); 269 DISALLOW_COPY_AND_ASSIGN(TestCases);
(...skipping 11 matching lines...) Expand all
224 L" if (banners[i].parentElement.id == 'templates')" 281 L" if (banners[i].parentElement.id == 'templates')"
225 L" continue;" 282 L" continue;"
226 L" if (window.getComputedStyle(banners[i]).display != 'none')" 283 L" if (window.getComputedStyle(banners[i]).display != 'none')"
227 L" visible = true;" 284 L" visible = true;"
228 L"}" 285 L"}"
229 L"domAutomationController.send(visible);", 286 L"domAutomationController.send(visible);",
230 &result)); 287 &result));
231 return result; 288 return result;
232 } 289 }
233 290
291 void VerifyControlledSettingIndicators(Browser* browser,
292 const std::string& pref,
293 const std::string& value,
294 const std::string& controlled_by,
295 bool readonly) {
296 std::wstringstream javascript;
297 javascript << "var nodes = document.querySelectorAll("
298 << " 'span.controlled-setting-indicator["
299 << " pref=" << pref.c_str() << "]');"
300 << "var indicators = [];"
301 << "for (var i = 0; i < nodes.length; i++) {"
302 << " var node = nodes[i];"
303 << " var indicator = {};"
304 << " indicator.value = node.value || '';"
305 << " indicator.controlledBy = node.controlledBy || '';"
306 << " indicator.readOnly = node.readOnly || false;"
307 << " indicator.visible ="
308 << " window.getComputedStyle(node).display != 'none';"
309 << " indicators.push(indicator)"
310 << "}"
311 << "domAutomationController.send(JSON.stringify(indicators));";
312 content::WebContents* contents = chrome::GetActiveWebContents(browser);
313 std::string json;
314 // Retrieve the state of all controlled setting indicators for |pref| as JSON.
315 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString(
Joao da Silva 2012/10/09 14:09:14 ASSERT_* will add a failure and just return from t
bartfab (slow) 2012/10/09 14:33:02 I know. This is what I want: An ASSERT_* failure s
Joao da Silva 2012/10/09 15:00:13 I see, makes sense.
316 contents->GetRenderViewHost(), L"", javascript.str(), &json));
317 scoped_ptr<base::Value> value_ptr(base::JSONReader::Read(json));
318 const base::ListValue* indicators;
Joao da Silva 2012/10/09 14:09:14 initialize to NULL
bartfab (slow) 2012/10/09 14:33:02 Done.
319 ASSERT_TRUE(value_ptr.get());
320 ASSERT_TRUE(value_ptr->GetAsList(&indicators));
321 // Verify that controlled setting indicators representing |value| are visible
322 // and have the correct state while those not representing |value| are
323 // invisible.
324 for (base::ListValue::const_iterator indicator = indicators->begin();
325 indicator != indicators->end(); ++indicator) {
326 const base::DictionaryValue* properties;
Joao da Silva 2012/10/09 14:09:14 nit: initialize to NULL
bartfab (slow) 2012/10/09 14:33:02 Done.
327 ASSERT_TRUE((*indicator)->GetAsDictionary(&properties));
328 std::string indicator_value;
329 std::string indicator_controlled_by;
330 bool indicator_readonly;
331 bool indicator_visible;
332 EXPECT_TRUE(properties->GetString("value", &indicator_value));
333 EXPECT_TRUE(properties->GetString("controlledBy",
334 &indicator_controlled_by));
335 EXPECT_TRUE(properties->GetBoolean("readOnly", &indicator_readonly));
336 EXPECT_TRUE(properties->GetBoolean("visible", &indicator_visible));
337 if (!controlled_by.empty() && (indicator_value == value)) {
338 EXPECT_EQ(controlled_by, indicator_controlled_by);
339 EXPECT_EQ(readonly, indicator_readonly);
340 EXPECT_TRUE(indicator_visible);
341 } else {
342 EXPECT_FALSE(indicator_visible);
343 }
344 }
345 }
346
234 } // namespace 347 } // namespace
235 348
236 // A class of tests parameterized by a settings page URL. 349 // A class of tests parameterized by a settings page URL.
237 class PolicyPrefsSettingsBannerTest 350 class PolicyPrefsSettingsBannerTest
238 : public InProcessBrowserTest, 351 : public InProcessBrowserTest,
239 public testing::WithParamInterface<const char*> {}; 352 public testing::WithParamInterface<const char*> {};
240 353
241 // Base class for tests that change policies. 354 // Base class for tests that change policies.
242 class PolicyBaseTest : public InProcessBrowserTest { 355 class PolicyBaseTest : public InProcessBrowserTest {
243 protected: 356 protected:
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 ui_test_utils::NavigateToURL(browser(), pages[i]); 466 ui_test_utils::NavigateToURL(browser(), pages[i]);
354 EXPECT_FALSE(IsBannerVisible(browser())); 467 EXPECT_FALSE(IsBannerVisible(browser()));
355 provider_.UpdateChromePolicy(test_case->test_policy()); 468 provider_.UpdateChromePolicy(test_case->test_policy());
356 EXPECT_TRUE(IsBannerVisible(browser())); 469 EXPECT_TRUE(IsBannerVisible(browser()));
357 const PolicyMap kNoPolicies; 470 const PolicyMap kNoPolicies;
358 provider_.UpdateChromePolicy(kNoPolicies); 471 provider_.UpdateChromePolicy(kNoPolicies);
359 EXPECT_FALSE(IsBannerVisible(browser())); 472 EXPECT_FALSE(IsBannerVisible(browser()));
360 } 473 }
361 } 474 }
362 475
476 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, CheckPolicyIndicators) {
477 // Verifies that controlled setting indicators correctly show whether a pref's
478 // value is recommended or enforced by a corresponding policy.
479 const PolicyTestCase* policy_test_case = test_cases_.Get(GetParam().name);
480 ASSERT_TRUE(policy_test_case);
481 const ScopedVector<IndicatorTestCase>& indicator_test_cases =
482 policy_test_case->indicator_test_cases();
483 if (!policy_test_case->IsSupported() || indicator_test_cases.empty())
484 return;
485 LOG(INFO) << "Testing policy: " << policy_test_case->name();
486
487 PrefService* prefs = policy_test_case->is_local_state() ?
488 g_browser_process->local_state() : browser()->profile()->GetPrefs();
489 // The preference must have been registered.
490 const PrefService::Preference* pref =
491 prefs->FindPreference(policy_test_case->pref_name());
492 ASSERT_TRUE(pref);
493 ui_test_utils::NavigateToURL(browser(), GURL(kSettingsPages[0]));
494
495 for (ScopedVector<IndicatorTestCase>::const_iterator
496 indicator_test_case = indicator_test_cases.begin();
Joao da Silva 2012/10/09 14:09:14 nit: indent (+4 spaces)
bartfab (slow) 2012/10/09 14:33:02 I originally had 4 spaces but changed them to 5 (a
Joao da Silva 2012/10/09 15:00:13 I meant 5 + 4, because the 2nd line is a continuat
bartfab (slow) 2012/10/09 15:02:24 Done.
497 indicator_test_case != indicator_test_cases.end();
498 ++indicator_test_case) {
499 // Check that no controlled setting indicator is visible when no value is
500 // set by policy.
501 PolicyMap policies;
502 provider_.UpdateChromePolicy(policies);
503 VerifyControlledSettingIndicators(browser(), policy_test_case->pref(),
504 "", "", false);
505 // Check that the appropriate controlled setting indicator is shown when a
506 // value is enforced by policy.
507 policies.LoadFrom(&(*indicator_test_case)->policy(),
508 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER);
509 provider_.UpdateChromePolicy(policies);
510 VerifyControlledSettingIndicators(browser(), policy_test_case->pref(),
511 (*indicator_test_case)->value(),
512 "policy",
513 (*indicator_test_case)->readonly());
514 if (!policy_test_case->can_be_recommended())
515 return;
516 // Check that the appropriate controlled setting indicator is shown when a
517 // value is recommended by policy and the user has not overridden the
518 // recommendation.
519 policies.LoadFrom(&(*indicator_test_case)->policy(),
520 POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER);
521 provider_.UpdateChromePolicy(policies);
522 VerifyControlledSettingIndicators(browser(), policy_test_case->pref(),
523 (*indicator_test_case)->value(),
524 "recommended",
525 (*indicator_test_case)->readonly());
526 // Check that the appropriate controlled setting indicator is shown when a
527 // value is recommended by policy and the user has overriddent the
528 // recommendation.
529 prefs->Set(policy_test_case->pref_name(), *pref->GetValue());
530 VerifyControlledSettingIndicators(browser(), policy_test_case->pref(),
531 (*indicator_test_case)->value(),
532 "hasRecommendation",
533 (*indicator_test_case)->readonly());
534 prefs->ClearPref(policy_test_case->pref_name());
535 }
536 }
537
363 INSTANTIATE_TEST_CASE_P( 538 INSTANTIATE_TEST_CASE_P(
364 PolicyPrefsTestInstance, 539 PolicyPrefsTestInstance,
365 PolicyPrefsTest, 540 PolicyPrefsTest,
366 testing::ValuesIn(GetChromePolicyDefinitionList()->begin, 541 testing::ValuesIn(GetChromePolicyDefinitionList()->begin,
367 GetChromePolicyDefinitionList()->end)); 542 GetChromePolicyDefinitionList()->end));
368 543
369 } // namespace policy 544 } // namespace policy
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/resources/options/home_page_overlay.css » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698