~compiz-team/compiz/compiz.performance_1040478

« back to all changes in this revision

Viewing changes to compizconfig/integration/gnome/gsettings/tests/compizconfig_test_ccs_gnome_gsettings_integrated_setting.cpp

  • Committer: Sam Spilsbury
  • Date: 2012-10-02 01:21:28 UTC
  • mfrom: (3360.1.45 compiz)
  • Revision ID: sam.spilsbury@canonical.com-20121002012128-sw01jhz41guvcvnw
MergeĀ lp:compiz

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Compiz configuration system library
 
3
 *
 
4
 * Copyright (C) 2012 Canonical Ltd.
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Lesser General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2.1 of the License, or (at your option) any later version.
 
10
 
 
11
 * This library is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * Lesser General Public License for more details.
 
15
 
 
16
 * You should have received a copy of the GNU Lesser General Public
 
17
 * License along with this library; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 *
 
20
 * Authored By:
 
21
 * Sam Spilsbury <sam.spilsbury@canonical.com>
 
22
 */
 
23
#include <gtest/gtest.h>
 
24
#include <gmock/gmock.h>
 
25
 
 
26
#include <map>
 
27
#include <memory>
 
28
#include <tr1/tuple>
 
29
#include <boost/shared_ptr.hpp>
 
30
 
 
31
#include <glib_gslice_off_env.h>
 
32
#include <gtest_shared_autodestroy.h>
 
33
 
 
34
#include <ccs.h>
 
35
#include <ccs-backend.h>
 
36
#include <ccs_gnome_integrated_setting.h>
 
37
#include <ccs_gnome_integration_gsettings_integrated_setting.h>
 
38
#include <ccs_gsettings_wrapper_mock.h>
 
39
 
 
40
using ::testing::Combine;
 
41
using ::testing::Return;
 
42
using ::testing::IsNull;
 
43
using ::testing::ValuesIn;
 
44
using ::testing::Values;
 
45
using ::testing::Eq;
 
46
using ::testing::WithArgs;
 
47
using ::testing::_;
 
48
 
 
49
namespace compiz
 
50
{
 
51
    namespace config
 
52
    {
 
53
        namespace integration
 
54
        {
 
55
            namespace test
 
56
            {
 
57
                const std::string KEYBINDING_ONE = "keybinding_one";
 
58
                const std::string KEYBINDING_TWO = "keybinding_two";
 
59
                const std::string STRING = "string";
 
60
                const Bool        BOOLEAN = TRUE;
 
61
                const int         INTEGER = 2;
 
62
 
 
63
                const std::string STRING_ALT = "string_alt";
 
64
                const Bool        BOOLEAN_ALT = FALSE;
 
65
                const int         INTEGER_ALT = 1;
 
66
 
 
67
                namespace variant_generators
 
68
                {
 
69
                    GVariant * i ();
 
70
                    GVariant * s ();
 
71
                    GVariant * b ();
 
72
                    GVariant * as ();
 
73
                    GVariant * fromValue (CCSSettingValue *v, CCSSettingType type);
 
74
                }
 
75
 
 
76
                namespace value_generators
 
77
                {
 
78
                    CCSSettingValue * integer ();
 
79
                    CCSSettingValue * string ();
 
80
                    CCSSettingValue * key ();
 
81
                    CCSSettingValue * boolean ();
 
82
                }
 
83
 
 
84
                namespace expectations
 
85
                {
 
86
                    void integer (CCSSettingValue *);
 
87
                    void string  (CCSSettingValue *);
 
88
                    void boolean (CCSSettingValue *);
 
89
                    void key     (CCSSettingValue *);
 
90
 
 
91
                    void integerVariant (GVariant *, int);
 
92
                    void stringVariant (GVariant *, const std::string &);
 
93
                    void booleanVariant (GVariant *, bool);
 
94
                    void keyVariant (GVariant *, const std::string &);
 
95
                }
 
96
 
 
97
                typedef GVariant * (*VariantGenerator) ();
 
98
                typedef CCSSettingValue * (*ValueGenerator) ();
 
99
                typedef void (*Expectation) (CCSSettingValue *);
 
100
 
 
101
                struct GSettingsIntegratedSettingInfo
 
102
                {
 
103
                    VariantGenerator variantGenerator;
 
104
                    ValueGenerator   valueGenerator;
 
105
                    Expectation      expectation;
 
106
                    CCSSettingType   settingType;
 
107
                    CCSSettingType   returnType;
 
108
                };
 
109
 
 
110
                namespace impl
 
111
                {
 
112
                    namespace ccit = compiz::config::integration::test;
 
113
                    namespace vg = compiz::config::integration::test::variant_generators;
 
114
                    namespace cvg = compiz::config::integration::test::value_generators;
 
115
                    namespace ex = compiz::config::integration::test::expectations;
 
116
 
 
117
                    ccit::GSettingsIntegratedSettingInfo settingsInfo[] =
 
118
                    {
 
119
                        { vg::i, cvg::integer, ex::integer, TypeInt, TypeInt },
 
120
                        { vg::b, cvg::boolean, ex::boolean, TypeBool, TypeBool },
 
121
                        { vg::s, cvg::string, ex::string, TypeString, TypeString },
 
122
                        { vg::as, cvg::key, ex::key, TypeKey, TypeString }
 
123
                    };
 
124
                }
 
125
            }
 
126
        }
 
127
    }
 
128
}
 
