OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "services/service_manager/background/background_service_manager.h" | 5 #include "services/service_manager/background/background_service_manager.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "services/service_manager/background/tests/test.mojom.h" | 11 #include "services/service_manager/background/tests/test.mojom.h" |
12 #include "services/service_manager/public/cpp/connector.h" | 12 #include "services/service_manager/public/cpp/connector.h" |
13 #include "services/service_manager/public/cpp/service.h" | 13 #include "services/service_manager/public/cpp/service.h" |
14 #include "services/service_manager/public/cpp/service_context.h" | 14 #include "services/service_manager/public/cpp/service_context.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 namespace service_manager { | 17 namespace service_manager { |
18 namespace { | 18 namespace { |
19 | 19 |
20 const char kTestName[] = "background_service_manager_unittest"; | 20 const char kTestName[] = "background_service_manager_unittest"; |
21 const char kAppName[] = "background_service_manager_test_service"; | 21 const char kAppName[] = "background_service_manager_test_service"; |
22 | 22 |
| 23 // The parent unit test suite service, not the underlying test service. |
23 class ServiceImpl : public Service { | 24 class ServiceImpl : public Service { |
24 public: | 25 public: |
25 ServiceImpl() {} | 26 ServiceImpl() {} |
26 ~ServiceImpl() override {} | 27 ~ServiceImpl() override {} |
27 | 28 |
28 // Service: | 29 // Service: |
29 bool OnConnect(const ServiceInfo& remote_info, | 30 bool OnConnect(const ServiceInfo& remote_info, |
30 InterfaceRegistry* registry) override { | 31 InterfaceRegistry* registry) override { |
31 return false; | 32 return false; |
32 } | 33 } |
33 | 34 |
34 private: | 35 private: |
35 DISALLOW_COPY_AND_ASSIGN(ServiceImpl); | 36 DISALLOW_COPY_AND_ASSIGN(ServiceImpl); |
36 }; | 37 }; |
37 | 38 |
38 void SetFlagAndRunClosure(bool* flag, const base::Closure& closure) { | 39 void SetFlagAndRunClosure(bool* flag, const base::Closure& closure) { |
39 *flag = true; | 40 *flag = true; |
40 closure.Run(); | 41 closure.Run(); |
41 } | 42 } |
42 | 43 |
43 } // namespace | |
44 | |
45 // Uses BackgroundServiceManager to start the service manager in the background | 44 // Uses BackgroundServiceManager to start the service manager in the background |
46 // and connects to background_service_manager_test_service, verifying we can | 45 // and connects to background_service_manager_test_service, verifying we can |
47 // send a message to the service. | 46 // send a message to the service. |
48 #if defined(OS_ANDROID) | 47 #if defined(OS_ANDROID) |
49 // TODO(crbug.com/589784): This test is disabled, as it fails | 48 // TODO(crbug.com/589784): This test is disabled, as it fails |
50 // on the Android GN bot. | 49 // on the Android GN bot. |
51 #define MAYBE_Basic DISABLED_Basic | 50 #define MAYBE_Basic DISABLED_Basic |
52 #else | 51 #else |
53 #define MAYBE_Basic Basic | 52 #define MAYBE_Basic Basic |
54 #endif | 53 #endif |
55 TEST(BackgroundServiceManagerTest, MAYBE_Basic) { | 54 TEST(BackgroundServiceManagerTest, MAYBE_Basic) { |
56 BackgroundServiceManager background_service_manager(nullptr, nullptr); | 55 BackgroundServiceManager background_service_manager(nullptr, nullptr); |
57 base::MessageLoop message_loop; | 56 base::MessageLoop message_loop; |
58 mojom::ServicePtr service; | 57 mojom::ServicePtr service; |
59 ServiceContext service_context(base::MakeUnique<ServiceImpl>(), | 58 ServiceContext service_context(base::MakeUnique<ServiceImpl>(), |
60 mojom::ServiceRequest(&service)); | 59 mojom::ServiceRequest(&service)); |
61 background_service_manager.RegisterService( | 60 background_service_manager.RegisterService( |
62 Identity(kTestName, mojom::kRootUserID), std::move(service), nullptr); | 61 Identity(kTestName, mojom::kRootUserID), std::move(service), nullptr); |
63 | 62 |
64 mojom::TestServicePtr test_service; | 63 mojom::TestServicePtr test_service; |
65 service_context.connector()->BindInterface(kAppName, &test_service); | 64 service_context.connector()->BindInterface(kAppName, &test_service); |
66 base::RunLoop run_loop; | 65 base::RunLoop run_loop; |
67 bool got_result = false; | 66 bool got_result = false; |
68 test_service->Test( | 67 test_service->Test( |
69 base::Bind(&SetFlagAndRunClosure, &got_result, run_loop.QuitClosure())); | 68 base::Bind(&SetFlagAndRunClosure, &got_result, run_loop.QuitClosure())); |
70 run_loop.Run(); | 69 run_loop.Run(); |
71 EXPECT_TRUE(got_result); | 70 EXPECT_TRUE(got_result); |
72 } | 71 } |
73 | 72 |
| 73 // The out param cannot be last due to base::Bind() currying. |
| 74 void QuitCallback(bool* callback_called, |
| 75 const base::Closure& quit_closure, |
| 76 const Identity& identity) { |
| 77 EXPECT_EQ(kAppName, identity.name()); |
| 78 *callback_called = true; |
| 79 quit_closure.Run(); |
| 80 } |
| 81 |
| 82 // Verifies that quitting a service invokes the quit callback. |
| 83 TEST(BackgroundServiceManagerTest, SetInstanceQuitCallback) { |
| 84 BackgroundServiceManager background_service_manager(nullptr, nullptr); |
| 85 base::MessageLoop message_loop; |
| 86 mojom::ServicePtr service; |
| 87 ServiceContext service_context(base::MakeUnique<ServiceImpl>(), |
| 88 mojom::ServiceRequest(&service)); |
| 89 background_service_manager.RegisterService( |
| 90 Identity(kTestName, mojom::kRootUserID), std::move(service), nullptr); |
| 91 |
| 92 mojom::TestServicePtr test_service; |
| 93 service_context.connector()->BindInterface(kAppName, &test_service); |
| 94 |
| 95 // Set a callback for when the service quits that will quit |run_loop|. |
| 96 base::RunLoop run_loop; |
| 97 bool callback_called = false; |
| 98 background_service_manager.SetInstanceQuitCallback( |
| 99 base::Bind(&QuitCallback, &callback_called, run_loop.QuitClosure())); |
| 100 |
| 101 // Ask the service to quit itself. |
| 102 test_service->Quit(); |
| 103 run_loop.Run(); |
| 104 |
| 105 // The run loop was quit by the callback and not by something else. |
| 106 EXPECT_TRUE(callback_called); |
| 107 } |
| 108 |
| 109 } // namespace |
74 } // namespace service_manager | 110 } // namespace service_manager |
OLD | NEW |