object_proxy_unittest.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "dbus/object_proxy.h"
  5. #include "base/bind.h"
  6. #include "base/memory/ref_counted.h"
  7. #include "base/run_loop.h"
  8. #include "base/test/task_environment.h"
  9. #include "dbus/bus.h"
  10. #include "dbus/test_service.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace dbus {
  13. namespace {
  14. class ObjectProxyTest : public testing::Test {
  15. protected:
  16. ObjectProxyTest() {}
  17. void SetUp() override {
  18. Bus::Options bus_options;
  19. bus_options.bus_type = Bus::SESSION;
  20. bus_options.connection_type = Bus::PRIVATE;
  21. bus_ = new Bus(bus_options);
  22. }
  23. void TearDown() override { bus_->ShutdownAndBlock(); }
  24. base::test::TaskEnvironment task_environment_{
  25. base::test::TaskEnvironment::MainThreadType::IO};
  26. scoped_refptr<Bus> bus_;
  27. };
  28. // Used as a WaitForServiceToBeAvailableCallback.
  29. void OnServiceIsAvailable(bool* dest_service_is_available,
  30. int* num_calls,
  31. bool src_service_is_available) {
  32. *dest_service_is_available = src_service_is_available;
  33. (*num_calls)++;
  34. }
  35. // Used as a callback for TestService::RequestOwnership().
  36. void OnOwnershipRequestDone(bool success) {
  37. ASSERT_TRUE(success);
  38. }
  39. // Used as a callback for TestService::ReleaseOwnership().
  40. void OnOwnershipReleased() {}
  41. TEST_F(ObjectProxyTest, WaitForServiceToBeAvailableRunOnce) {
  42. TestService::Options options;
  43. TestService test_service(options);
  44. ObjectProxy* object_proxy = bus_->GetObjectProxy(
  45. test_service.service_name(), ObjectPath("/org/chromium/TestObject"));
  46. // The callback is not yet called because the service is not available.
  47. int num_calls = 0;
  48. bool service_is_available = false;
  49. object_proxy->WaitForServiceToBeAvailable(
  50. base::BindOnce(&OnServiceIsAvailable, &service_is_available, &num_calls));
  51. base::RunLoop().RunUntilIdle();
  52. EXPECT_EQ(0, num_calls);
  53. // Start the service. The callback should be called asynchronously.
  54. ASSERT_TRUE(test_service.StartService());
  55. test_service.WaitUntilServiceIsStarted();
  56. ASSERT_TRUE(test_service.has_ownership());
  57. num_calls = 0;
  58. base::RunLoop().RunUntilIdle();
  59. EXPECT_EQ(1, num_calls);
  60. EXPECT_TRUE(service_is_available);
  61. // Release the service's ownership of its name. The callback should not be
  62. // invoked again.
  63. test_service.ReleaseOwnership(base::BindOnce(&OnOwnershipReleased));
  64. num_calls = 0;
  65. base::RunLoop().RunUntilIdle();
  66. EXPECT_EQ(0, num_calls);
  67. // Take ownership of the name and check that the callback is not called.
  68. test_service.RequestOwnership(base::BindOnce(&OnOwnershipRequestDone));
  69. num_calls = 0;
  70. base::RunLoop().RunUntilIdle();
  71. EXPECT_EQ(0, num_calls);
  72. }
  73. TEST_F(ObjectProxyTest, WaitForServiceToBeAvailableAlreadyRunning) {
  74. TestService::Options options;
  75. TestService test_service(options);
  76. ObjectProxy* object_proxy = bus_->GetObjectProxy(
  77. test_service.service_name(), ObjectPath("/org/chromium/TestObject"));
  78. ASSERT_TRUE(test_service.StartService());
  79. test_service.WaitUntilServiceIsStarted();
  80. ASSERT_TRUE(test_service.has_ownership());
  81. // Since the service is already running, the callback should be invoked
  82. // immediately (but asynchronously, rather than the callback being invoked
  83. // directly within WaitForServiceToBeAvailable()).
  84. int num_calls = 0;
  85. bool service_is_available = false;
  86. object_proxy->WaitForServiceToBeAvailable(
  87. base::BindOnce(&OnServiceIsAvailable, &service_is_available, &num_calls));
  88. EXPECT_EQ(0, num_calls);
  89. base::RunLoop().RunUntilIdle();
  90. EXPECT_EQ(1, num_calls);
  91. EXPECT_TRUE(service_is_available);
  92. }
  93. TEST_F(ObjectProxyTest, WaitForServiceToBeAvailableMultipleCallbacks) {
  94. TestService::Options options;
  95. TestService test_service(options);
  96. ObjectProxy* object_proxy = bus_->GetObjectProxy(
  97. test_service.service_name(), ObjectPath("/org/chromium/TestObject"));
  98. // Register two callbacks.
  99. int num_calls_1 = 0, num_calls_2 = 0;
  100. bool service_is_available_1 = false, service_is_available_2 = false;
  101. object_proxy->WaitForServiceToBeAvailable(base::BindOnce(
  102. &OnServiceIsAvailable, &service_is_available_1, &num_calls_1));
  103. object_proxy->WaitForServiceToBeAvailable(base::BindOnce(
  104. &OnServiceIsAvailable, &service_is_available_2, &num_calls_2));
  105. base::RunLoop().RunUntilIdle();
  106. EXPECT_EQ(0, num_calls_1);
  107. EXPECT_EQ(0, num_calls_2);
  108. // Start the service and confirm that both callbacks are invoked.
  109. ASSERT_TRUE(test_service.StartService());
  110. test_service.WaitUntilServiceIsStarted();
  111. ASSERT_TRUE(test_service.has_ownership());
  112. num_calls_1 = 0;
  113. num_calls_2 = 0;
  114. base::RunLoop().RunUntilIdle();
  115. EXPECT_EQ(1, num_calls_1);
  116. EXPECT_EQ(1, num_calls_2);
  117. EXPECT_TRUE(service_is_available_1);
  118. EXPECT_TRUE(service_is_available_2);
  119. }
  120. } // namespace
  121. } // namespace dbus