~vanvugt/compiz/fix-1056615

« back to all changes in this revision

Viewing changes to compizconfig/gsettings/tests/test_gsettings_tests.cpp

  • Committer: Tarmac
  • Author(s): Sam Spilsbury
  • Date: 2012-10-16 11:43:02 UTC
  • mfrom: (3411.1.11 compiz.fix_1064791)
  • Revision ID: tarmac-20121016114302-svbjerf1lf8jb7lm
Don't write plugins-with-set-keys every single time we read a schema, if 
the value is already in plugins-with-set-keys. Added tests to demonstrate 
behaviour.
(LP: #1064791)

vanvugt says: This also seems to fix LP: #1044662, somehow.. Fixes: https://bugs.launchpad.net/bugs/1064791. Approved by Daniel van Vugt.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
#include <boost/shared_ptr.hpp>
8
8
#include <boost/scoped_array.hpp>
9
9
 
 
10
#include "gtest_shared_autodestroy.h"
 
11
 
10
12
#include "test_gsettings_tests.h"
11
13
#include "gsettings.h"
12
14
#include "ccs_gsettings_backend.h"
 
15
#include "ccs_gsettings_backend_interface.h"
13
16
#include "ccs_gsettings_backend_mock.h"
14
17
#include "compizconfig_ccs_context_mock.h"
15
18
#include "compizconfig_ccs_plugin_mock.h"
16
19
#include "compizconfig_ccs_setting_mock.h"
 
20
#include "compizconfig_ccs_integration_mock.h"
 
21
#include "ccs_gsettings_wrapper_mock.h"
 
22
#include "ccs_gsettings_wrapper_factory_mock.h"
 
23
#include "ccs_gsettings_wrapper_factory_interface.h"
 
24
#include "ccs_gnome_integration.h"
17
25
#include "gtest_shared_characterwrapper.h"
18
26
#include "compizconfig_test_value_combiners.h"
19
27
#include "compizconfig_ccs_mocked_allocator.h"
1959
1967
    boost::shared_ptr <CCSContext> context (ccsMockContextNew (),
1960
1968
                                            boost::bind (ccsFreeMockContext, _1));
1961
1969
 
1962
 
    GVariantBuilder pluginsWithChangedKeysBuilder;
1963
 
 
1964
1970
    const unsigned short NUM_KEYS = 3;
1965
1971
 
1966
1972
    gchar ** fooKeys = (gchar **) calloc (1, sizeof (char *) * (NUM_KEYS + 1));
1975
1981
    barKeys[2] = g_strdup (KEY_EXAMPLE_THREE.c_str ());
1976
1982
    barKeys[3] = NULL;
1977
1983
 
 
1984
    GVariantBuilder pluginsWithChangedKeysBuilder;
 
1985
 
1978
1986
    g_variant_builder_init (&pluginsWithChangedKeysBuilder, G_VARIANT_TYPE ("as"));
1979
1987
    g_variant_builder_add (&pluginsWithChangedKeysBuilder, "s", PLUGIN_FOO.c_str ());
1980
1988
    g_variant_builder_add (&pluginsWithChangedKeysBuilder, "s", PLUGIN_BAR.c_str ());
2017
2025
                                                                  pluginsWithChangedKeys.get (),
2018
2026
                                                                  "mock");
2019
2027
}
 
2028
 
 
2029
namespace
 
2030
{
 
2031
    const CCSBackendInfo stubBackendInfo =
 
2032
    {
 
2033
        "stub",
 
2034
        "stub",
 
2035
        "stub",
 
2036
        FALSE,
 
2037
        FALSE
 
2038
    };
 
2039
 
 
2040
    const CCSBackendInfo *
 
2041
    stubBackendGetInfo (CCSBackend *backend)
 
2042
    {
 
2043
        return &stubBackendInfo;
 
2044
    }
 
2045
 
 
2046
    Bool
 
2047
    stubBackendInit (CCSBackend *backend, CCSContext *context)
 
2048
    {
 
2049
        return TRUE;
 
2050
    }
 
2051
 
 
2052
    Bool
 
2053
    stubBackendFini (CCSBackend *backend)
 
2054
    {
 
2055
        return TRUE;
 
2056
    }
 
2057
 
 
2058
    CCSBackendInterface stubBackendInterface =
 
2059
    {
 
2060
        stubBackendGetInfo,
 
2061
        NULL,
 
2062
        stubBackendInit,
 
2063
        stubBackendFini,
 
2064
        NULL,
 
2065
        NULL,
 
2066
        NULL,
 
2067
        NULL,
 
2068
        NULL,
 
2069
        NULL,
 
2070
        NULL,
 
2071
        NULL,
 
2072
        NULL,
 
2073
        NULL,
 
2074
        NULL,
 
2075
        NULL
 
2076
    };
 
2077
}
 