129
 
 
130
MATCHER_P (VariantEqual, lhs, "Variants Equal")
 
131
{
 
132
    return g_variant_equal (lhs, arg);
 
133
}
 
134
 
 
135
namespace
 
136
{
 
137
    std::map <CCSSettingType, SpecialOptionType> &
 
138
    ccsTypeToSpecialType ()
 
139
    {
 
140
        static std::map <CCSSettingType, SpecialOptionType> types;
 
141
        static bool initialized = false;
 
142
 
 
143
        if (!initialized)
 
144
        {
 
145
            types[TypeInt] = OptionInt;
 
146
            types[TypeBool] = OptionBool;
 
147
            types[TypeString] = OptionString;
 
148
            types[TypeKey] = OptionKey;
 
149
        }
 
150
 
 
151
        return types;
 
152
    }
 
153
}
 
154
 
 
155
namespace ccit = compiz::config::integration::test;
 
156
namespace cciti = compiz::config::integration::test::impl;
 
157
namespace ccvg = compiz::config::integration::test::variant_generators;
 
158
namespace ccvalg = compiz::config::integration::test::value_generators;
 
159
namespace ccex = compiz::config::integration::test::expectations;
 
160
 
 
161
typedef std::tr1::tuple <CCSSettingType,
 
162
                         ccit::GSettingsIntegratedSettingInfo> CCSGSettingsIntegratedSettingTestInfo;
 
163
 
 
164
class CCSGSettingsIntegratedSettingTest :
 
165
    public ::testing::TestWithParam <CCSGSettingsIntegratedSettingTestInfo>
 
166
{
 
167
    public:
 
168
 
 
169
        virtual void SetUp ();
 
170
        virtual void TearDown ();
 
171
 
 
172
    protected:
 
173
 
 
174
        CompizGLibGSliceOffEnv                  env;
 
175
        boost::shared_ptr <CCSGSettingsWrapper> mWrapper;
 
176
        CCSGSettingsWrapperGMock                *mWrapperMock;
 
177
};
 
178
 
 
179
GVariant *
 
180
ccvg::fromValue (CCSSettingValue *v,
 
181
                 CCSSettingType  type)
 
