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
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "dbus/object_proxy.h"
#include "base/functional/bind.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "dbus/bus.h"
#include "dbus/test_service.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace dbus {
namespace {
class ObjectProxyTest : public testing::Test {
protected:
ObjectProxyTest() {}
void SetUp() override {
Bus::Options bus_options;
bus_options.bus_type = Bus::SESSION;
bus_options.connection_type = Bus::PRIVATE;
bus_ = new Bus(bus_options);
}
void TearDown() override { bus_->ShutdownAndBlock(); }
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::MainThreadType::IO};
scoped_refptr<Bus> bus_;
};
// Used as a WaitForServiceToBeAvailableCallback.
void OnServiceIsAvailable(bool* dest_service_is_available,
int* num_calls,
bool src_service_is_available) {
*dest_service_is_available = src_service_is_available;
(*num_calls)++;
}
// Used as a callback for TestService::RequestOwnership().
void OnOwnershipRequestDone(bool success) {
ASSERT_TRUE(success);
}
// Used as a callback for TestService::ReleaseOwnership().
void OnOwnershipReleased() {}
TEST_F(ObjectProxyTest, WaitForServiceToBeAvailableRunOnce) {
TestService::Options options;
TestService test_service(options);
ObjectProxy* object_proxy = bus_->GetObjectProxy(
test_service.service_name(), ObjectPath("/org/chromium/TestObject"));
// The callback is not yet called because the service is not available.
int num_calls = 0;
bool service_is_available = false;
object_proxy->WaitForServiceToBeAvailable(
base::BindOnce(&OnServiceIsAvailable, &service_is_available, &num_calls));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, num_calls);
// Start the service. The callback should be called asynchronously.
ASSERT_TRUE(test_service.StartService());
test_service.WaitUntilServiceIsStarted();
ASSERT_TRUE(test_service.has_ownership());
num_calls = 0;
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, num_calls);
EXPECT_TRUE(service_is_available);
// Release the service's ownership of its name. The callback should not be
// invoked again.
test_service.ReleaseOwnership(base::BindOnce(&OnOwnershipReleased));
num_calls = 0;
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, num_calls);
// Take ownership of the name and check that the callback is not called.
test_service.RequestOwnership(base::BindOnce(&OnOwnershipRequestDone));
num_calls = 0;
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, num_calls);
}
TEST_F(ObjectProxyTest, WaitForServiceToBeAvailableAlreadyRunning) {
TestService::Options options;
TestService test_service(options);
ObjectProxy* object_proxy = bus_->GetObjectProxy(
test_service.service_name(), ObjectPath("/org/chromium/TestObject"));
ASSERT_TRUE(test_service.StartService());
test_service.WaitUntilServiceIsStarted();
ASSERT_TRUE(test_service.has_ownership());
// Since the service is already running, the callback should be invoked
// immediately (but asynchronously, rather than the callback being invoked
// directly within WaitForServiceToBeAvailable()).
int num_calls = 0;
bool service_is_available = false;
object_proxy->WaitForServiceToBeAvailable(
base::BindOnce(&OnServiceIsAvailable, &service_is_available, &num_calls));
EXPECT_EQ(0, num_calls);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, num_calls);
EXPECT_TRUE(service_is_available);
}
TEST_F(ObjectProxyTest, WaitForServiceToBeAvailableMultipleCallbacks) {
TestService::Options options;
TestService test_service(options);
ObjectProxy* object_proxy = bus_->GetObjectProxy(
test_service.service_name(), ObjectPath("/org/chromium/TestObject"));
// Register two callbacks.
int num_calls_1 = 0, num_calls_2 = 0;
bool service_is_available_1 = false, service_is_available_2 = false;
object_proxy->WaitForServiceToBeAvailable(base::BindOnce(
&OnServiceIsAvailable, &service_is_available_1, &num_calls_1));
object_proxy->WaitForServiceToBeAvailable(base::BindOnce(
&OnServiceIsAvailable, &service_is_available_2, &num_calls_2));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, num_calls_1);
EXPECT_EQ(0, num_calls_2);
// Start the service and confirm that both callbacks are invoked.
ASSERT_TRUE(test_service.StartService());
test_service.WaitUntilServiceIsStarted();
ASSERT_TRUE(test_service.has_ownership());
num_calls_1 = 0;
num_calls_2 = 0;
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, num_calls_1);
EXPECT_EQ(1, num_calls_2);
EXPECT_TRUE(service_is_available_1);
EXPECT_TRUE(service_is_available_2);
}
} // namespace
} // namespace dbus
|