[gjs/299-can-not-access-gobject-properties-of-classes-without-gi-information] Add test for no introspection data.



commit 59c15f75f13469d2ccd843c8267160ea30397168
Author: Juan Pablo Ugarte <ugarte endlessm com>
Date:   Mon Feb 17 10:11:21 2020 -0300

    Add test for no introspection data.
    
    Added g_object_new() wrapper in private library to be able to get an
    object instance with no GI data for testing.

 libgjs-private/gjs-util.c                 |  12 ++++
 libgjs-private/gjs-util.h                 |   3 +
 test/gjs-test-no-introspection-object.cpp | 100 ++++++++++++++++++++++++++++++
 test/gjs-test-no-introspection-object.h   |  38 ++++++++++++
 test/gjs-tests.cpp                        |  29 +++++++++
 test/meson.build                          |   1 +
 6 files changed, 183 insertions(+)
---
diff --git a/libgjs-private/gjs-util.c b/libgjs-private/gjs-util.c
index 20a732d5..f0e8efa6 100644
--- a/libgjs-private/gjs-util.c
+++ b/libgjs-private/gjs-util.c
@@ -310,3 +310,15 @@ out:
     g_clear_pointer(&base_info, g_base_info_unref);
     g_clear_pointer(&child_set_property_fun, g_base_info_unref);
 }
+
+/**
+ * gjs_object_new:
+ * @name: GType name
+ *
+ * Creates an new object
+ *
+ * Returns: (transfer full)
+ */
+GObject *gjs_object_new(const gchar *name) {
+    return g_object_new(g_type_from_name(name), NULL);
+}
diff --git a/libgjs-private/gjs-util.h b/libgjs-private/gjs-util.h
index 945dc8ad..5d545bfc 100644
--- a/libgjs-private/gjs-util.h
+++ b/libgjs-private/gjs-util.h
@@ -77,6 +77,9 @@ void gjs_gtk_container_child_set_property(GObject* container, GObject* child,
 GJS_EXPORT
 int gjs_open_bytes(GBytes* bytes, GError** error);
 
+GJS_EXPORT
+GObject *gjs_object_new(const gchar *name);
+
 G_END_DECLS
 
 #endif /* LIBGJS_PRIVATE_GJS_UTIL_H_ */
diff --git a/test/gjs-test-no-introspection-object.cpp b/test/gjs-test-no-introspection-object.cpp
new file mode 100644
index 00000000..9420ac51
--- /dev/null
+++ b/test/gjs-test-no-introspection-object.cpp
@@ -0,0 +1,100 @@
+/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright © 2020 Endless Mobile Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include "test/gjs-test-no-introspection-object.h"
+
+struct _GjsTestNoIntrospectionObject
+{
+    GObject parent_instance;
+
+    gint a_int;
+};
+
+G_DEFINE_TYPE(GjsTestNoIntrospectionObject, gjstest_no_introspection_object, G_TYPE_OBJECT)
+
+static GjsTestNoIntrospectionObject *last_object = NULL;
+
+static void
+gjstest_no_introspection_object_init(GjsTestNoIntrospectionObject *self)
+{
+    self->a_int = 0;
+    last_object = self;
+}
+
+static void
+gjstest_no_introspection_object_set_property(GObject      *object,
+                                             guint         prop_id,
+                                             const GValue *value,
+                                             GParamSpec   *pspec)
+{
+    GjsTestNoIntrospectionObject *self = reinterpret_cast<GjsTestNoIntrospectionObject *>object;
+
+    switch (prop_id) {
+        case 1:
+            self->a_int = g_value_get_int(value);
+        break;
+        default:
+            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+        break;
+    }
+}
+
+static void
+gjstest_no_introspection_object_get_property(GObject    *object,
+                                             guint       prop_id,
+                                             GValue     *value,
+                                             GParamSpec *pspec)
+{
+    GjsTestNoIntrospectionObject *self = reinterpret_cast<GjsTestNoIntrospectionObject *>object;
+
+    switch (prop_id) {
+        case 1:
+            g_value_set_int(value, self->a_int);
+        break;
+        default:
+            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+        break;
+    }
+}
+
+static void
+gjstest_no_introspection_object_class_init(GjsTestNoIntrospectionObjectClass *klass)
+{
+    GObjectClass *object_class = G_OBJECT_CLASS(klass);
+
+    object_class->set_property = gjstest_no_introspection_object_set_property;
+    object_class->get_property = gjstest_no_introspection_object_get_property;
+
+    g_object_class_install_property(object_class, 1,
+                                    g_param_spec_int("a-int",
+                                                     "An integer",
+                                                     "An integer property",
+                                                     0, 100000000, 0,
+                                                     G_PARAM_READWRITE));
+}
+
+GjsTestNoIntrospectionObject *
+gjstest_no_introspection_object_peek()
+{
+    return last_object;
+}
diff --git a/test/gjs-test-no-introspection-object.h b/test/gjs-test-no-introspection-object.h
new file mode 100644
index 00000000..9b9c2095
--- /dev/null
+++ b/test/gjs-test-no-introspection-object.h
@@ -0,0 +1,38 @@
+/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright © 2020 Endless Mobile Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#ifndef TEST_GJS_TEST_NO_INTROSPECTION_OBJECT_H_
+#define TEST_GJS_TEST_NO_INTROSPECTION_OBJECT_H_
+
+#include <glib-object.h>
+
+#define GJSTEST_TYPE_NO_INTROSPECTION_OBJECT gjstest_no_introspection_object_get_type()
+G_DECLARE_FINAL_TYPE(GjsTestNoIntrospectionObject,
+                     gjstest_no_introspection_object,
+                     GJSTEST,
+                     NO_INTROSPECTION_OBJECT,
+                     GObject)
+
+GjsTestNoIntrospectionObject *gjstest_no_introspection_object_peek();
+
+#endif
diff --git a/test/gjs-tests.cpp b/test/gjs-tests.cpp
index 45ebe243..d292ffbd 100644
--- a/test/gjs-tests.cpp
+++ b/test/gjs-tests.cpp
@@ -35,6 +35,7 @@
 #include "gjs/jsapi-util.h"
 #include "gjs/profiler.h"
 #include "test/gjs-test-utils.h"
+#include "test/gjs-test-no-introspection-object.h"
 #include "util/misc.h"
 
 #define VALID_UTF8_STRING "\303\211\303\226 foobar \343\203\237"
@@ -130,6 +131,33 @@ gjstest_test_func_gjs_gobject_js_defined_type(void)
     g_object_unref(context);
 }
 
