Chromium Code Reviews| Index: chrome/browser/extensions/api/enterprise_device_attributes/enterprise_device_attributes_apitest.cc |
| diff --git a/chrome/browser/extensions/api/enterprise_device_attributes/enterprise_device_attributes_apitest.cc b/chrome/browser/extensions/api/enterprise_device_attributes/enterprise_device_attributes_apitest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9c94954e6d5e2b326c3a4e6a07cd924115866d38 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/enterprise_device_attributes/enterprise_device_attributes_apitest.cc |
| @@ -0,0 +1,110 @@ |
| +// Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" |
| +#include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h" |
| +#include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h" |
| +#include "chrome/browser/extensions/api/enterprise_device_attributes/enterprise_device_attributes_api.h" |
| +#include "chrome/browser/extensions/extension_apitest.h" |
| +#include "chromeos/dbus/fake_session_manager_client.h" |
| +#include "chromeos/login/user_names.h" |
| +#include "extensions/browser/api_test_utils.h" |
| +#include "extensions/common/manifest_constants.h" |
|
pneubeck (no reviews)
2015/08/05 12:19:20
this and the includes below may not be required an
Polina Bondarenko
2015/08/05 14:15:12
Done.
|
| +#include "extensions/common/permissions/permissions_data.h" |
| +#include "extensions/common/permissions/permissions_info.h" |
| + |
| +namespace { |
| +const std::string kDeviceId = "device_id"; |
| +} // namespace |
| + |
| +namespace extensions { |
| +class EnterpriseDeviceAttributesTest : public ExtensionApiTest { |
|
pneubeck (no reviews)
2015/08/05 12:19:20
nit: empty line before this one
Polina Bondarenko
2015/08/05 14:15:11
Done.
|
| + public: |
| + explicit EnterpriseDeviceAttributesTest(const std::string& domain) |
| + : fake_session_manager_client_(new chromeos::FakeSessionManagerClient), |
| + test_domain_(domain), |
| + get_device_id_function_( |
| + new EnterpriseDeviceAttributesGetDirectoryDeviceIdFunction()) {} |
| + |
| + protected: |
| + void SetUpInProcessBrowserTestFixture() override { |
| + chromeos::DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient( |
| + scoped_ptr<chromeos::SessionManagerClient>( |
|
pneubeck (no reviews)
2015/08/05 12:19:20
does make_scoped_ptr work here? (you would not hav
Polina Bondarenko
2015/08/05 14:15:12
Sure, done.
|
| + fake_session_manager_client_)); |
| + ExtensionApiTest::SetUpInProcessBrowserTestFixture(); |
| + InitTest(); |
| + } |
| + |
| + // Inits the device policy and install attributes, should be invoked before |
|
pneubeck (no reviews)
2015/08/05 12:19:20
you could inline this function into SetUpInProcess
Polina Bondarenko
2015/08/05 14:15:12
Done.
|
| + // browser init. |
| + void InitTest() { |
| + // Set up fake install attributes. |
| + scoped_ptr<policy::StubEnterpriseInstallAttributes> attributes( |
| + new policy::StubEnterpriseInstallAttributes()); |
| + |
| + attributes->SetDomain(test_domain_); |
| + attributes->SetRegistrationUser(chromeos::login::kStubUser); |
| + policy::BrowserPolicyConnectorChromeOS::SetInstallAttributesForTesting( |
| + attributes.release()); |
| + |
| + test_helper_.InstallOwnerKey(); |
| + // Init the device policy. |
| + policy::DevicePolicyBuilder* device_policy = test_helper_.device_policy(); |
| + device_policy->SetDefaultSigningKey(); |
| + device_policy->policy_data().set_directory_api_id(kDeviceId); |
| + device_policy->Build(); |
| + |
| + fake_session_manager_client_->set_device_policy(device_policy->GetBlob()); |
| + fake_session_manager_client_->OnPropertyChangeComplete(true); |
| + } |
| + |
| + void RunAffiliationTest(const std::string& expect_value) { |
| + scoped_ptr<base::Value> result( |
| + api_test_utils::RunFunctionAndReturnSingleResult( |
| + get_device_id_function_.get(), "[]", browser()->profile())); |
| + |
| + std::string value; |
| + EXPECT_TRUE(result->GetAsString(&value)); |
| + |
| + EXPECT_EQ(expect_value, value); |
| + } |
| + |
| + private: |
| + chromeos::FakeSessionManagerClient* fake_session_manager_client_; |
|
pneubeck (no reviews)
2015/08/05 12:19:20
nit: const
Polina Bondarenko
2015/08/05 14:15:11
Some non-const methods are invoked from this insta
|
| + policy::DevicePolicyCrosTestHelper test_helper_; |
| + std::string test_domain_; |
|
pneubeck (no reviews)
2015/08/05 12:19:20
nit: const
Polina Bondarenko
2015/08/05 14:15:12
Done.
|
| + scoped_refptr<EnterpriseDeviceAttributesGetDirectoryDeviceIdFunction> |
| + get_device_id_function_; |
| +}; |
| + |
| +// Creates affiliated user before browser initializes. |
| +class EnterpriseDeviceAttributesAffiliatedTest |
| + : public EnterpriseDeviceAttributesTest { |
| + public: |
| + EnterpriseDeviceAttributesAffiliatedTest() |
| + : EnterpriseDeviceAttributesTest("gmail.com") {} |
| +}; |
| + |
| +// Creates non-affiliated user before browser init. |
| +class EnterpriseDeviceAttributesNonAffiliatedTest |
| + : public EnterpriseDeviceAttributesTest { |
| + public: |
| + EnterpriseDeviceAttributesNonAffiliatedTest() |
| + : EnterpriseDeviceAttributesTest("example.com") {} |
| +}; |
| + |
| +// Tests the case when the user is affiliated. |
| +// Expected result: successful device id fetch. |
| +IN_PROC_BROWSER_TEST_F(EnterpriseDeviceAttributesAffiliatedTest, Success) { |
| + RunAffiliationTest(kDeviceId); |
| +} |
| + |
| +// Tests the case when the user is not affiliated Expected result: fetch empty |
| +// string as a device id.. |
| +IN_PROC_BROWSER_TEST_F(EnterpriseDeviceAttributesNonAffiliatedTest, |
| + EmptyString) { |
| + RunAffiliationTest(""); |
| +} |
| + |
| +} // namespace extensions |