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
3// found in the LICENSE file.
4
5#include "chrome/browser/chromeos/dbus/cros_dbus_service.h"
6
7#include "base/bind.h"
8#include "base/logging.h"
9#include "base/memory/ref_counted.h"
10#include "dbus/message.h"
11#include "dbus/mock_bus.h"
12#include "dbus/mock_exported_object.h"
13#include "dbus/mock_object_proxy.h"
14#include "dbus/object_path.h"
15#include "testing/gmock/include/gmock/gmock.h"
16#include "testing/gtest/include/gtest/gtest.h"
17#include "third_party/cros_system_api/dbus/service_constants.h"
18
19using ::testing::Eq;
20using ::testing::Invoke;
21using ::testing::Return;
22
23namespace chromeos {
24
25class MockProxyResolutionService
26    : public CrosDBusService::ServiceProviderInterface {
27 public:
28  MOCK_METHOD1(Start, void(scoped_refptr<dbus::ExportedObject>
29                           exported_object));
30};
31
32class CrosDBusServiceTest : public testing::Test {
33 public:
34  CrosDBusServiceTest() {
35  }
36
37  // Creates an instance of CrosDBusService with mocks injected.
38  virtual void SetUp() {
39    // Create a mock bus.
40    dbus::Bus::Options options;
41    options.bus_type = dbus::Bus::SYSTEM;
42    mock_bus_ = new dbus::MockBus(options);
43
44    // ShutdownAndBlock() will be called in TearDown().
45    EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return());
46
47    // Create a mock exported object that behaves as
48    // org.chromium.CrosDBusService.
49    mock_exported_object_ =
50        new dbus::MockExportedObject(mock_bus_.get(),
51                                     dbus::ObjectPath(kLibCrosServicePath));
52
53    // |mock_bus_|'s GetExportedObject() will return mock_exported_object_|
54    // for the given service name and the object path.
55    EXPECT_CALL(*mock_bus_.get(),
56                GetExportedObject(dbus::ObjectPath(kLibCrosServicePath)))
57        .WillOnce(Return(mock_exported_object_.get()));
58
59    // Create a mock proxy resolution service.
60    MockProxyResolutionService* mock_proxy_resolution_service_provider =
61        new MockProxyResolutionService;
62
63    // Start() will be called with |mock_exported_object_|.
64    EXPECT_CALL(*mock_proxy_resolution_service_provider,
65                Start(Eq(mock_exported_object_))).WillOnce(Return());
66    // Initialize the cros service with the mocks injected.
67    CrosDBusService::InitializeForTesting(
68        mock_bus_.get(), mock_proxy_resolution_service_provider);
69  }
70
71  virtual void TearDown() {
72    // Shutdown the cros service.
73    CrosDBusService::Shutdown();
74
75    // Shutdown the bus.
76    mock_bus_->ShutdownAndBlock();
77  }
78
79 protected:
80  scoped_refptr<dbus::MockBus> mock_bus_;
81  scoped_refptr<dbus::MockExportedObject> mock_exported_object_;
82};
83
84TEST_F(CrosDBusServiceTest, Start) {
85  // Simply start the service and see if mock expectations are met:
86  // - The service object is exported by GetExportedObject()
87  // - The proxy resolution service is started.
88}
89
90}  // namespace chromeos
91