From 59bc1c00682f45d54a05aadc2a2e0559f85e4499 Mon Sep 17 00:00:00 2001
From: Sonny Sasaka <sonnysasaka@chromium.org>
Date: Thu, 16 Aug 2018 05:09:20 +0000
Subject: [PATCH] dbus: Make Bus::is_connected() mockable

It's currently not possible to have a unit test that triggers
Bus::is_connected() because it always returns false. This is currently
needed by the Bluetooth dispatcher (btdispatch) in Chrome OS.

Bug: 866704
Change-Id: I04f7e8a22792886d421479c1c7c621eeb27d3a2a
Reviewed-on: https://chromium-review.googlesource.com/1175216
Reviewed-by: Ryo Hashimoto <hashimoto@chromium.org>
Commit-Queue: Sonny Sasaka <sonnysasaka@chromium.org>
Cr-Commit-Position: refs/heads/master@{#583543}
---
 dbus/bus.cc             | 4 ++++
 dbus/bus.h              | 2 +-
 dbus/bus_unittest.cc    | 4 ++--
 dbus/exported_object.cc | 2 +-
 dbus/mock_bus.h         | 1 +
 dbus/object_proxy.cc    | 2 +-
 6 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/dbus/bus.cc b/dbus/bus.cc
index 2f3db885f561..9d37656ac21c 100644
--- a/dbus/bus.cc
+++ b/dbus/bus.cc
@@ -997,6 +997,10 @@ std::string Bus::GetConnectionName() {
   return dbus_bus_get_unique_name(connection_);
 }
 
+bool Bus::IsConnected() {
+  return connection_ != nullptr;
+}
+
 dbus_bool_t Bus::OnAddWatch(DBusWatch* raw_watch) {
   AssertOnDBusThread();
 
diff --git a/dbus/bus.h b/dbus/bus.h
index 704a4c3a0b54..b082110e589b 100644
--- a/dbus/bus.h
+++ b/dbus/bus.h
@@ -601,7 +601,7 @@ class CHROME_DBUS_EXPORT Bus : public base::RefCountedThreadSafe<Bus> {
   std::string GetConnectionName();
 
   // Returns true if the bus is connected to D-Bus.
-  bool is_connected() { return connection_ != nullptr; }
+  virtual bool IsConnected();
 
  protected:
   // This is protected, so we can define sub classes.
diff --git a/dbus/exported_object.cc b/dbus/exported_object.cc
index d6c91b6d2046..5fa1b916f251 100644
--- a/dbus/exported_object.cc
+++ b/dbus/exported_object.cc
@@ -280,7 +280,7 @@ void ExportedObject::OnMethodCompleted(std::unique_ptr<MethodCall> method_call,
 
   // Check if the bus is still connected. If the method takes long to
   // complete, the bus may be shut down meanwhile.
-  if (!bus_->is_connected())
+  if (!bus_->IsConnected())
     return;
 
   if (!response) {
diff --git a/dbus/mock_bus.h b/dbus/mock_bus.h
index 6b3495db6014..22807622786a 100644
--- a/dbus/mock_bus.h
+++ b/dbus/mock_bus.h
@@ -73,6 +73,7 @@ class MockBus : public Bus {
   MOCK_METHOD0(HasDBusThread, bool());
   MOCK_METHOD0(AssertOnOriginThread, void());
   MOCK_METHOD0(AssertOnDBusThread, void());
+  MOCK_METHOD0(IsConnected, bool());
 
  protected:
   ~MockBus() override;
diff --git a/dbus/object_proxy.cc b/dbus/object_proxy.cc
index aa5102aec792..3046dbb5f38b 100644
--- a/dbus/object_proxy.cc
+++ b/dbus/object_proxy.cc
@@ -288,7 +288,7 @@ void ObjectProxy::WaitForServiceToBeAvailable(
 void ObjectProxy::Detach() {
   bus_->AssertOnDBusThread();
 
-  if (bus_->is_connected())
+  if (bus_->IsConnected())
     bus_->RemoveFilterFunction(&ObjectProxy::HandleMessageThunk, this);
 
   for (const auto& match_rule : match_rules_) {
-- 
2.21.0.392.gf8f6787159e-goog