summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorqsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-05-15 18:54:04 +0000
committerqsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-05-15 18:54:04 +0000
commit230db69d88522c52638fe436b35f1314b0b83fe4 (patch)
tree94925144f1f975cf3bcd5cd381d292590c6d6d18
parent8bb5a3434d475e98a1bab6d6183096360d3eb67c (diff)
downloadchromium_src-230db69d88522c52638fe436b35f1314b0b83fe4.zip
chromium_src-230db69d88522c52638fe436b35f1314b0b83fe4.tar.gz
chromium_src-230db69d88522c52638fe436b35f1314b0b83fe4.tar.bz2
Helper classes for java bindings.
Review URL: https://codereview.chromium.org/288133005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@270757 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsHelperTest.java55
-rw-r--r--mojo/bindings/java/src/org/chromium/mojo/bindings/BindingsHelper.java64
-rw-r--r--mojo/mojo.gyp3
-rw-r--r--mojo/mojo_public.gypi11
4 files changed, 133 insertions, 0 deletions
diff --git a/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsHelperTest.java b/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsHelperTest.java
new file mode 100644
index 0000000..a46a157
--- /dev/null
+++ b/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsHelperTest.java
@@ -0,0 +1,55 @@
+// 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 java.nio.charset.Charset;
+
+/**
+ * Testing {@link BindingsHelper}.
+ */
+public class BindingsHelperTest extends TestCase {
+
+ /**
+ * Testing {@link BindingsHelper#utf8StringSizeInBytes(String)}.
+ */
+ @SmallTest
+ public void testUTF8StringLength() {
+ String[] stringsToTest = {
+ "",
+ "a",
+ "hello world",
+ "éléphant",
+ "𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕",
+ "你午饭想吃什么",
+ "你午饭想吃什么\0éléphant",
+ };
+ for (String s : stringsToTest) {
+ assertEquals(s.getBytes(Charset.forName("utf8")).length,
+ BindingsHelper.utf8StringSizeInBytes(s));
+ }
+ assertEquals(1, BindingsHelper.utf8StringSizeInBytes("\0"));
+ String s = new StringBuilder().appendCodePoint(0x0).appendCodePoint(0x80).
+ appendCodePoint(0x800).appendCodePoint(0x10000).toString();
+ assertEquals(10, BindingsHelper.utf8StringSizeInBytes(s));
+ assertEquals(10, s.getBytes(Charset.forName("utf8")).length);
+ }
+
+ /**
+ * Testing {@link BindingsHelper#align(int)}.
+ */
+ @SmallTest
+ public void testAlign() {
+ for (int i = 0; i < 3 * BindingsHelper.ALIGNMENT; ++i) {
+ int j = BindingsHelper.align(i);
+ assertTrue(j >= i);
+ assertTrue(j % BindingsHelper.ALIGNMENT == 0);
+ assertTrue(j - i < BindingsHelper.ALIGNMENT);
+ }
+ }
+}
diff --git a/mojo/bindings/java/src/org/chromium/mojo/bindings/BindingsHelper.java b/mojo/bindings/java/src/org/chromium/mojo/bindings/BindingsHelper.java
new file mode 100644
index 0000000..f2aa046
--- /dev/null
+++ b/mojo/bindings/java/src/org/chromium/mojo/bindings/BindingsHelper.java
@@ -0,0 +1,64 @@
+// 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;
+
+/**
+ * Helper functions.
+ */
+public class BindingsHelper {
+ /**
+ * Alignment in byte for mojo serialization.
+ */
+ public static final int ALIGNMENT = 8;
+
+ /**
+ * Align |size| on {@link BindingsHelper#ALIGNMENT}.
+ */
+ public static int align(int size) {
+ return (size + ALIGNMENT - 1) & ~(ALIGNMENT - 1);
+ }
+
+ /**
+ * Compute the size in bytes of the given string encoded as utf8.
+ */
+ public static int utf8StringSizeInBytes(String s) {
+ int res = 0;
+ for (int i = 0; i < s.length(); ++i) {
+ char c = s.charAt(i);
+ int codepoint = c;
+ if (isSurrogate(c)) {
+ i++;
+ char c2 = s.charAt(i);
+ codepoint = Character.toCodePoint(c, c2);
+ }
+ res += 1;
+ if (codepoint > 0x7f) {
+ res += 1;
+ if (codepoint > 0x7ff) {
+ res += 1;
+ if (codepoint > 0xffff) {
+ res += 1;
+ if (codepoint > 0x1fffff) {
+ res += 1;
+ if (codepoint > 0x3ffffff) {
+ res += 1;
+ }
+ }
+ }
+ }
+ }
+ }
+ return res;
+ }
+
+ /**
+ * Determines if the given {@code char} value is a Unicode <i>surrogate code unit</i>. See
+ * {@link Character#isSurrogate}. Extracting here because the method only exists at API level
+ * 19.
+ */
+ private static boolean isSurrogate(char c) {
+ return c >= Character.MIN_SURROGATE && c < (Character.MAX_SURROGATE + 1);
+ }
+}
diff --git a/mojo/mojo.gyp b/mojo/mojo.gyp
index f86252f..9d0b02c 100644
--- a/mojo/mojo.gyp
+++ b/mojo/mojo.gyp
@@ -69,6 +69,7 @@
}],
['OS == "android"', {
'dependencies': [
+ 'mojo_bindings_java',
'mojo_public_java',
'mojo_system_java',
'libmojo_system_java',
@@ -737,6 +738,8 @@
'target_name': 'mojo_test_apk',
'type': 'none',
'dependencies': [
+ 'mojo_bindings_java',
+ 'mojo_public_test_interfaces',
'mojo_system_java',
'../base/base.gyp:base_java_test_support',
],
diff --git a/mojo/mojo_public.gypi b/mojo/mojo_public.gypi
index 65102eb..9dac104 100644
--- a/mojo/mojo_public.gypi
+++ b/mojo/mojo_public.gypi
@@ -387,6 +387,17 @@
},
'includes': [ '../build/java.gypi' ],
},
+ {
+ 'target_name': 'mojo_bindings_java',
+ 'type': 'none',
+ 'variables': {
+ 'java_in_dir': 'bindings/java',
+ },
+ 'dependencies': [
+ 'mojo_public_java',
+ ],
+ 'includes': [ '../build/java.gypi' ],
+ },
],
}],
],