ephy-snapshot-service-test.c 8.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 *  Copyright © 2012 Igalia S.L.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
16
 *  along with Epiphany.  If not, see <http://www.gnu.org/licenses/>.
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
 */

#include "config.h"
#include "ephy-debug.h"
#include "ephy-snapshot-service.h"

#include <libsoup/soup.h>
#include <string.h>

#define TEST_SERVER_URI "http://127.0.0.1:45716"
static time_t mtime;
SoupServer *server;

static gboolean
quit_when_test_done (GtkWidget *w,
                     GdkEvent *event,
                     gint *tests)
{
  if (--(*tests) == 0)
    gtk_main_quit ();

  return FALSE;
}

static void
on_snapshot_ready (GObject *source,
                   GAsyncResult *res,
                   gint *tests)
{
  GdkPixbuf *pixbuf;
#if 0
  GtkWidget *w,*i;
#endif
  GError *error = NULL;

  pixbuf = ephy_snapshot_service_get_snapshot_finish (EPHY_SNAPSHOT_SERVICE (source),
53
                                                      res, NULL, &error);
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 196 197 198 199 200 201 202 203 204 205 206 207 208
  g_assert (GDK_IS_PIXBUF (pixbuf) || error != NULL);

  if (error) {
    g_print ("Error loading pixbuf: %s\n", error->message);
    g_error_free (error);
    quit_when_test_done (NULL, NULL, tests);
    return;
  } else

#if 0
  w = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  i = gtk_image_new_from_pixbuf (pixbuf);
  gtk_container_add (GTK_CONTAINER (w), i);
  gtk_widget_show_all (w);
  g_signal_connect (w, "delete-event",
                    G_CALLBACK (quit_when_test_done), tests);
#else
  quit_when_test_done (NULL, NULL, tests);
#endif
}

static void
test_snapshot (void)
{
  EphySnapshotService *service = ephy_snapshot_service_get_default ();
  WebKitWebView *webview = WEBKIT_WEB_VIEW (webkit_web_view_new ());
  gint tests = 1;

  webkit_web_view_load_uri (webview, TEST_SERVER_URI);
  ephy_snapshot_service_get_snapshot_async (service,
                                            webview,
                                            mtime,
                                            NULL,
                                            (GAsyncReadyCallback)on_snapshot_ready,
                                            &tests);
  gtk_main ();
}

static void
test_cached_snapshot (void)
{
  gint tests = 1;
  EphySnapshotService *service = ephy_snapshot_service_get_default ();
  WebKitWebView *webview = WEBKIT_WEB_VIEW (webkit_web_view_new ());

  webkit_web_view_load_uri (webview, TEST_SERVER_URI);
  ephy_snapshot_service_get_snapshot_async (service,
                                            webview,
                                            mtime,
                                            NULL,
                                            (GAsyncReadyCallback)on_snapshot_ready,
                                            &tests);
  gtk_main ();
}

static void
test_many_snapshots (void)
{
  WebKitWebView *webview;
  gint tests = 3;
  EphySnapshotService *service = ephy_snapshot_service_get_default ();

  webview = WEBKIT_WEB_VIEW (webkit_web_view_new ());
  webkit_web_view_load_uri (webview, TEST_SERVER_URI "/some");
  ephy_snapshot_service_get_snapshot_async (service,
                                            webview,
                                            mtime,
                                            NULL,
                                            (GAsyncReadyCallback)on_snapshot_ready,
                                            &tests);

  webview = WEBKIT_WEB_VIEW (webkit_web_view_new ());
  webkit_web_view_load_uri (webview, TEST_SERVER_URI "/other");
  ephy_snapshot_service_get_snapshot_async (service,
                                            webview,
                                            mtime,
                                            NULL,
                                            (GAsyncReadyCallback)on_snapshot_ready,
                                            &tests);

  webview = WEBKIT_WEB_VIEW (webkit_web_view_new ());
  webkit_web_view_load_uri (webview, TEST_SERVER_URI "/place");
  ephy_snapshot_service_get_snapshot_async (service,
                                            webview,
                                            mtime,
                                            NULL,
                                            (GAsyncReadyCallback)on_snapshot_ready,
                                            &tests);
  gtk_main ();
}

