seq_test.go 6.7 KB

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