vala-tests r9 - trunk/tests/examples



Author: malureau
Date: Fri Oct 17 21:48:59 2008
New Revision: 9
URL: http://svn.gnome.org/viewvc/vala-tests?rev=9&view=rev

Log:
Add some more tests, with --interactive option

Added:
   trunk/tests/examples/async-gio.test   (contents, props changed)
   trunk/tests/examples/async-gio.vala
   trunk/tests/examples/bluez-dbus-sample.test   (contents, props changed)
   trunk/tests/examples/bluez-dbus-sample.vala
   trunk/tests/examples/cairo.test   (contents, props changed)
   trunk/tests/examples/cairo.vala
   trunk/tests/examples/dbus-sample.test   (contents, props changed)
   trunk/tests/examples/dbus-sample.vala
   trunk/tests/examples/dbus-server-sample.test   (contents, props changed)
   trunk/tests/examples/dbus-server-sample.vala
   trunk/tests/examples/egg-clock.test   (contents, props changed)
   trunk/tests/examples/egg-clock.vala
Modified:
   trunk/tests/examples/advanced.test
   trunk/tests/examples/basic.test
   trunk/tests/examples/list.test
   trunk/tests/examples/properties-construction.test
   trunk/tests/examples/properties.test
   trunk/tests/examples/string.test
   trunk/tests/examples/update.sh

Modified: trunk/tests/examples/advanced.test
==============================================================================
--- trunk/tests/examples/advanced.test	(original)
+++ trunk/tests/examples/advanced.test	Fri Oct 17 21:48:59 2008
@@ -1,15 +1,30 @@
 #!/bin/sh
 
-SRCDIR=../tests/examples # this is not nice
+set -e
+
+SRCDIR=../tests/examples
 
 if [ "x$VALAC" = "x" ] ; then
   VALAC=valac
   SRCDIR=.
 fi
 
-set -e
+while true ; do
+    case "$1" in
+	--interact) INTERACT=1 ; shift ;;
+	--) shift ; break ;;
+	*) break ;;
+    esac
+done
 
 TESTNAME=`basename $0 .test`
 
-$VALAC -o $TESTNAME $SRCDIR/$TESTNAME.vala
-./$TESTNAME
+$VALAC  -o $TESTNAME $SRCDIR/$TESTNAME.vala
+
+if [ "x1" = "x1" -o "x$INTERACT" = "x1" ] ; then
+  ./$TESTNAME
+else
+  echo ""
+  echo "WARNING: This vala test is interactive and will not be run (try --interactive)"
+fi
+

Added: trunk/tests/examples/async-gio.test
==============================================================================
--- (empty file)
+++ trunk/tests/examples/async-gio.test	Fri Oct 17 21:48:59 2008
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+set -e
+
+SRCDIR=../tests/examples
+
+if [ "x$VALAC" = "x" ] ; then
+  VALAC=valac
+  SRCDIR=.
+fi
+
+while true ; do
+    case "$1" in
+	--interact) INTERACT=1 ; shift ;;
+	--) shift ; break ;;
+	*) break ;;
+    esac
+done
+
+TESTNAME=`basename $0 .test`
+
+$VALAC  --pkg gtk+-2.0 --pkg gio-2.0 -o $TESTNAME $SRCDIR/$TESTNAME.vala
+
+if [ "x0" = "x1" -o "x$INTERACT" = "x1" ] ; then
+  ./$TESTNAME
+else
+  echo ""
+  echo "WARNING: This vala test is interactive and will not be run (try --interactive)"
+fi
+

