Migrate java/androidmk.go to new system #1

This change migrates some of AndroidMk()s in java/androidmk.go to
AndroidMkEntries(), mainly focusing on deduping test-related helper
funcs.

Test: Soong tests
Test: Built a system image
Test: Manual inspection of diffs
Change-Id: I7810085521600d9ea2df078837688ade41c04825
diff --git a/java/androidmk_test.go b/java/androidmk_test.go
index fbf2baa..438b66a 100644
--- a/java/androidmk_test.go
+++ b/java/androidmk_test.go
@@ -15,107 +15,12 @@
 package java
 
 import (
-	"bytes"
-	"io"
-	"io/ioutil"
-	"strings"
+	"reflect"
 	"testing"
 
 	"android/soong/android"
 )
 
-type testAndroidMk struct {
-	*testing.T
-	body []byte
-}
-
-type testAndroidMkModule struct {
-	*testing.T
-	props map[string]string
-}
-
-func newTestAndroidMk(t *testing.T, r io.Reader) *testAndroidMk {
-	t.Helper()
-	buf, err := ioutil.ReadAll(r)
-	if err != nil {
-		t.Fatal("failed to open read Android.mk.", err)
-	}
-	return &testAndroidMk{
-		T:    t,
-		body: buf,
-	}
-}
-
-func parseAndroidMkProps(lines []string) map[string]string {
-	props := make(map[string]string)
-	for _, line := range lines {
-		line = strings.TrimLeft(line, " ")
-		if line == "" || strings.HasPrefix(line, "#") {
-			continue
-		}
-		tokens := strings.Split(line, " ")
-		if tokens[1] == "+=" {
-			props[tokens[0]] += " " + strings.Join(tokens[2:], " ")
-		} else {
-			props[tokens[0]] = strings.Join(tokens[2:], " ")
-		}
-	}
-	return props
-}
-
-func (t *testAndroidMk) moduleFor(moduleName string) *testAndroidMkModule {
-	t.Helper()
-	lines := strings.Split(string(t.body), "\n")
-	index := android.IndexList("LOCAL_MODULE := "+moduleName, lines)
-	if index == -1 {
-		t.Fatalf("%q is not found.", moduleName)
-	}
-	lines = lines[index:]
-	includeIndex := android.IndexListPred(func(line string) bool {
-		return strings.HasPrefix(line, "include")
-	}, lines)
-	if includeIndex == -1 {
-		t.Fatalf("%q is not properly defined. (\"include\" not found).", moduleName)
-	}
-	props := parseAndroidMkProps(lines[:includeIndex])
-	return &testAndroidMkModule{
-		T:     t.T,
-		props: props,
-	}
-}
-
-func (t *testAndroidMkModule) hasRequired(dep string) {
-	t.Helper()
-	required, ok := t.props["LOCAL_REQUIRED_MODULES"]
-	if !ok {
-		t.Error("LOCAL_REQUIRED_MODULES is not found.")
-		return
-	}
-	if !android.InList(dep, strings.Split(required, " ")) {
-		t.Errorf("%q is expected in LOCAL_REQUIRED_MODULES, but not found in %q.", dep, required)
-	}
-}
-
-func (t *testAndroidMkModule) hasNoRequired(dep string) {
-	t.Helper()
-	required, ok := t.props["LOCAL_REQUIRED_MODULES"]
-	if !ok {
-		return
-	}
-	if android.InList(dep, strings.Split(required, " ")) {
-		t.Errorf("%q is not expected in LOCAL_REQUIRED_MODULES, but found.", dep)
-	}
-}
-
-func getAndroidMk(t *testing.T, ctx *android.TestContext, config android.Config, name string) *testAndroidMk {
-	t.Helper()
-	lib, _ := ctx.ModuleForTests(name, "android_common").Module().(*Library)
-	data := android.AndroidMkDataForTest(t, config, "", lib)
-	w := &bytes.Buffer{}
-	data.Custom(w, name, "", "", data)
-	return newTestAndroidMk(t, w)
-}
-
 func TestRequired(t *testing.T) {
 	ctx, config := testJava(t, `
 		java_library {
@@ -125,8 +30,14 @@
 		}
 	`)
 
-	mk := getAndroidMk(t, ctx, config, "foo")
-	mk.moduleFor("foo").hasRequired("libfoo")
+	mod := ctx.ModuleForTests("foo", "android_common").Module()
+	entries := android.AndroidMkEntriesForTest(t, config, "", mod)
+
+	expected := []string{"libfoo"}
+	actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
+	if !reflect.DeepEqual(expected, actual) {
+		t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
+	}
 }
 
 func TestHostdex(t *testing.T) {
@@ -138,9 +49,19 @@
 		}
 	`)
 
-	mk := getAndroidMk(t, ctx, config, "foo")
-	mk.moduleFor("foo")
-	mk.moduleFor("foo-hostdex")
+	mod := ctx.ModuleForTests("foo", "android_common").Module()
+	entries := android.AndroidMkEntriesForTest(t, config, "", mod)
+
+	expected := []string{"foo"}
+	actual := entries.EntryMap["LOCAL_MODULE"]
+	if !reflect.DeepEqual(expected, actual) {
+		t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual)
+	}
+
+	footerLines := entries.FooterLinesForTests()
+	if !android.InList("LOCAL_MODULE := foo-hostdex", footerLines) {
+		t.Errorf("foo-hostdex is not found in the footers: %q", footerLines)
+	}
 }
 
 func TestHostdexRequired(t *testing.T) {
@@ -153,9 +74,19 @@
 		}
 	`)
 
-	mk := getAndroidMk(t, ctx, config, "foo")
-	mk.moduleFor("foo").hasRequired("libfoo")
-	mk.moduleFor("foo-hostdex").hasRequired("libfoo")
+	mod := ctx.ModuleForTests("foo", "android_common").Module()
+	entries := android.AndroidMkEntriesForTest(t, config, "", mod)
+
+	expected := []string{"libfoo"}
+	actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
+	if !reflect.DeepEqual(expected, actual) {
+		t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
+	}
+
+	footerLines := entries.FooterLinesForTests()
+	if !android.InList("LOCAL_REQUIRED_MODULES := libfoo", footerLines) {
+		t.Errorf("Wrong or missing required line for foo-hostdex in the footers: %q", footerLines)
+	}
 }
 
 func TestHostdexSpecificRequired(t *testing.T) {
@@ -172,7 +103,15 @@
 		}
 	`)
 
-	mk := getAndroidMk(t, ctx, config, "foo")
-	mk.moduleFor("foo").hasNoRequired("libfoo")
-	mk.moduleFor("foo-hostdex").hasRequired("libfoo")
+	mod := ctx.ModuleForTests("foo", "android_common").Module()
+	entries := android.AndroidMkEntriesForTest(t, config, "", mod)
+
+	if r, ok := entries.EntryMap["LOCAL_REQUIRED_MODULES"]; ok {
+		t.Errorf("Unexpected required modules: %q", r)
+	}
+
+	footerLines := entries.FooterLinesForTests()
+	if !android.InList("LOCAL_REQUIRED_MODULES += libfoo", footerLines) {
+		t.Errorf("Wrong or missing required line for foo-hostdex in the footers: %q", footerLines)
+	}
 }