summaryrefslogtreecommitdiff
path: root/tests/libpeas/extension-lua.c
blob: 79f42a6f68744bf635ae720268d600184ac080d3 (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * extension-lua.c
 * This file is part of libpeas
 *
 * Copyright (C) 2014 - Garrett Regier
 *
 * libpeas is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * libpeas is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <lua.h>
#include <lauxlib.h>

#include <libpeas/peas-activatable.h>
#include "libpeas/peas-engine-priv.h"

#include "testing/testing-extension.h"
#include "introspection/introspection-base.h"


/* We must stop and start the garbage collector
 * when testing reference counts otherwise issues
 * will occur if the garbage collector runs preemptively.
 */
static void
set_garbage_collector_state (PeasEngine     *engine,
                             PeasPluginInfo *info,
                             gboolean        start)
{
  PeasExtension *extension;

  extension = peas_engine_create_extension (engine, info,
                                            PEAS_TYPE_ACTIVATABLE,
                                            NULL);

  if (start)
    {
      /* collectgarbage('restart') */
      peas_activatable_activate (PEAS_ACTIVATABLE (extension));
    }
  else
    {
      /* collectgarbage('stop') */
      peas_activatable_deactivate (PEAS_ACTIVATABLE (extension));
    }

  g_object_unref (extension);
}

static void
test_extension_lua_instance_refcount (PeasEngine     *engine,
                                      PeasPluginInfo *info)
{
  PeasExtension *extension;

  set_garbage_collector_state (engine, info, FALSE);

  extension = peas_engine_create_extension (engine, info,
                                            PEAS_TYPE_ACTIVATABLE,
                                            NULL);
  g_object_add_weak_pointer (extension, (gpointer *) &extension);

  g_assert (PEAS_IS_EXTENSION (extension));

  /* The Lua wrapper created around the extension
   * object should have increased its refcount by 1.
   */
  g_assert_cmpint (extension->ref_count, ==, 2);

  /* The Lua wrapper around the extension has been garbage collected */
  peas_engine_garbage_collect (engine);
  g_assert_cmpint (G_OBJECT (extension)->ref_count, ==, 1);

  /* Create a new Lua wrapper around the extension */
  peas_activatable_update_state (PEAS_ACTIVATABLE (extension));
  g_assert_cmpint (G_OBJECT (extension)->ref_count, ==, 2);

  /* The Lua wrapper still exists */
  g_object_unref (extension);
  g_assert_cmpint (extension->ref_count, ==, 1);

  /* The Lua wrapper around the extension has been garbage collected */
  peas_engine_garbage_collect (engine);
  g_assert (extension == NULL);

  set_garbage_collector_state (engine, info, TRUE);
}

static void
test_extension_lua_activatable_subject_refcount (PeasEngine     *engine,
                                                 PeasPluginInfo *info)
{
  PeasExtension *extension;
  GObject *object;

  set_garbage_collector_state (engine, info, FALSE);

  /* Create the 'object' property value, to be similar to a GtkWindow
   * instance: a sunk GInitiallyUnowned object.
   */
  object = g_object_new (G_TYPE_INITIALLY_UNOWNED, NULL);
  g_object_add_weak_pointer (object, (gpointer *) &object);
  g_object_ref_sink (object);
  g_assert_cmpint (object->ref_count, ==, 1);

  /* We pre-create the wrapper to make it easier to check reference count */
  extension = peas_engine_create_extension (engine, info,
                                            PEAS_TYPE_ACTIVATABLE,
                                            "object", object,
                                            NULL);
  g_object_add_weak_pointer (extension, (gpointer *) &extension);

  g_assert (PEAS_IS_EXTENSION (extension));

  /* The Lua wrapper created around our dummy
   * object should have increased its refcount by 1.
   */
  peas_engine_garbage_collect (engine);
  g_assert_cmpint (object->ref_count, ==, 2);

  g_object_unref (extension);
  g_assert (extension == NULL);

  /* We unreffed the extension, so it should have been
   * destroyed and our dummy object's refcount should be back to 1.
   */
  peas_engine_garbage_collect (engine);
  g_assert_cmpint (object->ref_count, ==, 1);

  g_object_unref (object);
  g_assert (object == NULL);

  set_garbage_collector_state (engine, info, TRUE);
}

static void
test_extension_lua_nonexistent (PeasEngine *engine)
{
  PeasPluginInfo *info;

  testing_util_push_log_hook ("Error loading plugin "
                              "'extension-lua51-nonexistent'*");

  info = peas_engine_get_plugin_info (engine, "extension-lua51-nonexistent");

  g_assert (!peas_engine_load_plugin (engine, info));
}

int
main (int   argc,
      char *argv[])
{
  testing_init (&argc, &argv);

  /* Only test the basics */
  testing_extension_basic ("lua5.1");

  /* We still need to add the callable tests
   * because of peas_extension_call()
   */
  testing_extension_callable ("lua5.1");

#undef EXTENSION_TEST
#undef EXTENSION_TEST_FUNC
  
#define EXTENSION_TEST(loader, path, func) \
  testing_extension_add (EXTENSION_TEST_NAME (loader, path), \
                         (gpointer) test_extension_lua_##func)

#define EXTENSION_TEST_FUNC(loader, path, func) \
  g_test_add_func (EXTENSION_TEST_NAME (loader, path), \
                   (gpointer) test_extension_lua_##func)

  EXTENSION_TEST (lua5.1, "instance-refcount", instance_refcount);
  EXTENSION_TEST (lua5.1, "activatable-subject-refcount",
                  activatable_subject_refcount);
  EXTENSION_TEST (lua5.1, "nonexistent", nonexistent);

  return testing_extension_run_tests ();
}