+static void
+gjstest_test_func_gjs_gobject_without_introspection(void)
+{
+    GjsAutoUnref<GjsContext> context = gjs_context_new();
+    GError *error = nullptr;
+    int status;
+
+    /* Ensure class */
+    g_type_class_ref(GJSTEST_TYPE_NO_INTROSPECTION_OBJECT);
+
+#define TESTJS "\
+var obj = imports.gi.GjsPrivate.object_new('GjsTestNoIntrospectionObject');\
+obj.a_int = 1234;\
+"
+    if (!gjs_context_eval(context, TESTJS, -1, "<input>", &status, &error))
+        g_printerr("ERROR: %s", error->message);
+
+    GjsTestNoIntrospectionObject *obj = gjstest_no_introspection_object_peek();
+    g_assert(obj != NULL);
+
+    gint val = 0;
+    g_object_get(obj, "a-int", &val, NULL);
+    g_assert(val == 1234);
+
+#undef TESTJS
+}
+
 static void gjstest_test_func_gjs_jsapi_util_string_js_string_utf8(
     GjsUnitTestFixture* fx, const void*) {
     JS::RootedValue js_string(fx->cx);
@@ -403,6 +431,7 @@ main(int    argc,
                     gjstest_test_func_gjs_context_eval_non_zero_terminated);
     g_test_add_func("/gjs/context/exit", gjstest_test_func_gjs_context_exit);
     g_test_add_func("/gjs/gobject/js_defined_type", gjstest_test_func_gjs_gobject_js_defined_type);
+    g_test_add_func("/gjs/gobject/without_introspection", 
gjstest_test_func_gjs_gobject_without_introspection);
     g_test_add_func("/gjs/jsutil/strip_shebang/no_shebang", 
gjstest_test_strip_shebang_no_advance_for_no_shebang);
     g_test_add_func("/gjs/jsutil/strip_shebang/short_string",
                     gjstest_test_strip_shebang_no_advance_for_too_short_string);
diff --git a/test/meson.build b/test/meson.build
index de2d7c0e..8cea4989 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -11,6 +11,7 @@ gjs_tests_sources = [
     'gjs-test-call-args.cpp',
     'gjs-test-coverage.cpp',
     'gjs-test-rooting.cpp',
+    'gjs-test-no-introspection-object.cpp', 'gjs-test-no-introspection-object.h',
 ]
 
 gjs_tests = executable('gjs-tests', gjs_tests_sources, mock_js_resources_files,


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