Added: trunk/tests/examples/async-gio.vala
==============================================================================
--- (empty file)
+++ trunk/tests/examples/async-gio.vala	Fri Oct 17 21:48:59 2008
@@ -0,0 +1,91 @@
+/* Asynchronous GIO in Vala sample code */
+
+using GLib;
+using Gtk;
+
+/**
+ * Loads the list of files in users home directory and displays them
+ * in a GTK+ list view.
+ */
+public class ASyncGIOSample : Gtk.Window {
+
+    private File dir;
+    private Gtk.ListStore model;
+
+    construct {
+        this.setup ();
+    }
+
+    /* Set up the window and widget */
+    private void setup () {
+        var sc = new Gtk.ScrolledWindow (null, null);
+        sc.set_policy (Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
+
+        var tv = new Gtk.TreeView ();
+        sc.add (tv);
+
+        tv.insert_column_with_attributes (-1, "Filename",
+										  new Gtk.CellRendererText (), "text", 0);
+
+        this.model = new Gtk.ListStore (1, typeof (string));
+        tv.set_model (model);
+
+        this.add (sc);
+
+        this.set_default_size (300, 200);
+
+        this.destroy += Gtk.main_quit;
+
+        // start file listing process
+        this.list_dir ();
+    }
+
+    private void list_dir () {
+        stdout.printf ("startscan\n");
+        this.dir = File.new_for_path (Environment.get_home_dir ());
+        // asynchronous call, with callback, to get dir entries
+        this.dir.enumerate_children_async (FILE_ATTRIBUTE_STANDARD_NAME, 0,
+										   Priority.DEFAULT, null, list_ready);
+    }
+
+    /* Callback for enumerate_children_async */
+    private void list_ready (GLib.Object file, AsyncResult res) {
+        try {
+            FileEnumerator e = ((File) file).enumerate_children_finish (res);
+            // asynchronous call, with callback, to get entries so far
+            e.next_files_async (10, Priority.DEFAULT, null, list_files);
+        } catch (GLib.Error err) {
+            stdout.printf ("error async_ready failed %s\n", err.message);
+        }
+    }
+
+    /* Callback for next_files_async */
+    private void list_files (GLib.Object sender, AsyncResult res) {
+        Gtk.TreeIter iter;
+        try {
+            var enumer = (FileEnumerator) sender;
+
+            // get a list of the files found so far
+            List<FileInfo> list = enumer.next_files_finish (res);
+            foreach (FileInfo info in list) {
+                this.model.append (out iter);
+                this.model.set (iter, 0, info.get_name ());
+            }
+
+            // asynchronous call, with callback, to get any more entries
+            enumer.next_files_async (10, Priority.DEFAULT, null, list_files);
+        } catch (GLib.Error err) {
+            stdout.printf ("error list_files failed %s\n", err.message);
+        }
+    }
+
+    public static int main (string[] args) {
+        Gtk.init (ref args);
+
+        var test = new ASyncGIOSample ();
+        test.show_all ();
+
+        Gtk.main ();
+        return 0;
+    }
+}

Modified: trunk/tests/examples/basic.test
==============================================================================
--- trunk/tests/examples/basic.test	(original)
+++ trunk/tests/examples/basic.test	Fri Oct 17 21:48:59 2008
@@ -1,15 +1,30 @@
 #!/bin/sh
 
-SRCDIR=../tests/examples # this is not nice
+set -e
+
+SRCDIR=../tests/examples
 
 if [ "x$VALAC" = "x" ] ; then
   VALAC=valac
   SRCDIR=.
 fi
 
-set -e
+while true ; do
+    case "$1" in
+	--interact) INTERACT=1 ; shift ;;
+	--) shift ; break ;;
+	*) break ;;
+    esac
+done
 
 TESTNAME=`basename $0 .test`
 
-$VALAC -o $TESTNAME $SRCDIR/$TESTNAME.vala
-./$TESTNAME
+$VALAC  -o $TESTNAME $SRCDIR/$TESTNAME.vala
+
+if [ "x1" = "x1" -o "x$INTERACT" = "x1" ] ; then
+  ./$TESTNAME
+else
+  echo ""
+  echo "WARNING: This vala test is interactive and will not be run (try --interactive)"
+fi
+

Added: trunk/tests/examples/bluez-dbus-sample.test
==============================================================================
--- (empty file)
+++ trunk/tests/examples/bluez-dbus-sample.test	Fri Oct 17 21:48:59 2008
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+set -e
+
+SRCDIR=../tests/examples
+
+if [ "x$VALAC" = "x" ] ; then
+  VALAC=valac
+  SRCDIR=.
+fi
+
+while true ; do
+    case "$1" in
+	--interact) INTERACT=1 ; shift ;;
+	--) shift ; break ;;
+	*) break ;;
+    esac
+done
+
+TESTNAME=`basename $0 .test`
+
+$VALAC  --pkg dbus-glib-1 -o $TESTNAME $SRCDIR/$TESTNAME.vala
+
+if [ "x1" = "x1" -o "x$INTERACT" = "x1" ] ; then
+  ./$TESTNAME
+else
+  echo ""
+  echo "WARNING: This vala test is interactive and will not be run (try --interactive)"
+fi
+

