File: service_provider_test_helper.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (165 lines) | stat: -rw-r--r-- 6,170 bytes parent folder | download | duplicates (8)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/dbus/services/service_provider_test_helper.h"

#include <utility>

#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "dbus/message.h"
#include "dbus/mock_bus.h"
#include "dbus/object_path.h"

using ::testing::_;
using ::testing::AllOf;
using ::testing::Invoke;
using ::testing::ResultOf;
using ::testing::Return;
using ::testing::Unused;

namespace ash {

ServiceProviderTestHelper::ServiceProviderTestHelper() = default;

ServiceProviderTestHelper::~ServiceProviderTestHelper() = default;

void ServiceProviderTestHelper::SetUp(
    const std::string& service_name,
    const dbus::ObjectPath& service_path,
    const std::string& interface_name,
    const std::string& exported_method_name,
    CrosDBusService::ServiceProviderInterface* service_provider) {
  exported_method_name_ = exported_method_name;
  // Create a mock bus.
  dbus::Bus::Options options;
  options.bus_type = dbus::Bus::SYSTEM;
  mock_bus_ = new dbus::MockBus(options);

  // ShutdownAndBlock() will be called in TearDown().
  EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return());

  // Create a mock exported object that behaves as the service.
  mock_exported_object_ =
      new dbus::MockExportedObject(mock_bus_.get(), service_path);

  // |mock_exported_object_|'s ExportMethod() will use
  // |MockExportedObject().
  EXPECT_CALL(*mock_exported_object_.get(),
              ExportMethod(interface_name, _, _, _))
      .WillRepeatedly(
          Invoke(this, &ServiceProviderTestHelper::MockExportMethod));

  // Create a mock object proxy, with which we call a method of
  // |mock_exported_object_|.
  mock_object_proxy_ =
      new dbus::MockObjectProxy(mock_bus_.get(), service_name, service_path);
  // |mock_object_proxy_|'s CallMethodAndBlock() will use CallMethodAndBlock()
  // to return responses.
  EXPECT_CALL(*mock_object_proxy_.get(),
              CallMethodAndBlock(
                  AllOf(ResultOf(std::mem_fn(&dbus::MethodCall::GetInterface),
                                 interface_name),
                        ResultOf(std::mem_fn(&dbus::MethodCall::GetMember),
                                 exported_method_name)),
                  _))
      .WillOnce(Invoke(this, &ServiceProviderTestHelper::CallMethodAndBlock));

  service_provider->Start(mock_exported_object_.get());
}

void ServiceProviderTestHelper::TearDown() {
  mock_bus_->ShutdownAndBlock();
  mock_exported_object_.reset();
  mock_object_proxy_.reset();
  mock_bus_.reset();
}

void ServiceProviderTestHelper::SetUpReturnSignal(
    const std::string& interface_name,
    const std::string& signal_name,
    dbus::ObjectProxy::SignalCallback signal_callback,
    dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
  // |mock_exported_object_|'s SendSignal() will use
  // MockSendSignal().
  EXPECT_CALL(*mock_exported_object_.get(), SendSignal(_))
      .WillOnce(Invoke(this, &ServiceProviderTestHelper::MockSendSignal));

  // |mock_object_proxy_|'s ConnectToSignal will use
  // MockConnectToSignal().
  EXPECT_CALL(*mock_object_proxy_.get(),
              DoConnectToSignal(interface_name, signal_name, _, _))
      .WillOnce(Invoke(this, &ServiceProviderTestHelper::MockConnectToSignal));

  mock_object_proxy_->ConnectToSignal(interface_name, signal_name,
                                      signal_callback,
                                      std::move(on_connected_callback));
}

std::unique_ptr<dbus::Response> ServiceProviderTestHelper::CallMethod(
    dbus::MethodCall* method_call) {
  return mock_object_proxy_
      ->CallMethodAndBlock(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)
      .value_or(nullptr);
}

void ServiceProviderTestHelper::MockExportMethod(
    const std::string& interface_name,
    const std::string& method_name,
    dbus::ExportedObject::MethodCallCallback method_callback,
    dbus::ExportedObject::OnExportedCallback on_exported_callback) {
  // Tell the call back that the method is exported successfully.
  std::move(on_exported_callback).Run(interface_name, method_name, true);
  // Capture the callback, so we can run this at a later time.
  if (method_name == exported_method_name_) {
    method_callback_ = method_callback;
  }
}

std::unique_ptr<dbus::Response> ServiceProviderTestHelper::CallMethodAndBlock(
    dbus::MethodCall* method_call,
    Unused) {
  // Set the serial number to non-zero, so
  // dbus_message_new_method_return() won't emit a warning.
  method_call->SetSerial(1);
  // Run the callback captured in MockExportMethod(). In addition to returning
  // a response that the caller will ignore, this will send a signal, which
  // will be received by |on_signal_callback_|.
  std::unique_ptr<dbus::Response> response;
  method_callback_.Run(method_call,
                       base::BindOnce(&ServiceProviderTestHelper::OnResponse,
                                      base::Unretained(this), &response));
  // Check for a response.
  if (!response)
    loop_.Run();
  // Return response.
  return response;
}

void ServiceProviderTestHelper::MockConnectToSignal(
    const std::string& interface_name,
    const std::string& signal_name,
    dbus::ObjectProxy::SignalCallback signal_callback,
    dbus::ObjectProxy::OnConnectedCallback* connected_callback) {
  // Tell the callback that the object proxy is connected to the signal.
  std::move(*connected_callback).Run(interface_name, signal_name, true);
  // Capture the callback, so we can run this at a later time.
  on_signal_callback_ = signal_callback;
}

void ServiceProviderTestHelper::MockSendSignal(dbus::Signal* signal) {
  // Run the callback captured in MockConnectToSignal(). This will call
  // OnSignalReceived().
  on_signal_callback_.Run(signal);
}

void ServiceProviderTestHelper::OnResponse(
    std::unique_ptr<dbus::Response>* out_response,
    std::unique_ptr<dbus::Response> response) {
  *out_response = std::move(response);
  loop_.QuitWhenIdle();
}

}  // namespace ash