OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef SERVICES_SHELL_PUBLIC_CPP_SERVICE_TEST_H_ | |
6 #define SERVICES_SHELL_PUBLIC_CPP_SERVICE_TEST_H_ | |
7 | |
8 #include <memory> | |
9 | |
10 #include "base/macros.h" | |
11 #include "services/shell/public/cpp/connector.h" | |
12 #include "services/shell/public/cpp/service.h" | |
13 #include "services/shell/public/cpp/service_context.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 namespace base { | |
17 class MessageLoop; | |
18 } | |
19 | |
20 namespace shell { | |
21 | |
22 class BackgroundShell; | |
23 | |
24 namespace test { | |
25 | |
26 class ServiceTest; | |
27 | |
28 // A default implementation of Service for use in ServiceTests. Tests wishing | |
29 // to customize this should subclass this class instead of Service, | |
30 // otherwise they will have to call ServiceTest::OnStartCalled() to forward | |
31 // metadata from OnStart() to the test. | |
32 class ServiceTestClient : public Service { | |
33 public: | |
34 explicit ServiceTestClient(ServiceTest* test); | |
35 ~ServiceTestClient() override; | |
36 | |
37 protected: | |
38 void OnStart(const Identity& identity) override; | |
39 | |
40 private: | |
41 ServiceTest* test_; | |
42 | |
43 DISALLOW_COPY_AND_ASSIGN(ServiceTestClient); | |
44 }; | |
45 | |
46 class ServiceTest : public testing::Test { | |
47 public: | |
48 ServiceTest(); | |
49 // Initialize passing the name to use as the identity for the test itself. | |
50 // Once set via this constructor, it cannot be changed later by calling | |
51 // InitTestName(). The test executable must provide a manifest in the | |
52 // appropriate location that specifies this name also. | |
53 explicit ServiceTest(const std::string& test_name); | |
54 ~ServiceTest() override; | |
55 | |
56 protected: | |
57 // See constructor. Can only be called once. | |
58 void InitTestName(const std::string& test_name); | |
59 | |
60 Connector* connector() { return connector_; } | |
61 | |
62 // Instance information received from the Service Manager during OnStart(). | |
63 const std::string& test_name() const { return initialize_name_; } | |
64 const std::string& test_userid() const { return initialize_userid_; } | |
65 uint32_t test_instance_id() const { return initialize_instance_id_; } | |
66 | |
67 // By default, creates a simple Service that captures the metadata sent | |
68 // via OnStart(). Override to customize, but custom implementations must | |
69 // call OnStartCalled() to forward the metadata so test_name() etc all | |
70 // work. | |
71 virtual std::unique_ptr<Service> CreateService(); | |
72 | |
73 virtual std::unique_ptr<base::MessageLoop> CreateMessageLoop(); | |
74 | |
75 // Call to set OnStart() metadata when GetService() is overridden. | |
76 void OnStartCalled(Connector* connector, | |
77 const std::string& name, | |
78 const std::string& userid); | |
79 | |
80 // testing::Test: | |
81 void SetUp() override; | |
82 void TearDown() override; | |
83 | |
84 private: | |
85 friend ServiceTestClient; | |
86 | |
87 std::unique_ptr<Service> service_; | |
88 | |
89 std::unique_ptr<base::MessageLoop> message_loop_; | |
90 std::unique_ptr<BackgroundShell> background_shell_; | |
91 std::unique_ptr<ServiceContext> service_context_; | |
92 | |
93 // See constructor. | |
94 std::string test_name_; | |
95 | |
96 Connector* connector_ = nullptr; | |
97 std::string initialize_name_; | |
98 std::string initialize_userid_ = shell::mojom::kInheritUserID; | |
99 uint32_t initialize_instance_id_ = shell::mojom::kInvalidInstanceID; | |
100 | |
101 base::Closure initialize_called_; | |
102 | |
103 DISALLOW_COPY_AND_ASSIGN(ServiceTest); | |
104 }; | |
105 | |
106 } // namespace test | |
107 } // namespace shell | |
108 | |
109 #endif // SERVICES_SHELL_PUBLIC_CPP_SERVICE_TEST_H_ | |
OLD | NEW |