mobile/bind: merge iOS and Android Go test packages

Currently there is a Go test package for each platform, iOS and
Android. This CL merges them into a single, shared  package. Apart
from the reduced code duplication, the merger stops the tests
diverging further. Most importantly, one shared package clarifies
that the intent of gobind is that the same Go package can be
reused across platforms.

This CL only merges the obvious test duplicates. The rest have been
copied from the ObjC package into the Android test under different
names.

While we're here, demote the long string test to the basictypes
bind test; the test never had a runtime part.

Change-Id: I7838b16999968fae7b012016a5b5f6bb80f94023
Reviewed-on: https://go-review.googlesource.com/20300
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
This commit is contained in:
Elias Naur
2016-03-07 16:10:51 +00:00
parent 5604bcf91f
commit 976d0710d0
10 changed files with 172 additions and 275 deletions
+1 -1
View File
@@ -19,7 +19,7 @@ import (
// This requires the gradle command in PATH and
// the Android SDK whose path is available through ANDROID_HOME environment variable.
func TestJavaSeqTest(t *testing.T) {
runTest(t, "golang.org/x/mobile/bind/java/testpkg", "SeqTest")
runTest(t, "golang.org/x/mobile/bind/testpkg", "SeqTest")
}
// TestJavaSeqBench runs java test SeqBench.java, with the same
-1
View File
@@ -1 +0,0 @@
Hello, Assets.
-285
View File
@@ -1,285 +0,0 @@
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin linux
// Package testpkg contains bound functions for testing the cgo-JNI interface.
// This is used in tests of golang.org/x/mobile/bind/java.
package testpkg
//go:generate gobind -lang=go -outdir=go_testpkg .
//go:generate gobind -lang=java -outdir=. .
import (
"errors"
"fmt"
"io/ioutil"
"log"
"math"
"runtime"
"time"
"golang.org/x/mobile/asset"
)
const (
AString = "a string"
AnInt = 7
ABool = true
AFloat = 0.12345
ALongString = "LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString"
MinInt32 int32 = math.MinInt32
MaxInt32 int32 = math.MaxInt32
MinInt64 = math.MinInt64
MaxInt64 = math.MaxInt64
SmallestNonzeroFloat64 = math.SmallestNonzeroFloat64
MaxFloat64 = math.MaxFloat64
SmallestNonzeroFloat32 float32 = math.SmallestNonzeroFloat64
MaxFloat32 float32 = math.MaxFloat32
Log2E = math.Log2E
)
var (
StringVar = "a string var"
IntVar = 77
StructVar = &S{name: "a struct var"}
InterfaceVar I
)
type I interface {
F()
E() error
V() int
VE() (int, error)
I() I
S() *S
StoString(seq *S) string
String() string
}
func CallF(i I) {
i.F()
}
func CallE(i I) error {
return i.E()
}
func CallV(i I) int {
return i.V()
}
func CallVE(i I) (int, error) {
return i.VE()
}
func CallI(i I) I {
return i
}
func CallS(i I) *S {
return &S{}
}
var keep []I
func Keep(i I) {
keep = append(keep, i)
}
var numSCollected int
type S struct {
// *S already has a finalizer, so we need another object
// to count successful collections.
innerObj *int
name string
}
func (s *S) F() {
fmt.Printf("called F on *S{%s}\n", s.name)
}
func (s *S) String() string {
return s.name
}
func finalizeInner(a *int) {
numSCollected++
}
var seq = 0
func New() *S {
s := &S{innerObj: new(int), name: fmt.Sprintf("new%d", seq)}
runtime.SetFinalizer(s.innerObj, finalizeInner)
return s
}
func GC() {
runtime.GC()
time.Sleep(10 * time.Millisecond)
runtime.GC()
}
func Add(x, y int) int {
return x + y
}
func NumSCollected() int {
return numSCollected
}
func StrDup(s string) string {
return s
}
func Negate(x bool) bool {
return !x
}
func Err(s string) error {
if s != "" {
return errors.New(s)
}
return nil
}
func BytesAppend(a []byte, b []byte) []byte {
return append(a, b...)
}
func AppendToString(str string, someBytes []byte) []byte {
a := []byte(str)
fmt.Printf("str=%q (len=%d), someBytes=%v (len=%d)\n", str, len(str), someBytes, len(someBytes))
return append(a, someBytes...)
}
func UnnamedParams(_, _ int, p0 string) int {
return len(p0)
}
type Node struct {
V string
Next *Node
Err error
}
func NewNode(name string) *Node {
return &Node{V: name}
}
func (a *Node) String() string {
if a == nil {
return "<end>"
}
return a.V + ":" + a.Next.String()
}
type Receiver interface {
Hello(message string)
}
func Hello(r Receiver, name string) {
r.Hello(fmt.Sprintf("Hello, %s!\n", name))
}
func GarbageCollect() {
runtime.GC()
}
type (
Concrete struct{}
Interface interface {
F()
}
)
func (_ *Concrete) F() {
}
func NewConcrete() *Concrete {
return new(Concrete)
}
func ReadAsset() string {
rc, err := asset.Open("hello.txt")
if err != nil {
log.Fatal(err)
}
defer rc.Close()
b, err := ioutil.ReadAll(rc)
if err != nil {
log.Fatal(err)
}
return string(b)
}
type GoCallback interface {
VarUpdate()
}
func CallWithCallback(gcb GoCallback) {
for i := 0; i < 1000; i++ {
gcb.VarUpdate()
}
}
type NullTest interface {
Null() NullTest
}
func NewNullInterface() I {
return nil
}
func NewNullStruct() *S {
return nil
}
func CallWithNull(_null NullTest, nuller NullTest) bool {
return _null == nil && nuller.Null() == nil
}
type Issue14168 interface {
F(seq int32)
}
func ReadIntoByteArray(s []byte) (int, error) {
if len(s) != cap(s) {
return 0, fmt.Errorf("cap %d != len %d", cap(s), len(s))
}
for i := 0; i < len(s); i++ {
s[i] = byte(i)
}
return len(s), nil
}
type B interface {
B(b []byte)
}
func PassByteArray(b B) {
b.B([]byte{1, 2, 3, 4})
}
func GoroutineCallback(r Receiver) {
done := make(chan struct{})
go func() {
// Run it multiple times to increase the chance that the goroutine
// will use different threads for the call. Use a long argument string to
// make sure the JNI calls take more time.
for i := 0; i < 100000; i++ {
r.Hello("HelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHello")
}
close(done)
}()
<-done
}