0

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}
This commit is contained in:
Sonny Sasaka
2018-08-16 05:09:20 +00:00
committed by Commit Bot
parent 6d238de77b
commit 59bc1c0068
6 changed files with 10 additions and 5 deletions

@ -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();

@ -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.

@ -402,14 +402,14 @@ TEST(BusTest, GetConnectionName) {
scoped_refptr<Bus> bus = new Bus(options);
// Connection name is empty since bus is not connected.
EXPECT_FALSE(bus->is_connected());
EXPECT_FALSE(bus->IsConnected());
EXPECT_TRUE(bus->GetConnectionName().empty());
// Connect bus to D-Bus.
bus->Connect();
// Connection name is not empty after connection is established.
EXPECT_TRUE(bus->is_connected());
EXPECT_TRUE(bus->IsConnected());
EXPECT_FALSE(bus->GetConnectionName().empty());
// Shut down synchronously.

@ -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) {

@ -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;

@ -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_) {