| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/command_line.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 10 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" | 13 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/extensions/test_extension_system.h" | 14 #include "chrome/browser/extensions/test_extension_system.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 17 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 18 #include "chrome/common/chrome_switches.h" |
| 17 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
| 18 #include "chrome/test/base/in_process_browser_test.h" | 20 #include "chrome/test/base/in_process_browser_test.h" |
| 19 #include "chrome/test/base/ui_test_utils.h" | 21 #include "chrome/test/base/ui_test_utils.h" |
| 20 #include "components/policy/core/browser/browser_policy_connector.h" | 22 #include "components/policy/core/browser/browser_policy_connector.h" |
| 21 #include "components/policy/core/common/external_data_fetcher.h" | 23 #include "components/policy/core/common/external_data_fetcher.h" |
| 22 #include "components/policy/core/common/mock_configuration_policy_provider.h" | 24 #include "components/policy/core/common/mock_configuration_policy_provider.h" |
| 23 #include "components/policy/core/common/policy_map.h" | 25 #include "components/policy/core/common/policy_map.h" |
| 24 #include "components/policy/core/common/policy_types.h" | 26 #include "components/policy/core/common/policy_types.h" |
| 25 #include "components/policy/core/common/schema.h" | 27 #include "components/policy/core/common/schema.h" |
| 26 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 expected_policy.push_back(l10n_util::GetStringUTF8(IDS_POLICY_UNSET)); | 83 expected_policy.push_back(l10n_util::GetStringUTF8(IDS_POLICY_UNSET)); |
| 82 | 84 |
| 83 // Populate expected expanded policy value. | 85 // Populate expected expanded policy value. |
| 84 expected_policy.push_back(value); | 86 expected_policy.push_back(value); |
| 85 | 87 |
| 86 return expected_policy; | 88 return expected_policy; |
| 87 } | 89 } |
| 88 | 90 |
| 89 } // namespace | 91 } // namespace |
| 90 | 92 |
| 91 class PolicyUITest : public InProcessBrowserTest { | 93 class PolicyUITest |
| 94 : public InProcessBrowserTest, |
| 95 public testing::WithParamInterface<bool> { |
| 92 public: | 96 public: |
| 93 PolicyUITest(); | 97 PolicyUITest(); |
| 94 ~PolicyUITest() override; | 98 ~PolicyUITest() override; |
| 95 | 99 |
| 100 // InProcessBrowserTest: |
| 101 void SetUpCommandLine(base::CommandLine* command_line) override; |
| 102 |
| 103 GURL url() const; |
| 104 |
| 96 protected: | 105 protected: |
| 97 // InProcessBrowserTest implementation. | 106 // InProcessBrowserTest implementation. |
| 98 void SetUpInProcessBrowserTestFixture() override; | 107 void SetUpInProcessBrowserTestFixture() override; |
| 99 | 108 |
| 100 void UpdateProviderPolicy(const policy::PolicyMap& policy); | 109 void UpdateProviderPolicy(const policy::PolicyMap& policy); |
| 101 | 110 |
| 102 void VerifyPolicies(const std::vector<std::vector<std::string> >& expected); | 111 void VerifyPolicies(const std::vector<std::vector<std::string> >& expected); |
| 103 | 112 |
| 104 protected: | 113 protected: |
| 105 policy::MockConfigurationPolicyProvider provider_; | 114 policy::MockConfigurationPolicyProvider provider_; |
| 106 | 115 |
| 107 private: | 116 private: |
| 117 bool has_material_design_flag_; |
| 118 |
| 108 DISALLOW_COPY_AND_ASSIGN(PolicyUITest); | 119 DISALLOW_COPY_AND_ASSIGN(PolicyUITest); |
| 109 }; | 120 }; |
| 110 | 121 |
| 111 PolicyUITest::PolicyUITest() { | 122 PolicyUITest::PolicyUITest() : has_material_design_flag_(GetParam()) { |
| 112 } | 123 } |
| 113 | 124 |
| 114 PolicyUITest::~PolicyUITest() { | 125 PolicyUITest::~PolicyUITest() { |
| 115 } | 126 } |
| 116 | 127 |
| 128 GURL PolicyUITest::url() const { |
| 129 return GURL(has_material_design_flag_ ? |
| 130 chrome::kChromeUIPolicyInternalsURL : |
| 131 chrome::kChromeUIPolicyURL); |
| 132 } |
| 133 |
| 134 void PolicyUITest::SetUpCommandLine(base::CommandLine* command_line) { |
| 135 InProcessBrowserTest::SetUpCommandLine(command_line); |
| 136 if (has_material_design_flag_) |
| 137 command_line->AppendSwitch(switches::kEnableMaterialDesignPolicyPage); |
| 138 } |
| 139 |
| 117 void PolicyUITest::SetUpInProcessBrowserTestFixture() { | 140 void PolicyUITest::SetUpInProcessBrowserTestFixture() { |
| 118 EXPECT_CALL(provider_, IsInitializationComplete(_)) | 141 EXPECT_CALL(provider_, IsInitializationComplete(_)) |
| 119 .WillRepeatedly(Return(true)); | 142 .WillRepeatedly(Return(true)); |
| 120 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); | 143 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); |
| 121 } | 144 } |
| 122 | 145 |
| 123 void PolicyUITest::UpdateProviderPolicy(const policy::PolicyMap& policy) { | 146 void PolicyUITest::UpdateProviderPolicy(const policy::PolicyMap& policy) { |
| 124 provider_.UpdateChromePolicy(policy); | 147 provider_.UpdateChromePolicy(policy); |
| 125 base::RunLoop loop; | 148 base::RunLoop loop; |
| 126 loop.RunUntilIdle(); | 149 loop.RunUntilIdle(); |
| 127 } | 150 } |
| 128 | 151 |
| 129 void PolicyUITest::VerifyPolicies( | 152 void PolicyUITest::VerifyPolicies( |
| 130 const std::vector<std::vector<std::string> >& expected_policies) { | 153 const std::vector<std::vector<std::string> >& expected_policies) { |
| 131 ui_test_utils::NavigateToURL(browser(), GURL("chrome://policy")); | 154 ui_test_utils::NavigateToURL(browser(), url()); |
| 132 | 155 |
| 133 // Retrieve the text contents of the policy table cells for all policies. | 156 // Retrieve the text contents of the policy table cells for all policies. |
| 134 const std::string javascript = | 157 const std::string javascript = |
| 135 "var entries = document.querySelectorAll(" | 158 "var entries = document.querySelectorAll(" |
| 136 " 'section.policy-table-section > * > tbody');" | 159 " 'section.policy-table-section > * > tbody');" |
| 137 "var policies = [];" | 160 "var policies = [];" |
| 138 "for (var i = 0; i < entries.length; ++i) {" | 161 "for (var i = 0; i < entries.length; ++i) {" |
| 139 " var items = entries[i].querySelectorAll('tr > td');" | 162 " var items = entries[i].querySelectorAll('tr > td');" |
| 140 " var values = [];" | 163 " var values = [];" |
| 141 " for (var j = 0; j < items.length; ++j) {" | 164 " for (var j = 0; j < items.length; ++j) {" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 169 ASSERT_TRUE(actual_policies->GetList(i, &actual_policy)); | 192 ASSERT_TRUE(actual_policies->GetList(i, &actual_policy)); |
| 170 ASSERT_EQ(expected_policy.size(), actual_policy->GetSize()); | 193 ASSERT_EQ(expected_policy.size(), actual_policy->GetSize()); |
| 171 for (size_t j = 0; j < expected_policy.size(); ++j) { | 194 for (size_t j = 0; j < expected_policy.size(); ++j) { |
| 172 std::string value; | 195 std::string value; |
| 173 ASSERT_TRUE(actual_policy->GetString(j, &value)); | 196 ASSERT_TRUE(actual_policy->GetString(j, &value)); |
| 174 EXPECT_EQ(expected_policy[j], value); | 197 EXPECT_EQ(expected_policy[j], value); |
| 175 } | 198 } |
| 176 } | 199 } |
| 177 } | 200 } |
| 178 | 201 |
| 179 IN_PROC_BROWSER_TEST_F(PolicyUITest, SendPolicyNames) { | 202 IN_PROC_BROWSER_TEST_P(PolicyUITest, SendPolicyNames) { |
| 180 // Verifies that the names of known policies are sent to the UI and processed | 203 // Verifies that the names of known policies are sent to the UI and processed |
| 181 // there correctly by checking that the policy table contains all policies in | 204 // there correctly by checking that the policy table contains all policies in |
| 182 // the correct order. | 205 // the correct order. |
| 183 | 206 |
| 184 // Expect that the policy table contains all known policies in alphabetical | 207 // Expect that the policy table contains all known policies in alphabetical |
| 185 // order and none of the policies have a set value. | 208 // order and none of the policies have a set value. |
| 186 std::vector<std::vector<std::string> > expected_policies; | 209 std::vector<std::vector<std::string> > expected_policies; |
| 187 policy::Schema chrome_schema = | 210 policy::Schema chrome_schema = |
| 188 policy::Schema::Wrap(policy::GetChromeSchemaData()); | 211 policy::Schema::Wrap(policy::GetChromeSchemaData()); |
| 189 ASSERT_TRUE(chrome_schema.valid()); | 212 ASSERT_TRUE(chrome_schema.valid()); |
| 190 for (policy::Schema::Iterator it = chrome_schema.GetPropertiesIterator(); | 213 for (policy::Schema::Iterator it = chrome_schema.GetPropertiesIterator(); |
| 191 !it.IsAtEnd(); it.Advance()) { | 214 !it.IsAtEnd(); it.Advance()) { |
| 192 expected_policies.push_back( | 215 expected_policies.push_back( |
| 193 PopulateExpectedPolicy( | 216 PopulateExpectedPolicy( |
| 194 it.key(), std::string(), std::string(), nullptr, false)); | 217 it.key(), std::string(), std::string(), nullptr, false)); |
| 195 } | 218 } |
| 196 | 219 |
| 197 // Retrieve the contents of the policy table from the UI and verify that it | 220 // Retrieve the contents of the policy table from the UI and verify that it |
| 198 // matches the expectation. | 221 // matches the expectation. |
| 199 VerifyPolicies(expected_policies); | 222 VerifyPolicies(expected_policies); |
| 200 } | 223 } |
| 201 | 224 |
| 202 IN_PROC_BROWSER_TEST_F(PolicyUITest, SendPolicyValues) { | 225 IN_PROC_BROWSER_TEST_P(PolicyUITest, SendPolicyValues) { |
| 203 // Verifies that policy values are sent to the UI and processed there | 226 // Verifies that policy values are sent to the UI and processed there |
| 204 // correctly by setting the values of four known and one unknown policy and | 227 // correctly by setting the values of four known and one unknown policy and |
| 205 // checking that the policy table contains the policy names, values and | 228 // checking that the policy table contains the policy names, values and |
| 206 // metadata in the correct order. | 229 // metadata in the correct order. |
| 207 policy::PolicyMap values; | 230 policy::PolicyMap values; |
| 208 std::map<std::string, std::string> expected_values; | 231 std::map<std::string, std::string> expected_values; |
| 209 | 232 |
| 210 // Set the values of four existing policies. | 233 // Set the values of four existing policies. |
| 211 base::ListValue* restore_on_startup_urls = new base::ListValue; | 234 base::ListValue* restore_on_startup_urls = new base::ListValue; |
| 212 restore_on_startup_urls->Append(new base::StringValue("aaa")); | 235 restore_on_startup_urls->Append(new base::StringValue("aaa")); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 expected_values[kUnknownPolicy], | 303 expected_values[kUnknownPolicy], |
| 281 "Platform", | 304 "Platform", |
| 282 values.Get(kUnknownPolicy), | 305 values.Get(kUnknownPolicy), |
| 283 true)); | 306 true)); |
| 284 | 307 |
| 285 // Retrieve the contents of the policy table from the UI and verify that it | 308 // Retrieve the contents of the policy table from the UI and verify that it |
| 286 // matches the expectation. | 309 // matches the expectation. |
| 287 VerifyPolicies(expected_policies); | 310 VerifyPolicies(expected_policies); |
| 288 } | 311 } |
| 289 | 312 |
| 290 IN_PROC_BROWSER_TEST_F(PolicyUITest, ExtensionLoadAndSendPolicy) { | 313 IN_PROC_BROWSER_TEST_P(PolicyUITest, ExtensionLoadAndSendPolicy) { |
| 291 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIPolicyURL)); | 314 ui_test_utils::NavigateToURL(browser(), url()); |
| 292 base::ScopedTempDir temp_dir_; | 315 base::ScopedTempDir temp_dir_; |
| 293 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 316 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 294 | 317 |
| 295 const std::string newly_added_policy_name = "new_policy"; | 318 const std::string newly_added_policy_name = "new_policy"; |
| 296 std::string json_data = "{\"type\": \"object\",\"properties\": {\"" + | 319 std::string json_data = "{\"type\": \"object\",\"properties\": {\"" + |
| 297 newly_added_policy_name + | 320 newly_added_policy_name + |
| 298 "\": { \"type\": \"string\"}}}"; | 321 "\": { \"type\": \"string\"}}}"; |
| 299 | 322 |
| 300 const std::string schema_file = "schema.json"; | 323 const std::string schema_file = "schema.json"; |
| 301 base::FilePath schema_path = temp_dir_.path().AppendASCII(schema_file); | 324 base::FilePath schema_path = temp_dir_.path().AppendASCII(schema_file); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 PopulateExpectedPolicy( | 356 PopulateExpectedPolicy( |
| 334 it.key(), std::string(), std::string(), nullptr, false)); | 357 it.key(), std::string(), std::string(), nullptr, false)); |
| 335 } | 358 } |
| 336 // Add newly added policy to expected policy list. | 359 // Add newly added policy to expected policy list. |
| 337 expected_policies.push_back(PopulateExpectedPolicy( | 360 expected_policies.push_back(PopulateExpectedPolicy( |
| 338 newly_added_policy_name, std::string(), std::string(), nullptr, false)); | 361 newly_added_policy_name, std::string(), std::string(), nullptr, false)); |
| 339 | 362 |
| 340 // Verify if policy UI includes policy that extension have. | 363 // Verify if policy UI includes policy that extension have. |
| 341 VerifyPolicies(expected_policies); | 364 VerifyPolicies(expected_policies); |
| 342 } | 365 } |
| 366 |
| 367 INSTANTIATE_TEST_CASE_P(FlaggedPolicyUITest, |
| 368 PolicyUITest, |
| 369 testing::Bool()); |
| OLD | NEW |