Added: trunk/tests/examples/bluez-dbus-sample.vala
==============================================================================
--- (empty file)
+++ trunk/tests/examples/bluez-dbus-sample.vala	Fri Oct 17 21:48:59 2008
@@ -0,0 +1,63 @@
+/* Bluetooth discovery using DBus in Vala sample code */
+
+using GLib;
+
+public class BlueZDiscoverySample : GLib.Object {
+
+    private DBus.Connection conn;
+    private dynamic DBus.Object bluez;
+    public MainLoop loop { set; get; }
+
+    public void run () throws DBus.Error, GLib.Error {
+        // remove the space before SYSTEM, it is just needed for this wiki
+        this.conn = DBus.Bus.get (DBus.BusType. SYSTEM);
+
+        this.bluez = conn.get_object ("org.bluez", "/org/bluez/hci0", "org.bluez.Adapter");
+
+        // dbus signals
+        this.bluez.RemoteDeviceFound += remote_device_found;
+        this.bluez.DiscoveryStarted += discovery_started;
+        this.bluez.DiscoveryCompleted += discovery_completed;
+        this.bluez.RemoteNameUpdated += remote_name_updated;
+
+        // async dbus call
+        this.bluez.DiscoverDevices ();
+    }
+
+    private void remote_device_found (dynamic DBus.Object bluez,
+                                      string address_, uint class_, int rssi_) {
+        message ("Signal: RemoteDeviceFound(%s, %u, %d)", address_, class_, rssi_);
+    }
+
+    private void discovery_started (dynamic DBus.Object bluez) {
+        message ("Signal: DiscoveryStarted()");
+    }
+
+    private void remote_name_updated (dynamic DBus.Object bluez, string address_, string name_) {
+        message ("Signal: RemoteNameUpdated(%s, %s)", address_, name_);
+    }
+
+    private void discovery_completed (dynamic DBus.Object bluez) {
+        message ("Signal: DiscoveryCompleted()");
+        this.loop.quit ();
+    }
+
+    static int main (string[] args) {
+        var loop = new MainLoop (null, false);
+
+        var test = new BlueZDiscoverySample ();
+        test.loop = loop;
+
+        try {
+            test.run ();
+        } catch (DBus.Error e) {
+            error ("Failed to initialise");
+        } catch {
+            error ("Dynamic method failure");
+        }
+
+        loop.run ();
+
+        return 0;
+    }
+}

Added: trunk/tests/examples/cairo.test
==============================================================================
--- (empty file)
+++ trunk/tests/examples/cairo.test	Fri Oct 17 21:48:59 2008
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+set -e
+
+SRCDIR=../tests/examples
+
+if [ "x$VALAC" = "x" ] ; then
+  VALAC=valac
+  SRCDIR=.
+fi
+
+while true ; do
+    case "$1" in
+	--interact) INTERACT=1 ; shift ;;
+	--) shift ; break ;;
+	*) break ;;
+    esac
+done
+
+TESTNAME=`basename $0 .test`
+
+$VALAC  --pkg gtk+-2.0 --pkg cairo -o $TESTNAME $SRCDIR/$TESTNAME.vala
+
+if [ "x0" = "x1" -o "x$INTERACT" = "x1" ] ; then
+  ./$TESTNAME
+else
+  echo ""
+  echo "WARNING: This vala test is interactive and will not be run (try --interactive)"
+fi
+

