Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / policy_ui_browsertest.cc
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
3 // found in the LICENSE file.
4
5 #include <vector>
6
7 #include "base/callback.h"
8 #include "base/json/json_reader.h"
9 #include "base/run_loop.h"
10 #include "base/values.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/test/base/in_process_browser_test.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "components/policy/core/browser/browser_policy_connector.h"
16 #include "components/policy/core/common/external_data_fetcher.h"
17 #include "components/policy/core/common/mock_configuration_policy_provider.h"
18 #include "components/policy/core/common/policy_map.h"
19 #include "components/policy/core/common/policy_types.h"
20 #include "components/policy/core/common/schema.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/test/browser_test_utils.h"
23 #include "grit/components_strings.h"
24 #include "policy/policy_constants.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "ui/base/l10n/l10n_util.h"
28 #include "url/gurl.h"
29
30 using testing::Return;
31 using testing::_;
32
33 namespace {
34
35 std::vector<std::string> PopulateExpectedPolicy(
36     const std::string& name,
37     const std::string& value,
38     const policy::PolicyMap::Entry* metadata,
39     bool unknown) {
40   std::vector<std::string> expected_policy;
41
42   // Populate expected scope.
43   if (metadata) {
44     expected_policy.push_back(l10n_util::GetStringUTF8(
45         metadata->scope == policy::POLICY_SCOPE_MACHINE ?
46             IDS_POLICY_SCOPE_DEVICE : IDS_POLICY_SCOPE_USER));
47   } else {
48     expected_policy.push_back(std::string());
49   }
50
51   // Populate expected level.
52   if (metadata) {
53     expected_policy.push_back(l10n_util::GetStringUTF8(
54         metadata->level == policy::POLICY_LEVEL_RECOMMENDED ?
55             IDS_POLICY_LEVEL_RECOMMENDED : IDS_POLICY_LEVEL_MANDATORY));
56   } else {
57     expected_policy.push_back(std::string());
58   }
59
60   // Populate expected policy name.
61   expected_policy.push_back(name);
62
63   // Populate expected policy value.
64   expected_policy.push_back(value);
65
66   // Populate expected status.
67   if (unknown)
68     expected_policy.push_back(l10n_util::GetStringUTF8(IDS_POLICY_UNKNOWN));
69   else if (metadata)
70     expected_policy.push_back(l10n_util::GetStringUTF8(IDS_POLICY_OK));
71   else
72     expected_policy.push_back(l10n_util::GetStringUTF8(IDS_POLICY_UNSET));
73
74   // Populate expected expanded policy value.
75   expected_policy.push_back(value);
76
77   return expected_policy;
78 }
79
80 }  // namespace
81
82 class PolicyUITest : public InProcessBrowserTest {
83  public:
84   PolicyUITest();
85   ~PolicyUITest() override;
86
87  protected:
88   // InProcessBrowserTest implementation.
89   void SetUpInProcessBrowserTestFixture() override;
90
91   void UpdateProviderPolicy(const policy::PolicyMap& policy);
92
93   void VerifyPolicies(const std::vector<std::vector<std::string> >& expected);
94
95  private:
96   policy::MockConfigurationPolicyProvider provider_;
97
98   DISALLOW_COPY_AND_ASSIGN(PolicyUITest);
99 };
100
101 PolicyUITest::PolicyUITest() {
102 }
103
104 PolicyUITest::~PolicyUITest() {
105 }
106
107 void PolicyUITest::SetUpInProcessBrowserTestFixture() {
108   EXPECT_CALL(provider_, IsInitializationComplete(_))
109       .WillRepeatedly(Return(true));
110   policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
111 }
112
113 void PolicyUITest::UpdateProviderPolicy(const policy::PolicyMap& policy) {
114  provider_.UpdateChromePolicy(policy);
115  base::RunLoop loop;
116  loop.RunUntilIdle();
117 }
118
119 void PolicyUITest::VerifyPolicies(
120     const std::vector<std::vector<std::string> >& expected_policies) {
121   ui_test_utils::NavigateToURL(browser(), GURL("chrome://policy"));
122
123   // Retrieve the text contents of the policy table cells for all policies.
124   const std::string javascript =
125       "var entries = document.querySelectorAll("
126       "    'section.policy-table-section > * > tbody');"
127       "var policies = [];"
128       "for (var i = 0; i < entries.length; ++i) {"
129       "  var items = entries[i].querySelectorAll('tr > td');"
130       "  var values = [];"
131       "  for (var j = 0; j < items.length; ++j) {"
132       "    var item = items[j];"
133       "    var children = item.getElementsByTagName('div');"
134       "    if (children.length == 1)"
135       "      item = children[0];"
136       "    children = item.getElementsByTagName('span');"
137       "    if (children.length == 1)"
138       "      item = children[0];"
139       "    values.push(item.textContent);"
140       "  }"
141       "  policies.push(values);"
142       "}"
143       "domAutomationController.send(JSON.stringify(policies));";
144   content::WebContents* contents =
145       browser()->tab_strip_model()->GetActiveWebContents();
146   std::string json;
147   ASSERT_TRUE(content::ExecuteScriptAndExtractString(contents, javascript,
148                                                      &json));
149   scoped_ptr<base::Value> value_ptr(base::JSONReader::Read(json));
150   const base::ListValue* actual_policies = NULL;
151   ASSERT_TRUE(value_ptr.get());
152   ASSERT_TRUE(value_ptr->GetAsList(&actual_policies));
153
154   // Verify that the cells contain the expected strings for all policies.
155   ASSERT_EQ(expected_policies.size(), actual_policies->GetSize());
156   for (size_t i = 0; i < expected_policies.size(); ++i) {
157     const std::vector<std::string> expected_policy = expected_policies[i];
158     const base::ListValue* actual_policy;
159     ASSERT_TRUE(actual_policies->GetList(i, &actual_policy));
160     ASSERT_EQ(expected_policy.size(), actual_policy->GetSize());
161     for (size_t j = 0; j < expected_policy.size(); ++j) {
162       std::string value;
163       ASSERT_TRUE(actual_policy->GetString(j, &value));
164       EXPECT_EQ(expected_policy[j], value);
165     }
166   }
167 }
168
169 IN_PROC_BROWSER_TEST_F(PolicyUITest, SendPolicyNames) {
170   // Verifies that the names of known policies are sent to the UI and processed
171   // there correctly by checking that the policy table contains all policies in
172   // the correct order.
173
174   // Expect that the policy table contains all known policies in alphabetical
175   // order and none of the policies have a set value.
176   std::vector<std::vector<std::string> > expected_policies;
177   policy::Schema chrome_schema =
178       policy::Schema::Wrap(policy::GetChromeSchemaData());
179   ASSERT_TRUE(chrome_schema.valid());
180   for (policy::Schema::Iterator it = chrome_schema.GetPropertiesIterator();
181        !it.IsAtEnd(); it.Advance()) {
182     expected_policies.push_back(
183         PopulateExpectedPolicy(it.key(), std::string(), NULL, false));
184   }
185
186   // Retrieve the contents of the policy table from the UI and verify that it
187   // matches the expectation.
188   VerifyPolicies(expected_policies);
189 }
190
191 IN_PROC_BROWSER_TEST_F(PolicyUITest, SendPolicyValues) {
192   // Verifies that policy values are sent to the UI and processed there
193   // correctly by setting the values of four known and one unknown policy and
194   // checking that the policy table contains the policy names, values and
195   // metadata in the correct order.
196   policy::PolicyMap values;
197   std::map<std::string, std::string> expected_values;
198
199   // Set the values of four existing policies.
200   base::ListValue* restore_on_startup_urls = new base::ListValue;
201   restore_on_startup_urls->Append(new base::StringValue("aaa"));
202   restore_on_startup_urls->Append(new base::StringValue("bbb"));
203   restore_on_startup_urls->Append(new base::StringValue("ccc"));
204   values.Set(policy::key::kRestoreOnStartupURLs,
205              policy::POLICY_LEVEL_MANDATORY,
206              policy::POLICY_SCOPE_USER,
207              restore_on_startup_urls,
208              NULL);
209   expected_values[policy::key::kRestoreOnStartupURLs] = "aaa,bbb,ccc";
210   values.Set(policy::key::kHomepageLocation,
211              policy::POLICY_LEVEL_MANDATORY,
212              policy::POLICY_SCOPE_MACHINE,
213              new base::StringValue("http://google.com"),
214              NULL);
215   expected_values[policy::key::kHomepageLocation] = "http://google.com";
216   values.Set(policy::key::kRestoreOnStartup,
217              policy::POLICY_LEVEL_RECOMMENDED,
218              policy::POLICY_SCOPE_USER,
219              new base::FundamentalValue(4),
220              NULL);
221   expected_values[policy::key::kRestoreOnStartup] = "4";
222   values.Set(policy::key::kShowHomeButton,
223              policy::POLICY_LEVEL_RECOMMENDED,
224              policy::POLICY_SCOPE_MACHINE,
225              new base::FundamentalValue(true),
226              NULL);
227   expected_values[policy::key::kShowHomeButton] = "true";
228   // Set the value of a policy that does not exist.
229   const std::string kUnknownPolicy = "NoSuchThing";
230   values.Set(kUnknownPolicy,
231              policy::POLICY_LEVEL_MANDATORY,
232              policy::POLICY_SCOPE_USER,
233              new base::FundamentalValue(true),
234              NULL);
235   expected_values[kUnknownPolicy] = "true";
236   UpdateProviderPolicy(values);
237
238   // Expect that the policy table contains, in order:
239   // * All known policies whose value has been set, in alphabetical order.
240   // * The unknown policy.
241   // * All known policies whose value has not been set, in alphabetical order.
242   std::vector<std::vector<std::string> > expected_policies;
243   size_t first_unset_position = 0;
244   policy::Schema chrome_schema =
245       policy::Schema::Wrap(policy::GetChromeSchemaData());
246   ASSERT_TRUE(chrome_schema.valid());
247   for (policy::Schema::Iterator props = chrome_schema.GetPropertiesIterator();
248        !props.IsAtEnd(); props.Advance()) {
249     std::map<std::string, std::string>::const_iterator it =
250         expected_values.find(props.key());
251     const std::string value =
252         it == expected_values.end() ? std::string() : it->second;
253     const policy::PolicyMap::Entry* metadata = values.Get(props.key());
254     expected_policies.insert(
255         metadata ? expected_policies.begin() + first_unset_position++ :
256                    expected_policies.end(),
257         PopulateExpectedPolicy(props.key(), value, metadata, false));
258   }
259   expected_policies.insert(
260       expected_policies.begin() + first_unset_position++,
261       PopulateExpectedPolicy(kUnknownPolicy,
262                              expected_values[kUnknownPolicy],
263                              values.Get(kUnknownPolicy),
264                              true));
265
266   // Retrieve the contents of the policy table from the UI and verify that it
267   // matches the expectation.
268   VerifyPolicies(expected_policies);
269 }