| 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 <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 #include "base/values.h" | 16 #include "base/values.h" |
| 17 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
| 18 #include "chrome/browser/policy/browser_policy_connector.h" | 18 #include "chrome/browser/policy/browser_policy_connector.h" |
| 19 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 19 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 20 #include "chrome/browser/policy/policy_map.h" | 20 #include "chrome/browser/policy/policy_map.h" |
| 21 #include "chrome/browser/prefs/pref_service.h" | 21 #include "chrome/browser/prefs/pref_service.h" |
| 22 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
| 23 #include "chrome/browser/ui/browser.h" | 23 #include "chrome/browser/ui/browser.h" |
| 24 #include "chrome/browser/ui/browser_tabstrip.h" |
| 24 #include "chrome/test/base/in_process_browser_test.h" | 25 #include "chrome/test/base/in_process_browser_test.h" |
| 25 #include "chrome/test/base/ui_test_utils.h" | 26 #include "chrome/test/base/ui_test_utils.h" |
| 27 #include "content/public/browser/web_contents.h" |
| 28 #include "content/public/test/browser_test_utils.h" |
| 26 #include "googleurl/src/gurl.h" | 29 #include "googleurl/src/gurl.h" |
| 27 #include "policy/policy_constants.h" | 30 #include "policy/policy_constants.h" |
| 28 #include "testing/gmock/include/gmock/gmock.h" | 31 #include "testing/gmock/include/gmock/gmock.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 30 | 33 |
| 31 #if defined(OS_MACOSX) | 34 #if defined(OS_MACOSX) |
| 32 #include "base/base_paths.h" | 35 #include "base/base_paths.h" |
| 33 #include "base/mac/foundation_util.h" | 36 #include "base/mac/foundation_util.h" |
| 34 #include "base/path_service.h" | 37 #include "base/path_service.h" |
| 35 #include "chrome/common/chrome_constants.h" | 38 #include "chrome/common/chrome_constants.h" |
| 36 #endif | 39 #endif |
| 37 | 40 |
| 38 using testing::Return; | 41 using testing::Return; |
| 39 | 42 |
| 40 namespace policy { | 43 namespace policy { |
| 41 | 44 |
| 42 namespace { | 45 namespace { |
| 43 | 46 |
| 47 const char* kSettingsPages[] = { |
| 48 "chrome://settings-frame", |
| 49 "chrome://settings-frame/searchEngines", |
| 50 "chrome://settings-frame/passwords", |
| 51 "chrome://settings-frame/autofill", |
| 52 "chrome://settings-frame/content", |
| 53 "chrome://settings-frame/homePageOverlay", |
| 54 "chrome://settings-frame/languages", |
| 55 #if defined(OS_CHROMEOS) |
| 56 "chrome://settings-frame/accounts", |
| 57 #endif |
| 58 }; |
| 59 |
| 44 // Contains the testing details for a single policy, loaded from | 60 // Contains the testing details for a single policy, loaded from |
| 45 // chrome/test/data/policy/policy_test_cases.json. | 61 // chrome/test/data/policy/policy_test_cases.json. |
| 46 class PolicyTestCase { | 62 class PolicyTestCase { |
| 47 public: | 63 public: |
| 48 explicit PolicyTestCase(const std::string& name) | 64 explicit PolicyTestCase(const std::string& name) |
| 49 : name_(name), | 65 : name_(name), |
| 50 is_local_state_(false), | 66 is_local_state_(false), |
| 51 official_only_(false) {} | 67 official_only_(false) {} |
| 52 ~PolicyTestCase() {} | 68 ~PolicyTestCase() {} |
| 53 | 69 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 #endif | 147 #endif |
| 132 | 148 |
| 133 FilePath path = ui_test_utils::GetTestFilePath( | 149 FilePath path = ui_test_utils::GetTestFilePath( |
| 134 FilePath(FILE_PATH_LITERAL("policy")), | 150 FilePath(FILE_PATH_LITERAL("policy")), |
| 135 FilePath(FILE_PATH_LITERAL("policy_test_cases.json"))); | 151 FilePath(FILE_PATH_LITERAL("policy_test_cases.json"))); |
| 136 std::string json; | 152 std::string json; |
| 137 ASSERT_TRUE(file_util::ReadFileToString(path, &json)); | 153 ASSERT_TRUE(file_util::ReadFileToString(path, &json)); |
| 138 int error_code = -1; | 154 int error_code = -1; |
| 139 std::string error_string; | 155 std::string error_string; |
| 140 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( | 156 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( |
| 141 json, base::JSON_ALLOW_TRAILING_COMMAS, &error_code, &error_string)); | 157 json, base::JSON_PARSE_RFC, &error_code, &error_string)); |
| 142 ASSERT_TRUE(value.get()) | 158 ASSERT_TRUE(value.get()) |
| 143 << "Error parsing policy_test_cases.json: " << error_string; | 159 << "Error parsing policy_test_cases.json: " << error_string; |
| 144 base::DictionaryValue* dict = NULL; | 160 base::DictionaryValue* dict = NULL; |
| 145 ASSERT_TRUE(value->GetAsDictionary(&dict)); | 161 ASSERT_TRUE(value->GetAsDictionary(&dict)); |
| 146 policy_test_cases_ = new std::map<std::string, PolicyTestCase*>(); | 162 policy_test_cases_ = new std::map<std::string, PolicyTestCase*>(); |
| 147 const PolicyDefinitionList* list = GetChromePolicyDefinitionList(); | 163 const PolicyDefinitionList* list = GetChromePolicyDefinitionList(); |
| 148 for (const PolicyDefinitionList::Entry* policy = list->begin; | 164 for (const PolicyDefinitionList::Entry* policy = list->begin; |
| 149 policy != list->end; ++policy) { | 165 policy != list->end; ++policy) { |
| 150 PolicyTestCase* test_case = GetTestCase(dict, policy->name); | 166 PolicyTestCase* test_case = GetTestCase(dict, policy->name); |
| 151 if (test_case) | 167 if (test_case) |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 test_case->set_official_only(flag); | 217 test_case->set_official_only(flag); |
| 202 return test_case; | 218 return test_case; |
| 203 } | 219 } |
| 204 | 220 |
| 205 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | 221 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { |
| 206 EXPECT_CALL(provider_, IsInitializationComplete()) | 222 EXPECT_CALL(provider_, IsInitializationComplete()) |
| 207 .WillRepeatedly(Return(true)); | 223 .WillRepeatedly(Return(true)); |
| 208 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); | 224 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); |
| 209 } | 225 } |
| 210 | 226 |
| 227 bool IsBannerVisible() { |
| 228 content::WebContents* contents = chrome::GetActiveWebContents(browser()); |
| 229 bool result = false; |
| 230 EXPECT_TRUE(content::ExecuteJavaScriptAndExtractBool( |
| 231 contents->GetRenderViewHost(), |
| 232 std::wstring(), |
| 233 L"var visible = false;" |
| 234 L"var banners = document.querySelectorAll('.page-banner');" |
| 235 L"for (var i = 0; i < banners.length; i++) {" |
| 236 L" if (banners[i].parentElement.id == 'templates')" |
| 237 L" continue;" |
| 238 L" if (window.getComputedStyle(banners[i]).display != 'none')" |
| 239 L" visible = true;" |
| 240 L"}" |
| 241 L"domAutomationController.send(visible);", |
| 242 &result)); |
| 243 return result; |
| 244 } |
| 245 |
| 211 static std::map<std::string, PolicyTestCase*>* policy_test_cases_; | 246 static std::map<std::string, PolicyTestCase*>* policy_test_cases_; |
| 212 MockConfigurationPolicyProvider provider_; | 247 MockConfigurationPolicyProvider provider_; |
| 213 }; | 248 }; |
| 214 | 249 |
| 215 std::map<std::string, PolicyTestCase*>* PolicyPrefsTest::policy_test_cases_ = 0; | 250 std::map<std::string, PolicyTestCase*>* PolicyPrefsTest::policy_test_cases_ = 0; |
| 216 | 251 |
| 217 IN_PROC_BROWSER_TEST_F(PolicyPrefsTest, AllPoliciesHaveATestCase) { | 252 IN_PROC_BROWSER_TEST_F(PolicyPrefsTest, AllPoliciesHaveATestCase) { |
| 218 // Verifies that all known policies have a test case in the JSON file. | 253 // Verifies that all known policies have a test case in the JSON file. |
| 219 // This test fails when a policy is added to | 254 // This test fails when a policy is added to |
| 220 // chrome/app/policy/policy_templates.json but a test case is not added to | 255 // chrome/app/policy/policy_templates.json but a test case is not added to |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 EXPECT_FALSE(pref->IsManaged()); | 293 EXPECT_FALSE(pref->IsManaged()); |
| 259 | 294 |
| 260 provider_.UpdateChromePolicy(test_case->test_policy()); | 295 provider_.UpdateChromePolicy(test_case->test_policy()); |
| 261 EXPECT_FALSE(pref->IsDefaultValue()); | 296 EXPECT_FALSE(pref->IsDefaultValue()); |
| 262 EXPECT_FALSE(pref->IsUserModifiable()); | 297 EXPECT_FALSE(pref->IsUserModifiable()); |
| 263 EXPECT_FALSE(pref->IsUserControlled()); | 298 EXPECT_FALSE(pref->IsUserControlled()); |
| 264 EXPECT_TRUE(pref->IsManaged()); | 299 EXPECT_TRUE(pref->IsManaged()); |
| 265 } | 300 } |
| 266 } | 301 } |
| 267 | 302 |
| 303 IN_PROC_BROWSER_TEST_F(PolicyPrefsTest, NoPoliciesNoBanner) { |
| 304 // Verifies that the banner isn't shown in the settings UI when no policies |
| 305 // are set. |
| 306 for (size_t i = 0; i < arraysize(kSettingsPages); ++i) { |
| 307 ui_test_utils::NavigateToURL(browser(), GURL(kSettingsPages[i])); |
| 308 EXPECT_FALSE(IsBannerVisible()); |
| 309 } |
| 310 } |
| 311 |
| 312 IN_PROC_BROWSER_TEST_F(PolicyPrefsTest, TogglePolicyTogglesBanner) { |
| 313 // Verifies that the banner appears and disappears as policies are added and |
| 314 // removed. |
| 315 // |test_case| is just a particular policy that should trigger the banner |
| 316 // on the main settings page. |
| 317 PolicyTestCase* test_case = (*policy_test_cases_)["ShowHomeButton"]; |
| 318 ASSERT_TRUE(test_case); |
| 319 // No banner by default. |
| 320 ui_test_utils::NavigateToURL(browser(), GURL(kSettingsPages[0])); |
| 321 EXPECT_FALSE(IsBannerVisible()); |
| 322 // Adding a policy makes the banner show up. |
| 323 provider_.UpdateChromePolicy(test_case->test_policy()); |
| 324 EXPECT_TRUE(IsBannerVisible()); |
| 325 // And removing it makes the banner go away. |
| 326 const PolicyMap kNoPolicies; |
| 327 provider_.UpdateChromePolicy(kNoPolicies); |
| 328 EXPECT_FALSE(IsBannerVisible()); |
| 329 // Do it again, just in case. |
| 330 provider_.UpdateChromePolicy(test_case->test_policy()); |
| 331 EXPECT_TRUE(IsBannerVisible()); |
| 332 provider_.UpdateChromePolicy(kNoPolicies); |
| 333 EXPECT_FALSE(IsBannerVisible()); |
| 334 } |
| 335 |
| 336 IN_PROC_BROWSER_TEST_F(PolicyPrefsTest, CheckAllPoliciesThatShowTheBanner) { |
| 337 // Verifies that the banner appears for each policy that affects a control |
| 338 // in the settings UI. |
| 339 const PolicyMap kNoPolicies; |
| 340 std::map<std::string, PolicyTestCase*>::iterator it; |
| 341 for (it = policy_test_cases_->begin(); |
| 342 it != policy_test_cases_->end(); ++it) { |
| 343 PolicyTestCase* test_case = it->second; |
| 344 if (!test_case->IsSupported() || test_case->settings_pages().empty()) |
| 345 continue; |
| 346 LOG(INFO) << "Testing policy: " << test_case->name(); |
| 347 |
| 348 const std::vector<GURL>& pages = test_case->settings_pages(); |
| 349 for (size_t i = 0; i < pages.size(); ++i) { |
| 350 ui_test_utils::NavigateToURL(browser(), pages[i]); |
| 351 EXPECT_FALSE(IsBannerVisible()); |
| 352 provider_.UpdateChromePolicy(test_case->test_policy()); |
| 353 EXPECT_TRUE(IsBannerVisible()); |
| 354 provider_.UpdateChromePolicy(kNoPolicies); |
| 355 EXPECT_FALSE(IsBannerVisible()); |
| 356 } |
| 357 } |
| 358 } |
| 359 |
| 268 } // namespace policy | 360 } // namespace policy |
| OLD | NEW |