Added: trunk/tests/examples/cairo.vala
==============================================================================
--- (empty file)
+++ trunk/tests/examples/cairo.vala	Fri Oct 17 21:48:59 2008
@@ -0,0 +1,134 @@
+/* Cairo in Vala sample code */
+
+using GLib;
+using Gtk;
+using Cairo;
+
+public class CairoSample : Gtk.Window {
+
+    private const int SIZE = 30;
+
+    construct {
+        this.title = "Cairo Vala Demo";
+        this.destroy += Gtk.main_quit;
+        this.set_default_size (450, 550);
+
+        this.create_widgets ();
+    }
+
+    private void create_widgets () {
+        var drawing_area = new DrawingArea ();
+        drawing_area.expose_event += this.on_expose;
+        this.add (drawing_area);
+    }
+
+    private bool on_expose (DrawingArea da, Gdk.EventExpose event) {
+        var ctx = Gdk.cairo_create (da.window);
+
+        ctx.set_source_rgb (0, 0, 0);
+
+        ctx.set_line_width (SIZE / 4);
+        ctx.set_tolerance (0.1);
+
+        ctx.set_line_join (LineJoin.ROUND);
+        ctx.set_dash (new double[] {SIZE / 4.0, SIZE / 4.0}, 0);
+        stroke_shapes (ctx, 0, 0);
+
+        ctx.set_dash (null, 0);
+        stroke_shapes (ctx, 0, 3 * SIZE);
+
+        ctx.set_line_join (LineJoin.BEVEL);
+        stroke_shapes (ctx, 0, 6 * SIZE);
+
+        ctx.set_line_join (LineJoin.MITER);
+        stroke_shapes(ctx, 0, 9 * SIZE);
+
+        fill_shapes (ctx, 0, 12 * SIZE);
+
+        ctx.set_line_join (LineJoin.BEVEL);
+        fill_shapes (ctx, 0, 15 * SIZE);
+        ctx.set_source_rgb (1, 0, 0);
+        stroke_shapes (ctx, 0, 15 * SIZE);
+
+        return true;
+    }
+
+    private void stroke_shapes (Context ctx, int x, int y) {
+        this.draw_shapes (ctx, x, y, ctx.stroke);
+    }
+
+    private void fill_shapes (Context ctx, int x, int y) {
+        this.draw_shapes (ctx, x, y, ctx.fill);
+    }
+
+    private delegate void DrawMethod ();
+
+    private void draw_shapes (Context ctx, int x, int y, DrawMethod draw_method) {
+        ctx.save ();
+
+        ctx.new_path ();
+        ctx.translate (x + SIZE, y + SIZE);
+        bowtie (ctx);
+        draw_method ();
+
+        ctx.new_path ();
+        ctx.translate (3 * SIZE, 0);
+        square (ctx);
+        draw_method ();
+
+        ctx.new_path ();
+        ctx.translate (3 * SIZE, 0);
+        triangle (ctx);
+        draw_method ();
+
+        ctx.new_path ();
+        ctx.translate (3 * SIZE, 0);
+        inf (ctx);
+        draw_method ();
+
+        ctx.restore();
+    }
+
+    private void triangle (Context ctx) {
+        ctx.move_to (SIZE, 0);
+        ctx.rel_line_to (SIZE, 2 * SIZE);
+        ctx.rel_line_to (-2 * SIZE, 0);
+        ctx.close_path ();
+    }
+
+    private void square (Context ctx) {
+        ctx.move_to (0, 0);
+        ctx.rel_line_to (2 * SIZE, 0);
+        ctx.rel_line_to (0, 2 * SIZE);
+        ctx.rel_line_to (-2 * SIZE, 0);
+        ctx.close_path ();
+    }
+
+    private void bowtie (Context ctx) {
+        ctx.move_to (0, 0);
+        ctx.rel_line_to (2 * SIZE, 2 * SIZE);
+        ctx.rel_line_to (-2 * SIZE, 0);
+        ctx.rel_line_to (2 * SIZE, -2 * SIZE);
+        ctx.close_path ();
+    }
+
+    private void inf (Context ctx) {
+        ctx.move_to (0, SIZE);
+        ctx.rel_curve_to (0, SIZE, SIZE, SIZE, 2 * SIZE, 0);
+        ctx.rel_curve_to (SIZE, -SIZE, 2 * SIZE, -SIZE, 2 * SIZE, 0);
+        ctx.rel_curve_to (0, SIZE, -SIZE, SIZE, -2 * SIZE, 0);
+        ctx.rel_curve_to (-SIZE, -SIZE, -2 * SIZE, -SIZE, -2 * SIZE, 0);
+        ctx.close_path ();
+    }
+
+    static int main (string[] args) {
+        Gtk.init (ref args);
+
+        var cairo_sample = new CairoSample ();
+        cairo_sample.show_all ();
+
+        Gtk.main ();
+
+        return 0;
+    }
+}
\ No newline at end of file

Added: trunk/tests/examples/dbus-sample.test
==============================================================================
--- (empty file)
+++ trunk/tests/examples/dbus-sample.test	Fri Oct 17 21:48:59 2008
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+set -e
+
+SRCDIR=../tests/examples
+
+if [ "x$VALAC" = "x" ] ; then
+  VALAC=valac
+  SRCDIR=.
+fi
+
+while true ; do
+    case "$1" in
+	--interact) INTERACT=1 ; shift ;;
+	--) shift ; break ;;
+	*) break ;;
+    esac
+done
+
+TESTNAME=`basename $0 .test`
+
+$VALAC  --pkg dbus-glib-1 -o $TESTNAME $SRCDIR/$TESTNAME.vala
+
+if [ "x0" = "x1" -o "x$INTERACT" = "x1" ] ; then
+  ./$TESTNAME
+else
+  echo ""
+  echo "WARNING: This vala test is interactive and will not be run (try --interactive)"
+fi
+

Added: trunk/tests/examples/dbus-sample.vala
==============================================================================
--- (empty file)
+++ trunk/tests/examples/dbus-sample.vala	Fri Oct 17 21:48:59 2008
@@ -0,0 +1,65 @@
+/* DBus in Vala sample code */
+
+using GLib;
+using DBus;
+
+public class DBusSample : GLib.Object {
+
+    private DBus.Connection conn;
+    private dynamic DBus.Object purple;
+
+    public void run () throws DBus.Error, GLib.Error {
+        // remove the space before SESSION, it is only required for this wiki
+        conn = DBus.Bus.get (DBus.BusType. SESSION);
+
+        purple = conn.get_object ("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject", "im.pidgin.purple.PurpleInterface");
+
+        // async dbus call
+        purple.PurpleAccountsGetAllActive (getall_reply);
+
+        // dbus signals
+        purple.ReceivedImMsg += msg_received;
+    }
+
+    private void msg_received (dynamic DBus.Object purple, int dummy1, string sender, string message_, int dummy2, uint dummy3) {
+        // dbus signal handler
+
+        message ("Message received %s %s", sender, message_);
+    }
+
+    private void getall_reply (int[] ids, GLib.Error e) {
+        // callback for async dbus call
+        if (e != null) {
+            critical (e.message);
+            return;
+        }
+        try {
+            foreach (int id in ids) {
+                // sync dbus call
+                string s = purple.PurpleAccountGetUsername (id);
+                message ("Account %s", s);
+            }
+        } catch (DBus.Error dbus_error) {
+            critical (dbus_error.message);
+        }
+    }
+
+    static int main (string[] args) {
+        var loop = new MainLoop (null, false);
+
+        var test = new DBusSample ();
+        try {
+            test.run ();
+        } catch (DBus.Error e) {
+            stderr.printf ("Failed to initialise");
+            return 1;
+        } catch {
+            stderr.printf ("Dynamic method failure");
+            return 1;
+        }
+
+        loop.run ();
+
+        return 0;
+    }
+}
\ No newline at end of file

