summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--mojo/android/javatests/src/org/chromium/mojo/bindings/CallbacksTest.java59
-rw-r--r--mojo/bindings/java/src/org/chromium/mojo/bindings/Callbacks.java121
-rw-r--r--mojo/tools/generate_java_callback_interfaces.py60
3 files changed, 240 insertions, 0 deletions
diff --git a/mojo/android/javatests/src/org/chromium/mojo/bindings/CallbacksTest.java b/mojo/android/javatests/src/org/chromium/mojo/bindings/CallbacksTest.java
new file mode 100644
index 0000000..dfb8e21
--- /dev/null
+++ b/mojo/android/javatests/src/org/chromium/mojo/bindings/CallbacksTest.java
@@ -0,0 +1,59 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.mojo.bindings;
+
+import android.test.suitebuilder.annotation.SmallTest;
+
+import junit.framework.TestCase;
+
+import org.chromium.mojo.bindings.Callbacks.Callback1;
+import org.chromium.mojo.bindings.Callbacks.Callback7;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Testing generated callbacks
+ */
+public class CallbacksTest extends TestCase {
+
+ /**
+ * Testing {@link Callback1}.
+ */
+ @SmallTest
+ public void testCallback1() {
+ final List<Integer> parameters = new ArrayList<Integer>();
+ new Callback1<Integer>() {
+ @Override
+ public void call(Integer i1) {
+ parameters.add(i1);
+ }
+ }.call(1);
+ assertEquals(Arrays.asList(1), parameters);
+ }
+
+ /**
+ * Testing {@link Callback7}.
+ */
+ @SmallTest
+ public void testCallback7() {
+ final List<Integer> parameters = new ArrayList<Integer>();
+ new Callback7<Integer, Integer, Integer, Integer, Integer, Integer, Integer>() {
+ @Override
+ public void call(Integer i1, Integer i2, Integer i3, Integer i4, Integer i5, Integer i6,
+ Integer i7) {
+ parameters.add(i1);
+ parameters.add(i2);
+ parameters.add(i3);
+ parameters.add(i4);
+ parameters.add(i5);
+ parameters.add(i6);
+ parameters.add(i7);
+ }
+ }.call(1, 2, 3, 4, 5, 6, 7);
+ assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6, 7), parameters);
+ }
+}
diff --git a/mojo/bindings/java/src/org/chromium/mojo/bindings/Callbacks.java b/mojo/bindings/java/src/org/chromium/mojo/bindings/Callbacks.java
new file mode 100644
index 0000000..7b35cc0
--- /dev/null
+++ b/mojo/bindings/java/src/org/chromium/mojo/bindings/Callbacks.java
@@ -0,0 +1,121 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// This file was generated using
+// mojo/tools/generate_java_callback_interfaces.py
+
+package org.chromium.mojo.bindings;
+
+/**
+ * Contains a generic interface for callbacks.
+ */
+public interface Callbacks {
+
+ /**
+ * A generic 1-argument callback.
+ *
+ * @param <T1> the type of argument 1.
+ */
+ interface Callback1<T1> {
+ /**
+ * Call the callback.
+ */
+ public void call(T1 arg1);
+ }
+
+ /**
+ * A generic 2-argument callback.
+ *
+ * @param <T1> the type of argument 1.
+ * @param <T2> the type of argument 2.
+ */
+ interface Callback2<T1, T2> {
+ /**
+ * Call the callback.
+ */
+ public void call(T1 arg1, T2 arg2);
+ }
+
+ /**
+ * A generic 3-argument callback.
+ *
+ * @param <T1> the type of argument 1.
+ * @param <T2> the type of argument 2.
+ * @param <T3> the type of argument 3.
+ */
+ interface Callback3<T1, T2, T3> {
+ /**
+ * Call the callback.
+ */
+ public void call(T1 arg1, T2 arg2, T3 arg3);
+ }
+
+ /**
+ * A generic 4-argument callback.
+ *
+ * @param <T1> the type of argument 1.
+ * @param <T2> the type of argument 2.
+ * @param <T3> the type of argument 3.
+ * @param <T4> the type of argument 4.
+ */
+ interface Callback4<T1, T2, T3, T4> {
+ /**
+ * Call the callback.
+ */
+ public void call(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
+ }
+
+ /**
+ * A generic 5-argument callback.
+ *
+ * @param <T1> the type of argument 1.
+ * @param <T2> the type of argument 2.
+ * @param <T3> the type of argument 3.
+ * @param <T4> the type of argument 4.
+ * @param <T5> the type of argument 5.
+ */
+ interface Callback5<T1, T2, T3, T4, T5> {
+ /**
+ * Call the callback.
+ */
+ public void call(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5);
+ }
+
+ /**
+ * A generic 6-argument callback.
+ *
+ * @param <T1> the type of argument 1.
+ * @param <T2> the type of argument 2.
+ * @param <T3> the type of argument 3.
+ * @param <T4> the type of argument 4.
+ * @param <T5> the type of argument 5.
+ * @param <T6> the type of argument 6.
+ */
+ interface Callback6<T1, T2, T3, T4, T5, T6> {
+ /**
+ * Call the callback.
+ */
+ public void call(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6);
+ }
+
+ /**
+ * A generic 7-argument callback.
+ *
+ * @param <T1> the type of argument 1.
+ * @param <T2> the type of argument 2.
+ * @param <T3> the type of argument 3.
+ * @param <T4> the type of argument 4.
+ * @param <T5> the type of argument 5.
+ * @param <T6> the type of argument 6.
+ * @param <T7> the type of argument 7.
+ */
+ interface Callback7<T1, T2, T3, T4, T5, T6, T7> {
+ /**
+ * Call the callback.
+ */
+ public void call(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7);
+ }
+
+}
+
diff --git a/mojo/tools/generate_java_callback_interfaces.py b/mojo/tools/generate_java_callback_interfaces.py
new file mode 100644
index 0000000..f19ef6d
--- /dev/null
+++ b/mojo/tools/generate_java_callback_interfaces.py
@@ -0,0 +1,60 @@
+"""Generate the org.chromium.mojo.bindings.Callbacks interface"""
+
+import argparse
+import sys
+
+CALLBACK_TEMPLATE = ("""
+ /**
+ * A generic %d-argument callback.
+ *
+ * %s
+ */
+ interface Callback%d<%s> {
+ /**
+ * Call the callback.
+ */
+ public void call(%s);
+ }
+""")
+
+INTERFACE_TEMPLATE = (
+"""// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// This file was generated using
+// mojo/tools/generate_java_callback_interfaces.py
+
+package org.chromium.mojo.bindings;
+
+/**
+ * Contains a generic interface for callbacks.
+ */
+public interface Callbacks {
+%s
+}
+""")
+
+def GenerateCallback(nb_args):
+ params = '\n * '.join(
+ ['@param <T%d> the type of argument %d.' % (i+1, i+1)
+ for i in xrange(nb_args)])
+ template_parameters = ', '.join(['T%d' % (i+1) for i in xrange(nb_args)])
+ callback_parameters = ', '.join(['T%d arg%d' % ((i+1), (i+1))
+ for i in xrange(nb_args)])
+ return CALLBACK_TEMPLATE % (nb_args, params, nb_args, template_parameters,
+ callback_parameters)
+
+def main():
+ parser = argparse.ArgumentParser(
+ description="Generate org.chromium.mojo.bindings.Callbacks")
+ parser.add_argument("max_args", nargs=1, type=int,
+ help="maximal number of arguments to generate callbacks for")
+ args = parser.parse_args()
+ max_args = args.max_args[0]
+ print INTERFACE_TEMPLATE % ''.join([GenerateCallback(i+1)
+ for i in xrange(max_args)])
+ return 0
+
+if __name__ == "__main__":
+ sys.exit(main())