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  }
87 
88  void SetUp()
89  {
90  ASSERT_NO_THROW(scope->start(ScopeTraits<Scope>::name()));
91  ASSERT_NO_THROW(scope->run());
92  }
93 
94  void set_scope_directory(std::string const& path)
95  {
96  TypedScopeFixtureHelper::set_scope_directory(scope, path);
97  }
98 
99  void set_cache_directory(std::string const& path)
100  {
101  TypedScopeFixtureHelper::set_cache_directory(scope, path);
102  }
103 
104  void set_app_directory(std::string const& path)
105  {
106  TypedScopeFixtureHelper::set_app_directory(scope, path);
107  }
108 
109  void set_tmp_directory(std::string const& path)
110  {
111  TypedScopeFixtureHelper::set_tmp_directory(scope, path);
112  }
113 
114  static void set_registry(std::shared_ptr<ScopeBase> const& scope, RegistryProxy const& r)
115  {
116  TypedScopeFixtureHelper::set_registry(scope, r);
117  }
118 
119  void TearDown()
120  {
121  EXPECT_NO_THROW(scope->stop());
122  }
123 
124 protected:
125  unity::scopes::testing::MockRegistry registry;
126  unity::scopes::RegistryProxy registry_proxy;
127  std::shared_ptr<Scope> scope;
129 };
130 
131 
132 } // namespace testing
133 
134 } // namespace scopes
135 
136 } // 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