Added: trunk/tests/examples/dbus-server-sample.test
==============================================================================
--- (empty file)
+++ trunk/tests/examples/dbus-server-sample.test	Fri Oct 17 21:48:59 2008
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+set -e
+
+SRCDIR=../tests/examples
+
+if [ "x$VALAC" = "x" ] ; then
+  VALAC=valac
+  SRCDIR=.
+fi
+
+while true ; do
+    case "$1" in
+	--interact) INTERACT=1 ; shift ;;
+	--) shift ; break ;;
+	*) break ;;
+    esac
+done
+
+TESTNAME=`basename $0 .test`
+
+$VALAC  --pkg dbus-glib-1 -o $TESTNAME $SRCDIR/$TESTNAME.vala
+
+if [ "x0" = "x1" -o "x$INTERACT" = "x1" ] ; then
+  ./$TESTNAME
+else
+  echo ""
+  echo "WARNING: This vala test is interactive and will not be run (try --interactive)"
+fi
+

Added: trunk/tests/examples/dbus-server-sample.vala
==============================================================================
--- (empty file)
+++ trunk/tests/examples/dbus-server-sample.vala	Fri Oct 17 21:48:59 2008
@@ -0,0 +1,58 @@
+using GLib;
+
+[DBus (name = "org.gnome.TestServer")]
+public class TestServer : Object {
+	int64 counter;
+
+	public int64 ping (string msg) {
+		message (msg);
+		return counter++;
+	}
+}
+
+void main () {
+	MainLoop loop = new MainLoop (null, false);
+
+	try {
+		var conn = DBus.Bus.get (DBus.BusType. SESSION);
+
+		dynamic DBus.Object bus = conn.get_object (
+			"org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus");
+
+		// try to register service in session bus
+		uint request_name_result = bus.request_name ("org.gnome.TestService", (uint) 0);
+
+		if (request_name_result == DBus.RequestNameReply.PRIMARY_OWNER) {
+			// start server
+
+			var server = new TestServer ();
+			conn.register_object ("/org/gnome/test", server);
+
+			loop.run ();
+		} else {
+			// client
+			dynamic DBus.Object test_server_object = conn.get_object ("org.gnome.TestService", "/org/gnome/test", "org.gnome.TestServer");
+
+			int64 pong = test_server_object.ping ("Hello from Vala");
+			message (pong.to_string ());
+		}
+	} catch (Error foo) {
+		stderr.printf("Oops %s\n", foo.message);
+	}
+}
+
+// Since May 25 you can also do this with interfaces
+
+[DBus (name = "org.gnome.TestServer")]
+public interface TestServerAPI {
+	public abstract int64 ping (string msg);
+}
+
+public class Test : Object, TestServerAPI {
+	int64 counter;
+
+	public int64 ping (string msg) {
+		message (msg);
+		return counter++;
+	}
+}
\ No newline at end of file

Added: trunk/tests/examples/egg-clock.test
==============================================================================
--- (empty file)
+++ trunk/tests/examples/egg-clock.test	Fri Oct 17 21:48:59 2008
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+set -e
+
+SRCDIR=../tests/examples
+
+if [ "x$VALAC" = "x" ] ; then
+  VALAC=valac
+  SRCDIR=.
+fi
+
+while true ; do
+    case "$1" in
+	--interact) INTERACT=1 ; shift ;;
+	--) shift ; break ;;
+	*) break ;;
+    esac
+done
+
+TESTNAME=`basename $0 .test`
+
+$VALAC  --pkg gtk+-2.0 --pkg cairo -o $TESTNAME $SRCDIR/$TESTNAME.vala
+
+if [ "x0" = "x1" -o "x$INTERACT" = "x1" ] ; then
+  ./$TESTNAME
+else
+  echo ""
+  echo "WARNING: This vala test is interactive and will not be run (try --interactive)"
+fi
+

