tests: Unit test for LiveResponse
authorZeeshan Ali (Khattak) <zeeshanak@gnome.org>
Tue, 16 Mar 2010 16:35:18 +0000 (18:35 +0200)
committerZeeshan Ali (Khattak) <zeeshanak@gnome.org>
Wed, 17 Mar 2010 15:45:25 +0000 (17:45 +0200)
tests/Makefile.am
tests/rygel-gst-utils.vala [new symlink]
tests/rygel-http-seek.vala [new symlink]
tests/rygel-live-response-test.vala [new file with mode: 0644]
tests/rygel-live-response.vala [new symlink]

index fbdfd7b..65b4c5b 100644 (file)
@@ -23,15 +23,33 @@ LDADD = $(LIBGUPNP_LIBS) \
 AM_VALAFLAGS = --thread \
               --pkg gupnp-1.0 --pkg gupnp-av-1.0 --pkg dbus-glib-1 \
                --pkg gconf-2.0 --pkg gstreamer-0.10 \
-               --pkg gio-2.0 --pkg gee-1.0 -g
+               --pkg gio-2.0 --pkg gee-1.0
 
 check_PROGRAMS = rygel-http-item-uri-test \
-                rygel-http-response-test
+                rygel-http-response-test \
+                rygel-live-response-test
 TESTS = $(check_PROGRAMS)
 
 rygel_http_item_uri_test_SOURCES = rygel-http-item-uri-test.vala \
                                    rygel-http-item-uri.vala
 
