github.com/SkycoinProject/gomobile@v0.0.0-20190312151609-d3739f865fa6/bind/java/seq_test.go (about)

     1  // Copyright 2015 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 java
     6  
     7  import (
     8  	"fmt"
     9  	"io"
    10  	"io/ioutil"
    11  	"log"
    12  	"os"
    13  	"os/exec"
    14  	"path/filepath"
    15  	"runtime"
    16  	"strings"
    17  	"testing"
    18  
    19  	"golang.org/x/mobile/internal/importers/java"
    20  )
    21  
    22  var gomobileBin string
    23  
    24  func TestMain(m *testing.M) {
    25  	os.Exit(testMain(m))
    26  }
    27  
    28  func testMain(m *testing.M) int {
    29  	// Build gomobile and gobind and put them into PATH.
    30  	binDir, err := ioutil.TempDir("", "bind-java-test-")
    31  	if err != nil {
    32  		log.Fatal(err)
    33  	}
    34  	defer os.RemoveAll(binDir)
    35  	exe := ""
    36  	if runtime.GOOS == "windows" {
    37  		exe = ".exe"
    38  	}
    39  	if runtime.GOOS != "android" {
    40  		gomobileBin = filepath.Join(binDir, "gomobile"+exe)
    41  		gobindBin := filepath.Join(binDir, "gobind"+exe)
    42  		if out, err := exec.Command("go", "build", "-o", gomobileBin, "golang.org/x/mobile/cmd/gomobile").CombinedOutput(); err != nil {
    43  			log.Fatalf("gomobile build failed: %v: %s", err, out)
    44  		}
    45  		if out, err := exec.Command("go", "build", "-o", gobindBin, "golang.org/x/mobile/cmd/gobind").CombinedOutput(); err != nil {
    46  			log.Fatalf("gobind build failed: %v: %s", err, out)
    47  		}
    48  		PATH := os.Getenv("PATH")
    49  		if PATH != "" {
    50  			PATH += string(filepath.ListSeparator)
    51  		}
    52  		PATH += binDir
    53  		os.Setenv("PATH", PATH)
    54  	}
    55  	return m.Run()
    56  }
    57  
    58  func TestClasses(t *testing.T) {
    59  	if !java.IsAvailable() {
    60  		t.Skipf("java importer is not available")
    61  	}
    62  	runTest(t, []string{
    63  		"golang.org/x/mobile/bind/testdata/testpkg/javapkg",
    64  	}, "", "ClassesTest")
    65  }
    66  
    67  func TestCustomPkg(t *testing.T) {
    68  	runTest(t, []string{
    69  		"golang.org/x/mobile/bind/testdata/testpkg",
    70  	}, "org.golang.custompkg", "CustomPkgTest")
    71  }
    72  
    73  func TestJavaSeqTest(t *testing.T) {
    74  	runTest(t, []string{
    75  		"golang.org/x/mobile/bind/testdata/testpkg",
    76  		"golang.org/x/mobile/bind/testdata/testpkg/secondpkg",
    77  		"golang.org/x/mobile/bind/testdata/testpkg/simplepkg",
    78  	}, "", "SeqTest")
    79  }
    80  
    81  // TestJavaSeqBench runs java test SeqBench.java, with the same
    82  // environment requirements as TestJavaSeqTest.
    83  //
    84  // The benchmarks runs on the phone, so the benchmarkpkg implements
    85  // rudimentary timing logic and outputs benchcmp compatible runtimes
    86  // to logcat. Use
    87  //
    88  // adb logcat -v raw GoLog:* *:S
    89  //
    90  // while running the benchmark to see the results.
    91  func TestJavaSeqBench(t *testing.T) {
    92  	if testing.Short() {
    93  		t.Skip("skipping benchmark in short mode.")
    94  	}
    95  	runTest(t, []string{"golang.org/x/mobile/bind/testdata/benchmark"}, "", "SeqBench")
    96  }
    97  
    98  // runTest runs the Android java test class specified with javaCls. If javaPkg is
    99  // set, it is passed with the -javapkg flag to gomobile. The pkgNames lists the Go
   100  // packages to bind for the test.
   101  // This requires the gradle command in PATH and
   102  // the Android SDK whose path is available through ANDROID_HOME environment variable.
   103  func runTest(t *testing.T, pkgNames []string, javaPkg, javaCls string) {
   104  	if gomobileBin == "" {
   105  		t.Skipf("no gomobile on %s", runtime.GOOS)
   106  	}
   107  	gradle, err := exec.LookPath("gradle")
   108  	if err != nil {
   109  		t.Skip("command gradle not found, skipping")
   110  	}
   111  	if sdk := os.Getenv("ANDROID_HOME"); sdk == "" {
   112  		t.Skip("ANDROID_HOME environment var not set, skipping")
   113  	}
   114  
   115  	cwd, err := os.Getwd()
   116  	if err != nil {
   117  		t.Fatalf("failed pwd: %v", err)
   118  	}
   119  	tmpdir, err := ioutil.TempDir("", "bind-java-seq-test-")
   120  	if err != nil {
   121  		t.Fatalf("failed to prepare temp dir: %v", err)
   122  	}
   123  	defer os.RemoveAll(tmpdir)
   124  	t.Logf("tmpdir = %s", tmpdir)
   125  
   126  	if err := os.Chdir(tmpdir); err != nil {
   127  		t.Fatalf("failed chdir: %v", err)
   128  	}
   129  	defer os.Chdir(cwd)
   130  
   131  	for _, d := range []string{"src/main", "src/androidTest/java/go", "libs", "src/main/res/values"} {
   132  		err = os.MkdirAll(filepath.Join(tmpdir, d), 0700)
   133  		if err != nil {
   134  			t.Fatal(err)
   135  		}
   136  	}
   137  
   138  	args := []string{"bind", "-tags", "aaa bbb", "-o", "pkg.aar"}
   139  	if javaPkg != "" {
   140  		args = append(args, "-javapkg", javaPkg)
   141  	}
   142  	args = append(args, pkgNames...)
   143  	buf, err := exec.Command(gomobileBin, args...).CombinedOutput()
   144  	if err != nil {
   145  		t.Logf("%s", buf)
   146  		t.Fatalf("failed to run gomobile bind: %v", err)
   147  	}
   148  
   149  	fname := filepath.Join(tmpdir, "libs", "pkg.aar")
   150  	err = cp(fname, filepath.Join(tmpdir, "pkg.aar"))
   151  	if err != nil {
   152  		t.Fatalf("failed to copy pkg.aar: %v", err)
   153  	}
   154  
   155  	fname = filepath.Join(tmpdir, "src/androidTest/java/go/"+javaCls+".java")
   156  	err = cp(fname, filepath.Join(cwd, javaCls+".java"))
   157  	if err != nil {
   158  		t.Fatalf("failed to copy SeqTest.java: %v", err)
   159  	}
   160  
   161  	fname = filepath.Join(tmpdir, "src/main/AndroidManifest.xml")
   162  	err = ioutil.WriteFile(fname, []byte(androidmanifest), 0700)
   163  	if err != nil {
   164  		t.Fatalf("failed to write android manifest file: %v", err)
   165  	}
   166  
   167  	// Add a dummy string resource to avoid errors from the Android build system.
   168  	fname = filepath.Join(tmpdir, "src/main/res/values/strings.xml")
   169  	err = ioutil.WriteFile(fname, []byte(stringsxml), 0700)
   170  	if err != nil {
   171  		t.Fatalf("failed to write strings.xml file: %v", err)
   172  	}
   173  
   174  	fname = filepath.Join(tmpdir, "build.gradle")
   175  	err = ioutil.WriteFile(fname, []byte(buildgradle), 0700)
   176  	if err != nil {
   177  		t.Fatalf("failed to write build.gradle file: %v", err)
   178  	}
   179  
   180  	if buf, err := run(gradle + " connectedAndroidTest"); err != nil {
   181  		t.Logf("%s", buf)
   182  		t.Errorf("failed to run gradle test: %v", err)
   183  	}
   184  }
   185  
   186  func run(cmd string) ([]byte, error) {
   187  	c := strings.Split(cmd, " ")
   188  	return exec.Command(c[0], c[1:]...).CombinedOutput()
   189  }
   190  
   191  func cp(dst, src string) error {
   192  	r, err := os.Open(src)
   193  	if err != nil {
   194  		return fmt.Errorf("failed to read source: %v", err)
   195  	}
   196  	defer r.Close()
   197  	w, err := os.Create(dst)
   198  	if err != nil {
   199  		return fmt.Errorf("failed to open destination: %v", err)
   200  	}
   201  	_, err = io.Copy(w, r)
   202  	cerr := w.Close()
   203  	if err != nil {
   204  		return err
   205  	}
   206  	return cerr
   207  }
   208  
   209  const androidmanifest = `<?xml version="1.0" encoding="utf-8"?>
   210  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
   211            package="com.example.BindTest"
   212  	  android:versionCode="1"
   213  	  android:versionName="1.0">
   214  </manifest>`
   215  
   216  const buildgradle = `buildscript {
   217      repositories {
   218          google()
   219          jcenter()
   220      }
   221      dependencies {
   222          classpath 'com.android.tools.build:gradle:3.1.0'
   223      }
   224  }
   225  
   226  allprojects {
   227      repositories {
   228  		google()
   229  		jcenter()
   230  	}
   231  }
   232  
   233  apply plugin: 'com.android.library'
   234  
   235  android {
   236      compileSdkVersion 'android-19'
   237      defaultConfig { minSdkVersion 16 }
   238  }
   239  
   240  repositories {
   241      flatDir { dirs 'libs' }
   242  }
   243  
   244  dependencies {
   245      implementation(name: "pkg", ext: "aar")
   246  }
   247  `
   248  
   249  const stringsxml = `<?xml version="1.0" encoding="utf-8"?>
   250  <resources>
   251  	<string name="dummy">dummy</string>
   252  </resources>`