182
{
 
183
    switch (type)
 
184
    {
 
185
        case TypeInt:
 
186
            return g_variant_new ("i", v->value.asInt);
 
187
            break;
 
188
        case TypeBool:
 
189
            return g_variant_new ("b", v->value.asBool);
 
190
            break;
 
191
        case TypeString:
 
192
            return g_variant_new ("s", v->value.asString);
 
193
            break;
 
194
        case TypeKey:
 
195
        {
 
196
            GVariantBuilder builder;
 
197
            g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
 
198
 
 
199
            /* Represented internally as strings */
 
200
            std::string kb (v->value.asString);
 
201
            if (kb == "Disabled")
 
202
                kb[0] = 'd';
 
203
 
 
204
            g_variant_builder_add (&builder, "s", kb.c_str ());
 
205
            return g_variant_builder_end (&builder);
 
206
        }
 
207
        default:
 
208
            break;
 
209
    }
 
210
 
 
211
    return NULL;
 
212
}
 
213
 
 
214
GVariant *
 
215
ccvg::as ()
 
216
{
 
217
    GVariantBuilder builder;
 
218
    g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
 
219
    g_variant_builder_add (&builder, "s", ccit::KEYBINDING_ONE.c_str ());
 
220
    g_variant_builder_add (&builder, "s", ccit::KEYBINDING_TWO.c_str ());
 
221
 
 
222
    return g_variant_builder_end (&builder);
 
223
}
 
224
 
 
225
GVariant *
 
226
ccvg::i ()
 
227
{
 
228
    return g_variant_new ("i", ccit::INTEGER);
 
229
}
 
230
 
 
231
GVariant *
 
232
ccvg::b ()
 
233
{
 
234
    return g_variant_new ("b", ccit::BOOLEAN);
 
235
}
 
236
 
 
237
GVariant *
 
238
ccvg::s ()
 
239
{
 
240
    return g_variant_new ("s", ccit::STRING.c_str ());
 
241
}
 
242
 
 
243
namespace
 
244
{
 
245
    CCSSettingValue * createSettingValue ()
 
246
    {
 
247
        CCSSettingValue *v = reinterpret_cast <CCSSettingValue *> (calloc (1, sizeof (CCSSettingValue)));
 
248
 
 
249
        v->isListChild = FALSE;
 
250
        v->parent = NULL;
 
251
        v->refCount = 1;
 
252
 
 
253
        return v;
 
254
    }
 
255
}
 
256
 
 
257
CCSSettingValue *
 
258
ccvalg::integer ()
 
259
{
 
260
    CCSSettingValue *v = createSettingValue ();
 
261
    v->value.asInt = ccit::INTEGER_ALT;
 
262
    return v;
 
263
}
 
264
 
 
265
CCSSettingValue *
 
266
ccvalg::string ()
 
267
{
 
268
    CCSSettingValue *v = createSettingValue ();
 
269
    v->value.asString = strdup (ccit::STRING_ALT.c_str ());
 
270
    return v;
 
271
}
 
272
 
 
273
CCSSettingValue *
 
274
ccvalg::key ()
 
275
{
 
276
    CCSSettingValue *v = createSettingValue ();
 
277
    v->value.asString = strdup (ccit::KEYBINDING_TWO.c_str ());
 
278
    return v;
 
279
}
 
280
 
 
281
CCSSettingValue *
 
282
ccvalg::boolean ()
 
283
{
 
284
    CCSSettingValue *v = createSettingValue ();
 
285
    v->value.asBool = ccit::BOOLEAN_ALT;
 
286
    return v;
 
287
}
 
288
 
 
289
void
 
290
ccex::boolean (CCSSettingValue *v)
 
291
{
 
292
    EXPECT_EQ (v->value.asBool, ccit::BOOLEAN);
 
293
}
 
294
 
 
295
void
 
296
ccex::integer (CCSSettingValue *v)
 
297
{
 
298
    EXPECT_EQ (v->value.asInt, ccit::INTEGER);
 
299
}
 
300
 
 
301
void
 
302
ccex::string (CCSSettingValue *v)
 
303
{
 
304
    EXPECT_EQ (v->value.asString, ccit::STRING);
 
305
}
 
306
 
 
307
void
 
308
ccex::key (CCSSettingValue *v)
 