static void
test_snapshot_with_cancellable (void)
{
  gint tests = 1;
  EphySnapshotService *service = ephy_snapshot_service_get_default ();
  WebKitWebView *webview = WEBKIT_WEB_VIEW (webkit_web_view_new ());
  GCancellable *cancellable = g_cancellable_new ();

  webkit_web_view_load_uri (webview, TEST_SERVER_URI "/and");
  ephy_snapshot_service_get_snapshot_async (service,
                                            webview,
                                            mtime,
                                            cancellable,
                                            (GAsyncReadyCallback)on_snapshot_ready,
                                            &tests);
  gtk_main ();
}

static void
test_already_cancelled_snapshot (void)
{
  gint tests = 1;
  EphySnapshotService *service = ephy_snapshot_service_get_default ();
  GCancellable *cancellable = g_cancellable_new ();
  WebKitWebView *webview = WEBKIT_WEB_VIEW (webkit_web_view_new ());
  webkit_web_view_load_uri (webview, TEST_SERVER_URI "/so");

  g_cancellable_cancel (cancellable);
  ephy_snapshot_service_get_snapshot_async (service,
                                            webview,
                                            mtime,
                                            cancellable,
                                            (GAsyncReadyCallback)on_snapshot_ready,
                                            &tests);
  gtk_main ();
}

static gboolean
cancel (GCancellable *cancellable)
{
  g_cancellable_cancel (cancellable);
  return FALSE;
}

static void
test_snapshot_and_timed_cancellation (void)
{
  gint tests = 1;
  EphySnapshotService *service = ephy_snapshot_service_get_default ();
  GCancellable *cancellable = g_cancellable_new ();
  WebKitWebView *webview = WEBKIT_WEB_VIEW (webkit_web_view_new ());
  webkit_web_view_load_uri (webview, TEST_SERVER_URI "/on");

  ephy_snapshot_service_get_snapshot_async (service,
                                            webview,
                                            mtime,
                                            cancellable,
                                            (GAsyncReadyCallback)on_snapshot_ready,
                                            &tests);
  g_timeout_add (15, (GSourceFunc)cancel, cancellable);
  gtk_main ();
}

static void
209
server_callback (SoupServer *s, SoupMessage *msg,
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
                 const char *path, GHashTable *query,
                 SoupClientContext *context, gpointer data)
{
  const char *response = "<html><h1>This is a header</h1></html>";

  if (msg->method == SOUP_METHOD_GET) {
		soup_message_set_status (msg, SOUP_STATUS_OK);
		soup_message_body_append (msg->response_body, SOUP_MEMORY_STATIC,
                              response, strlen (response));
		soup_message_body_complete (msg->response_body);
  }
  else
    soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
}

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

231
  server = soup_server_new (SOUP_SERVER_SERVER_HEADER, "snapshot-service-test-server",
232 233 234
                            NULL);
	soup_server_add_handler (server, NULL,
                           server_callback, NULL, NULL);
235 236 237
  soup_server_listen_local (server, 45716,
                            SOUP_SERVER_LISTEN_IPV4_ONLY,
                            NULL);
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
  mtime = time(NULL);

  g_test_add_func ("/lib/ephy-snapshot-service/test_snapshot",
                   test_snapshot);
  g_test_add_func ("/lib/ephy-snapshot-service/test_cached_snapshot",
                   test_cached_snapshot);
  g_test_add_func ("/lib/ephy-snapshot-service/test_many_snapshots",
                   test_many_snapshots);
  g_test_add_func ("/lib/ephy-snapshot-service/test_snapshot_with_cancellable",
                   test_snapshot_with_cancellable);
  g_test_add_func ("/lib/ephy-snapshot-service/test_already_cancelled_snapshot",
                   test_already_cancelled_snapshot);
  g_test_add_func ("/lib/ephy-snapshot-service/test_snapshot_and_timed_cancellation",
                   test_snapshot_and_timed_cancellation);
  return g_test_run ();
}