Added: trunk/tests/examples/egg-clock.vala
==============================================================================
--- (empty file)
+++ trunk/tests/examples/egg-clock.vala	Fri Oct 17 21:48:59 2008
@@ -0,0 +1,203 @@
+using GLib;
+using Gtk;
+using Cairo;
+
+namespace Egg {
+
+    public class ClockFace : DrawingArea {
+
+        private Time time;
+        private int minute_offset;
+        private bool dragging;
+
+        public signal void time_changed (int hour, int minute);
+
+        construct {
+            this.expose_event += on_expose;
+            this.button_press_event += on_button_press;
+            this.button_release_event += on_button_release;
+            this.motion_notify_event += on_motion_notify;
+
+            add_events (Gdk.EventMask.BUTTON_PRESS_MASK
+						| Gdk.EventMask.BUTTON_RELEASE_MASK
+						| Gdk.EventMask.POINTER_MOTION_MASK);
+            update ();
+
+            // update the clock once a second
+            Timeout.add (1000, update);
+        }
+
+        private bool on_expose (ClockFace clock, Gdk.EventExpose event) {
+            var cr = Gdk.cairo_create (this.window);
+            cr.rectangle (event.area.x, event.area.y,
+						  event.area.width, event.area.height);
+            cr.clip ();
+            draw (cr);
+            return false;
+        }
+
+        private bool on_button_press (ClockFace clock, Gdk.EventButton event) {
+            var minutes = this.time.minute + this.minute_offset;
+
+            // From
+            // http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
+            var px = event.x - this.allocation.width / 2;
+            var py = this.allocation.height / 2 - event.y;
+            var lx = Math.sin (Math.PI / 30 * minutes);
+            var ly = Math.cos (Math.PI / 30 * minutes);
+            var u = lx * px + ly * py;
+
+            // on opposite side of origin
+            if (u < 0) {
+                return false;
+            }
+
+            var d2 = Math.pow (px - u * lx, 2) + Math.pow (py - u * ly, 2);
+
+            if (d2 < 25) {      // 5 pixels away from the line
+                this.dragging = true;
+                print ("got minute hand\n");
+            }
+
+            return false;
+        }
+
+        private bool on_button_release (ClockFace clock, Gdk.EventButton event) {
+            if (this.dragging) {
+                this.dragging = false;
+                emit_time_changed_signal ((int) event.x, (int) event.y);
+            }
+            return false;
+        }
+
+        private bool on_motion_notify (ClockFace clock, Gdk.EventMotion event) {
+            if (this.dragging) {
+                emit_time_changed_signal ((int) event.x, (int) event.y);
+            }
+            return false;
+        }
+
+        private void emit_time_changed_signal (int x, int y) {
+            // decode the minute hand
+            // normalise the coordinates around the origin
+            x -= this.allocation.width / 2;
+            y -= this.allocation.height / 2;
+
+            // phi is a bearing from north clockwise, use the same geometry as
+            // we did to position the minute hand originally
+            var phi = Math.atan2 (x, -y);
+            if (phi < 0) {
+                phi += Math.PI * 2;
+            }
+
+            var hour = this.time.hour;
+            var minute = (int) (phi * 30 / Math.PI);
+        
+            // update the offset
+            this.minute_offset = minute - this.time.minute;
+            redraw_canvas ();
+
+            time_changed (hour, minute);
+        }
+
+        private bool update () {
+            // update the time
+            this.time = Time.local (time_t ());
+            redraw_canvas ();
+            return true;        // keep running this event
+        }
+
+        private void draw (Context cr) {
+            var x = this.allocation.x + this.allocation.width / 2;
+            var y = this.allocation.y + this.allocation.height / 2;
+            var radius = Math.fmin (this.allocation.width / 2,
+                                    this.allocation.height / 2) - 5;
+
+            // clock back
+            cr.arc (x, y, radius, 0, 2 * Math.PI);
+            cr.set_source_rgb (1, 1, 1);
+            cr.fill_preserve ();
+            cr.set_source_rgb (0, 0, 0);
+            cr.stroke ();
+
+            // clock ticks
+            for (int i = 0; i < 12; i++) {
+                int inset;
+
+                cr.save ();             // stack pen-size
+
+                if (i % 3 == 0) {
+                    inset = (int) (0.2 * radius);
+                } else {
+                    inset = (int) (0.1 * radius);
+                    cr.set_line_width (0.5 * cr.get_line_width ());
+                }
+
+                cr.move_to (x + (radius - inset) * Math.cos (i * Math.PI / 6),
+                            y + (radius - inset) * Math.sin (i * Math.PI / 6));
+                cr.line_to (x + radius * Math.cos (i * Math.PI / 6),
+                            y + radius * Math.sin (i * Math.PI / 6));
+                cr.stroke ();
+                cr.restore ();  // stack pen-size
+            }
+
+            // clock hands
+
+            var hours = this.time.hour;
+            var minutes = this.time.minute + this.minute_offset;
+            var seconds = this.time.second;
+                        
+            // hour hand:
+            // the hour hand is rotated 30 degrees (pi/6 r) per hour +
+            // 1/2 a degree (pi/360 r) per minute
+            cr.save ();
+            cr.set_line_width (2.5 * cr.get_line_width ());
+            cr.move_to (x, y);
+            cr.line_to (x + radius / 2 * Math.sin (Math.PI / 6 * hours
+												   + Math.PI / 360 * minutes),
+                        y + radius / 2 * -Math.cos (Math.PI / 6 * hours
+													+ Math.PI / 360 * minutes));
+            cr.stroke ();
+            cr.restore ();
+
+            // minute hand:
+            // the minute hand is rotated 6 degrees (pi/30 r) per minute
+            cr.move_to (x, y);
+            cr.line_to (x + radius * 0.75 * Math.sin (Math.PI / 30 * minutes),
+                        y + radius * 0.75 * -Math.cos (Math.PI / 30 * minutes));
+            cr.stroke ();
+                        
+            // seconds hand:
+            // operates identically to the minute hand
+            cr.save ();
+            cr.set_source_rgb (1, 0, 0); // red
+            cr.move_to (x, y);
+            cr.line_to (x + radius * 0.7 * Math.sin (Math.PI / 30 * seconds),
+                        y + radius * 0.7 * -Math.cos (Math.PI / 30 * seconds));
+            cr.stroke ();
+            cr.restore ();
+        }
+
+        private void redraw_canvas () {
+            if (null == this.window) {
+                return;
+            }
+
+            weak Gdk.Region region = this.window.get_clip_region ();
+            // redraw the cairo canvas completely by exposing it
+            this.window.invalidate_region (region, true);
+            this.window.process_updates (true);
+        }
+
+        static int main (string[] args) {
+            Gtk.init (ref args);
+            var window = new Window (WindowType.TOPLEVEL);
+            var clock = new ClockFace ();
+            window.add (clock);
+            window.destroy += Gtk.main_quit;
+            window.show_all ();
+            Gtk.main ();
+            return 0;
+        }
+    }
+}

