From cdf2e1a2a0799ec85651cfae447f88b6244538de Mon Sep 17 00:00:00 2001 From: Laurent Sansonetti Date: Thu, 12 Jun 2014 20:05:11 +0200 Subject: [PATCH] add specs for basic java types argument/return values calling pure-java / overridden methods --- test/test-android/Rakefile | 17 +++++ .../app/rubymotion/java_methods_spec.rb | 64 +++++++++++++++++++ .../com/rubymotion/test/TestJavaMethods.java | 38 +++++++++++ 3 files changed, 119 insertions(+) create mode 100644 test/test-android/app/rubymotion/java_methods_spec.rb create mode 100644 test/test-android/vendor/java/com/rubymotion/test/TestJavaMethods.java diff --git a/test/test-android/Rakefile b/test/test-android/Rakefile index f5ab7c1e..e927049a 100644 --- a/test/test-android/Rakefile +++ b/test/test-android/Rakefile @@ -9,6 +9,23 @@ Motion::Project::App.setup do |app| app.sdk_path = File.expand_path('~/src/android-sdk-macosx') app.ndk_path = File.expand_path('~/src/android-ndk-r9d') + # Build the vendor jar if needed. + testjava_jar = 'vendor/testjava.jar' + if !File.exist?(testjava_jar) \ + or Dir.glob('vendor/java/**/*.java').any? { |x| File.mtime(x) > File.mtime(testjava_jar) } + FileUtils.rm_rf('vendor/classes') + FileUtils.mkdir('vendor/classes') + Dir.chdir('vendor/java') do + Dir.glob('**/*.java').each do |java| + sh "/usr/bin/javac -d ../classes \"#{java}\"" + end + end + Dir.chdir('vendor/classes') do + sh "/usr/bin/jar cvf ../testjava.jar ." + end + end + app.vendor_project :jar => testjava_jar + # Make sure the main file is compiled and executed first. ary = app.files ary.delete('./app/main.rb') diff --git a/test/test-android/app/rubymotion/java_methods_spec.rb b/test/test-android/app/rubymotion/java_methods_spec.rb new file mode 100644 index 00000000..53307990 --- /dev/null +++ b/test/test-android/app/rubymotion/java_methods_spec.rb @@ -0,0 +1,64 @@ +class TestJavaMethodsOverride < Com::RubyMotion::Test::TestJavaMethods + def booleanMethod(arg); arg; end + def byteMethod(arg); arg; end + def shortMethod(arg); arg; end + def intMethod(arg); arg; end + def longMethod(arg); arg; end + def floatMethod(arg); arg; end + def doubleMethod(arg); arg; end + def objectMethod(arg); arg; end +end + +2.times do |spec_n| + describe "Java methods" + (spec_n == 0 ? "" : " overriden in Ruby") + " can be called when accepting/returning" do + before :each do + @obj = spec_n == 0 ? Com::RubyMotion::Test::TestJavaMethods.new : TestJavaMethodsOverride.new + end + + it "'boolean'" do + @obj.booleanMethod(true).should == true + @obj.booleanMethod(42).should == true + @obj.booleanMethod(false).should == false + @obj.booleanMethod(nil).should == false + end + + it "'byte'" do + ret = @obj.byteMethod(42) + ret.should be_an_instance_of(Java::Lang::Byte) + ret.intValue.should == 42 + end + + it "'short'" do + ret = @obj.shortMethod(42) + ret.should be_an_instance_of(Java::Lang::Short) + ret.intValue.should == 42 + end + + it "'int'" do + @obj.intMethod(42).should == 42 + end + + it "'long'" do + ret = @obj.longMethod(42) + ret.should be_an_instance_of(Java::Lang::Long) + ret.intValue.should == 42 + end + + it "'float'" do + @obj.floatMethod(3.14).should == 3.14 + end + + it "'double'" do + ret = @obj.doubleMethod(3.14) + ret.should be_an_instance_of(Java::Lang::Double) + ret.floatValue.should == 3.14 + end + + it "'java.lang.Object'" do + obj = Java::Lang::Object.new + ret = @obj.objectMethod(obj) + obj.should == ret + obj.should equal(ret) + end + end +end diff --git a/test/test-android/vendor/java/com/rubymotion/test/TestJavaMethods.java b/test/test-android/vendor/java/com/rubymotion/test/TestJavaMethods.java new file mode 100644 index 00000000..690ce0b0 --- /dev/null +++ b/test/test-android/vendor/java/com/rubymotion/test/TestJavaMethods.java @@ -0,0 +1,38 @@ +package com.rubymotion.test; + +public class TestJavaMethods extends java.lang.Object { + public TestJavaMethods() { + } + + public boolean booleanMethod(boolean arg) { + return arg; + } + + public byte byteMethod(byte arg) { + return arg; + } + + public short shortMethod(short arg) { + return arg; + } + + public long longMethod(long arg) { + return arg; + } + + public int intMethod(int arg) { + return arg; + } + + public float floatMethod(float arg) { + return arg; + } + + public double doubleMethod(double arg) { + return arg; + } + + public java.lang.Object objectMethod(java.lang.Object arg) { + return arg; + } +}