This source file includes following definitions.
- on_ownership_called_
- SetUp
- TearDown
- OnOwnership
- OnOwnershipInternal
- OnNameOwnerChanged
- OnTestSignal
- OnConnected
- WaitForTestSignal
- SafeServiceStop
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/bind.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/histogram.h"
#include "base/metrics/histogram_samples.h"
#include "base/metrics/statistics_recorder.h"
#include "base/test/test_timeouts.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread_restrictions.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_proxy.h"
#include "dbus/test_service.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace dbus {
class SignalSenderVerificationTest : public testing::Test {
public:
SignalSenderVerificationTest()
: on_name_owner_changed_called_(false),
on_ownership_called_(false) {
}
virtual void SetUp() {
base::StatisticsRecorder::Initialize();
base::ThreadRestrictions::SetIOAllowed(false);
dbus_thread_.reset(new base::Thread("D-Bus Thread"));
base::Thread::Options thread_options;
thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
Bus::Options bus_options;
bus_options.bus_type = Bus::SESSION;
bus_options.connection_type = Bus::PRIVATE;
bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy();
bus_ = new Bus(bus_options);
object_proxy_ = bus_->GetObjectProxy(
"org.chromium.TestService",
ObjectPath("/org/chromium/TestObject"));
ASSERT_TRUE(bus_->HasDBusThread());
object_proxy_->SetNameOwnerChangedCallback(
base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged,
base::Unretained(this),
&on_name_owner_changed_called_));
object_proxy_->ConnectToSignal(
"org.chromium.TestInterface",
"Test",
base::Bind(&SignalSenderVerificationTest::OnTestSignal,
base::Unretained(this)),
base::Bind(&SignalSenderVerificationTest::OnConnected,
base::Unretained(this)));
message_loop_.Run();
TestService::Options options;
options.dbus_task_runner = dbus_thread_->message_loop_proxy();
test_service_.reset(new TestService(options));
ASSERT_TRUE(test_service_->StartService());
ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
ASSERT_TRUE(test_service_->HasDBusThread());
ASSERT_TRUE(test_service_->has_ownership());
test_service2_.reset(new TestService(options));
ASSERT_TRUE(test_service2_->StartService());
ASSERT_TRUE(test_service2_->WaitUntilServiceIsStarted());
ASSERT_TRUE(test_service2_->HasDBusThread());
ASSERT_FALSE(test_service2_->has_ownership());
if (!on_name_owner_changed_called_)
message_loop_.Run();
ASSERT_FALSE(latest_name_owner_.empty());
}
virtual void TearDown() {
bus_->ShutdownOnDBusThreadAndBlock();
test_service_->ShutdownAndBlock();
test_service2_->ShutdownAndBlock();
base::ThreadRestrictions::SetIOAllowed(true);
test_service_->Stop();
test_service2_->Stop();
}
void OnOwnership(bool expected, bool success) {
ASSERT_EQ(expected, success);
message_loop_.PostTask(
FROM_HERE,
base::Bind(&SignalSenderVerificationTest::OnOwnershipInternal,
base::Unretained(this)));
}
void OnOwnershipInternal() {
on_ownership_called_ = true;
message_loop_.Quit();
}
void OnNameOwnerChanged(bool* called_flag,
const std::string& old_owner,
const std::string& new_owner) {
latest_name_owner_ = new_owner;
*called_flag = true;
message_loop_.Quit();
}
void OnTestSignal(Signal* signal) {
MessageReader reader(signal);
ASSERT_TRUE(reader.PopString(&test_signal_string_));
message_loop_.Quit();
}
void OnConnected(const std::string& interface_name,
const std::string& signal_name,
bool success) {
ASSERT_TRUE(success);
message_loop_.Quit();
}
protected:
void WaitForTestSignal() {
message_loop_.Run();
}
void SafeServiceStop(TestService* test_service) {
base::ThreadRestrictions::SetIOAllowed(true);
test_service->Stop();
base::ThreadRestrictions::SetIOAllowed(false);
}
base::MessageLoop message_loop_;
scoped_ptr<base::Thread> dbus_thread_;
scoped_refptr<Bus> bus_;
ObjectProxy* object_proxy_;
scoped_ptr<TestService> test_service_;
scoped_ptr<TestService> test_service2_;
std::string test_signal_string_;
std::string latest_name_owner_;
bool on_name_owner_changed_called_;
bool on_ownership_called_;
};
TEST_F(SignalSenderVerificationTest, TestSignalAccepted) {
const char kMessage[] = "hello, world";
test_service_->SendTestSignal(kMessage);
WaitForTestSignal();
ASSERT_EQ(kMessage, test_signal_string_);
}
TEST_F(SignalSenderVerificationTest, TestSignalRejected) {
UMA_HISTOGRAM_COUNTS("DBus.RejectedSignalCount", 0);
base::HistogramBase* reject_signal_histogram =
base::StatisticsRecorder::FindHistogram("DBus.RejectedSignalCount");
scoped_ptr<base::HistogramSamples> samples1(
reject_signal_histogram->SnapshotSamples());
const char kNewMessage[] = "hello, new world";
test_service2_->SendTestSignal(kNewMessage);
base::PlatformThread::Sleep(TestTimeouts::action_timeout());
scoped_ptr<base::HistogramSamples> samples2(
reject_signal_histogram->SnapshotSamples());
ASSERT_EQ("", test_signal_string_);
EXPECT_EQ(samples1->TotalCount() + 1, samples2->TotalCount());
}
TEST_F(SignalSenderVerificationTest, TestOwnerChanged) {
const char kMessage[] = "hello, world";
test_service_->SendTestSignal(kMessage);
WaitForTestSignal();
ASSERT_EQ(kMessage, test_signal_string_);
ASSERT_FALSE(latest_name_owner_.empty());
test_service_->ShutdownAndBlock();
message_loop_.Run();
ASSERT_TRUE(latest_name_owner_.empty());
on_name_owner_changed_called_ = false;
on_ownership_called_ = false;
test_service2_->RequestOwnership(
base::Bind(&SignalSenderVerificationTest::OnOwnership,
base::Unretained(this), true));
message_loop_.Run();
if (!on_name_owner_changed_called_ || !on_ownership_called_)
message_loop_.Run();
ASSERT_TRUE(on_name_owner_changed_called_);
ASSERT_TRUE(on_ownership_called_);
ASSERT_FALSE(latest_name_owner_.empty());
const char kNewMessage[] = "hello, new world";
test_service2_->SendTestSignal(kNewMessage);
WaitForTestSignal();
ASSERT_EQ(kNewMessage, test_signal_string_);
}
TEST_F(SignalSenderVerificationTest, TestOwnerStealing) {
ASSERT_FALSE(latest_name_owner_.empty());
test_service_->ShutdownAndBlock();
message_loop_.Run();
ASSERT_TRUE(latest_name_owner_.empty());
on_name_owner_changed_called_ = false;
TestService::Options options;
options.dbus_task_runner = dbus_thread_->message_loop_proxy();
options.request_ownership_options = Bus::REQUIRE_PRIMARY_ALLOW_REPLACEMENT;
TestService stealable_test_service(options);
ASSERT_TRUE(stealable_test_service.StartService());
ASSERT_TRUE(stealable_test_service.WaitUntilServiceIsStarted());
ASSERT_TRUE(stealable_test_service.HasDBusThread());
ASSERT_TRUE(stealable_test_service.has_ownership());
message_loop_.Run();
const char kMessage[] = "hello, world";
stealable_test_service.SendTestSignal(kMessage);
WaitForTestSignal();
ASSERT_EQ(kMessage, test_signal_string_);
on_name_owner_changed_called_ = false;
test_service2_->RequestOwnership(
base::Bind(&SignalSenderVerificationTest::OnOwnership,
base::Unretained(this), true));
message_loop_.Run();
if (!on_name_owner_changed_called_ || !on_ownership_called_)
message_loop_.Run();
ASSERT_TRUE(on_name_owner_changed_called_);
ASSERT_TRUE(on_ownership_called_);
const char kNewMessage[] = "hello, new world";
test_service2_->SendTestSignal(kNewMessage);
WaitForTestSignal();
ASSERT_EQ(kNewMessage, test_signal_string_);
SafeServiceStop(&stealable_test_service);
}
TEST_F(SignalSenderVerificationTest, DISABLED_TestMultipleObjects) {
const char kMessage[] = "hello, world";
ObjectProxy* object_proxy2 = bus_->GetObjectProxy(
"org.chromium.TestService",
ObjectPath("/org/chromium/DifferentObject"));
bool second_name_owner_changed_called = false;
object_proxy2->SetNameOwnerChangedCallback(
base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged,
base::Unretained(this),
&second_name_owner_changed_called));
object_proxy2->ConnectToSignal(
"org.chromium.DifferentTestInterface",
"Test",
base::Bind(&SignalSenderVerificationTest::OnTestSignal,
base::Unretained(this)),
base::Bind(&SignalSenderVerificationTest::OnConnected,
base::Unretained(this)));
message_loop_.Run();
test_service_->SendTestSignal(kMessage);
WaitForTestSignal();
ASSERT_EQ(kMessage, test_signal_string_);
ASSERT_FALSE(latest_name_owner_.empty());
test_service_->ShutdownAndBlock();
message_loop_.Run();
ASSERT_TRUE(latest_name_owner_.empty());
on_name_owner_changed_called_ = false;
second_name_owner_changed_called = false;
test_service2_->RequestOwnership(
base::Bind(&SignalSenderVerificationTest::OnOwnership,
base::Unretained(this), true));
while (!on_name_owner_changed_called_ || !second_name_owner_changed_called ||
!on_ownership_called_)
message_loop_.Run();
ASSERT_TRUE(on_name_owner_changed_called_);
ASSERT_TRUE(second_name_owner_changed_called);
ASSERT_TRUE(on_ownership_called_);
ASSERT_FALSE(latest_name_owner_.empty());
const char kNewMessage[] = "hello, new world";
test_service2_->SendTestSignal(kNewMessage);
WaitForTestSignal();
ASSERT_EQ(kNewMessage, test_signal_string_);
}
}