Modified: trunk/tests/examples/list.test
==============================================================================
--- trunk/tests/examples/list.test	(original)
+++ trunk/tests/examples/list.test	Fri Oct 17 21:48:59 2008
@@ -1,15 +1,30 @@
 #!/bin/sh
 
-SRCDIR=../tests/examples # this is not nice
+set -e
+
+SRCDIR=../tests/examples
 
 if [ "x$VALAC" = "x" ] ; then
   VALAC=valac
   SRCDIR=.
 fi
 
-set -e
+while true ; do
+    case "$1" in
+	--interact) INTERACT=1 ; shift ;;
+	--) shift ; break ;;
+	*) break ;;
+    esac
+done
 
 TESTNAME=`basename $0 .test`
 
-$VALAC -o $TESTNAME $SRCDIR/$TESTNAME.vala
-./$TESTNAME
+$VALAC  -o $TESTNAME $SRCDIR/$TESTNAME.vala
+
+if [ "x1" = "x1" -o "x$INTERACT" = "x1" ] ; then
+  ./$TESTNAME
+else
+  echo ""
+  echo "WARNING: This vala test is interactive and will not be run (try --interactive)"
+fi
+

Modified: trunk/tests/examples/properties-construction.test
==============================================================================
--- trunk/tests/examples/properties-construction.test	(original)
+++ trunk/tests/examples/properties-construction.test	Fri Oct 17 21:48:59 2008
@@ -1,15 +1,30 @@
 #!/bin/sh
 
-SRCDIR=../tests/examples # this is not nice
+set -e
+
+SRCDIR=../tests/examples
 
 if [ "x$VALAC" = "x" ] ; then
   VALAC=valac
   SRCDIR=.
 fi
 
-set -e
+while true ; do
+    case "$1" in
+	--interact) INTERACT=1 ; shift ;;
+	--) shift ; break ;;
+	*) break ;;
+    esac
+done
 
 TESTNAME=`basename $0 .test`
 
-$VALAC -o $TESTNAME $SRCDIR/$TESTNAME.vala
-./$TESTNAME
+$VALAC  -o $TESTNAME $SRCDIR/$TESTNAME.vala
+
+if [ "x1" = "x1" -o "x$INTERACT" = "x1" ] ; then
+  ./$TESTNAME
+else
+  echo ""
+  echo "WARNING: This vala test is interactive and will not be run (try --interactive)"
+fi
+

