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