1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/*
* Copyright 2013 Canonical Ltd.
*
* Authors:
* Charles Kerr <charles.kerr@canonical.com>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <map>
#include <glib.h>
#include <glib/gstdio.h>
#include <gio/gio.h>
#include <gtest/gtest.h>
class GlibFixture : public ::testing::Test
{
private:
GLogFunc realLogHandler;
protected:
std::map<GLogLevelFlags,int> logCounts;
void testLogCount (GLogLevelFlags log_level, int expected G_GNUC_UNUSED)
{
ASSERT_EQ (expected, logCounts[log_level]);
logCounts.erase (log_level);
}
private:
static void default_log_handler (const gchar * log_domain,
GLogLevelFlags log_level,
const gchar * message,
gpointer self)
{
g_print ("%s - %d - %s", log_domain, (int)log_level, message);
static_cast<GlibFixture*>(self)->logCounts[log_level]++;
}
protected:
virtual void SetUp ()
{
loop = g_main_loop_new (NULL, FALSE);
g_log_set_default_handler (default_log_handler, this);
// only use local, temporary settings
g_setenv ("GSETTINGS_SCHEMA_DIR", SCHEMA_DIR, TRUE);
g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
g_debug ("SCHEMA_DIR is %s", SCHEMA_DIR);
}
virtual void TearDown()
{
// confirm there aren't any unexpected log messages
ASSERT_EQ (0, logCounts[G_LOG_LEVEL_ERROR]);
ASSERT_EQ (0, logCounts[G_LOG_LEVEL_CRITICAL]);
ASSERT_EQ (0, logCounts[G_LOG_LEVEL_WARNING]);
ASSERT_EQ (0, logCounts[G_LOG_LEVEL_MESSAGE]);
ASSERT_EQ (0, logCounts[G_LOG_LEVEL_INFO]);
// revert to glib's log handler
g_log_set_default_handler (realLogHandler, this);
g_clear_pointer (&loop, g_main_loop_unref);
}
private:
static gboolean
wait_for_signal__timeout (gpointer name)
{
g_error ("%s: timed out waiting for signal '%s'", G_STRLOC, (char*)name);
return G_SOURCE_REMOVE;
}
protected:
/* convenience func to loop while waiting for a GObject's signal */
void wait_for_signal (gpointer o, const gchar * signal, const int timeout_seconds=5)
{
// wait for the signal or for timeout, whichever comes first
guint handler_id = g_signal_connect_swapped (o, signal,
G_CALLBACK(g_main_loop_quit),
loop);
gulong timeout_id = g_timeout_add_seconds (timeout_seconds,
wait_for_signal__timeout,
loop);
g_main_loop_run (loop);
g_source_remove (timeout_id);
g_signal_handler_disconnect (o, handler_id);
}
/* convenience func to loop for N msec */
void wait_msec (int msec=50)
{
guint id = g_timeout_add (msec, (GSourceFunc)g_main_loop_quit, loop);
g_main_loop_run (loop);
g_source_remove (id);
}
GMainLoop * loop;
};
|