Modified: trunk/tests/examples/properties.test
==============================================================================
--- trunk/tests/examples/properties.test	(original)
+++ trunk/tests/examples/properties.test	Fri Oct 17 21:48:59 2008
@@ -1,15 +1,30 @@
 #!/bin/sh
 
-SRCDIR=../tests/examples # this is not nice
+set -e
+
+SRCDIR=../tests/examples
 
 if [ "x$VALAC" = "x" ] ; then
   VALAC=valac
   SRCDIR=.
 fi
 
-set -e
+while true ; do
+    case "$1" in
+	--interact) INTERACT=1 ; shift ;;
+	--) shift ; break ;;
+	*) break ;;
+    esac
+done
 
 TESTNAME=`basename $0 .test`
 
-$VALAC -o $TESTNAME $SRCDIR/$TESTNAME.vala
-./$TESTNAME
+$VALAC  -o $TESTNAME $SRCDIR/$TESTNAME.vala
+
+if [ "x1" = "x1" -o "x$INTERACT" = "x1" ] ; then
+  ./$TESTNAME
+else
+  echo ""
+  echo "WARNING: This vala test is interactive and will not be run (try --interactive)"
+fi
+

Modified: trunk/tests/examples/string.test
==============================================================================
--- trunk/tests/examples/string.test	(original)
+++ trunk/tests/examples/string.test	Fri Oct 17 21:48:59 2008
@@ -1,15 +1,30 @@
 #!/bin/sh
 
-SRCDIR=../tests/examples # this is not nice
+set -e
+
+SRCDIR=../tests/examples
 
 if [ "x$VALAC" = "x" ] ; then
   VALAC=valac
   SRCDIR=.
 fi
 
-set -e
+while true ; do
+    case "$1" in
+	--interact) INTERACT=1 ; shift ;;
+	--) shift ; break ;;
+	*) break ;;
+    esac
+done
 
 TESTNAME=`basename $0 .test`
 
-$VALAC -o $TESTNAME $SRCDIR/$TESTNAME.vala
-./$TESTNAME
+$VALAC  -o $TESTNAME $SRCDIR/$TESTNAME.vala
+
+if [ "x1" = "x1" -o "x$INTERACT" = "x1" ] ; then
+  ./$TESTNAME
+else
+  echo ""
+  echo "WARNING: This vala test is interactive and will not be run (try --interactive)"
+fi
+

Modified: trunk/tests/examples/update.sh
==============================================================================
--- trunk/tests/examples/update.sh	(original)
+++ trunk/tests/examples/update.sh	Fri Oct 17 21:48:59 2008
@@ -1,28 +1,76 @@
 #!/bin/sh
 
+interactive="
+async-gio
+cairo
+dbus-sample
+dbus-server-sample
+egg-clock
+"
 set -e
 
+while true ; do
+    case "$1" in
+	--check) check=1 ; shift ;;
+	--interact) interact="--interact" ; shift ;;
+	--) shift ; break ;;
+	*) break ;;
+    esac
+done
+
 for file in *.vala ; do
 
+# hackish
+PACKAGES=
+grep "Gtk\." $file >/dev/null && PACKAGES="$PACKAGES --pkg gtk+-2.0"
+grep -i " gio" $file >/dev/null && PACKAGES="$PACKAGES --pkg gio-2.0"
+grep "DBus\." $file >/dev/null && PACKAGES="$PACKAGES --pkg dbus-glib-1"
+grep -i "cairo" $file >/dev/null && PACKAGES="$PACKAGES --pkg cairo"
+
 test=`basename $file .vala`
+
+RUN=1
+for nr in $interactive ; do
+  [ $nr = $test ] && RUN=0
+done
+
 cat >$test.test <<EOF
 #!/bin/sh
 
-SRCDIR=../tests/examples # this is not nice
+set -e
+
+SRCDIR=../tests/examples
 
 if [ "x\$VALAC" = "x" ] ; then
   VALAC=valac
   SRCDIR=.
 fi
 
-set -e
+while true ; do
+    case "\$1" in
+	--interact) INTERACT=1 ; shift ;;
+	--) shift ; break ;;
+	*) break ;;
+    esac
+done
 
 TESTNAME=\`basename \$0 .test\`
 
-\$VALAC -o \$TESTNAME \$SRCDIR/\$TESTNAME.vala
-./\$TESTNAME
+\$VALAC $PACKAGES -o \$TESTNAME \$SRCDIR/\$TESTNAME.vala
+
+if [ "x$RUN" = "x1" -o "x\$INTERACT" = "x1" ] ; then
+  ./\$TESTNAME
+else
+  echo ""
+  echo "WARNING: This vala test is interactive and will not be run (try --interactive)"
+fi
+
 EOF
 
 chmod +x $test.test
 
+[ "$check" = "1" ] && echo "Running $test..." && ./$test.test $interact
+
+rm -f $test
+
 done
\ No newline at end of file



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]