1// Copyright 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 "base/values.h"
6#include "chrome/browser/ui/tabs/tab_strip_model.h"
7#include "chrome/browser/ui/webui/options/options_ui_browsertest.h"
8#include "components/policy/core/browser/browser_policy_connector.h"
9#include "components/policy/core/common/external_data_fetcher.h"
10#include "components/policy/core/common/mock_configuration_policy_provider.h"
11#include "components/policy/core/common/policy_map.h"
12#include "components/policy/core/common/policy_types.h"
13#include "content/public/browser/render_frame_host.h"
14#include "content/public/browser/web_contents.h"
15#include "content/public/test/browser_test_utils.h"
16#include "content/public/test/test_utils.h"
17#include "policy/policy_constants.h"
18#include "testing/gmock/include/gmock/gmock.h"
19#include "testing/gtest/include/gtest/gtest.h"
20
21#if defined(OS_CHROMEOS)
22#include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h"
23#include "chromeos/network/onc/onc_test_utils.h"
24#endif
25
26using testing::Return;
27using testing::_;
28
29class CertificateManagerBrowserTest : public options::OptionsUIBrowserTest {
30 public:
31  CertificateManagerBrowserTest() {}
32  virtual ~CertificateManagerBrowserTest() {}
33
34 protected:
35  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
36#if defined(OS_CHROMEOS)
37    device_policy_test_helper_.MarkAsEnterpriseOwned();
38#endif
39    // Setup the policy provider for injecting certs through ONC policy.
40    EXPECT_CALL(provider_, IsInitializationComplete(_))
41        .WillRepeatedly(Return(true));
42    policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
43  }
44
45#if defined(OS_CHROMEOS)
46  void LoadONCPolicy(const std::string& filename) {
47    const std::string& user_policy_blob =
48        chromeos::onc::test_utils::ReadTestData(filename);
49    policy::PolicyMap policy;
50    policy.Set(policy::key::kOpenNetworkConfiguration,
51               policy::POLICY_LEVEL_MANDATORY,
52               policy::POLICY_SCOPE_USER,
53               new base::StringValue(user_policy_blob),
54               NULL);
55    provider_.UpdateChromePolicy(policy);
56    content::RunAllPendingInMessageLoop();
57  }
58#endif
59
60  void ClickElement(const std::string& selector) {
61    EXPECT_TRUE(content::ExecuteScript(
62        GetSettingsFrame(),
63        "document.querySelector(\"" + selector + "\").click()"));
64  }
65
66  bool HasElement(const std::string& selector) {
67    bool result;
68    EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
69        GetSettingsFrame(),
70        "window.domAutomationController.send("
71        "    !!document.querySelector('" + selector + "'));",
72        &result));
73    return result;
74  }
75
76  policy::MockConfigurationPolicyProvider provider_;
77#if defined(OS_CHROMEOS)
78  policy::DevicePolicyCrosTestHelper device_policy_test_helper_;
79#endif
80};
81
82#if defined(OS_CHROMEOS)
83// Ensure policy-installed certificates without web trust do not display
84// the managed setting indicator (only on Chrome OS).
85IN_PROC_BROWSER_TEST_F(CertificateManagerBrowserTest,
86                       PolicyCertificateWithoutWebTrustHasNoIndicator) {
87  LoadONCPolicy("certificate-authority.onc");
88  NavigateToSettings();
89  ClickElement("#certificatesManageButton");
90  ClickElement("#ca-certs-nav-tab");
91  EXPECT_FALSE(HasElement(".cert-policy"));
92}
93#endif
94
95#if defined(OS_CHROMEOS)
96// Ensure policy-installed certificates with web trust display the
97// managed setting indicator (only on Chrome OS).
98IN_PROC_BROWSER_TEST_F(CertificateManagerBrowserTest,
99                       PolicyCertificateWithWebTrustHasIndicator) {
100  LoadONCPolicy("certificate-web-authority.onc");
101  NavigateToSettings();
102  ClickElement("#certificatesManageButton");
103  ClickElement("#ca-certs-nav-tab");
104  EXPECT_TRUE(HasElement(".cert-policy"));
105}
106#endif
107