2078
 
 
2079
namespace
 
2080
{
 
2081
    const std::string MOCK_PLUGIN_NAME ("mock");
 
2082
    const std::string MOCK_SCHEMA_NAME ("org.compiz.mock");
 
2083
    const std::string MOCK_PROFILE_NAME ("mock");
 
2084
    const std::string MOCK_GSCHEMA_PATH ("/org/compiz/profiles/mock/plugins/mock");
 
2085
    const std::string PLUGINS_WITH_SET_KEYS ("plugins-with-set-keys");
 
2086
 
 
2087
    boost::shared_ptr <GVariant>
 
2088
    GetEmptyPluginsWithSetKeys ()
 
2089
    {
 
2090
        GVariantBuilder pluginsWithChangedKeysBuilder;
 
2091
 
 
2092
        g_variant_builder_init (&pluginsWithChangedKeysBuilder, G_VARIANT_TYPE ("as"));
 
2093
        return AutoDestroy (g_variant_ref_sink (g_variant_builder_end (&pluginsWithChangedKeysBuilder)),
 
2094
                                                g_variant_unref);
 
2095
    }
 
2096
}
 
2097
 
 
2098
class CCSGSettingsTestCCSGSettingsBackend :
 
2099
    public CCSGSettingsTestIndependent
 
2100
{
 
2101
    public:
 
2102
 
 
2103
        CCSGSettingsTestCCSGSettingsBackend () :
 
2104
            mockContext (AutoDestroy (ccsMockContextNew (), ccsFreeMockContext)),
 
2105
            stubBackend (AutoDestroy (ccsBackendNewWithDynamicInterface (mockContext.get (), &stubBackendInterface),
 
2106
                                      ccsBackendUnref)),
 
2107
            mockCompizconfigSettings (ccsMockGSettingsWrapperNew ()),
 
2108
            mockCurrentProfileSettings (ccsMockGSettingsWrapperNew ()),
 
2109
            mockWrapperFactory (ccsMockGSettingsWrapperFactoryNew ()),
 
2110
            mockIntegration (ccsMockIntegrationBackendNew (&ccsDefaultObjectAllocator)),
 
2111
            valueChangeData (reinterpret_cast <CCSGNOMEValueChangeData *> (calloc (1, sizeof (CCSGNOMEValueChangeData)))),
 
2112
            currentProfile (strdup (MOCK_PROFILE_NAME.c_str ())),
 
2113
            mockMockPluginWrapper (ccsMockGSettingsWrapperNew ()),
 
2114
            gmockWrapperFactory (reinterpret_cast <CCSGSettingsWrapperFactoryGMock *> (ccsObjectGetPrivate (mockWrapperFactory))),
 
2115
            gmockWrapper (reinterpret_cast <CCSGSettingsWrapperGMock *> (ccsObjectGetPrivate (mockMockPluginWrapper))),
 
2116
            gmockCurrentProfileSettings (reinterpret_cast <CCSGSettingsWrapperGMock *> (ccsObjectGetPrivate (mockCurrentProfileSettings)))
 
2117
 
 
2118
        {
 
2119
            valueChangeData->integration = mockIntegration;
 
2120
            valueChangeData->factory = NULL;
 
2121
            valueChangeData->storage = NULL;
 
2122
            valueChangeData->context = mockContext.get ();
 
2123
 
 
2124
            if (!ccsGSettingsBackendAttachNewToBackend (stubBackend.get (),
 
2125
                                                        mockContext.get (),
 
2126
                                                        mockCompizconfigSettings,
 
2127
                                                        mockCurrentProfileSettings,
 
2128
                                                        mockWrapperFactory,
 
2129
                                                        mockIntegration,
 
2130
                                                        valueChangeData,
 
2131
                                                        currentProfile))
 
2132
                throw std::runtime_error ("Failed to attach GSettings backend");
 
2133
        }
 
2134
 
 
2135
        virtual void TearDown ()
 
2136
        {
 
2137
            ccsGSettingsBackendDetachFromBackend (stubBackend.get ());
 
2138
 
 
2139
            CCSGSettingsTestIndependent::TearDown ();
 
2140
        }
 
2141
 
 
2142
        boost::shared_ptr <CCSContext> mockContext;
 
2143
        boost::shared_ptr <CCSBackend> stubBackend;
 
2144
        CCSGSettingsWrapper *mockCompizconfigSettings;
 
2145
        CCSGSettingsWrapper *mockCurrentProfileSettings;
 
2146
        CCSGSettingsWrapperFactory *mockWrapperFactory;
 
2147
        CCSIntegration *mockIntegration;
 
2148
        CCSGNOMEValueChangeData           *valueChangeData;
 
2149
        char                *currentProfile;
 
2150
        CCSGSettingsWrapper *mockMockPluginWrapper;
 
2151
 
 
2152
        CCSGSettingsWrapperFactoryGMock *gmockWrapperFactory;
 
2153
        CCSGSettingsWrapperGMock        *gmockWrapper;
 
2154
        CCSGSettingsWrapperGMock        *gmockCurrentProfileSettings;
 
2155
 
 
2156
};
 
