File: test_lua_factory_source_errors.c

package info (click to toggle)
grilo-plugins 0.3.18-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,348 kB
  • sloc: ansic: 52,656; xml: 2,879; python: 1,137; makefile: 9
file content (189 lines) | stat: -rw-r--r-- 5,741 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (C) 2016. All rights reserved.
 *
 * Author: Victor Toso <me@victortoso.com>
 *
 * This library 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; version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library 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
 *
 */

#include <locale.h>
#include <grilo.h>
#include <string.h>

#define TEST_URL_XML_PARSER "http://xml.parser.test/lua-factory/"
#define LOCAL_CONTENT       TEST_URL_XML_PARSER "simple.xml"

#define LUA_FACTORY_ID "grl-lua-factory"
#define FAKE_SOURCE_ID "test-source-lua-errors"

#define TEST_NOT_CALLBACK_SIMPLE      "test-not-calback-simple"
#define TEST_NOT_CALLBACK_ASYNC       "test-not-callback-on-async"
#define TEST_CALLBACK_ON_FINISHED_OP  "test-callback-after-finished"
#define TEST_MULTIPLE_FETCH           "multiple-fetch-with-callback"

static void
test_lua_factory_setup (GrlConfig *config)
{
  GrlRegistry *registry;
  GError *error = NULL;

  registry = grl_registry_get_default ();

  if (config != NULL) {
    grl_registry_add_config (registry, config, &error);
    g_assert_no_error (error);
  }

  grl_registry_load_all_plugins (registry, FALSE, NULL);
  grl_registry_activate_plugin_by_id (registry, LUA_FACTORY_ID, &error);
  g_assert_no_error (error);
}

static GrlSource*
test_lua_factory_get_source (gchar *source_id,
                             GrlSupportedOps source_ops)
{
  GrlRegistry *registry = grl_registry_get_default ();
  GrlSource *source = grl_registry_lookup_source (registry, source_id);
  g_assert_nonnull (source);
  g_assert (grl_source_supported_operations (source) & source_ops);
  return source;
}

static void
test_lua_factory_shutdown (void)
{
  GrlRegistry *registry;
  GError *error = NULL;

  registry = grl_registry_get_default ();
  grl_registry_unload_plugin (registry, LUA_FACTORY_ID, &error);
  g_assert_no_error (error);
}

static void
set_test_data (GrlSource **source,
               GrlMedia **media,
               GrlOperationOptions **options,
               GList **keys,
               GrlSupportedOps source_op)
{
  *source = test_lua_factory_get_source (FAKE_SOURCE_ID, source_op);
  g_assert_nonnull (*source);

  *media = grl_media_new ();
  grl_data_add_string (GRL_DATA (*media), GRL_METADATA_KEY_URL, LOCAL_CONTENT);

  *keys = grl_metadata_key_list_new (GRL_METADATA_KEY_TITLE, NULL);
  *options = grl_operation_options_new (NULL);
  grl_operation_options_set_resolution_flags (*options, GRL_RESOLVE_NORMAL);
}

static void
free_test_data (GrlMedia **media,
                GrlOperationOptions **options,
                GList **keys)
{
  g_object_unref (*media);
  g_list_free (*keys);
  g_object_unref (*options);
}

static void
execute_resolve_test (const gchar *test_id)
{
  GrlSource *source;
  GrlMedia *media;
  GrlOperationOptions *options;
  GList *keys;

  set_test_data (&source, &media, &options, &keys, GRL_OP_RESOLVE);
  grl_media_set_id (media, test_id);
  grl_source_resolve_sync (source, media, keys, options, NULL);
  free_test_data (&media, &options, &keys);
}

static void
execute_search_test (const gchar *test_id)
{
  GrlSource *source;
  GrlMedia *media;
  GrlOperationOptions *options;
  GList *keys, *list_medias;

  set_test_data (&source, &media, &options, &keys, GRL_OP_RESOLVE);
  list_medias = grl_source_search_sync (source, test_id, keys, options, NULL);
  g_list_free_full (list_medias, g_object_unref);
  free_test_data (&media, &options, &keys);
}

static void
test_correct_state_on_multiple_fetch (void)
{
  execute_search_test (TEST_MULTIPLE_FETCH);
}

static void
test_callback_after_end_of_operation (void)
{
  g_test_expect_message("Grilo", G_LOG_LEVEL_WARNING, "*Can't retrieve current operation*");
  execute_resolve_test (TEST_CALLBACK_ON_FINISHED_OP);
}

static void
test_not_callback_on_async (void)
{
  g_test_expect_message("Grilo", G_LOG_LEVEL_WARNING,
        "*Source 'test-source-lua-errors' is broken, as the finishing callback was not called for*");
  execute_resolve_test (TEST_NOT_CALLBACK_ASYNC);
}

static void
test_not_callback_simple (void)
{
  g_test_expect_message("Grilo", G_LOG_LEVEL_WARNING,
        "*Source 'test-source-lua-errors' is broken, as the finishing callback was not called for*");
  execute_resolve_test (TEST_NOT_CALLBACK_SIMPLE);
}

gint
main (gint argc, gchar **argv)
{
  setlocale (LC_ALL, "");

  g_setenv ("GRL_PLUGIN_PATH", LUA_FACTORY_PLUGIN_PATH, TRUE);
  g_setenv ("GRL_PLUGIN_LIST", LUA_FACTORY_ID, TRUE);
  g_setenv ("GRL_NET_MOCKED", LUA_FACTORY_DATA_PATH "config.ini", TRUE);
  g_setenv ("GRL_LUA_SOURCES_PATH", LUA_FACTORY_DATA_PATH, TRUE);

  grl_init (&argc, &argv);
  g_test_init (&argc, &argv, NULL);

  test_lua_factory_setup (NULL);

  g_test_add_func ("/lua-factory/lua-library/not-callback-simple", test_not_callback_simple);
  g_test_add_func ("/lua-factory/lua-library/not-callback-after-async", test_not_callback_on_async);
  g_test_add_func ("/lua-factory/lua-library/callback-after-end", test_callback_after_end_of_operation);
  g_test_add_func ("/lua-factory/lua-library/multiple-fetch", test_correct_state_on_multiple_fetch);

  gint result = g_test_run ();

  test_lua_factory_shutdown ();
  grl_deinit ();

  return result;
}