gi-gio
CopyrightWill Thompson and Iñaki García Etxebarria
LicenseLGPL-2.1
MaintainerIñaki García Etxebarria
Safe HaskellNone
LanguageHaskell2010

GI.Gio.Objects.TestDBus

Description

A helper class for testing code which uses D-Bus without touching the user’s session bus.

Note that GTestDBus modifies the user’s environment, calling `setenv()`). This is not thread-safe, so all GTestDBus calls should be completed before threads are spawned, or should have appropriate locking to ensure no access conflicts to environment variables shared between GTestDBus and other threads.

Creating unit tests using GTestDBus

Testing of D-Bus services can be tricky because normally we only ever run D-Bus services over an existing instance of the D-Bus daemon thus we usually don’t activate D-Bus services that are not yet installed into the target system. The GTestDBus object makes this easier for us by taking care of the lower level tasks such as running a private D-Bus daemon and looking up uninstalled services in customizable locations, typically in your source code tree.

The first thing you will need is a separate service description file for the D-Bus daemon. Typically a services subdirectory of your tests directory is a good place to put this file.

The service file should list your service along with an absolute path to the uninstalled service executable in your source tree. Using autotools we would achieve this by adding a file such as my-server.service.in in the services directory and have it processed by configure.

[D-BUS Service]
Name=org.gtk.GDBus.Examples.ObjectManager
Exec=@abs_top_builddir@/gio/tests/gdbus-example-objectmanager-server

You will also need to indicate this service directory in your test fixtures, so you will need to pass the path while compiling your test cases. Typically this is done with autotools with an added preprocessor flag specified to compile your tests such as:

-DTEST_SERVICES=\""$(abs_top_builddir)/tests/services"\"

Once you have a service definition file which is local to your source tree, you can proceed to set up a GTest fixture using the GTestDBus scaffolding.

An example of a test fixture for D-Bus services can be found here: gdbus-test-fixture.c

Note that these examples only deal with isolating the D-Bus aspect of your service. To successfully run isolated unit tests on your service you may need some additional modifications to your test case fixture. For example; if your service uses Settings and installs a schema then it is important that your test service not load the schema in the ordinary installed location (chances are that your service and schema files are not yet installed, or worse; there is an older version of the schema file sitting in the install location).

Most of the time we can work around these obstacles using the environment. Since the environment is inherited by the D-Bus daemon created by GTestDBus and then in turn inherited by any services the D-Bus daemon activates, using the setup routine for your fixture is a practical place to help sandbox your runtime environment. For the rather typical GSettings case we can work around this by setting GSETTINGS_SCHEMA_DIR to the in tree directory holding your schemas in the above fixture_setup() routine.

The GSettings schemas need to be locally pre-compiled for this to work. This can be achieved by compiling the schemas locally as a step before running test cases, an autotools setup might do the following in the directory holding schemas:

   all-am:
           $(GLIB_COMPILE_SCHEMAS) .

   CLEANFILES += gschemas.compiled

Since: 2.34

Synopsis

Exported types

newtype TestDBus Source #

Memory-managed wrapper type.

Constructors

TestDBus (ManagedPtr TestDBus) 

Instances

Instances details
Eq TestDBus Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

GObject TestDBus Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

ManagedPtrNewtype TestDBus Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

TypedObject TestDBus Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

Methods

glibType :: IO GType #

HasParentTypes TestDBus Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

HasAttributeList TestDBus Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

(info ~ ResolveTestDBusMethod t TestDBus, OverloadedMethod info TestDBus p, HasField t TestDBus p) => HasField (t :: Symbol) TestDBus (p :: Type) Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

Methods

getField :: TestDBus -> p #

(info ~ ResolveTestDBusMethod t TestDBus, OverloadedMethodInfo info TestDBus) => IsLabel t (MethodProxy info TestDBus) Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

(info ~ ResolveTestDBusMethod t TestDBus, OverloadedMethod info TestDBus p) => IsLabel t (TestDBus -> p) Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

Methods

fromLabel :: TestDBus -> p #

IsGValue (Maybe TestDBus) Source #

Convert TestDBus to and from GValue. See toGValue and fromGValue.

Instance details

Defined in GI.Gio.Objects.TestDBus

type AttributeList TestDBus Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

type ParentTypes TestDBus Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

type SignalList TestDBus Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

class (GObject o, IsDescendantOf TestDBus o) => IsTestDBus o Source #

Type class for types which can be safely cast to TestDBus, for instance with toTestDBus.

Instances

