| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/memory/ref_counted.h" | 6 #include "base/memory/ref_counted.h" |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "dbus/bus.h" | 8 #include "dbus/bus.h" |
| 9 #include "dbus/object_proxy.h" | 9 #include "dbus/object_proxy.h" |
| 10 #include "dbus/test_service.h" | 10 #include "dbus/test_service.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 namespace dbus { | 13 namespace dbus { |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 class ObjectProxyTest : public testing::Test { | 16 class ObjectProxyTest : public testing::Test { |
| 17 protected: | 17 protected: |
| 18 void SetUp() override { | 18 void SetUp() override { |
| 19 Bus::Options bus_options; | 19 Bus::Options bus_options; |
| 20 bus_options.bus_type = Bus::SESSION; | 20 bus_options.bus_type = Bus::SESSION; |
| 21 bus_options.connection_type = Bus::PRIVATE; | 21 bus_options.connection_type = Bus::PRIVATE; |
| 22 bus_ = new Bus(bus_options); | 22 bus_ = new Bus(bus_options); |
| 23 | |
| 24 object_proxy_ = bus_->GetObjectProxy( | |
| 25 "org.chromium.TestService", ObjectPath("/org/chromium/TestObject")); | |
| 26 } | 23 } |
| 27 | 24 |
| 28 void TearDown() override { bus_->ShutdownAndBlock(); } | 25 void TearDown() override { bus_->ShutdownAndBlock(); } |
| 29 | 26 |
| 30 base::MessageLoopForIO message_loop_; | 27 base::MessageLoopForIO message_loop_; |
| 31 scoped_refptr<Bus> bus_; | 28 scoped_refptr<Bus> bus_; |
| 32 ObjectProxy* object_proxy_; | |
| 33 }; | 29 }; |
| 34 | 30 |
| 35 // Used as a WaitForServiceToBeAvailableCallback. | 31 // Used as a WaitForServiceToBeAvailableCallback. |
| 36 void OnServiceIsAvailable(scoped_ptr<base::RunLoop>* run_loop, | 32 void OnServiceIsAvailable(scoped_ptr<base::RunLoop>* run_loop, |
| 37 bool service_is_available) { | 33 bool service_is_available) { |
| 38 EXPECT_TRUE(service_is_available); | 34 EXPECT_TRUE(service_is_available); |
| 39 ASSERT_TRUE(*run_loop); | 35 ASSERT_TRUE(*run_loop); |
| 40 (*run_loop)->Quit(); | 36 (*run_loop)->Quit(); |
| 41 } | 37 } |
| 42 | 38 |
| 43 TEST_F(ObjectProxyTest, WaitForServiceToBeAvailable) { | 39 TEST_F(ObjectProxyTest, WaitForServiceToBeAvailable) { |
| 44 scoped_ptr<base::RunLoop> run_loop; | 40 scoped_ptr<base::RunLoop> run_loop; |
| 45 | 41 |
| 42 TestService::Options options; |
| 43 TestService test_service(options); |
| 44 |
| 46 // Callback is not yet called because the service is not available. | 45 // Callback is not yet called because the service is not available. |
| 47 object_proxy_->WaitForServiceToBeAvailable( | 46 ObjectProxy* object_proxy = bus_->GetObjectProxy( |
| 47 test_service.service_name(), ObjectPath("/org/chromium/TestObject")); |
| 48 object_proxy->WaitForServiceToBeAvailable( |
| 48 base::Bind(&OnServiceIsAvailable, &run_loop)); | 49 base::Bind(&OnServiceIsAvailable, &run_loop)); |
| 49 base::RunLoop().RunUntilIdle(); | 50 base::RunLoop().RunUntilIdle(); |
| 50 | 51 |
| 51 // Start the service. | 52 // Start the service. |
| 52 TestService::Options options; | |
| 53 TestService test_service(options); | |
| 54 ASSERT_TRUE(test_service.StartService()); | 53 ASSERT_TRUE(test_service.StartService()); |
| 55 ASSERT_TRUE(test_service.WaitUntilServiceIsStarted()); | 54 ASSERT_TRUE(test_service.WaitUntilServiceIsStarted()); |
| 56 ASSERT_TRUE(test_service.has_ownership()); | 55 ASSERT_TRUE(test_service.has_ownership()); |
| 57 | 56 |
| 58 // Callback is called beacuse the service became available. | 57 // Callback is called beacuse the service became available. |
| 59 run_loop.reset(new base::RunLoop); | 58 run_loop.reset(new base::RunLoop); |
| 60 run_loop->Run(); | 59 run_loop->Run(); |
| 61 | 60 |
| 62 // Callback is called because the service is already available. | 61 // Callback is called because the service is already available. |
| 63 run_loop.reset(new base::RunLoop); | 62 run_loop.reset(new base::RunLoop); |
| 64 object_proxy_->WaitForServiceToBeAvailable( | 63 object_proxy->WaitForServiceToBeAvailable( |
| 65 base::Bind(&OnServiceIsAvailable, &run_loop)); | 64 base::Bind(&OnServiceIsAvailable, &run_loop)); |
| 66 run_loop->Run(); | 65 run_loop->Run(); |
| 67 | 66 |
| 68 // Shut down the service. | 67 // Shut down the service. |
| 69 test_service.ShutdownAndBlock(); | 68 test_service.ShutdownAndBlock(); |
| 70 test_service.Stop(); | 69 test_service.Stop(); |
| 71 } | 70 } |
| 72 | 71 |
| 73 } // namespace | 72 } // namespace |
| 74 } // namespace dbus | 73 } // namespace dbus |
| OLD | NEW |