github.com/adwpc/xmobile@v0.0.0-20231212131043-3f9720cf0e99/bind/java/ClassesTest.java (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package go;
     6  
     7  import android.test.InstrumentationTestCase;
     8  import android.test.MoreAsserts;
     9  
    10  import java.io.InputStream;
    11  import java.io.IOException;
    12  import java.util.Arrays;
    13  import java.util.Random;
    14  
    15  import javapkg.Javapkg;
    16  import javapkg.I;
    17  import javapkg.GoObject;
    18  import javapkg.GoRunnable;
    19  import javapkg.GoSubset;
    20  import javapkg.GoInputStream;
    21  import javapkg.GoArrayList;
    22  
    23  public class ClassesTest extends InstrumentationTestCase {
    24  	public void testConst() {
    25  		assertEquals("const Float", Float.MIN_VALUE, Javapkg.floatMin());
    26  		assertEquals("const String", java.util.jar.JarFile.MANIFEST_NAME, Javapkg.manifestName());
    27  		assertEquals("const Int", 7, Integer.SIZE, Javapkg.integerBytes());
    28  	}
    29  
    30  	public void testFunction() {
    31  		Javapkg.systemCurrentTimeMillis();
    32  	}
    33  
    34  	public void testMethod() {
    35  		try {
    36  			assertEquals("Integer.decode", 0xff, Javapkg.integerDecode("0xff"));
    37  		} catch (Exception e) {
    38  			throw new RuntimeException(e);
    39  		}
    40  		Exception exc = null;
    41  		try {
    42  			Javapkg.integerDecode("obviously wrong");
    43  		} catch (Exception e) {
    44  			exc = e;
    45  		}
    46  		assertNotNull("IntegerDecode Exception", exc);
    47  	}
    48  
    49  	public void testOverloadedMethod() {
    50  		try {
    51  			assertEquals("Integer.parseInt", 0xc4, Javapkg.integerParseInt("c4", 16));
    52  		} catch (Exception e) {
    53  			throw new RuntimeException(e);
    54  		}
    55  		Exception exc = null;
    56  		try {
    57  			Javapkg.integerParseInt("wrong", 16);
    58  		} catch (Exception e) {
    59  			exc = e;
    60  		}
    61  		assertNotNull("integerParseInt Exception", exc);
    62  		assertEquals("Integer.valueOf", 42, Javapkg.integerValueOf(42));
    63  	}
    64  
    65  	public void testException() {
    66  		Exception exc = null;
    67  		try {
    68  			Javapkg.provokeRuntimeException();
    69  		} catch (Exception e) {
    70  			exc = e;
    71  		}
    72  		assertNotNull("RuntimeException", exc);
    73  	}
    74  
    75  	public void testField() {
    76  		GoRunnable r = new GoRunnable();
    77  		String f = r.getField();
    78  	}
    79  
    80  	public void testGoObject() {
    81  		Runnable r = new GoRunnable();
    82  		r.run();
    83  		assertEquals("GoRunnable.toString", r.toString(), Javapkg.ToStringPrefix);
    84  		Runnable r2 = ((GoRunnable)r).getThis();
    85  		assertTrue("GoObject.this", r == r2);
    86  		Object o = new GoObject();
    87  		assertEquals("GoObject hashCode", 42, o.hashCode());
    88  		Object o2 = Javapkg.constructGoObject();
    89  		assertEquals("GoObject hashCode", 42, o2.hashCode());
    90  		assertTrue("GoObject.toString", o.toString().startsWith(Javapkg.ToStringPrefix));
    91  		Javapkg.runRunnable(r);
    92  		final boolean[] ran = new boolean[1];
    93  		Runnable r3 = new Runnable(){
    94  			@Override public void run() {
    95  				ran[0] = true;
    96  			}
    97  		};
    98  		Javapkg.runRunnable(r3);
    99  		assertTrue("RunRunnable", ran[0]);
   100  		assertTrue("RunnableRoundtrip Java", r3 == Javapkg.runnableRoundtrip(r3));
   101  		assertTrue("RunnableRoundtrip Go", r == Javapkg.runnableRoundtrip(r));
   102  		Runnable r5 = Javapkg.constructGoRunnable();
   103  		r5.run();
   104  	}
   105  
   106  	public void testTypedException() {
   107  		InputStream is = new GoInputStream();
   108  		Exception exc = null;
   109  		try {
   110  			is.read();
   111  		} catch (IOException e) {
   112  			exc = e;
   113  		}
   114  		assertNotNull("IOException", exc);
   115  		assertEquals("IOException message", Javapkg.IOExceptionMessage, exc.getMessage());
   116  	}
   117  
   118  	public void testInnerClass() {
   119  		Character.Subset s = new Character.Subset(""){};
   120  		Character.Subset s2 = new GoSubset("");
   121  		Javapkg.callSubset(s);
   122  		Javapkg.callSubset(s2);
   123  	}
   124  
   125  	public void testNew() {
   126  		Object o = Javapkg.newJavaObject();
   127  		assertTrue("new Object()", o != null);
   128  		Integer i = Javapkg.newJavaInteger();
   129  		assertEquals("new Integer(42)", 42, i.intValue());
   130  	}
   131  
   132  	private static class InterfaceRunnable implements I, Runnable {
   133  		@Override public void run() {
   134  		}
   135  	}
   136  
   137  	public void testCast() {
   138  		Runnable r1 = new GoRunnable();
   139  		Runnable r1c = Javapkg.castRunnable((Object)r1);
   140  		assertTrue("Casting Go object", r1c != null);
   141  		Runnable r2 = new Runnable() {
   142  			@Override public void run() {
   143  			}
   144  		};
   145  		Runnable r2c = Javapkg.castRunnable((Object)r2);
   146  		assertTrue("Casting Java object", r2c != null);
   147  		Runnable r3c = Javapkg.castInterface(new InterfaceRunnable());
   148  		assertTrue("Casting Go interface implementation", r3c != null);
   149  		Runnable r4c = Javapkg.castRunnable(new Object());
   150  		assertTrue("Invalid cast", r4c == null);
   151  	}
   152  
   153  	public void testUnwrap() {
   154  		GoArrayList l = new GoArrayList();
   155  		Javapkg.unwrapGoArrayList(l);
   156  	}
   157  }