Instances details
(GObject o, IsDescendantOf TestDBus o) => IsTestDBus o Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

toTestDBus :: (MonadIO m, IsTestDBus o) => o -> m TestDBus Source #

Cast to TestDBus, for types for which this is known to be safe. For general casts, use castTo.

Methods

type family ResolveTestDBusMethod (t :: Symbol) o where ... Source #

Equations

ResolveTestDBusMethod "addServiceDir" o = TestDBusAddServiceDirMethodInfo 
ResolveTestDBusMethod "bindProperty" o = ObjectBindPropertyMethodInfo 
ResolveTestDBusMethod "bindPropertyFull" o = ObjectBindPropertyFullMethodInfo 
ResolveTestDBusMethod "down" o = TestDBusDownMethodInfo 
ResolveTestDBusMethod "forceFloating" o = ObjectForceFloatingMethodInfo 
ResolveTestDBusMethod "freezeNotify" o = ObjectFreezeNotifyMethodInfo 
ResolveTestDBusMethod "getv" o = ObjectGetvMethodInfo 
ResolveTestDBusMethod "isFloating" o = ObjectIsFloatingMethodInfo 
ResolveTestDBusMethod "notify" o = ObjectNotifyMethodInfo 
ResolveTestDBusMethod "notifyByPspec" o = ObjectNotifyByPspecMethodInfo 
ResolveTestDBusMethod "ref" o = ObjectRefMethodInfo 
ResolveTestDBusMethod "refSink" o = ObjectRefSinkMethodInfo 
ResolveTestDBusMethod "runDispose" o = ObjectRunDisposeMethodInfo 
ResolveTestDBusMethod "stealData" o = ObjectStealDataMethodInfo 
ResolveTestDBusMethod "stealQdata" o = ObjectStealQdataMethodInfo 
ResolveTestDBusMethod "stop" o = TestDBusStopMethodInfo 
ResolveTestDBusMethod "thawNotify" o = ObjectThawNotifyMethodInfo 
ResolveTestDBusMethod "unref" o = ObjectUnrefMethodInfo 
ResolveTestDBusMethod "up" o = TestDBusUpMethodInfo 
ResolveTestDBusMethod "watchClosure" o = ObjectWatchClosureMethodInfo 
ResolveTestDBusMethod "getBusAddress" o = TestDBusGetBusAddressMethodInfo 
ResolveTestDBusMethod "getData" o = ObjectGetDataMethodInfo 
ResolveTestDBusMethod "getFlags" o = TestDBusGetFlagsMethodInfo 
ResolveTestDBusMethod "getProperty" o = ObjectGetPropertyMethodInfo 
ResolveTestDBusMethod "getQdata" o = ObjectGetQdataMethodInfo 
ResolveTestDBusMethod "setData" o = ObjectSetDataMethodInfo 
ResolveTestDBusMethod "setDataFull" o = ObjectSetDataFullMethodInfo 
ResolveTestDBusMethod "setProperty" o = ObjectSetPropertyMethodInfo 
ResolveTestDBusMethod l o = MethodResolutionFailed l o :: Type 

addServiceDir

data TestDBusAddServiceDirMethodInfo Source #

Instances

Instances details
(signature ~ (Text -> m ()), MonadIO m, IsTestDBus a) => OverloadedMethod TestDBusAddServiceDirMethodInfo a signature Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

Methods

overloadedMethod :: a -> signature #

OverloadedMethodInfo TestDBusAddServiceDirMethodInfo (a :: Type) Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

testDBusAddServiceDir Source #

Arguments

:: (HasCallStack, MonadIO m, IsTestDBus a) 
=> a

self: a TestDBus

-> Text

path: path to a directory containing .service files

-> m () 

Add a path where dbus-daemon will look up .service files. This can't be called after testDBusUp.

down

data TestDBusDownMethodInfo Source #

Instances

Instances details
(signature ~ m (), MonadIO m, IsTestDBus a) => OverloadedMethod TestDBusDownMethodInfo a signature Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

Methods

overloadedMethod :: a -> signature #

OverloadedMethodInfo TestDBusDownMethodInfo (a :: Type) Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

testDBusDown Source #

Arguments

:: (HasCallStack, MonadIO m, IsTestDBus a) 
=> a

self: a TestDBus

-> m () 

Stop the session bus started by testDBusUp.

This will wait for the singleton returned by busGet or busGetSync to be destroyed. This is done to ensure that the next unit test won't get a leaked singleton from this test.

getBusAddress

testDBusGetBusAddress Source #

Arguments

