Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/dbus/services/cros_dbus_service.h" | 5 #include "chromeos/dbus/services/cros_dbus_service.h" |
| 6 | 6 |
| 7 #include <memory> | |
| 7 #include <utility> | 8 #include <utility> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 13 #include "dbus/message.h" | 14 #include "dbus/message.h" |
| 14 #include "dbus/mock_bus.h" | 15 #include "dbus/mock_bus.h" |
| 15 #include "dbus/mock_exported_object.h" | 16 #include "dbus/mock_exported_object.h" |
| 16 #include "dbus/mock_object_proxy.h" | 17 #include "dbus/mock_object_proxy.h" |
| 17 #include "dbus/object_path.h" | 18 #include "dbus/object_path.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "third_party/cros_system_api/dbus/service_constants.h" | 21 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 21 | 22 |
| 23 using ::testing::_; | |
| 22 using ::testing::Eq; | 24 using ::testing::Eq; |
| 23 using ::testing::Invoke; | 25 using ::testing::Invoke; |
| 24 using ::testing::Return; | 26 using ::testing::Return; |
| 25 | 27 |
| 26 namespace chromeos { | 28 namespace chromeos { |
| 27 | 29 |
| 28 class MockProxyResolutionService | 30 class MockProxyResolutionService |
| 29 : public CrosDBusService::ServiceProviderInterface { | 31 : public CrosDBusService::ServiceProviderInterface { |
| 30 public: | 32 public: |
| 31 MOCK_METHOD1(Start, void(scoped_refptr<dbus::ExportedObject> | 33 MOCK_METHOD1(Start, void(scoped_refptr<dbus::ExportedObject> |
| 32 exported_object)); | 34 exported_object)); |
| 33 }; | 35 }; |
|
satorux1
2017/02/15 06:48:01
while you are at it, i'd appreciate it if you coul
Daniel Erat
2017/02/15 16:57:00
i actually don't mind this particular usage of gmo
| |
| 34 | 36 |
| 35 class CrosDBusServiceTest : public testing::Test { | 37 class CrosDBusServiceTest : public testing::Test { |
| 36 public: | 38 public: |
| 37 CrosDBusServiceTest() { | 39 CrosDBusServiceTest() = default; |
| 38 } | |
| 39 | 40 |
| 40 // Creates an instance of CrosDBusService with mocks injected. | 41 // Creates an instance of CrosDBusService with mocks injected. |
| 41 void SetUp() override { | 42 void SetUp() override { |
| 42 // Create a mock bus. | 43 // Create a mock bus. |
| 43 dbus::Bus::Options options; | 44 dbus::Bus::Options options; |
| 44 options.bus_type = dbus::Bus::SYSTEM; | 45 options.bus_type = dbus::Bus::SYSTEM; |
| 45 mock_bus_ = new dbus::MockBus(options); | 46 mock_bus_ = new dbus::MockBus(options); |
| 46 | 47 |
| 47 // ShutdownAndBlock() will be called in TearDown(). | 48 // ShutdownAndBlock() will be called in TearDown(). |
| 48 EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return()); | 49 EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return()); |
| 49 | 50 |
| 50 // Create a mock exported object that behaves as | 51 // CrosDBusService should take ownership of the service name passed to it. |
| 51 // org.chromium.CrosDBusService. | 52 const char kServiceName[] = "org.example.TestService"; |
| 53 EXPECT_CALL( | |
| 54 *mock_bus_.get(), | |
| 55 RequestOwnership(kServiceName, | |
| 56 dbus::Bus::REQUIRE_PRIMARY_ALLOW_REPLACEMENT, _)) | |
| 57 .Times(1); | |
| 58 | |
| 59 // Create a mock exported object. | |
| 60 const dbus::ObjectPath kObjectPath("/org/example/TestService"); | |
| 52 mock_exported_object_ = | 61 mock_exported_object_ = |
| 53 new dbus::MockExportedObject(mock_bus_.get(), | 62 new dbus::MockExportedObject(mock_bus_.get(), kObjectPath); |
| 54 dbus::ObjectPath(kLibCrosServicePath)); | |
| 55 | 63 |
| 56 // |mock_bus_|'s GetExportedObject() will return mock_exported_object_| | 64 // |mock_bus_|'s GetExportedObject() will return mock_exported_object_| |
| 57 // for the given service name and the object path. | 65 // for the given service name and the object path. |
| 58 EXPECT_CALL(*mock_bus_.get(), | 66 EXPECT_CALL(*mock_bus_.get(), GetExportedObject(kObjectPath)) |
| 59 GetExportedObject(dbus::ObjectPath(kLibCrosServicePath))) | |
| 60 .WillOnce(Return(mock_exported_object_.get())); | 67 .WillOnce(Return(mock_exported_object_.get())); |
| 61 | 68 |
| 62 // Create a mock proxy resolution service. | 69 // Create a mock proxy resolution service. |
| 63 auto mock_proxy_resolution_service_provider = | 70 auto mock_proxy_resolution_service_provider = |
| 64 base::MakeUnique<MockProxyResolutionService>(); | 71 base::MakeUnique<MockProxyResolutionService>(); |
| 65 | 72 |
| 66 // Start() will be called with |mock_exported_object_|. | 73 // Start() will be called with |mock_exported_object_|. |
| 67 EXPECT_CALL(*mock_proxy_resolution_service_provider, | 74 EXPECT_CALL(*mock_proxy_resolution_service_provider, |
| 68 Start(Eq(mock_exported_object_))).WillOnce(Return()); | 75 Start(Eq(mock_exported_object_))).WillOnce(Return()); |
| 76 | |
| 69 // Initialize the cros service with the mocks injected. | 77 // Initialize the cros service with the mocks injected. |
| 70 CrosDBusService::ServiceProviderList service_providers; | 78 CrosDBusService::ServiceProviderList service_providers; |
| 71 service_providers.push_back( | 79 service_providers.push_back( |
| 72 std::move(mock_proxy_resolution_service_provider)); | 80 std::move(mock_proxy_resolution_service_provider)); |
| 73 CrosDBusService::InitializeForTesting(mock_bus_.get(), | 81 cros_dbus_service_ = CrosDBusService::CreateRealImpl( |
| 74 std::move(service_providers)); | 82 mock_bus_.get(), kServiceName, kObjectPath, |
| 83 std::move(service_providers)); | |
| 75 } | 84 } |
| 76 | 85 |
| 77 void TearDown() override { | 86 void TearDown() override { |
| 78 // Shutdown the cros service. | 87 cros_dbus_service_.reset(); |
| 79 CrosDBusService::Shutdown(); | |
| 80 | |
| 81 // Shutdown the bus. | |
| 82 mock_bus_->ShutdownAndBlock(); | 88 mock_bus_->ShutdownAndBlock(); |
| 83 } | 89 } |
| 84 | 90 |
| 85 protected: | 91 protected: |
| 86 scoped_refptr<dbus::MockBus> mock_bus_; | 92 scoped_refptr<dbus::MockBus> mock_bus_; |
| 87 scoped_refptr<dbus::MockExportedObject> mock_exported_object_; | 93 scoped_refptr<dbus::MockExportedObject> mock_exported_object_; |
| 94 std::unique_ptr<CrosDBusService> cros_dbus_service_; | |
| 88 }; | 95 }; |
| 89 | 96 |
| 90 TEST_F(CrosDBusServiceTest, Start) { | 97 TEST_F(CrosDBusServiceTest, Start) { |
| 91 // Simply start the service and see if mock expectations are met: | 98 // Simply start the service and see if mock expectations are met: |
| 92 // - The service object is exported by GetExportedObject() | 99 // - The service object is exported by GetExportedObject() |
| 93 // - The proxy resolution service is started. | 100 // - The proxy resolution service is started. |
| 94 } | 101 } |
| 95 | 102 |
| 96 } // namespace chromeos | 103 } // namespace chromeos |
| OLD | NEW |