~pete-woods/unity-api/gobject-backup

« back to all changes in this revision

Viewing changes to test/gtest/unity/util/GObjectMemory/GObjectMemoryMakeShared_test.cpp

  • Committer: Pete Woods
  • Date: 2017-01-17 13:38:34 UTC
  • Revision ID: pete.woods@canonical.com-20170117133834-3obv8rzo4411ijn6
unity::shell::util - add GObject shared memory utility classes and helper methods.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2017 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: Pete Woods <pete.woods@canonical.com>
 
17
 */
 
18
 
 
19
#include <unity/util/GObjectMemory.h>
 
20
 
 
21
#include <gtest/gtest.h>
 
22
#include <string>
 
23
 
 
24
using namespace std;
 
25
using namespace unity;
 
26
using namespace unity::util;
 
27
 
 
28
namespace {
 
29
 
 
30
//
 
31
// Below here is a basic implementation of a GObject type
 
32
//
 
33
 
 
34
// "public" header
 
35
 
 
36
G_BEGIN_DECLS
 
37
 
 
38
#define FOO_TYPE_BAR foo_bar_get_type ()
 
39
G_DECLARE_FINAL_TYPE (FooBar, foo_bar, FOO, BAR, GObject)
 
40
 
 
41
FooBar *foo_bar_new (void);
 
42
 
 
43
G_END_DECLS
 
44
 
 
45
// private implementation
 
46
 
 
47
struct _FooBar {
 
48
    GObject parent_instance;
 
49
 
 
50
    gchar *name;
 
51
 
 
52
    guint id;
 
53
};
 
54
 
 
55
G_DEFINE_TYPE (FooBar, foo_bar, G_TYPE_OBJECT)
 
56
 
 
57
enum
 
58
{
 
59
  PROP_NAME = 1,
 
60
  PROP_ID,
 
61
  N_PROPERTIES
 
62
};
 
63
 
 
64
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
 
65
 
 
66
static void foo_bar_set_property(GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) {
 
67
    FooBar *self = FOO_BAR (object);
 
68
 
 
69
    switch (property_id) {
 
70
        case PROP_NAME:
 
71
            g_free(self->name);
 
72
            self->name = g_value_dup_string(value);
 
73
            break;
 
74
 
 
75
        case PROP_ID:
 
76
            self->id = g_value_get_uint(value);
 
77
            break;
 
78
 
 
79
        default:
 
80
            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
 
81
            break;
 
82
    }
 
83
}
 
84
 
 
85
static void foo_bar_get_property(GObject *object, guint property_id, GValue *value, GParamSpec *pspec) {
 
86
    FooBar *self = FOO_BAR(object);
 
87
 
 
88
    switch (property_id) {
 
89
        case PROP_NAME:
 
90
            g_value_set_string(value, self->name);
 
91
            break;
 
92
 
 
93
        case PROP_ID:
 
94
            g_value_set_uint(value, self->id);
 
95
            break;
 
96
 
 
97
        default:
 
98
            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
 
99
            break;
 
100
    }
 
101
}
 
102
 
 
103
static void foo_bar_finalize (GObject *gobject) {
 
104
    FooBar* self = FOO_BAR(gobject);
 
105
 
 
106
    g_free (self->name);
 
107
 
 
108
    G_OBJECT_CLASS (foo_bar_parent_class)->finalize (gobject);
 
109
}
 
110
 
 
111
static void foo_bar_class_init (FooBarClass *klass) {
 
112
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
113
 
 
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;
 
117
 
 
118
    obj_properties[PROP_NAME] =
 
119
      g_param_spec_string ("name",
 
120
                           "Name",
 
121
                           "Name of the file to load and display from.",
 
122
                           "default-name"  /* default value */,
 
123
                           (GParamFlags) (G_PARAM_READWRITE));
 
124
 
 
125
    obj_properties[PROP_ID] =
 
126
      g_param_spec_uint ("id",
 
127
                         "ID",
 
128
                         "ID of this dummy class",
 
129
                         0  /* minimum value */,
 
130
                         10 /* maximum value */,
 
131
                         2  /* default value */,
 
132
                         (GParamFlags) G_PARAM_READWRITE);
 
133
 
 
134
    g_object_class_install_properties (object_class,
 
135
                                       N_PROPERTIES,
 
136
                                       obj_properties);
 
137
}
 
138
 
 
139
static void foo_bar_init (FooBar *) {
 
140
}
 
141
 
 
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));
 
144
}
 
145
 
 
146
//
 
147
// Test cases
 
148
//
 
149
 
 
150
typedef pair<const char*, guint> GObjectMemoryMakeSharedTestParam;
 
151
 
 
152
class GObjectMemoryMakeHelperMethodsTest: public testing::TestWithParam<GObjectMemoryMakeSharedTestParam> {
 
153
protected:
 
154
    /**
 
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.
 
158
     */
 
159
    static void checkProperties(gpointer obj, const char* expectedName, guint expectedId) {
 
160
        char* name = NULL;
 
161
        guint id = 0;
 
162
 
 
163
        g_object_get(obj, "name", &name, "id", &id, NULL);
 
164
        EXPECT_STREQ(expectedName, name);
 
165
        EXPECT_EQ(expectedId, id);
 
166
 
 
167
        g_free(name);
 
168
    }
 
169
};
 
170
 
 
171
TEST_P(GObjectMemoryMakeHelperMethodsTest, make_gobject_passes_arguments)
 
172
{
 
173
    auto p = GetParam();
 
174
    auto obj = make_gobject<FooBar>(FOO_TYPE_BAR, "name", p.first, "id", p.second, nullptr);
 
175
    checkProperties(obj.get(), p.first, p.second);
 
176
}
 
177
 
 
178
TEST_P(GObjectMemoryMakeHelperMethodsTest, make_gobject_no_arguments)
 
179
{
 
180
    auto p = GetParam();
 
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);
 
184
}
 
185
 
 
186
TEST_P(GObjectMemoryMakeHelperMethodsTest, foo_bar_new)
 
187
{
 
188
    auto p = GetParam();
 
189
    auto obj = gobj_ptr<FooBar>(foo_bar_new(p.first,p.second));
 
190
    checkProperties(obj.get(), p.first, p.second);
 
191
}
 
192
 
 
193
TEST_P(GObjectMemoryMakeHelperMethodsTest, share_gobject_from_gobj_ptr)
 
194
{
 
195
    auto p = GetParam();
 
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);
 
198
}
 
199
 
 
200
TEST_P(GObjectMemoryMakeHelperMethodsTest, share_gobject_from_raw_gobject)
 
201
{
 
202
    auto p = GetParam();
 
203
    auto obj = share_gobject(foo_bar_new(p.first, p.second));
 
204
    checkProperties(obj.get(), p.first, p.second);
 
205
}
 
206
 
 
207
INSTANTIATE_TEST_CASE_P(BunchOfNames,
 
208
                        GObjectMemoryMakeHelperMethodsTest,
 
209
                        ::testing::Values(GObjectMemoryMakeSharedTestParam{"meeny", 1},
 
210
                                          GObjectMemoryMakeSharedTestParam{"miny", 2},
 
211
                                          GObjectMemoryMakeSharedTestParam{"moe", 3}));
 
212
 
 
213
}