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/policy/browser_policy_connector.h"
12#include "chrome/browser/policy/external_data_fetcher.h"
13#include "chrome/browser/policy/mock_configuration_policy_provider.h"
14#include "chrome/browser/policy/policy_map.h"
15#include "chrome/browser/policy/policy_types.h"
16#include "chrome/browser/ui/browser.h"
17#include "chrome/browser/ui/tabs/tab_strip_model.h"
18#include "chrome/test/base/in_process_browser_test.h"
19#include "chrome/test/base/ui_test_utils.h"
20#include "content/public/browser/web_contents.h"
21#include "content/public/test/browser_test_utils.h"
22#include "grit/generated_resources.h"
23#include "policy/policy_constants.h"
24#include "testing/gmock/include/gmock/gmock.h"
25#include "testing/gtest/include/gtest/gtest.h"
26#include "ui/base/l10n/l10n_util.h"
27#include "url/gurl.h"
28
29using testing::AnyNumber;
30using testing::Return;
31using testing::_;
32
33namespace {
34
35std::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
82class PolicyUITest : public InProcessBrowserTest {
83 public:
84  PolicyUITest();
85  virtual ~PolicyUITest();
86
87 protected:
88  // InProcessBrowserTest implementation.
89  virtual 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
101PolicyUITest::PolicyUITest() {
102}
103
104PolicyUITest::~PolicyUITest() {
105}
106
107void PolicyUITest::SetUpInProcessBrowserTestFixture() {
108  EXPECT_CALL(provider_, IsInitializationComplete(_))
109      .WillRepeatedly(Return(true));
110  EXPECT_CALL(provider_, RegisterPolicyDomain(_)).Times(AnyNumber());
111  policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
112}
113
114void PolicyUITest::UpdateProviderPolicy(const policy::PolicyMap& policy) {
115 provider_.UpdateChromePolicy(policy);
116 base::RunLoop loop;
117 loop.RunUntilIdle();
118}
119
120void PolicyUITest::VerifyPolicies(
121    const std::vector<std::vector<std::string> > expected_policies) {
122  ui_test_utils::NavigateToURL(browser(), GURL("chrome://policy"));
123
124  // Retrieve the text contents of the policy table cells for all policies.
125  const std::string javascript =
126      "var entries = document.querySelectorAll("
127      "    'section.policy-table-section > * > tbody');"
128      "var policies = [];"
129      "for (var i = 0; i < entries.length; ++i) {"
130      "  var items = entries[i].querySelectorAll('tr > td');"
131      "  var values = [];"
132      "  for (var j = 0; j < items.length; ++j) {"
133      "    var item = items[j];"
134      "    var children = item.getElementsByTagName('div');"
135      "    if (children.length == 1)"
136      "      item = children[0];"
137      "    children = item.getElementsByTagName('span');"
138      "    if (children.length == 1)"
139      "      item = children[0];"
140      "    values.push(item.textContent);"
141      "  }"
142      "  policies.push(values);"
143      "}"
144      "domAutomationController.send(JSON.stringify(policies));";
145  content::WebContents* contents =
146      browser()->tab_strip_model()->GetActiveWebContents();
147  std::string json;
148  ASSERT_TRUE(content::ExecuteScriptAndExtractString(contents, javascript,
149                                                     &json));
150  scoped_ptr<base::Value> value_ptr(base::JSONReader::Read(json));
151  const base::ListValue* actual_policies = NULL;
152  ASSERT_TRUE(value_ptr.get());
153  ASSERT_TRUE(value_ptr->GetAsList(&actual_policies));
154
155  // Verify that the cells contain the expected strings for all policies.
156  ASSERT_EQ(expected_policies.size(), actual_policies->GetSize());
157  for (size_t i = 0; i < expected_policies.size(); ++i) {
158    const std::vector<std::string> expected_policy = expected_policies[i];
159    const base::ListValue* actual_policy;
160    ASSERT_TRUE(actual_policies->GetList(i, &actual_policy));
161    ASSERT_EQ(expected_policy.size(), actual_policy->GetSize());
162    for (size_t j = 0; j < expected_policy.size(); ++j) {
163      std::string value;
164      ASSERT_TRUE(actual_policy->GetString(j, &value));
165      EXPECT_EQ(expected_policy[j], value);
166    }
167  }
168}
169
170IN_PROC_BROWSER_TEST_F(PolicyUITest, SendPolicyNames) {
171  // Verifies that the names of known policies are sent to the UI and processed
172  // there correctly by checking that the policy table contains all policies in
173  // the correct order.
174
175  // Expect that the policy table contains all known policies in alphabetical
176  // order and none of the policies have a set value.
177  std::vector<std::vector<std::string> > expected_policies;
178  const policy::PolicyDefinitionList* policies =
179      policy::GetChromePolicyDefinitionList();
180  for (const policy::PolicyDefinitionList::Entry* policy = policies->begin;
181      policy != policies->end; ++policy) {
182    expected_policies.push_back(
183        PopulateExpectedPolicy(policy->name, 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
191IN_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(base::Value::CreateStringValue("aaa"));
202  restore_on_startup_urls->Append(base::Value::CreateStringValue("bbb"));
203  restore_on_startup_urls->Append(base::Value::CreateStringValue("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             base::Value::CreateStringValue("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             base::Value::CreateIntegerValue(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             base::Value::CreateBooleanValue(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             base::Value::CreateBooleanValue(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  const policy::PolicyDefinitionList* policies =
245      policy::GetChromePolicyDefinitionList();
246  for (const policy::PolicyDefinitionList::Entry* policy = policies->begin;
247       policy != policies->end; ++policy) {
248    std::map<std::string, std::string>::const_iterator it =
249        expected_values.find(policy->name);
250    const std::string value =
251        it == expected_values.end() ? std::string() : it->second;
252    const policy::PolicyMap::Entry* metadata = values.Get(policy->name);
253    expected_policies.insert(
254        metadata ? expected_policies.begin() + first_unset_position++ :
255                   expected_policies.end(),
256        PopulateExpectedPolicy(policy->name, value, metadata, false));
257  }
258  expected_policies.insert(
259      expected_policies.begin() + first_unset_position++,
260      PopulateExpectedPolicy(kUnknownPolicy,
261                             expected_values[kUnknownPolicy],
262                             values.Get(kUnknownPolicy),
263                             true));
264
265  // Retrieve the contents of the policy table from the UI and verify that it
266  // matches the expectation.
267  VerifyPolicies(expected_policies);
268}
269