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 #include <gtest/gtest.h>
26 
27 #include <memory>
28 
29 namespace unity
30 {
31 
32 namespace scopes
33 {
34 
35 class ScopeBase;
36 
37 namespace testing
38 {
39 
41 
42 template<typename Scope>
43 struct ScopeTraits
44 {
45  inline static const char* name()
46  {
47  return "unknown";
48  }
49 
50  inline static std::shared_ptr<Scope> construct()
51  {
52  return std::make_shared<Scope>();
53  }
54 };
55 
56 class TypedScopeFixtureHelper
57 {
58  static void set_scope_directory(std::shared_ptr<ScopeBase> const& scope, std::string const& path);
59  static void set_cache_directory(std::shared_ptr<ScopeBase> const& scope, std::string const& path);
60  static void set_app_directory(std::shared_ptr<ScopeBase> const& scope, std::string const& path);
61  static void set_tmp_directory(std::shared_ptr<ScopeBase> const& scope, std::string const& path);
62  static void set_registry(std::shared_ptr<ScopeBase> const& scope, RegistryProxy const& r);
63 
64  template<typename Scope>
65  friend class TypedScopeFixture;
66 };
67 
69 
76 template<typename Scope>
77 class TypedScopeFixture : public ::testing::Test
78 {
79 public:
82  : registry_proxy(&registry, [](unity::scopes::Registry*) {})
83  , scope(ScopeTraits<Scope>::construct())
84  {
85  TypedScopeFixtureHelper::set_registry(scope, registry_proxy);
86  TypedScopeFixtureHelper::set_scope_directory(scope, "/tmp");
87  TypedScopeFixtureHelper::set_app_directory(scope, "/tmp");
88  }
89 
90  void SetUp()
91  {
92  ASSERT_NO_THROW(scope->start(ScopeTraits<Scope>::name()));
93  ASSERT_NO_THROW(scope->run());
94  }
95 
96  void set_scope_directory(std::string const& path)
97  {
98  TypedScopeFixtureHelper::set_scope_directory(scope, path);
99  }
100 
101  void set_cache_directory(std::string const& path)
102  {
103  TypedScopeFixtureHelper::set_cache_directory(scope, path);
104  }
105 
106  void set_app_directory(std::string const& path)
107  {
108  TypedScopeFixtureHelper::set_app_directory(scope, path);
109  }
110 
111  void set_tmp_directory(std::string const& path)
112  {
113  TypedScopeFixtureHelper::set_tmp_directory(scope, path);
114  }
115 
116  static void set_registry(std::shared_ptr<ScopeBase> const& scope, RegistryProxy const& r)
117  {
118  TypedScopeFixtureHelper::set_registry(scope, r);
119  }
120 
121  void TearDown()
122  {
123  EXPECT_NO_THROW(scope->stop());
124  }
125 
126 protected:
127  unity::scopes::testing::MockRegistry registry;
128  unity::scopes::RegistryProxy registry_proxy;
129  std::shared_ptr<Scope> scope;
131 };
132 
133 
134 } // namespace testing
135 
136 } // namespace scopes
137 
138 } // namespace unity
Fixture for testing scope testing.
Definition: TypedScopeFixture.h:77
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