2157
 
 
2158
TEST_F (CCSGSettingsTestCCSGSettingsBackend, TestWriteOutSetKeysOnGetSettingsObject)
 
2159
{
 
2160
    /* Should create a new wrapper for this "plugin" */
 
2161
    EXPECT_CALL (*gmockWrapperFactory, newGSettingsWrapperWithPath (Eq (MOCK_SCHEMA_NAME),
 
2162
                                                                    Eq (MOCK_GSCHEMA_PATH),
 
2163
                                                                    _)).WillOnce (Return (mockMockPluginWrapper));
 
2164
    EXPECT_CALL (*gmockWrapper, connectToChangedSignal (_, stubBackend.get ()));
 
2165
 
 
2166
 
 
2167
    boost::shared_ptr <GVariant> pluginsWithSetKeysVariantEmpty (GetEmptyPluginsWithSetKeys ());
 
2168
 
 
2169
    /* Should now get the value of plugins-with-set-keys from
 
2170
     * mockCurrentProfileSettings */
 
2171
    EXPECT_CALL (*gmockCurrentProfileSettings, getValue (Eq (PLUGINS_WITH_SET_KEYS)))
 
2172
            .WillOnce (Return (g_variant_ref (pluginsWithSetKeysVariantEmpty.get ())));
 
2173
 
 
2174
    /* Should acknowledge that we wrote to this schema */
 
2175
    EXPECT_CALL (*gmockCurrentProfileSettings, setValue (Eq (PLUGINS_WITH_SET_KEYS),
 
2176
                                                         GVariantHasValueInArray <const gchar *> ("s",
 
2177
                                                                                                  MOCK_PLUGIN_NAME.c_str (),
 
2178
                                                                                                  boost::bind (streq, _1, _2))))
 
2179
            .WillOnce (WithArgs <1> (Invoke (g_variant_unref)));;
 
2180
 
 
2181
    CCSGSettingsWrapper *wrapper = ccsGSettingsGetSettingsObjectForPluginWithPath (stubBackend.get (),
 
2182
                                                                                   MOCK_PLUGIN_NAME.c_str (),
 
2183
                                                                                   MOCK_GSCHEMA_PATH.c_str (),
 
2184
                                                                                   mockContext.get ());
 
2185
 
 
2186
    EXPECT_EQ (wrapper, mockMockPluginWrapper);
 
2187
}
 
2188
 
 
2189
TEST_F (CCSGSettingsTestCCSGSettingsBackend, TestNoWriteOutSetKeysOnGetSettingsObjectIfAlreadyWritten)
 
