2
* Compiz configuration system library
4
* Copyright (C) 2012 Canonical Ltd.
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.
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.
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
21
* Sam Spilsbury <sam.spilsbury@canonical.com>
23
#include <gtest/gtest.h>
24
#include <gmock/gmock.h>
29
#include <boost/shared_ptr.hpp>
31
#include <glib_gslice_off_env.h>
32
#include <gtest_shared_autodestroy.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>
40
using ::testing::Combine;
41
using ::testing::Return;
42
using ::testing::IsNull;
43
using ::testing::ValuesIn;
44
using ::testing::Values;
46
using ::testing::WithArgs;
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;
63
const std::string STRING_ALT = "string_alt";
64
const Bool BOOLEAN_ALT = FALSE;
65
const int INTEGER_ALT = 1;
67
namespace variant_generators
73
GVariant * fromValue (CCSSettingValue *v, CCSSettingType type);
76
namespace value_generators
78
CCSSettingValue * integer ();
79
CCSSettingValue * string ();
80
CCSSettingValue * key ();
81
CCSSettingValue * boolean ();
84
namespace expectations
86
void integer (CCSSettingValue *);
87
void string (CCSSettingValue *);
88
void boolean (CCSSettingValue *);
89
void key (CCSSettingValue *);
91
void integerVariant (GVariant *, int);
92
void stringVariant (GVariant *, const std::string &);
93
void booleanVariant (GVariant *, bool);
94
void keyVariant (GVariant *, const std::string &);
97
typedef GVariant * (*VariantGenerator) ();
98
typedef CCSSettingValue * (*ValueGenerator) ();
99
typedef void (*Expectation) (CCSSettingValue *);
101
struct GSettingsIntegratedSettingInfo
103
VariantGenerator variantGenerator;
104
ValueGenerator valueGenerator;
105
Expectation expectation;
106
CCSSettingType settingType;
107
CCSSettingType returnType;
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;
117
ccit::GSettingsIntegratedSettingInfo settingsInfo[] =
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 }
130
MATCHER_P (VariantEqual, lhs, "Variants Equal")
132
return g_variant_equal (lhs, arg);
137
std::map <CCSSettingType, SpecialOptionType> &
138
ccsTypeToSpecialType ()
140
static std::map <CCSSettingType, SpecialOptionType> types;
141
static bool initialized = false;
145
types[TypeInt] = OptionInt;
146
types[TypeBool] = OptionBool;
147
types[TypeString] = OptionString;
148
types[TypeKey] = OptionKey;
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;
161
typedef std::tr1::tuple <CCSSettingType,
162
ccit::GSettingsIntegratedSettingInfo> CCSGSettingsIntegratedSettingTestInfo;
164
class CCSGSettingsIntegratedSettingTest :
165
public ::testing::TestWithParam <CCSGSettingsIntegratedSettingTestInfo>
169
virtual void SetUp ();
170
virtual void TearDown ();
174
CompizGLibGSliceOffEnv env;
175
boost::shared_ptr <CCSGSettingsWrapper> mWrapper;
176
CCSGSettingsWrapperGMock *mWrapperMock;
180
ccvg::fromValue (CCSSettingValue *v,
186
return g_variant_new ("i", v->value.asInt);
189
return g_variant_new ("b", v->value.asBool);
192
return g_variant_new ("s", v->value.asString);
196
GVariantBuilder builder;
197
g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
199
/* Represented internally as strings */
200
std::string kb (v->value.asString);
201
if (kb == "Disabled")
204
g_variant_builder_add (&builder, "s", kb.c_str ());
205
return g_variant_builder_end (&builder);
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 ());
222
return g_variant_builder_end (&builder);
228
return g_variant_new ("i", ccit::INTEGER);
234
return g_variant_new ("b", ccit::BOOLEAN);
240
return g_variant_new ("s", ccit::STRING.c_str ());
245
CCSSettingValue * createSettingValue ()
247
CCSSettingValue *v = reinterpret_cast <CCSSettingValue *> (calloc (1, sizeof (CCSSettingValue)));
249
v->isListChild = FALSE;
260
CCSSettingValue *v = createSettingValue ();
261
v->value.asInt = ccit::INTEGER_ALT;
268
CCSSettingValue *v = createSettingValue ();
269
v->value.asString = strdup (ccit::STRING_ALT.c_str ());
276
CCSSettingValue *v = createSettingValue ();
277
v->value.asString = strdup (ccit::KEYBINDING_TWO.c_str ());
284
CCSSettingValue *v = createSettingValue ();
285
v->value.asBool = ccit::BOOLEAN_ALT;
290
ccex::boolean (CCSSettingValue *v)
292
EXPECT_EQ (v->value.asBool, ccit::BOOLEAN);
296
ccex::integer (CCSSettingValue *v)
298
EXPECT_EQ (v->value.asInt, ccit::INTEGER);
302
ccex::string (CCSSettingValue *v)
304
EXPECT_EQ (v->value.asString, ccit::STRING);
308
ccex::key (CCSSettingValue *v)
310
EXPECT_EQ (v->value.asString, ccit::KEYBINDING_ONE);
314
ccex::integerVariant (GVariant *v , int i)
316
EXPECT_EQ (g_variant_get_int32 (v), i);
320
ccex::stringVariant (GVariant *v, const std::string &s)
323
EXPECT_EQ (g_variant_get_string (v, &len), s);
327
ccex::booleanVariant (GVariant *v, bool b)
329
EXPECT_EQ (g_variant_get_boolean (v), b);
333
ccex::keyVariant (GVariant *v, const std::string &s)
336
const gchar * const *strv = g_variant_get_strv (v, &len);
337
EXPECT_EQ (strv[0], s);
341
CCSGSettingsIntegratedSettingTest::SetUp ()
344
mWrapper.reset (ccsMockGSettingsWrapperNew (),
345
boost::bind (ccsMockGSettingsWrapperFree, _1));
346
mWrapperMock = reinterpret_cast <CCSGSettingsWrapperGMock *> (
347
ccsObjectGetPrivate (mWrapper.get ()));
351
CCSGSettingsIntegratedSettingTest::TearDown ()
358
TEST_P (CCSGSettingsIntegratedSettingTest, MatchedTypesReturnValueMismatchedTypesReturnNull)
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 ());
366
/* The GSettings Integrated setting takes ownership of these */
367
CCSIntegratedSettingInfo *integratedSetting = ccsSharedIntegratedSettingInfoNew (keyName.c_str (),
369
integratedSettingInfo.settingType,
370
&ccsDefaultObjectAllocator);
371
SpecialOptionType specialType = ccsTypeToSpecialType ()[integratedSettingInfo.settingType];
372
CCSGNOMEIntegratedSettingInfo *gnomeIntegratedSetting = ccsGNOMEIntegratedSettingInfoNew (integratedSetting,
375
&ccsDefaultObjectAllocator);
376
boost::shared_ptr <CCSIntegratedSetting> gsettingsIntegrated (AutoDestroy (ccsGSettingsIntegratedSettingNew (gnomeIntegratedSetting,
378
&ccsDefaultObjectAllocator),
379
ccsIntegratedSettingUnref));
381
GVariant *variant = (*integratedSettingInfo.variantGenerator) ();
382
EXPECT_CALL (*mWrapperMock, getValue (Eq (keyName))).WillOnce (Return (variant));
384
CCSSettingValue *value = ccsIntegratedSettingReadValue (gsettingsIntegrated.get (), createSettingType);
386
if (createSettingType == integratedSettingInfo.settingType)
387
(*integratedSettingInfo.expectation) (value);
389
EXPECT_THAT (value, IsNull ());
392
ccsFreeSettingValueWithType (value, integratedSettingInfo.returnType);
397
g_variant_unref (arg0);
400
TEST_P (CCSGSettingsIntegratedSettingTest, MatchedTypesReturnValueMismatchedTypesResetOrWrite)
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 ());
408
CCSIntegratedSettingInfo *integratedSetting = ccsSharedIntegratedSettingInfoNew (keyName.c_str (),
410
integratedSettingInfo.settingType,
411
&ccsDefaultObjectAllocator);
412
SpecialOptionType specialType = ccsTypeToSpecialType ()[integratedSettingInfo.settingType];
413
CCSGNOMEIntegratedSettingInfo *gnomeIntegratedSetting = ccsGNOMEIntegratedSettingInfoNew (integratedSetting,
416
&ccsDefaultObjectAllocator);
417
boost::shared_ptr <CCSIntegratedSetting> gsettingsIntegrated (AutoDestroy (ccsGSettingsIntegratedSettingNew (gnomeIntegratedSetting,
419
&ccsDefaultObjectAllocator),
420
ccsIntegratedSettingUnref));
422
boost::shared_ptr <CCSSettingValue> value ((*integratedSettingInfo.valueGenerator) (),
423
boost::bind (ccsFreeSettingValueWithType,
425
integratedSettingInfo.returnType));
426
boost::shared_ptr <GVariant> variant = AutoDestroy (g_variant_ref ((*integratedSettingInfo.variantGenerator) ()),
428
boost::shared_ptr <GVariant> newVariant = AutoDestroy (ccvg::fromValue (value.get (),
429
integratedSettingInfo.settingType),
431
EXPECT_CALL (*mWrapperMock, getValue (Eq (keyName))).WillOnce (Return (variant.get ()));
433
if (createSettingType == integratedSettingInfo.settingType)
434
EXPECT_CALL (*mWrapperMock, setValue (Eq (keyName), VariantEqual (newVariant.get ())))
435
.WillOnce (WithArgs <1> (FreeVariant ()));
437
EXPECT_CALL (*mWrapperMock, resetKey (Eq (keyName)));
439
ccsIntegratedSettingWriteValue (gsettingsIntegrated.get (), value.get (), createSettingType);
442
INSTANTIATE_TEST_CASE_P (CCSGSettingsIntegratedSettingTestMismatchedValues, CCSGSettingsIntegratedSettingTest,
443
Combine (Values (TypeInt, TypeString, TypeBool, TypeKey),
444
ValuesIn (cciti::settingsInfo)));