2
0
mirror of synced 2025-02-24 15:28:28 +00:00
mobile/bind/objc/seq_test.go
Elias Naur 976d0710d0 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>
2016-03-07 16:10:51 +00:00

126 lines
3.7 KiB
Go

// Copyright 2015 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.
package objc
import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// Use the Xcode XCTestCase framework to run the SeqTest.m tests and the SeqBench.m benchmarks.
//
// SeqTest.m runs in the xcodetest project as normal unit test (logic test in Xcode lingo).
// Unit tests execute faster but cannot run on a real device. That is why SeqBench.m runs as
// a UI unit test through the xcodebench project.
//
// Both xcodetest and xcodebench were constructed in Xcode 7 by:
//
// - Creating a new project through Xcode. Choose to include either unit tests or UI tests as
// needed.
// - Add SeqTest.m or SeqBench.m to the right unit test target.
// - Xcode schemes are per-user by default. The shared scheme is created by selecting
// Project => Schemes => Manage Schemes from the Xcode menu and selecting "Shared".
// - Remove files not needed for xcodebuild (determined empirically). In particular, the empty
// tests Xcode creates can be removed and the unused user scheme.
var destination = flag.String("device", "platform=iOS Simulator,name=iPhone 6s Plus", "Specify the -destination flag to xcodebuild")
// TestObjcSeqTest runs ObjC test SeqTest.m.
// This requires the xcode command lines tools.
func TestObjcSeqTest(t *testing.T) {
runTest(t, "golang.org/x/mobile/bind/testpkg", "xcodetest", "SeqTest.m", false)
}
// TestObjcSeqBench runs ObjC test SeqBench.m.
// This requires the xcode command lines tools.
func TestObjcSeqBench(t *testing.T) {
runTest(t, "golang.org/x/mobile/bind/benchmark", "xcodebench", "SeqBench.m", true)
}
func runTest(t *testing.T, pkgName, project, testfile string, dumpOutput bool) {
if _, err := run("which xcodebuild"); err != nil {
t.Skip("command xcodebuild not found, skipping")
}
if _, err := run("which gomobile"); err != nil {
_, err := run("go install golang.org/x/mobile/cmd/gomobile")
if err != nil {
t.Skip("gomobile not available, skipping")
}
}
// TODO(hyangah): gomobile init if necessary.
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("failed pwd: %v", err)
}
tmpdir, err := ioutil.TempDir("", "bind-objc-seq-test-")
if err != nil {
t.Fatalf("failed to prepare temp dir: %v", err)
}
defer os.RemoveAll(tmpdir)
t.Logf("tmpdir = %s", tmpdir)
if buf, err := exec.Command("cp", "-a", project, tmpdir).CombinedOutput(); err != nil {
t.Logf("%s", buf)
t.Fatalf("failed to copy %s to tmp dir: %v", project, err)
}
if err := cp(filepath.Join(tmpdir, testfile), testfile); err != nil {
t.Fatalf("failed to copy %s: %v", testfile, err)
}
if err := os.Chdir(filepath.Join(tmpdir, project)); err != nil {
t.Fatalf("failed chdir: %v", err)
}
defer os.Chdir(cwd)
buf, err := run("gomobile bind -target=ios " + pkgName)
if err != nil {
t.Logf("%s", buf)
t.Fatalf("failed to run gomobile bind: %v", err)
}
cmd := exec.Command("xcodebuild", "test", "-scheme", project, "-destination", *destination)
buf, err = cmd.CombinedOutput()
if err != nil {
t.Logf("%s", buf)
t.Errorf("failed to run xcodebuild: %v", err)
}
if dumpOutput {
t.Logf("%s", buf)
}
}
func run(cmd string) ([]byte, error) {
c := strings.Split(cmd, " ")
return exec.Command(c[0], c[1:]...).CombinedOutput()
}
func cp(dst, src string) error {
r, err := os.Open(src)
if err != nil {
return fmt.Errorf("failed to read source: %v", err)
}
defer r.Close()
w, err := os.Create(dst)
if err != nil {
return fmt.Errorf("failed to open destination: %v", err)
}
_, err = io.Copy(w, r)
cerr := w.Close()
if err != nil {
return err
}
return cerr
}