1From 59bc1c00682f45d54a05aadc2a2e0559f85e4499 Mon Sep 17 00:00:00 2001
2From: Sonny Sasaka <[email protected]>
3Date: Thu, 16 Aug 2018 05:09:20 +0000
4Subject: [PATCH] dbus: Make Bus::is_connected() mockable
5
6It's currently not possible to have a unit test that triggers
7Bus::is_connected() because it always returns false. This is currently
8needed by the Bluetooth dispatcher (btdispatch) in Chrome OS.
9
10Bug: 866704
11Change-Id: I04f7e8a22792886d421479c1c7c621eeb27d3a2a
12Reviewed-on: https://chromium-review.googlesource.com/1175216
13Reviewed-by: Ryo Hashimoto <[email protected]>
14Commit-Queue: Sonny Sasaka <[email protected]>
15Cr-Commit-Position: refs/heads/master@{#583543}
16---
17 dbus/bus.cc             | 4 ++++
18 dbus/bus.h              | 2 +-
19 dbus/bus_unittest.cc    | 4 ++--
20 dbus/exported_object.cc | 2 +-
21 dbus/mock_bus.h         | 1 +
22 dbus/object_proxy.cc    | 2 +-
23 6 files changed, 10 insertions(+), 5 deletions(-)
24
25diff --git a/dbus/bus.cc b/dbus/bus.cc
26index 2f3db885f561..9d37656ac21c 100644
27--- a/dbus/bus.cc
28+++ b/dbus/bus.cc
29@@ -997,6 +997,10 @@ std::string Bus::GetConnectionName() {
30   return dbus_bus_get_unique_name(connection_);
31 }
32
33+bool Bus::IsConnected() {
34+  return connection_ != nullptr;
35+}
36+
37 dbus_bool_t Bus::OnAddWatch(DBusWatch* raw_watch) {
38   AssertOnDBusThread();
39
40diff --git a/dbus/bus.h b/dbus/bus.h
41index 704a4c3a0b54..b082110e589b 100644
42--- a/dbus/bus.h
43+++ b/dbus/bus.h
44@@ -601,7 +601,7 @@ class CHROME_DBUS_EXPORT Bus : public base::RefCountedThreadSafe<Bus> {
45   std::string GetConnectionName();
46
47   // Returns true if the bus is connected to D-Bus.
48-  bool is_connected() { return connection_ != nullptr; }
49+  virtual bool IsConnected();
50
51  protected:
52   // This is protected, so we can define sub classes.
53diff --git a/dbus/exported_object.cc b/dbus/exported_object.cc
54index d6c91b6d2046..5fa1b916f251 100644
55--- a/dbus/exported_object.cc
56+++ b/dbus/exported_object.cc
57@@ -280,7 +280,7 @@ void ExportedObject::OnMethodCompleted(std::unique_ptr<MethodCall> method_call,
58
59   // Check if the bus is still connected. If the method takes long to
60   // complete, the bus may be shut down meanwhile.
61-  if (!bus_->is_connected())
62+  if (!bus_->IsConnected())
63     return;
64
65   if (!response) {
66diff --git a/dbus/mock_bus.h b/dbus/mock_bus.h
67index 6b3495db6014..22807622786a 100644
68--- a/dbus/mock_bus.h
69+++ b/dbus/mock_bus.h
70@@ -73,6 +73,7 @@ class MockBus : public Bus {
71   MOCK_METHOD0(HasDBusThread, bool());
72   MOCK_METHOD0(AssertOnOriginThread, void());
73   MOCK_METHOD0(AssertOnDBusThread, void());
74+  MOCK_METHOD0(IsConnected, bool());
75
76  protected:
77   ~MockBus() override;
78diff --git a/dbus/object_proxy.cc b/dbus/object_proxy.cc
79index aa5102aec792..3046dbb5f38b 100644
80--- a/dbus/object_proxy.cc
81+++ b/dbus/object_proxy.cc
82@@ -288,7 +288,7 @@ void ObjectProxy::WaitForServiceToBeAvailable(
83 void ObjectProxy::Detach() {
84   bus_->AssertOnDBusThread();
85
86-  if (bus_->is_connected())
87+  if (bus_->IsConnected())
88     bus_->RemoveFilterFunction(&ObjectProxy::HandleMessageThunk, this);
89
90   for (const auto& match_rule : match_rules_) {
91--
922.21.0.392.gf8f6787159e-goog
93
94