[MacViews] Show comboboxes with a native NSMenu
[chromium-blink-merge.git] / chrome / browser / extensions / api / enterprise_device_attributes / enterprise_device_attributes_apitest.cc
blobbd9a8d12c05e4d9f3325479099a5e63825b6eae3
1 // Copyright 2015 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.
5 #include "base/strings/stringprintf.h"
6 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
7 #include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h"
8 #include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h"
9 #include "chrome/browser/extensions/extension_apitest.h"
10 #include "chrome/browser/net/url_request_mock_util.h"
11 #include "chromeos/dbus/fake_session_manager_client.h"
12 #include "chromeos/login/user_names.h"
13 #include "components/policy/core/common/mock_configuration_policy_provider.h"
14 #include "content/public/browser/notification_service.h"
15 #include "content/public/test/test_utils.h"
16 #include "extensions/browser/api_test_utils.h"
17 #include "extensions/browser/extension_registry.h"
18 #include "extensions/browser/test_extension_registry_observer.h"
19 #include "net/test/url_request/url_request_mock_http_job.h"
20 #include "policy/policy_constants.h"
22 namespace {
24 const char kDeviceId[] = "device_id";
25 const base::FilePath::CharType kTestExtensionDir[] =
26 FILE_PATH_LITERAL("extensions/api_test/enterprise_device_attributes");
27 const base::FilePath::CharType kUpdateManifestFileName[] =
28 FILE_PATH_LITERAL("update_manifest.xml");
30 // The managed_storage extension has a key defined in its manifest, so that
31 // its extension ID is well-known and the policy system can push policies for
32 // the extension.
33 const char kTestExtensionID[] = "nbiliclbejdndfpchgkbmfoppjplbdok";
35 } // namespace
37 namespace extensions {
39 class EnterpriseDeviceAttributesTest : public ExtensionApiTest {
40 public:
41 explicit EnterpriseDeviceAttributesTest(const std::string& domain)
42 : fake_session_manager_client_(new chromeos::FakeSessionManagerClient),
43 test_domain_(domain) {}
45 protected:
46 void SetUpInProcessBrowserTestFixture() override {
47 chromeos::DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient(
48 make_scoped_ptr(fake_session_manager_client_));
49 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
51 // Set up fake install attributes.
52 scoped_ptr<policy::StubEnterpriseInstallAttributes> attributes(
53 new policy::StubEnterpriseInstallAttributes());
55 attributes->SetDomain(test_domain_);
56 attributes->SetRegistrationUser(chromeos::login::kStubUser);
57 policy::BrowserPolicyConnectorChromeOS::SetInstallAttributesForTesting(
58 attributes.release());
60 test_helper_.InstallOwnerKey();
61 // Init the device policy.
62 policy::DevicePolicyBuilder* device_policy = test_helper_.device_policy();
63 device_policy->SetDefaultSigningKey();
64 device_policy->policy_data().set_directory_api_id(kDeviceId);
65 device_policy->Build();
67 fake_session_manager_client_->set_device_policy(device_policy->GetBlob());
68 fake_session_manager_client_->OnPropertyChangeComplete(true);
70 // Init the user policy provider.
71 EXPECT_CALL(policy_provider_, IsInitializationComplete(testing::_))
72 .WillRepeatedly(testing::Return(true));
73 policy_provider_.SetAutoRefresh();
74 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(
75 &policy_provider_);
78 void SetUpOnMainThread() override {
79 ExtensionApiTest::SetUpOnMainThread();
81 // Enable the URLRequestMock, which is required for force-installing the
82 // test extension through policy.
83 content::BrowserThread::PostTask(
84 content::BrowserThread::IO, FROM_HERE,
85 base::Bind(chrome_browser_net::SetUrlRequestMocksEnabled, true));
87 SetPolicy();
90 private:
91 void SetPolicy() {
92 // Extensions that are force-installed come from an update URL, which
93 // defaults to the webstore. Use a mock URL for this test with an update
94 // manifest that includes the crx file of the test extension.
95 base::FilePath update_manifest_path =
96 base::FilePath(kTestExtensionDir).Append(kUpdateManifestFileName);
97 GURL update_manifest_url(
98 net::URLRequestMockHTTPJob::GetMockUrl(update_manifest_path));
100 scoped_ptr<base::ListValue> forcelist(new base::ListValue);
101 forcelist->AppendString(base::StringPrintf(
102 "%s;%s", kTestExtensionID, update_manifest_url.spec().c_str()));
104 policy::PolicyMap policy;
105 policy.Set(policy::key::kExtensionInstallForcelist,
106 policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_MACHINE,
107 forcelist.release(), NULL);
109 // Set the policy and wait until the extension is installed.
110 extensions::TestExtensionRegistryObserver observer(
111 ExtensionRegistry::Get(profile()));
112 policy_provider_.UpdateChromePolicy(policy);
113 observer.WaitForExtensionLoaded();
116 chromeos::FakeSessionManagerClient* const fake_session_manager_client_;
117 policy::MockConfigurationPolicyProvider policy_provider_;
118 policy::DevicePolicyCrosTestHelper test_helper_;
119 const std::string test_domain_;
122 // Creates affiliated user before browser initializes.
123 class EnterpriseDeviceAttributesAffiliatedTest
124 : public EnterpriseDeviceAttributesTest {
125 public:
126 EnterpriseDeviceAttributesAffiliatedTest()
127 : EnterpriseDeviceAttributesTest("gmail.com") {}
130 // Creates non-affiliated user before browser init.
131 class EnterpriseDeviceAttributesNonAffiliatedTest
132 : public EnterpriseDeviceAttributesTest {
133 public:
134 EnterpriseDeviceAttributesNonAffiliatedTest()
135 : EnterpriseDeviceAttributesTest("example.com") {}
138 // Tests the case of an affiliated user and pre-installed extension. Fetches
139 // the valid cloud directory device id.
140 IN_PROC_BROWSER_TEST_F(EnterpriseDeviceAttributesAffiliatedTest, Success) {
141 // Pass the expected value (device_id) to test.
142 ASSERT_TRUE(RunExtensionSubtest(
143 "", base::StringPrintf("chrome-extension://%s/basic.html?%s",
144 kTestExtensionID, kDeviceId)))
145 << message_;
148 // Test the case of non-affiliated user and pre-installed by policy extension.
149 // Extension API is available, but fetches the empty string.
150 IN_PROC_BROWSER_TEST_F(EnterpriseDeviceAttributesNonAffiliatedTest,
151 EmptyString) {
152 // Pass the expected value (empty string) to test.
153 ASSERT_TRUE(RunExtensionSubtest(
154 "", base::StringPrintf("chrome-extension://%s/basic.html?%s",
155 kTestExtensionID, "")))
156 << message_;
159 // Ensure that extensions that are not pre-installed by policy throw an install
160 // warning if they request the enterprise.deviceAttributes permission in the
161 // manifest and that such extensions don't see the
162 // chrome.enterprise.deviceAttributes namespace.
163 IN_PROC_BROWSER_TEST_F(
164 ExtensionApiTest,
165 EnterpriseDeviceAttributesIsRestrictedToPolicyExtension) {
166 ASSERT_TRUE(RunExtensionSubtest("enterprise_device_attributes",
167 "api_not_available.html",
168 kFlagIgnoreManifestWarnings));
170 base::FilePath extension_path =
171 test_data_dir_.AppendASCII("enterprise_device_attributes");
172 extensions::ExtensionRegistry* registry =
173 extensions::ExtensionRegistry::Get(profile());
174 const extensions::Extension* extension =
175 GetExtensionByPath(registry->enabled_extensions(), extension_path);
176 ASSERT_FALSE(extension->install_warnings().empty());
177 EXPECT_EQ(
178 "'enterprise.deviceAttributes' is not allowed for specified install "
179 "location.",
180 extension->install_warnings()[0].message);
183 } // namespace extensions