2190
{
 
2191
    /* Should create a new wrapper for this "plugin" */
 
2192
    EXPECT_CALL (*gmockWrapperFactory, newGSettingsWrapperWithPath (Eq (MOCK_SCHEMA_NAME),
 
2193
                                                                    Eq (MOCK_GSCHEMA_PATH),
 
2194
                                                                    _)).WillOnce (Return (mockMockPluginWrapper));
 
2195
    EXPECT_CALL (*gmockWrapper, connectToChangedSignal (_, stubBackend.get ()));
 
2196
 
 
2197
 
 
2198
    GVariantBuilder pluginsWithChangedKeysBuilder;
 
2199
 
 
2200
    g_variant_builder_init (&pluginsWithChangedKeysBuilder, G_VARIANT_TYPE ("as"));
 
2201
    g_variant_builder_add (&pluginsWithChangedKeysBuilder, "s", MOCK_PLUGIN_NAME.c_str ());
 
2202
    boost::shared_ptr <GVariant> pluginsWithSetKeysVariantNonEmpty (AutoDestroy (g_variant_ref_sink (g_variant_builder_end (&pluginsWithChangedKeysBuilder)),
 
2203
                                                                                 g_variant_unref));
 
2204
 
 
2205
    /* Should now get the value of plugins-with-set-keys from
 
2206
     * mockCurrentProfileSettings */
 
2207
    EXPECT_CALL (*gmockCurrentProfileSettings, getValue (Eq (PLUGINS_WITH_SET_KEYS)))
 
2208
            .WillOnce (Return (g_variant_ref (pluginsWithSetKeysVariantNonEmpty.get ())));
 
2209
 
 
2210
    /* No acknowledgement */
 
2211
    EXPECT_CALL (*gmockCurrentProfileSettings, setValue (_, _)).Times (0);
 
2212
 
 
2213
    CCSGSettingsWrapper *wrapper = ccsGSettingsGetSettingsObjectForPluginWithPath (stubBackend.get (),
 
2214
                                                                                   MOCK_PLUGIN_NAME.c_str (),
 
2215
                                                                                   MOCK_GSCHEMA_PATH.c_str (),
 
2216
                                                                                   mockContext.get ());
 
2217
 
 
2218
    EXPECT_EQ (wrapper, mockMockPluginWrapper);
 
2219
}
 
2220
 
 
2221
TEST_F (CCSGSettingsTestCCSGSettingsBackend, TestReturnExistingWrapper)
 
2222
{
 
2223
    /* Should create a new wrapper for this "plugin" */
 
2224
    EXPECT_CALL (*gmockWrapperFactory, newGSettingsWrapperWithPath (Eq (MOCK_SCHEMA_NAME),
 
2225
                                                                    Eq (MOCK_GSCHEMA_PATH),
 
2226
                                                                    _)).WillOnce (Return (mockMockPluginWrapper));
 
2227
    EXPECT_CALL (*gmockWrapper, connectToChangedSignal (_, stubBackend.get ()));
 
2228
 
 
2229
 
 
2230
    boost::shared_ptr <GVariant> pluginsWithSetKeysVariantEmpty (GetEmptyPluginsWithSetKeys ());
 
2231
 
 
2232
    /* Should now get the value of plugins-with-set-keys from
 
2233
     * mockCurrentProfileSettings */
 
2234
    EXPECT_CALL (*gmockCurrentProfileSettings, getValue (Eq (PLUGINS_WITH_SET_KEYS)))
 
2235
            .WillOnce (Return (g_variant_ref (pluginsWithSetKeysVariantEmpty.get ())));
 
2236
 
 
2237
    /* Should acknowledge that we wrote to this schema */
 
2238
    EXPECT_CALL (*gmockCurrentProfileSettings, setValue (Eq (PLUGINS_WITH_SET_KEYS),
 
2239
                                                         _))
 
2240
            .WillOnce (WithArgs <1> (Invoke (g_variant_unref)));
 
2241
 
 
2242
    CCSGSettingsWrapper *wrapper = ccsGSettingsGetSettingsObjectForPluginWithPath (stubBackend.get (),
 
2243
                                                                                   MOCK_PLUGIN_NAME.c_str (),
 
2244
                                                                                   MOCK_GSCHEMA_PATH.c_str (),
 
2245
                                                                                   mockContext.get ());
 
2246
 
 
2247
    EXPECT_CALL (*gmockWrapper, getSchemaName ()).WillOnce (Return (MOCK_SCHEMA_NAME.c_str ()));
 
2248
 
 
2249
    /* Shouldn't be called again */
 
2250
    EXPECT_CALL (*gmockWrapperFactory, newGSettingsWrapperWithPath (_, _, _)).Times (0);
 
2251
 
 
2252
    wrapper = ccsGSettingsGetSettingsObjectForPluginWithPath (stubBackend.get (),
 
2253
                                                              MOCK_PLUGIN_NAME.c_str (),
 
2254
                                                              MOCK_GSCHEMA_PATH.c_str (),
 
2255
                                                              mockContext.get ());
 
2256
 
 
2257
    /* It should return the cached one */
 
2258
    EXPECT_EQ (mockMockPluginWrapper, wrapper);
 
2259
}