1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3  *  Copyright © 2011 Igalia S.L.
4  *
5  *  This file is part of Epiphany.
6  *
7  *  Epiphany is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  Epiphany is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with Epiphany.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "config.h"
22 #include "ephy-debug.h"
23 #include "ephy-download.h"
24 #include "ephy-embed-prefs.h"
25 #include "ephy-file-helpers.h"
26 #include "ephy-shell.h"
27 
28 #include <glib.h>
29 #include <glib/gstdio.h>
30 #include <gtk/gtk.h>
31 #include <libsoup/soup.h>
32 #include <string.h>
33 
34 #define HTML_STRING "testing-ephy-download"
35 SoupURI *base_uri;
36 
37 static char *
get_uri_for_path(const char * path)38 get_uri_for_path (const char *path)
39 {
40   SoupURI *uri;
41   char *uri_string;
42 
43   uri = soup_uri_new_with_base (base_uri, path);
44   uri_string = soup_uri_to_string (uri, FALSE);
45   soup_uri_free (uri);
46 
47   return uri_string;
48 }
49 
50 static void
server_callback(SoupServer * server,SoupMessage * msg,const char * path,GHashTable * query,SoupClientContext * context,gpointer data)51 server_callback (SoupServer        *server,
52                  SoupMessage       *msg,
53                  const char        *path,
54                  GHashTable        *query,
55                  SoupClientContext *context,
56                  gpointer           data)
57 {
58   soup_message_set_status (msg, SOUP_STATUS_OK);
59 
60   if (!strcmp (path, "/cancelled"))
61     soup_message_set_status (msg, SOUP_STATUS_CANT_CONNECT);
62 
63   soup_message_body_append (msg->response_body, SOUP_MEMORY_STATIC,
64                             HTML_STRING, strlen (HTML_STRING));
65 
66   soup_message_body_complete (msg->response_body);
67 }
68 
69 typedef struct {
70   GMainLoop *loop;
71   EphyDownload *download;
72   char *destination;
73   char *source;
74 } Fixture;
75 
76 static void
fixture_setup(Fixture * fixture,gconstpointer data)77 fixture_setup (Fixture       *fixture,
78                gconstpointer  data)
79 {
80   char *tmp_filename;
81   char *dest_file;
82 
83   tmp_filename = ephy_file_tmp_filename (".ephy-download-XXXXXX", NULL);
84   dest_file = g_build_filename (ephy_file_tmp_dir (), tmp_filename, NULL);
85 
86   fixture->source = get_uri_for_path ("/default");
87   fixture->download = ephy_download_new_for_uri (fixture->source);
88   fixture->destination = g_filename_to_uri (dest_file, NULL, NULL);
89   fixture->loop = g_main_loop_new (NULL, TRUE);
90 
91   ephy_download_set_destination_uri (fixture->download, fixture->destination);
92 
93   g_free (tmp_filename);
94   g_free (dest_file);
95 }
96 
97 static void
fixture_teardown(Fixture * fixture,gconstpointer data)98 fixture_teardown (Fixture       *fixture,
99                   gconstpointer  data)
100 {
101   g_free (fixture->destination);
102   g_free (fixture->source);
103 
104   g_object_unref (fixture->download);
105 
106   g_main_loop_unref (fixture->loop);
107 }
108 
109 static gboolean
test_file_was_downloaded(EphyDownload * download)110 test_file_was_downloaded (EphyDownload *download)
111 {
112   char *filename;
113   gboolean ret;
114 
115   filename = g_filename_from_uri (ephy_download_get_destination_uri (download),
116                                   NULL, NULL);
117 
118   ret = g_file_test (filename, G_FILE_TEST_EXISTS);
119   g_free (filename);
120 
121   return ret;
122 }
123 
124 static void
completed_cb(EphyDownload * download,Fixture * fixture)125 completed_cb (EphyDownload *download,
126               Fixture      *fixture)
127 {
128   g_assert_true (test_file_was_downloaded (download));
129   g_main_loop_quit (fixture->loop);
130 }
131 
132 static void
test_ephy_download_new(Fixture * fixture,gconstpointer data)133 test_ephy_download_new (Fixture       *fixture,
134                         gconstpointer  data)
135 {
136   g_assert_true (EPHY_IS_DOWNLOAD (fixture->download));
137 }
138 
139 static void
test_ephy_download_new_for_uri(Fixture * fixture,gconstpointer data)140 test_ephy_download_new_for_uri (Fixture       *fixture,
141                                 gconstpointer  data)
142 {
143   WebKitDownload *download = ephy_download_get_webkit_download (fixture->download);
144   WebKitURIRequest *request = webkit_download_get_request (download);
145 
146   g_assert_nonnull (request);
147   g_assert_cmpstr (fixture->source, ==, webkit_uri_request_get_uri (request));
148 }
149 
150 static void
test_ephy_download_start(Fixture * fixture,gconstpointer data)151 test_ephy_download_start (Fixture       *fixture,
152                           gconstpointer  data)
153 {
154   g_signal_connect (G_OBJECT (fixture->download), "completed",
155                     G_CALLBACK (completed_cb), fixture);
156 
157   g_main_loop_run (fixture->loop);
158 }
159 
160 int
main(int argc,char * argv[])161 main (int   argc,
162       char *argv[])
163 {
164   int ret;
165   GSList *uris;
166   SoupServer *server;
167 
168   gtk_test_init (&argc, &argv);
169 
170   ephy_debug_init ();
171 
172   if (!ephy_file_helpers_init (NULL,
173                                EPHY_FILE_HELPERS_TESTING_MODE | EPHY_FILE_HELPERS_ENSURE_EXISTS,
174                                NULL)) {
175     g_debug ("Something wrong happened with ephy_file_helpers_init()");
176     return -1;
177   }
178 
179   _ephy_shell_create_instance (EPHY_EMBED_SHELL_MODE_TEST);
180   g_application_register (G_APPLICATION (ephy_shell_get_default ()), NULL, NULL);
181 
182   server = soup_server_new (NULL, NULL);
183   soup_server_listen_local (server, 0,
184                             SOUP_SERVER_LISTEN_IPV4_ONLY,
185                             NULL);
186   uris = soup_server_get_uris (server);
187   base_uri = (SoupURI *)uris->data;
188   g_slist_free (uris);
189 
190   soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
191 
192   g_test_add ("/embed/ephy-download/new",
193               Fixture, NULL, fixture_setup,
194               test_ephy_download_new, fixture_teardown);
195   g_test_add ("/embed/ephy-download/new_for_uri",
196               Fixture, NULL, fixture_setup,
197               test_ephy_download_new_for_uri, fixture_teardown);
198   g_test_add ("/embed/ephy-download/start",
199               Fixture, NULL, fixture_setup,
200               test_ephy_download_start, fixture_teardown);
201 
202   ret = g_test_run ();
203 
204   g_object_unref (ephy_shell_get_default ());
205   ephy_file_helpers_shutdown ();
206 
207   return ret;
208 }
209