309
{
 
310
    EXPECT_EQ (v->value.asString, ccit::KEYBINDING_ONE);
 
311
}
 
312
 
 
313
void
 
314
ccex::integerVariant (GVariant *v , int i)
 
315
{
 
316
    EXPECT_EQ (g_variant_get_int32 (v), i);
 
317
}
 
318
 
 
319
void
 
320
ccex::stringVariant (GVariant *v, const std::string &s)
 
321
{
 
322
    gsize len;
 
323
    EXPECT_EQ (g_variant_get_string (v, &len), s);
 
324
}
 
325
 
 
326
void
 
327
ccex::booleanVariant (GVariant *v, bool b)
 
328
{
 
329
    EXPECT_EQ (g_variant_get_boolean (v), b);
 
330
}
 
331
 
 
332
void
 
333
ccex::keyVariant (GVariant *v, const std::string &s)
 
334
{
 
335
    gsize len;
 
336
    const gchar * const *strv = g_variant_get_strv (v, &len);
 
337
    EXPECT_EQ (strv[0], s);
 
338
}
 
339
 
 
340
void
 
341
CCSGSettingsIntegratedSettingTest::SetUp ()
 
342
{
 
343
    env.SetUpEnv ();
 
344
    mWrapper.reset (ccsMockGSettingsWrapperNew (),
 
345
                    boost::bind (ccsMockGSettingsWrapperFree, _1));
 
346
    mWrapperMock = reinterpret_cast <CCSGSettingsWrapperGMock *> (
 
347
                       ccsObjectGetPrivate (mWrapper.get ()));
 
348
}
 
349
 
 
350
void
 
351
CCSGSettingsIntegratedSettingTest::TearDown ()
 
352
{
 
353
    mWrapper.reset ();
 
354
    mWrapperMock = NULL;
 
355
    env.TearDownEnv ();
 
356
}
 
357
 
 
358
TEST_P (CCSGSettingsIntegratedSettingTest, MatchedTypesReturnValueMismatchedTypesReturnNull)
 
359
{
 
360
    const std::string keyName ("mock");
 
361
    const ccit::GSettingsIntegratedSettingInfo &integratedSettingInfo =
 
362
            std::tr1::get <1> (GetParam ());
 
363
    const CCSSettingType                       createSettingType =
 
364
            std::tr1::get <0> (GetParam ());
 
365
 
 
366
    /* The GSettings Integrated setting takes ownership of these */
 
367
    CCSIntegratedSettingInfo *integratedSetting = ccsSharedIntegratedSettingInfoNew (keyName.c_str (),
 
368
                                                                                     keyName.c_str (),
 
369
                                                                                     integratedSettingInfo.settingType,
 
370
                                                                                     &ccsDefaultObjectAllocator);
 
371
    SpecialOptionType             specialType = ccsTypeToSpecialType ()[integratedSettingInfo.settingType];
 
372
    CCSGNOMEIntegratedSettingInfo *gnomeIntegratedSetting = ccsGNOMEIntegratedSettingInfoNew (integratedSetting,
 
373
                                                                                              specialType,
 
374
                                                                                              keyName.c_str (),
 
375
                                                                                              &ccsDefaultObjectAllocator);
 
376
    boost::shared_ptr <CCSIntegratedSetting> gsettingsIntegrated (AutoDestroy (ccsGSettingsIntegratedSettingNew (gnomeIntegratedSetting,
 
377
                                                                                                                 mWrapper.get (),
 
378
                                                                                                                 &ccsDefaultObjectAllocator),
 
379
                                                                               ccsIntegratedSettingUnref));
 
380
 
 
381
    GVariant *variant = (*integratedSettingInfo.variantGenerator) ();
 
382
    EXPECT_CALL (*mWrapperMock, getValue (Eq (keyName))).WillOnce (Return (variant));
 
383
 
 
384
    CCSSettingValue *value = ccsIntegratedSettingReadValue (gsettingsIntegrated.get (), createSettingType);
 
385
 
 
386
    if (createSettingType == integratedSettingInfo.settingType)
 
387
        (*integratedSettingInfo.expectation) (value);
 
388
    else
 
389
        EXPECT_THAT (value, IsNull ());
 
390
 
 
391
    if (value)
 
392
        ccsFreeSettingValueWithType (value, integratedSettingInfo.returnType);
 
393
}
 