-rygel_http_response_test_SOURCES = rygel-http-response-test.vala \
-                                  rygel-http-response.vala \
-                                  rygel-state-machine.vala
+noinst_LTLIBRARIES = librygel-http-response.la
+librygel_http_response_la_SOURCES = rygel-http-response.vala \
+                                   rygel-http-seek.vala \
+                                   rygel-state-machine.vala \
+                                   rygel-gst-utils.vala
+librygel_http_response_la_VALAFLAGS = $(AM_VALAFLAGS) \
+                                    --header rygel-http-response-external.h \
+                                    -h rygel-http-response-internal.h \
+                                    --internal-vapi rygel-http-response.vapi
+
+rygel_http_response_test_SOURCES = rygel-http-response-test.vala
+rygel_http_response_test_VALAFLAGS = $(AM_VALAFLAGS) --vapidir $(srcdir) \
+                                    --pkg rygel-http-response
+rygel_http_response_test_LDADD = $(LDADD) librygel-http-response.la
+
+rygel_live_response_test_SOURCES = rygel-live-response-test.vala \
+                                  rygel-live-response.vala
+rygel_live_response_test_VALAFLAGS = $(AM_VALAFLAGS) --vapidir $(srcdir) \
+                                    --pkg rygel-http-response
+rygel_live_response_test_LDADD = $(LDADD) librygel-http-response.la
diff --git a/tests/rygel-gst-utils.vala b/tests/rygel-gst-utils.vala
new file mode 120000 (symlink)
index 0000000..0cf84c7
--- /dev/null
@@ -0,0 +1 @@
+../src/rygel/rygel-gst-utils.vala
\ No newline at end of file
diff --git a/tests/rygel-http-seek.vala b/tests/rygel-http-seek.vala
new file mode 120000 (symlink)
index 0000000..343aaf6
--- /dev/null
@@ -0,0 +1 @@
+../src/rygel/rygel-http-seek.vala
\ No newline at end of file
diff --git a/tests/rygel-live-response-test.vala b/tests/rygel-live-response-test.vala
new file mode 100644 (file)
index 0000000..bd5ae4a
--- /dev/null
@@ -0,0 +1,194 @@
+/*
+ * Copyright (C) 2010 Nokia Corporation.
+ *
+ * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
+ *                               <zeeshan.ali@nokia.com>
+ *
+ * This file is part of Rygel.
+ *
+ * Rygel 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 of the License, or
+ * (at your option) any later version.
+ *
+ * Rygel 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 program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+using Soup;
+using Gst;
+
+public errordomain Rygel.TestError {
+    SKIP = 77,
+    TIMEOUT
+}
+
+public class Rygel.LiveResponseTest : GLib.Object {
+    private static const long MAX_BYTES = 1024;
+    private static const long BLOCK_SIZE = MAX_BYTES / 16;
+    private static const long MAX_BUFFERS = MAX_BYTES / BLOCK_SIZE;
+
+    private HTTPServer server;
+    private HTTPClient client;
+
+    private MainLoop main_loop;
+
+    private dynamic Element src;
+
+    private Error error;
+
+    public static int main (string[] args) {
+        Gst.init (ref args);
+
+        try {
+            var test = new LiveResponseTest ();
+
+            test.run ();
+        } catch (TestError.SKIP error) {
+            return error.code;
+        } catch (Error error) {
+            critical ("%s", error.message);
+
+            return -1;
+        }
+
+        return 0;
+    }
+
+    private LiveResponseTest () throws Error {
+        this.server = new HTTPServer ();
+        this.client = new HTTPClient (this.server.context,
+                                      this.server.uri,
+                                      MAX_BYTES);
+        this.main_loop = new MainLoop (null, false);
+        this.src = GstUtils.create_element ("audiotestsrc", null);
+        this.src.blocksize = BLOCK_SIZE;
+        this.src.num_buffers = MAX_BUFFERS;
+    }
+
+    private void run () throws Error {
+        Timeout.add_seconds (3, this.on_timeout);
+        this.server.message_received.connect (this.on_message_received);
+        this.client.completed.connect (this.on_client_completed);
+
+        this.client.run.begin ();
+
+        this.main_loop.run ();
+
+        if (this.error != null) {
+            throw this.error;
+        }
+    }
+
+    private void on_client_completed (StateMachine client) {
+        this.main_loop.quit ();
+    }
+
+    private void on_message_received (HTTPServer   server,
+                                      Soup.Message msg) {
+        try {
+            var response = new LiveResponse (server.context.server,
+                                             msg,
+                                             "TestingLiveResponse",
+                                             this.src,
+                                             null,
+                                             null);
+
+            response.run.begin ();
+        } catch (Error error) {
+            this.error = error;
+            this.main_loop.quit ();
+
+            return;
+        }
+    }
+
+    private bool on_timeout () {
+        this.error = new TestError.TIMEOUT ("Timeout");
+        this.main_loop.quit ();
+
+        return false;
+    }
+}
+
+private class Rygel.HTTPServer : GLib.Object {
+    private const string SERVER_PATH = "/RygelHTTPServer/Rygel/Test";
+
+    public GUPnP.Context context;
+
+    public string uri {
+        owned get { return "http://" +
+                           this.context.host_ip + ":" +
+                           this.context.port.to_string () +
+                           SERVER_PATH;
+        }
+    }
+
+    public signal void message_received (Soup.Message message);
+
+    public HTTPServer () throws TestError {
+        try {
+            this.context = new GUPnP.Context (null, "lo", 0);
+        } catch (Error error) {
+            throw new TestError.SKIP ("Network context not available");
+        }
+
+        assert (this.context != null);
+        assert (this.context.host_ip != null);
+        assert (this.context.port > 0);
+
+        this.context.server.add_handler (SERVER_PATH, this.server_cb);
+    }
+
+    private void server_cb (Server        server,
+                            Soup.Message  msg,
+                            string        path,
+                            HashTable?    query,
+                            ClientContext client) {
+        this.message_received (msg);
+    }
+}
+
+private class Rygel.HTTPClient : GLib.Object, StateMachine {
+    public GUPnP.Context context;
+    public Soup.Message msg;
+    public size_t total_bytes;
+
+    public Cancellable cancellable { get; set; }
+
+    public HTTPClient (GUPnP.Context context,
+                       string        uri,
+                       size_t        total_bytes) {
+        this.context = context;
+        this.total_bytes = total_bytes;
+
+        this.msg = new Soup.Message ("HTTP",  uri);
+        assert (this.msg != null);
+        this.msg.response_body.set_accumulate (false);
+    }
+
+    public async void run () {
+        SourceFunc run_continue = run.callback;
+        size_t bytes_received = 0;
+
+        this.msg.got_chunk.connect ((msg, chunk) => {
+            bytes_received += chunk.length;
+        });
+
+        this.context.session.queue_message (this.msg, (msg, chunk) => {
+            assert (bytes_received == this.total_bytes);
+
+            run_continue ();
+        });
+
+        yield;
+
+        this.completed ();
+    }
+}
diff --git a/tests/rygel-live-response.vala b/tests/rygel-live-response.vala
new file mode 120000 (symlink)
index 0000000..0fc760f
--- /dev/null
@@ -0,0 +1 @@
+../src/rygel/rygel-live-response.vala
\ No newline at end of file