Permalink
Browse files

object-store: Add more tests

The filter test does not pass yet.
  • Loading branch information...
1 parent adcfc7e commit d42a44938699ee30a998fc42bc149aebf69389db Rob Staudinger committed Nov 5, 2010
View
@@ -1,7 +1,18 @@
-PROGRAM = object-store-test
+PROGRAMS = \
+ object-store-example \
+ object-store-test \
+ $(NULL)
-SOURCES = \
+example_SOURCES = \
+ foo-object-store.c \
+ foo-object-store.h \
+ foo-test-object.c \
+ foo-test-object.h \
+ object-store-example.c \
+ $(NULL)
+
+test_SOURCES = \
foo-object-store.c \
foo-object-store.h \
foo-test-object.c \
@@ -11,12 +22,15 @@ SOURCES = \
PKGFLAGS = `pkg-config --cflags --libs clutter-1.0 mx-1.0`
-all: $(PROGRAM)
+all: $(PROGRAMS)
clean:
- rm -f $(PROGRAM)
+ rm -f $(PROGRAMS)
+
+object-store-example: $(example_SOURCES)
+ $(CC) $(CPPLAGS) $(CFLAGS) $(PKGFLAGS) -o $@ $^
-$(PROGRAM): $(SOURCES)
+object-store-test: $(test_SOURCES)
$(CC) $(CPPLAGS) $(CFLAGS) $(PKGFLAGS) -o $@ $^
.PHONY: clean
@@ -27,11 +27,13 @@ G_DEFINE_TYPE (FooTestObject, foo_test_object, G_TYPE_OBJECT)
enum
{
PROP_0,
+ PROP_NUMBER,
PROP_TEXT
};
typedef struct
{
+ int number;
char *text;
} FooTestObjectPrivate;
@@ -43,6 +45,11 @@ _get_property (GObject *object,
{
switch (property_id)
{
+ case PROP_NUMBER:
+ g_value_set_int (value,
+ foo_test_object_get_number (
+ FOO_TEST_OBJECT (object)));
+ break;
case PROP_TEXT:
g_value_set_string (value,
foo_test_object_get_text (
@@ -61,6 +68,10 @@ _set_property (GObject *object,
{
switch (property_id)
{
+ case PROP_NUMBER:
+ foo_test_object_set_number (FOO_TEST_OBJECT (object),
+ g_value_get_int (value));
+ break;
case PROP_TEXT:
foo_test_object_set_text (FOO_TEST_OBJECT (object),
g_value_get_string (value));
@@ -88,6 +99,12 @@ foo_test_object_class_init (FooTestObjectClass *klass)
object_class->finalize = _finalize;
g_object_class_install_property (object_class,
+ PROP_NUMBER,
+ g_param_spec_int ("number", "", "",
+ G_MININT32, G_MAXINT32, 0,
+ G_PARAM_READWRITE));
+
+ g_object_class_install_property (object_class,
PROP_TEXT,
g_param_spec_string ("text", "", "",
NULL,
@@ -105,6 +122,32 @@ foo_test_object_new (void)
return g_object_new (FOO_TYPE_TEST_OBJECT, NULL);
}
+int
+foo_test_object_get_number (FooTestObject *self)
+{
+ FooTestObjectPrivate *priv = GET_PRIVATE (self);
+
+ g_return_val_if_fail (FOO_IS_TEST_OBJECT (self), 0);
+
+ return priv->number;
+}
+
+void
+foo_test_object_set_number (FooTestObject *self,
+ int number)
+{
+ FooTestObjectPrivate *priv = GET_PRIVATE (self);
+
+ g_return_if_fail (FOO_IS_TEST_OBJECT (self));
+
+ if (number != priv->number)
+ {
+ priv->number = number;
+
+ g_object_notify (G_OBJECT (self), "number");
+ }
+}
+
char const *
foo_test_object_get_text (FooTestObject *self)
{
@@ -57,6 +57,13 @@ foo_test_object_get_type (void);
FooTestObject *
foo_test_object_new (void);
+int
+foo_test_object_get_number (FooTestObject *self);
+
+void
+foo_test_object_set_number (FooTestObject *self,
+ int number);
+
char const *
foo_test_object_get_text (FooTestObject *self);
@@ -0,0 +1,230 @@
+/*
+ * Copyright (c) 2010, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope 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 along with
+ * this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Author: Rob Staudinger <robsta@linux.intel.com>
+ */
+
+#include <stdbool.h>
+#include <stdlib.h>
+#include <mx/mx.h>
+#include "foo-object-store.h"
+#include "foo-test-object.h"
+
+typedef struct
+{
+ ClutterActor *view;
+ MxEntry *entry;
+ ClutterModel *store;
+} ObjectStoreTest;
+
+/*
+ * Look up item by index.
+ * Returned object needs to be g_object_unref'd.
+ * May return NULL.
+ */
+FooTestObject *
+store_get_object (ClutterModel *store,
+ unsigned index)
+{
+ ClutterModelIter *iter;
+ FooTestObject *object = NULL;
+
+ iter = clutter_model_get_iter_at_row (store, index);
+ if (iter &&
+ !clutter_model_iter_is_last (iter))
+ {
+ /* Column #0 of the model holds the actual object. */
+ clutter_model_iter_get (iter,
+ 0, &object,
+ -1);
+ }
+
+ return object;
+}
+
+/*
+ * Add object to the store.
+ */
+static void
+store_add_object (ClutterModel *store,
+ char const *text)
+{
+ FooTestObject *object;
+
+ object = foo_test_object_new ();
+ foo_test_object_set_text (object, text);
+
+ /* Column #0 holds the actual object, the other cols are mapped to
+ * its properties. */
+ clutter_model_append (store, 0, object, -1);
+ g_object_unref (object);
+}
+
+static void
+_update_clicked (MxButton *button,
+ ObjectStoreTest *app)
+{
+ char const *input;
+
+ input = mx_entry_get_text (app->entry);
+ if (input == NULL ||
+ input[0] == '\0')
+ {
+ g_warning ("Please enter text");
+ return;
+
+ } else if (input[0] == '-') {
+
+ /* Remove item */
+ int index = g_ascii_isdigit (input[1]) ?
+ atoi (&input[1]) :
+ -1;
+ if (index < 0)
+ {
+ g_warning ("Invalid number, can not remove");
+ return;
+ }
+
+ clutter_model_remove (app->store, index);
+
+ } else if (g_ascii_isdigit (input[0])) {
+
+ /* Update item */
+ unsigned index = atoi (input);
+ char **tokens = g_strsplit (input, ":", 2);
+ char const *text = tokens[1];
+ FooTestObject *object = store_get_object (app->store, index);
+
+ if (object == NULL)
+ {
+ g_warning ("Failed to find object");
+ return;
+ }
+
+ foo_test_object_set_text (FOO_TEST_OBJECT (object), text);
+ g_object_unref (object);
+
+ } else {
+
+ /* Add item */
+ store_add_object (app->store, input);
+ }
+
+ mx_entry_set_text (app->entry, "");
+}
+
+static void
+_dump_clicked (MxButton *button,
+ ObjectStoreTest *app)
+{
+ ClutterModelIter *iter;
+
+ for (iter = clutter_model_get_first_iter (app->store);
+ !clutter_model_iter_is_last (iter);
+ iter = clutter_model_iter_next (iter))
+ {
+ FooTestObject *object = NULL;
+ char *text = NULL;
+
+ clutter_model_iter_get (iter,
+ 0, &object,
+ 1, &text,
+ -1);
+ g_debug ("%p %s\n", object, text);
+ g_object_unref (object);
+ g_free (text);
+ }
+}
+
+int
+main (int argc,
+ char **argv)
+{
+ ClutterActor *stage;
+ MxBoxLayout *vbox;
+ MxBoxLayout *hbox;
+ ClutterActor *button;
+ ClutterActor *label;
+ ObjectStoreTest app = { 0, };
+
+ clutter_init (&argc, &argv);
+
+ stage = clutter_stage_get_default ();
+ clutter_actor_set_size (stage, 320.0, 240.0);
+
+ vbox = (MxBoxLayout *) mx_box_layout_new ();
+ clutter_actor_set_size (CLUTTER_ACTOR (vbox), 320.0, 240.0);
+ mx_box_layout_set_orientation (vbox, MX_ORIENTATION_VERTICAL);
+ clutter_container_add_actor (CLUTTER_CONTAINER (stage), CLUTTER_ACTOR (vbox));
+
+ /* Create model */
+ app.store = foo_object_store_new (2,
+ FOO_TYPE_TEST_OBJECT, "foo", /* column #0 */
+ G_TYPE_STRING, "text"); /* column #1 */
+
+ /*
+ * Create view
+ */
+ app.view = mx_list_view_new ();
+
+ /* Use MxButton to render the model's items */
+ mx_list_view_set_item_type (MX_LIST_VIEW (app.view), MX_TYPE_BUTTON);
+
+ /* Map column #1 to attribute "label" of view's GtkButton */
+ mx_list_view_add_attribute (MX_LIST_VIEW (app.view), "label", 1);
+
+ /* Connect to model */
+ mx_list_view_set_model (MX_LIST_VIEW (app.view), app.store);
+
+ mx_box_layout_add_actor_with_properties (vbox, app.view, -1,
+ "expand", true,
+ "x-fill", true,
+ "y-fill", true,
+ NULL);
+
+ hbox = (MxBoxLayout *) mx_box_layout_new ();
+ mx_box_layout_set_orientation (hbox, MX_ORIENTATION_HORIZONTAL);
+ mx_box_layout_add_actor_with_properties (vbox, CLUTTER_ACTOR (hbox), -1,
+ "expand", false,
+ "x-fill", true,
+ NULL);
+
+ app.entry = (MxEntry *) mx_entry_new ();
+ mx_box_layout_add_actor_with_properties (hbox, CLUTTER_ACTOR (app.entry), -1,
+ "expand", true,
+ "x-fill", true,
+ NULL);
+
+ button = mx_button_new_with_label ("Update");
+ g_signal_connect (button, "clicked",
+ G_CALLBACK (_update_clicked), &app);
+ clutter_container_add_actor (CLUTTER_CONTAINER (hbox), button);
+
+ button = mx_button_new_with_label ("Dump");
+ g_signal_connect (button, "clicked",
+ G_CALLBACK (_dump_clicked), &app);
+ clutter_container_add_actor (CLUTTER_CONTAINER (hbox), button);
+
+ label = mx_label_new_with_text ("Enter text and update to add item\n"
+ "Enter <number>:<text> to change item <number>\n"
+ "Enter -<number> to delete item <number>");
+ clutter_container_add_actor (CLUTTER_CONTAINER (vbox), label);
+
+ clutter_actor_show_all (stage);
+ clutter_main ();
+
+ return EXIT_SUCCESS;
+}
Binary file not shown.
Oops, something went wrong.

0 comments on commit d42a449

Please sign in to comment.