:: (HasCallStack, MonadIO m, IsTestDBus a) 
=> a

self: a TestDBus

-> m (Maybe Text)

Returns: the address of the bus, or Nothing.

Get the address on which dbus-daemon is running. If testDBusUp has not been called yet, Nothing is returned. This can be used with dBusConnectionNewForAddress.

getFlags

data TestDBusGetFlagsMethodInfo Source #

Instances

Instances details
(signature ~ m [TestDBusFlags], MonadIO m, IsTestDBus a) => OverloadedMethod TestDBusGetFlagsMethodInfo a signature Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

Methods

overloadedMethod :: a -> signature #

OverloadedMethodInfo TestDBusGetFlagsMethodInfo (a :: Type) Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

testDBusGetFlags Source #

Arguments

:: (HasCallStack, MonadIO m, IsTestDBus a) 
=> a

self: a TestDBus

-> m [TestDBusFlags]

Returns: the value of TestDBus:flags property

Get the flags of the TestDBus object.

new

testDBusNew Source #

Arguments

:: (HasCallStack, MonadIO m) 
=> [TestDBusFlags]

flags: a TestDBusFlags

-> m TestDBus

Returns: a new TestDBus.

Create a new TestDBus object.

stop

data TestDBusStopMethodInfo Source #

Instances

Instances details
(signature ~ m (), MonadIO m, IsTestDBus a) => OverloadedMethod TestDBusStopMethodInfo a signature Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

Methods

overloadedMethod :: a -> signature #

OverloadedMethodInfo TestDBusStopMethodInfo (a :: Type) Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

testDBusStop Source #

Arguments

:: (HasCallStack, MonadIO m, IsTestDBus a) 
=> a

self: a TestDBus

-> m () 

Stop the session bus started by testDBusUp.

Unlike testDBusDown, this won't verify the DBusConnection singleton returned by busGet or busGetSync is destroyed. Unit tests wanting to verify behaviour after the session bus has been stopped can use this function but should still call testDBusDown when done.

unset

testDBusUnset :: (HasCallStack, MonadIO m) => m () Source #

Unset DISPLAY and DBUS_SESSION_BUS_ADDRESS env variables to ensure the test won't use user's session bus.

This is useful for unit tests that want to verify behaviour when no session bus is running. It is not necessary to call this if unit test already calls testDBusUp before acquiring the session bus.

up

data TestDBusUpMethodInfo Source #

Instances

Instances details
(signature ~ m (), MonadIO m, IsTestDBus a) => OverloadedMethod TestDBusUpMethodInfo a signature Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

Methods

overloadedMethod :: a -> signature #

OverloadedMethodInfo TestDBusUpMethodInfo (a :: Type) Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

testDBusUp Source #

Arguments

:: (HasCallStack, MonadIO m, IsTestDBus a) 
=> a

self: a TestDBus

-> m () 

Start a dbus-daemon instance and set DBUS_SESSION_BUS_ADDRESS. After this call, it is safe for unit tests to start sending messages on the session bus.

If this function is called from setup callback of g_test_add(), testDBusDown must be called in its teardown callback.

If this function is called from unit test's main(), then testDBusDown must be called after testRun.

Properties

flags

TestDBusFlags specifying the behaviour of the D-Bus session.

Since: 2.34

data TestDBusFlagsPropertyInfo Source #

Instances

Instances details
AttrInfo TestDBusFlagsPropertyInfo Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

type AttrAllowedOps TestDBusFlagsPropertyInfo Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

type AttrBaseTypeConstraint TestDBusFlagsPropertyInfo Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

type AttrGetType TestDBusFlagsPropertyInfo Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

type AttrLabel TestDBusFlagsPropertyInfo Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

type AttrOrigin TestDBusFlagsPropertyInfo Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

type AttrSetTypeConstraint TestDBusFlagsPropertyInfo Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

type AttrTransferType TestDBusFlagsPropertyInfo Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

type AttrTransferTypeConstraint TestDBusFlagsPropertyInfo Source # 
Instance details

Defined in GI.Gio.Objects.TestDBus

constructTestDBusFlags :: (IsTestDBus o, MonadIO m) => [TestDBusFlags] -> m (GValueConstruct o) Source #

Construct a GValueConstruct with valid value for the “flags” property. This is rarely needed directly, but it is used by new.

getTestDBusFlags :: (MonadIO m, IsTestDBus o) => o -> m [TestDBusFlags] Source #

Get the value of the “flags” property. When overloading is enabled, this is equivalent to

get testDBus #flags