Unity Scopes API
TypedScopeFixture.h
1 /*
2  * Copyright (C) 2013 Canonical Ltd
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 3 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Thomas Voß <thomas.voss@canonical.com>
17  */
18 
19 #pragma once
20 
21 #include <unity/scopes/Version.h>
22 
23 #include <unity/scopes/testing/MockRegistry.h>
24 
25 #pragma GCC diagnostic push
26 #pragma GCC diagnostic ignored "-Wctor-dtor-privacy"
27 #include <gtest/gtest.h>
28 #pragma GCC diagnostic pop
29 
30 #include <memory>
31 
32 namespace unity
33 {
34 
35 namespace scopes
36 {
37 
38 class ScopeBase;
39 
40 namespace testing
41 {
42 
44 
45 template<typename Scope>
46 struct ScopeTraits
47 {
48  inline static const char* name()
49  {
50  return "unknown";
51  }
52 
53  inline static std::shared_ptr<Scope> construct()
54  {
55  return std::make_shared<Scope>();
56  }
57 };
58 
59 class TypedScopeFixtureHelper
60 {
61  static void set_scope_directory(std::shared_ptr<ScopeBase> const& scope, std::string const& path);
62  static void set_cache_directory(std::shared_ptr<ScopeBase> const& scope, std::string const& path);
63  static void set_app_directory(std::shared_ptr<ScopeBase> const& scope, std::string const& path);
64  static void set_tmp_directory(std::shared_ptr<ScopeBase> const& scope, std::string const& path);
65  static void set_registry(std::shared_ptr<ScopeBase> const& scope, RegistryProxy const& r);
66 
67  template<typename Scope>
68  friend class TypedScopeFixture;
69 };
70 
72 
79 template<typename Scope>
80 class TypedScopeFixture : public ::testing::Test
81 {
82 public:
85  : registry_proxy(&registry, [](unity::scopes::Registry*) {})
86  , scope(ScopeTraits<Scope>::construct())
87  {
88  TypedScopeFixtureHelper::set_registry(scope, registry_proxy);
89  TypedScopeFixtureHelper::set_scope_directory(scope, "/tmp");
90  TypedScopeFixtureHelper::set_app_directory(scope, "/tmp");
91  }
92 
93  void SetUp()
94  {
95  ASSERT_NO_THROW(scope->start(ScopeTraits<Scope>::name()));
96  ASSERT_NO_THROW(scope->run());
97  }
98 
99  void set_scope_directory(std::string const& path)
100  {
101  TypedScopeFixtureHelper::set_scope_directory(scope, path);
102  }
103 
104  void set_cache_directory(std::string const& path)
105  {
106  TypedScopeFixtureHelper::set_cache_directory(scope, path);
107  }
108 
109  void set_app_directory(std::string const& path)
110  {
111  TypedScopeFixtureHelper::set_app_directory(scope, path);
112  }
113 
114  void set_tmp_directory(std::string const& path)
115  {
116  TypedScopeFixtureHelper::set_tmp_directory(scope, path);
117  }
118 
119  static void set_registry(std::shared_ptr<ScopeBase> const& scope, RegistryProxy const& r)
120  {
121  TypedScopeFixtureHelper::set_registry(scope, r);
122  }
123 
124  void TearDown()
125  {
126  EXPECT_NO_THROW(scope->stop());
127  }
128 
129 protected:
130  unity::scopes::testing::MockRegistry registry;
131  unity::scopes::RegistryProxy registry_proxy;
132  std::shared_ptr<Scope> scope;
134 };
135 
136 
137 } // namespace testing
138 
139 } // namespace scopes
140 
141 } // namespace unity
Fixture for testing scope testing.
Definition: TypedScopeFixture.h:80
Definition: OnlineAccountClient.h:39
Top-level namespace for all things Unity-related.
Definition: Version.h:49
White pages service for available scopes.
Definition: Registry.h:51
std::shared_ptr< Registry > RegistryProxy
Convenience type definition.
Definition: RegistryProxyFwd.h:33