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