summaryrefslogtreecommitdiffstats
path: root/chromeos/dbus/services/service_provider_test_helper.h
blob: 196f6a61fffd9d9ca98193aa781135ce6d50265d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) 2012 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.

#ifndef CHROMEOS_DBUS_SERVICES_SERVICE_PROVIDER_TEST_HELPER_H_
#define CHROMEOS_DBUS_SERVICES_SERVICE_PROVIDER_TEST_HELPER_H_

#include <string>

#include "base/message_loop/message_loop.h"
#include "chromeos/dbus/services/cros_dbus_service.h"
#include "dbus/mock_exported_object.h"
#include "dbus/mock_object_proxy.h"
#include "dbus/object_proxy.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace dbus {

class MockBus;

}  // namespace dbus

namespace chromeos {

// Helps to implement |CrosDBusService::ServiceProviderInterface| unittests.
// Setups mocking of dbus classes.
// Class can test only one method call in time. SetUp() must be called before
// testing new call to the same method or different method.
//
// Sample usage:
//   ServiceProviderTestHelper helper;
//   helper.Setup(...);
//   helper.SetUpReturnSignal(...); // optional.
//   helper.CallMethod(...);
//   helper.TearDown();
class ServiceProviderTestHelper {
 public:
  ServiceProviderTestHelper();
  ~ServiceProviderTestHelper();

  // Sets up helper. Should be called before |CallMethod()|.
  void SetUp(const std::string& exported_method_name,
             CrosDBusService::ServiceProviderInterface* service_provider);

  // Setups return signal callback. It's optional and don't need to be called
  // if tested method doesn't use signal to return results.
  void SetUpReturnSignal(
      const std::string& interface_name,
      const std::string& signal_name,
      dbus::ObjectProxy::SignalCallback signal_callback,
      dbus::ObjectProxy::OnConnectedCallback on_connected_callback);

  // Calls tested dbus method.
  scoped_ptr<dbus::Response> CallMethod(dbus::MethodCall* method_call);

  // Cleanups helper. Should be called after |CallMethod()|.
  void TearDown();

 private:
  // Behaves as |mock_exported_object_|'s ExportMethod().
  void MockExportMethod(
      const std::string& interface_name,
      const std::string& method_name,
      dbus::ExportedObject::MethodCallCallback method_callback,
      dbus::ExportedObject::OnExportedCallback on_exported_callback);

  // Calls exported method and waits for a response for |mock_object_proxy_|.
  dbus::Response* MockCallMethodAndBlock(
      dbus::MethodCall* method_call,
      ::testing::Unused);

  // Behaves as |mock_object_proxy_|'s ConnectToSignal().
  void MockConnectToSignal(
      const std::string& interface_name,
      const std::string& signal_name,
      dbus::ObjectProxy::SignalCallback signal_callback,
      dbus::ObjectProxy::OnConnectedCallback connected_callback);

  // Behaves as |mock_exported_object_|'s SendSignal().
  void MockSendSignal(dbus::Signal* signal);

  // Receives a response and makes it available to MockCallMethodAndBlock().
  void OnResponse(scoped_ptr<dbus::Response> response);

  scoped_refptr<dbus::MockBus> mock_bus_;
  scoped_refptr<dbus::MockExportedObject> mock_exported_object_;
  scoped_refptr<dbus::MockObjectProxy> mock_object_proxy_;
  dbus::ExportedObject::MethodCallCallback method_callback_;
  dbus::ObjectProxy::SignalCallback on_signal_callback_;
  base::MessageLoop message_loop_;
  bool response_received_;
  scoped_ptr<dbus::Response> response_;
};

}  // namespace chromeos

#endif  // CHROMEOS_DBUS_SERVICES_SERVICE_PROVIDER_TEST_HELPER_H_