2
* Copyright (C) 2017 Canonical Ltd
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.
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.
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/>.
16
* Authored by: Pete Woods <pete.woods@canonical.com>
19
#include <unity/util/GObjectMemory.h>
21
#include <gtest/gtest.h>
25
using namespace unity;
26
using namespace unity::util;
31
// Below here is a basic implementation of a GObject type
38
#define FOO_TYPE_BAR foo_bar_get_type ()
39
G_DECLARE_FINAL_TYPE (FooBar, foo_bar, FOO, BAR, GObject)
41
FooBar *foo_bar_new (void);
45
// private implementation
48
GObject parent_instance;
55
G_DEFINE_TYPE (FooBar, foo_bar, G_TYPE_OBJECT)
64
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
66
static void foo_bar_set_property(GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) {
67
FooBar *self = FOO_BAR (object);
69
switch (property_id) {
72
self->name = g_value_dup_string(value);
76
self->id = g_value_get_uint(value);
80
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
85
static void foo_bar_get_property(GObject *object, guint property_id, GValue *value, GParamSpec *pspec) {
86
FooBar *self = FOO_BAR(object);
88
switch (property_id) {
90
g_value_set_string(value, self->name);
94
g_value_set_uint(value, self->id);
98
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
103
static void foo_bar_finalize (GObject *gobject) {
104
FooBar* self = FOO_BAR(gobject);
108
G_OBJECT_CLASS (foo_bar_parent_class)->finalize (gobject);
111
static void foo_bar_class_init (FooBarClass *klass) {
112
GObjectClass *object_class = G_OBJECT_CLASS (klass);
114
object_class->set_property = foo_bar_set_property;
115
object_class->get_property = foo_bar_get_property;
116
object_class->finalize = foo_bar_finalize;
118
obj_properties[PROP_NAME] =
119
g_param_spec_string ("name",
121
"Name of the file to load and display from.",
122
"default-name" /* default value */,
123
(GParamFlags) (G_PARAM_READWRITE));
125
obj_properties[PROP_ID] =
126
g_param_spec_uint ("id",
128
"ID of this dummy class",
129
0 /* minimum value */,
130
10 /* maximum value */,
131
2 /* default value */,
132
(GParamFlags) G_PARAM_READWRITE);
134
g_object_class_install_properties (object_class,
139
static void foo_bar_init (FooBar *) {
142
FooBar *foo_bar_new (const gchar* const name, guint id) {
143
return FOO_BAR(g_object_new(FOO_TYPE_BAR, "name", name, "id", id, NULL));
150
typedef pair<const char*, guint> GObjectMemoryMakeSharedTestParam;
152
class GObjectMemoryMakeHelperMethodsTest: public testing::TestWithParam<GObjectMemoryMakeSharedTestParam> {
155
* We test for multiple properties so that we can be sure the various
156
* helper methods correctly pass through different data types and
157
* multiple combinations of arguments correctly.
159
static void checkProperties(gpointer obj, const char* expectedName, guint expectedId) {
163
g_object_get(obj, "name", &name, "id", &id, NULL);
164
EXPECT_STREQ(expectedName, name);
165
EXPECT_EQ(expectedId, id);
171
TEST_P(GObjectMemoryMakeHelperMethodsTest, make_gobject_passes_arguments)
174
auto obj = make_gobject<FooBar>(FOO_TYPE_BAR, "name", p.first, "id", p.second, nullptr);
175
checkProperties(obj.get(), p.first, p.second);
178
TEST_P(GObjectMemoryMakeHelperMethodsTest, make_gobject_no_arguments)
181
auto obj = make_gobject<FooBar>(FOO_TYPE_BAR, nullptr);
182
g_object_set(obj.get(), "name", p.first, "id", p.second, nullptr);
183
checkProperties(obj.get(), p.first, p.second);
186
TEST_P(GObjectMemoryMakeHelperMethodsTest, foo_bar_new)
189
auto obj = gobj_ptr<FooBar>(foo_bar_new(p.first,p.second));
190
checkProperties(obj.get(), p.first, p.second);
193
TEST_P(GObjectMemoryMakeHelperMethodsTest, share_gobject_from_gobj_ptr)
196
auto obj = share_gobject(make_gobject<FooBar>(FOO_TYPE_BAR, "name", p.first, "id", p.second, nullptr));
197
checkProperties(obj.get(), p.first, p.second);
200
TEST_P(GObjectMemoryMakeHelperMethodsTest, share_gobject_from_raw_gobject)
203
auto obj = share_gobject(foo_bar_new(p.first, p.second));
204
checkProperties(obj.get(), p.first, p.second);
207
INSTANTIATE_TEST_CASE_P(BunchOfNames,
208
GObjectMemoryMakeHelperMethodsTest,
209
::testing::Values(GObjectMemoryMakeSharedTestParam{"meeny", 1},
210
GObjectMemoryMakeSharedTestParam{"miny", 2},
211
GObjectMemoryMakeSharedTestParam{"moe", 3}));