394
 
 
395
ACTION (FreeVariant)
 
396
{
 
397
    g_variant_unref (arg0);
 
398
}
 
399
 
 
400
TEST_P (CCSGSettingsIntegratedSettingTest, MatchedTypesReturnValueMismatchedTypesResetOrWrite)
 
401
{
 
402
    const std::string keyName ("mock");
 
403
    const ccit::GSettingsIntegratedSettingInfo &integratedSettingInfo =
 
404
            std::tr1::get <1> (GetParam ());
 
405
    const CCSSettingType                       createSettingType =
 
406
            std::tr1::get <0> (GetParam ());
 
407
 
 
408
    CCSIntegratedSettingInfo *integratedSetting = ccsSharedIntegratedSettingInfoNew (keyName.c_str (),
 
409
                                                                                     keyName.c_str (),
 
410
                                                                                     integratedSettingInfo.settingType,
 
411
                                                                                     &ccsDefaultObjectAllocator);
 
412
    SpecialOptionType             specialType = ccsTypeToSpecialType ()[integratedSettingInfo.settingType];
 
413
    CCSGNOMEIntegratedSettingInfo *gnomeIntegratedSetting = ccsGNOMEIntegratedSettingInfoNew (integratedSetting,
 
414
                                                                                              specialType,
 
415
                                                                                              keyName.c_str (),
 
416
                                                                                              &ccsDefaultObjectAllocator);
 
417
    boost::shared_ptr <CCSIntegratedSetting> gsettingsIntegrated (AutoDestroy (ccsGSettingsIntegratedSettingNew (gnomeIntegratedSetting,
 
418
                                                                                                                 mWrapper.get (),
 
419
                                                                                                                 &ccsDefaultObjectAllocator),
 
420
                                                                               ccsIntegratedSettingUnref));
 
421
 
 
422
    boost::shared_ptr <CCSSettingValue> value ((*integratedSettingInfo.valueGenerator) (),
 
423
                                               boost::bind (ccsFreeSettingValueWithType,
 
424
                                                            _1,
 
425
                                                            integratedSettingInfo.returnType));
 
426
    boost::shared_ptr <GVariant>        variant = AutoDestroy (g_variant_ref ((*integratedSettingInfo.variantGenerator) ()),
 
427
                                                               g_variant_unref);
 
428
    boost::shared_ptr <GVariant>        newVariant = AutoDestroy (ccvg::fromValue (value.get (),
 
429
                                                                                   integratedSettingInfo.settingType),
 
430
                                                                  g_variant_unref);
 
431
    EXPECT_CALL (*mWrapperMock, getValue (Eq (keyName))).WillOnce (Return (variant.get ()));
 
432
 
 
433
    if (createSettingType == integratedSettingInfo.settingType)
 
434
        EXPECT_CALL (*mWrapperMock, setValue (Eq (keyName), VariantEqual (newVariant.get ())))
 
435
                .WillOnce (WithArgs <1> (FreeVariant ()));
 
436
    else
 
437
        EXPECT_CALL (*mWrapperMock, resetKey (Eq (keyName)));
 
438
 
 
439
    ccsIntegratedSettingWriteValue (gsettingsIntegrated.get (), value.get (), createSettingType);
 
440
}
 
441
 
 
442
INSTANTIATE_TEST_CASE_P (CCSGSettingsIntegratedSettingTestMismatchedValues, CCSGSettingsIntegratedSettingTest,
 
443
                         Combine (Values (TypeInt, TypeString, TypeBool, TypeKey),
 
444
                                  ValuesIn (cciti::settingsInfo)));