123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262 |
- // Copyright 2017 Google Inc. All rights reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package java
- import (
- "fmt"
- "os"
- "path/filepath"
- "reflect"
- "regexp"
- "strconv"
- "strings"
- "testing"
- "github.com/google/blueprint/proptools"
- "android/soong/android"
- "android/soong/cc"
- "android/soong/dexpreopt"
- "android/soong/genrule"
- "android/soong/python"
- )
- // Legacy preparer used for running tests within the java package.
- //
- // This includes everything that was needed to run any test in the java package prior to the
- // introduction of the test fixtures. Tests that are being converted to use fixtures directly
- // rather than through the testJava...() methods should avoid using this and instead use the
- // various preparers directly, using android.GroupFixturePreparers(...) to group them when
- // necessary.
- //
- // deprecated
- var prepareForJavaTest = android.GroupFixturePreparers(
- genrule.PrepareForTestWithGenRuleBuildComponents,
- // Get the CC build components but not default modules.
- cc.PrepareForTestWithCcBuildComponents,
- // Include all the default java modules.
- PrepareForTestWithJavaDefaultModules,
- PrepareForTestWithOverlayBuildComponents,
- python.PrepareForTestWithPythonBuildComponents,
- android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
- ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
- }),
- PrepareForTestWithDexpreopt,
- )
- func TestMain(m *testing.M) {
- os.Exit(m.Run())
- }
- // testJavaError is a legacy way of running tests of java modules that expect errors.
- //
- // See testJava for an explanation as to how to stop using this deprecated method.
- //
- // deprecated
- func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
- t.Helper()
- result := android.GroupFixturePreparers(
- prepareForJavaTest, dexpreopt.PrepareForTestByEnablingDexpreopt).
- ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
- RunTestWithBp(t, bp)
- return result.TestContext, result.Config
- }
- // testJavaErrorWithConfig is a legacy way of running tests of java modules that expect errors.
- //
- // See testJava for an explanation as to how to stop using this deprecated method.
- //
- // deprecated
- func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
- t.Helper()
- // This must be done on the supplied config and not as part of the fixture because any changes to
- // the fixture's config will be ignored when RunTestWithConfig replaces it.
- pathCtx := android.PathContextForTesting(config)
- dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
- result := prepareForJavaTest.
- ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
- RunTestWithConfig(t, config)
- return result.TestContext, result.Config
- }
- // testJavaWithFS runs tests using the prepareForJavaTest
- //
- // See testJava for an explanation as to how to stop using this deprecated method.
- //
- // deprecated
- func testJavaWithFS(t *testing.T, bp string, fs android.MockFS) (*android.TestContext, android.Config) {
- t.Helper()
- result := android.GroupFixturePreparers(
- prepareForJavaTest, fs.AddToFixture()).RunTestWithBp(t, bp)
- return result.TestContext, result.Config
- }
- // testJava runs tests using the prepareForJavaTest
- //
- // Do not add any new usages of this, instead use the prepareForJavaTest directly as it makes it
- // much easier to customize the test behavior.
- //
- // If it is necessary to customize the behavior of an existing test that uses this then please first
- // convert the test to using prepareForJavaTest first and then in a following change add the
- // appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
- // that it did not change the test behavior unexpectedly.
- //
- // deprecated
- func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
- t.Helper()
- result := prepareForJavaTest.RunTestWithBp(t, bp)
- return result.TestContext, result.Config
- }
- // defaultModuleToPath constructs a path to the turbine generate jar for a default test module that
- // is defined in PrepareForIntegrationTestWithJava
- func defaultModuleToPath(name string) string {
- switch {
- case name == `""`:
- return name
- case strings.HasSuffix(name, ".jar"):
- return name
- default:
- return filepath.Join("out", "soong", ".intermediates", defaultJavaDir, name, "android_common", "turbine-combined", name+".jar")
- }
- }
- // Test that the PrepareForTestWithJavaDefaultModules provides all the files that it uses by
- // running it in a fixture that requires all source files to exist.
- func TestPrepareForTestWithJavaDefaultModules(t *testing.T) {
- android.GroupFixturePreparers(
- PrepareForTestWithJavaDefaultModules,
- android.PrepareForTestDisallowNonExistentPaths,
- ).RunTest(t)
- }
- func TestJavaLinkType(t *testing.T) {
- testJava(t, `
- java_library {
- name: "foo",
- srcs: ["a.java"],
- libs: ["bar"],
- static_libs: ["baz"],
- }
- java_library {
- name: "bar",
- sdk_version: "current",
- srcs: ["b.java"],
- }
- java_library {
- name: "baz",
- sdk_version: "system_current",
- srcs: ["c.java"],
- }
- `)
- testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
- java_library {
- name: "foo",
- srcs: ["a.java"],
- libs: ["bar"],
- sdk_version: "current",
- static_libs: ["baz"],
- }
- java_library {
- name: "bar",
- sdk_version: "current",
- srcs: ["b.java"],
- }
- java_library {
- name: "baz",
- sdk_version: "system_current",
- srcs: ["c.java"],
- }
- `)
- testJava(t, `
- java_library {
- name: "foo",
- srcs: ["a.java"],
- libs: ["bar"],
- sdk_version: "system_current",
- static_libs: ["baz"],
- }
- java_library {
- name: "bar",
- sdk_version: "current",
- srcs: ["b.java"],
- }
- java_library {
- name: "baz",
- sdk_version: "system_current",
- srcs: ["c.java"],
- }
- `)
- testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
- java_library {
- name: "foo",
- srcs: ["a.java"],
- libs: ["bar"],
- sdk_version: "system_current",
- static_libs: ["baz"],
- }
- java_library {
- name: "bar",
- sdk_version: "current",
- srcs: ["b.java"],
- }
- java_library {
- name: "baz",
- srcs: ["c.java"],
- }
- `)
- }
- func TestSimple(t *testing.T) {
- ctx, _ := testJava(t, `
- java_library {
- name: "foo",
- srcs: ["a.java"],
- libs: ["bar"],
- static_libs: ["baz"],
- }
- java_library {
- name: "bar",
- srcs: ["b.java"],
- }
- java_library {
- name: "baz",
- srcs: ["c.java"],
- }
- `)
- javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
- combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
- if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
- t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
- }
- baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
- barTurbine := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
- bazTurbine := filepath.Join("out", "soong", ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
- android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], barTurbine)
- android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], bazTurbine)
- if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
- t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
- }
- }
- func TestExportedPlugins(t *testing.T) {
- type Result struct {
- library string
- processors string
- disableTurbine bool
- }
- var tests = []struct {
- name string
- extra string
- results []Result
- }{
- {
- name: "Exported plugin is not a direct plugin",
- extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
- results: []Result{{library: "exports", processors: "-proc:none"}},
- },
- {
- name: "Exports plugin to dependee",
- extra: `
- java_library{name: "exports", exported_plugins: ["plugin"]}
- java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
- java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
- `,
- results: []Result{
- {library: "foo", processors: "-processor com.android.TestPlugin"},
- {library: "bar", processors: "-processor com.android.TestPlugin"},
- },
- },
- {
- name: "Exports plugin to android_library",
- extra: `
- java_library{name: "exports", exported_plugins: ["plugin"]}
- android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
- android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
- `,
- results: []Result{
- {library: "foo", processors: "-processor com.android.TestPlugin"},
- {library: "bar", processors: "-processor com.android.TestPlugin"},
- },
- },
- {
- name: "Exports plugin is not propagated via transitive deps",
- extra: `
- java_library{name: "exports", exported_plugins: ["plugin"]}
- java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
- java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
- `,
- results: []Result{
- {library: "foo", processors: "-processor com.android.TestPlugin"},
- {library: "bar", processors: "-proc:none"},
- },
- },
- {
- name: "Exports plugin appends to plugins",
- extra: `
- java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
- java_library{name: "exports", exported_plugins: ["plugin"]}
- java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
- `,
- results: []Result{
- {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
- },
- },
- {
- name: "Exports plugin to with generates_api to dependee",
- extra: `
- java_library{name: "exports", exported_plugins: ["plugin_generates_api"]}
- java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
- java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
- `,
- results: []Result{
- {library: "foo", processors: "-processor com.android.TestPlugin", disableTurbine: true},
- {library: "bar", processors: "-processor com.android.TestPlugin", disableTurbine: true},
- },
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- ctx, _ := testJava(t, `
- java_plugin {
- name: "plugin",
- processor_class: "com.android.TestPlugin",
- }
- java_plugin {
- name: "plugin_generates_api",
- generates_api: true,
- processor_class: "com.android.TestPlugin",
- }
- `+test.extra)
- for _, want := range test.results {
- javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
- if javac.Args["processor"] != want.processors {
- t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
- }
- turbine := ctx.ModuleForTests(want.library, "android_common").MaybeRule("turbine")
- disableTurbine := turbine.BuildParams.Rule == nil
- if disableTurbine != want.disableTurbine {
- t.Errorf("For library %v, expected disableTurbine %v, found %v", want.library, want.disableTurbine, disableTurbine)
- }
- }
- })
- }
- }
- func TestSdkVersionByPartition(t *testing.T) {
- testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
- java_library {
- name: "foo",
- srcs: ["a.java"],
- vendor: true,
- }
- `)
- testJava(t, `
- java_library {
- name: "bar",
- srcs: ["b.java"],
- }
- `)
- for _, enforce := range []bool{true, false} {
- bp := `
- java_library {
- name: "foo",
- srcs: ["a.java"],
- product_specific: true,
- }
- `
- errorHandler := android.FixtureExpectsNoErrors
- if enforce {
- errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern("sdk_version must have a value when the module is located at vendor or product")
- }
- android.GroupFixturePreparers(
- PrepareForTestWithJavaDefaultModules,
- android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
- variables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
- }),
- ).
- ExtendWithErrorHandler(errorHandler).
- RunTestWithBp(t, bp)
- }
- }
- func TestArchSpecific(t *testing.T) {
- ctx, _ := testJava(t, `
- java_library {
- name: "foo",
- srcs: ["a.java"],
- target: {
- android: {
- srcs: ["b.java"],
- },
- },
- }
- `)
- javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
- if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
- t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
- }
- }
- func TestBinary(t *testing.T) {
- ctx, _ := testJava(t, `
- java_library_host {
- name: "foo",
- srcs: ["a.java"],
- }
- java_binary_host {
- name: "bar",
- srcs: ["b.java"],
- static_libs: ["foo"],
- jni_libs: ["libjni"],
- }
- cc_library_shared {
- name: "libjni",
- host_supported: true,
- device_supported: false,
- stl: "none",
- }
- `)
- buildOS := android.BuildOs.String()
- bar := ctx.ModuleForTests("bar", buildOS+"_common")
- barJar := bar.Output("bar.jar").Output.String()
- barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
- barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
- libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
- libjniSO := libjni.Rule("Cp").Output.String()
- // Test that the install binary wrapper depends on the installed jar file
- if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
- t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
- }
- // Test that the install binary wrapper depends on the installed JNI libraries
- if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
- t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
- }
- }
- func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
- bp := `
- java_library {
- name: "target_library",
- srcs: ["a.java"],
- }
- java_binary_host {
- name: "host_binary",
- srcs: ["b.java"],
- }
- `
- result := android.GroupFixturePreparers(
- PrepareForTestWithJavaDefaultModules,
- android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
- variables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
- }),
- ).RunTestWithBp(t, bp)
- // first, check that the -g flag is added to target modules
- targetLibrary := result.ModuleForTests("target_library", "android_common")
- targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
- if !strings.Contains(targetJavaFlags, "-g:source,lines") {
- t.Errorf("target library javac flags %v should contain "+
- "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
- }
- // check that -g is not overridden for host modules
- buildOS := android.BuildOs.String()
- hostBinary := result.ModuleForTests("host_binary", buildOS+"_common")
- hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
- if strings.Contains(hostJavaFlags, "-g:source,lines") {
- t.Errorf("java_binary_host javac flags %v should not have "+
- "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
- }
- }
- func TestPrebuilts(t *testing.T) {
- ctx, _ := testJava(t, `
- java_library {
- name: "foo",
- srcs: ["a.java", ":stubs-source"],
- libs: ["bar", "sdklib"],
- static_libs: ["baz"],
- }
- java_import {
- name: "bar",
- jars: ["a.jar"],
- }
- java_import {
- name: "baz",
- jars: ["b.jar"],
- sdk_version: "current",
- compile_dex: true,
- }
- dex_import {
- name: "qux",
- jars: ["b.jar"],
- }
- java_sdk_library_import {
- name: "sdklib",
- public: {
- jars: ["c.jar"],
- },
- }
- prebuilt_stubs_sources {
- name: "stubs-source",
- srcs: ["stubs/sources"],
- }
- java_test_import {
- name: "test",
- jars: ["a.jar"],
- test_suites: ["cts"],
- test_config: "AndroidTest.xml",
- }
- `)
- fooModule := ctx.ModuleForTests("foo", "android_common")
- javac := fooModule.Rule("javac")
- combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
- barModule := ctx.ModuleForTests("bar", "android_common")
- barJar := barModule.Rule("combineJar").Output
- bazModule := ctx.ModuleForTests("baz", "android_common")
- bazJar := bazModule.Rule("combineJar").Output
- sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
- fooLibrary := fooModule.Module().(*Library)
- assertDeepEquals(t, "foo java sources incorrect",
- []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
- assertDeepEquals(t, "foo java source jars incorrect",
- []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
- android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
- if !strings.Contains(javac.Args["classpath"], barJar.String()) {
- t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
- }
- barDexJar := barModule.Module().(*Import).DexJarBuildPath()
- if barDexJar != nil {
- t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
- }
- if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
- t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
- }
- if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
- t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
- }
- bazDexJar := bazModule.Module().(*Import).DexJarBuildPath()
- expectedDexJar := "out/soong/.intermediates/baz/android_common/dex/baz.jar"
- android.AssertPathRelativeToTopEquals(t, "baz dex jar build path", expectedDexJar, bazDexJar)
- ctx.ModuleForTests("qux", "android_common").Rule("Cp")
- }
- func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
- if !reflect.DeepEqual(expected, actual) {
- t.Errorf("%s: expected %q, found %q", message, expected, actual)
- }
- }
- func TestPrebuiltStubsSources(t *testing.T) {
- test := func(t *testing.T, sourcesPath string, expectedInputs []string) {
- ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
- prebuilt_stubs_sources {
- name: "stubs-source",
- srcs: ["%s"],
- }`, sourcesPath), map[string][]byte{
- "stubs/sources/pkg/A.java": nil,
- "stubs/sources/pkg/B.java": nil,
- })
- zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src")
- if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) {
- t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual)
- }
- }
- t.Run("empty/missing directory", func(t *testing.T) {
- test(t, "empty-directory", nil)
- })
- t.Run("non-empty set of sources", func(t *testing.T) {
- test(t, "stubs/sources", []string{
- "stubs/sources/pkg/A.java",
- "stubs/sources/pkg/B.java",
- })
- })
- }
- func TestJavaSdkLibraryImport(t *testing.T) {
- result := prepareForJavaTest.RunTestWithBp(t, `
- java_library {
- name: "foo",
- srcs: ["a.java"],
- libs: ["sdklib"],
- sdk_version: "current",
- }
- java_library {
- name: "foo.system",
- srcs: ["a.java"],
- libs: ["sdklib"],
- sdk_version: "system_current",
- }
- java_library {
- name: "foo.test",
- srcs: ["a.java"],
- libs: ["sdklib"],
- sdk_version: "test_current",
- }
- java_sdk_library_import {
- name: "sdklib",
- public: {
- jars: ["a.jar"],
- },
- system: {
- jars: ["b.jar"],
- },
- test: {
- jars: ["c.jar"],
- stub_srcs: ["c.java"],
- },
- }
- `)
- for _, scope := range []string{"", ".system", ".test"} {
- fooModule := result.ModuleForTests("foo"+scope, "android_common")
- javac := fooModule.Rule("javac")
- sdklibStubsJar := result.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
- android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], sdklibStubsJar.String())
- }
- CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
- `prebuilt_sdklib.stubs`,
- `prebuilt_sdklib.stubs.source.test`,
- `prebuilt_sdklib.stubs.system`,
- `prebuilt_sdklib.stubs.test`,
- })
- }
- func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
- result := android.GroupFixturePreparers(
- prepareForJavaTest,
- PrepareForTestWithJavaSdkLibraryFiles,
- FixtureWithLastReleaseApis("sdklib"),
- ).RunTestWithBp(t, `
- java_sdk_library {
- name: "sdklib",
- srcs: ["a.java"],
- sdk_version: "none",
- system_modules: "none",
- public: {
- enabled: true,
- },
- }
- java_sdk_library_import {
- name: "sdklib",
- public: {
- jars: ["a.jar"],
- },
- }
- `)
- CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
- `dex2oatd`,
- `prebuilt_sdklib`,
- `sdklib.impl`,
- `sdklib.stubs`,
- `sdklib.stubs.source`,
- `sdklib.xml`,
- })
- CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
- `prebuilt_sdklib.stubs`,
- `sdklib.impl`,
- // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
- // dependency is added after prebuilts may have been renamed and so has to use
- // the renamed name.
- `sdklib.xml`,
- })
- }
- func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
- result := android.GroupFixturePreparers(
- prepareForJavaTest,
- PrepareForTestWithJavaSdkLibraryFiles,
- FixtureWithLastReleaseApis("sdklib"),
- ).RunTestWithBp(t, `
- java_sdk_library {
- name: "sdklib",
- srcs: ["a.java"],
- sdk_version: "none",
- system_modules: "none",
- public: {
- enabled: true,
- },
- }
- java_sdk_library_import {
- name: "sdklib",
- prefer: true,
- public: {
- jars: ["a.jar"],
- },
- }
- `)
- CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
- `dex2oatd`,
- `prebuilt_sdklib`,
- `sdklib.impl`,
- `sdklib.stubs`,
- `sdklib.stubs.source`,
- `sdklib.xml`,
- })
- CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
- `prebuilt_sdklib.stubs`,
- `sdklib.impl`,
- `sdklib.xml`,
- })
- }
- func TestJavaSdkLibraryEnforce(t *testing.T) {
- partitionToBpOption := func(partition string) string {
- switch partition {
- case "system":
- return ""
- case "vendor":
- return "soc_specific: true,"
- case "product":
- return "product_specific: true,"
- default:
- panic("Invalid partition group name: " + partition)
- }
- }
- type testConfigInfo struct {
- libraryType string
- fromPartition string
- toPartition string
- enforceVendorInterface bool
- enforceProductInterface bool
- enforceJavaSdkLibraryCheck bool
- allowList []string
- }
- createPreparer := func(info testConfigInfo) android.FixturePreparer {
- bpFileTemplate := `
- java_library {
- name: "foo",
- srcs: ["foo.java"],
- libs: ["bar"],
- sdk_version: "current",
- %s
- }
- %s {
- name: "bar",
- srcs: ["bar.java"],
- sdk_version: "current",
- %s
- }
- `
- bpFile := fmt.Sprintf(bpFileTemplate,
- partitionToBpOption(info.fromPartition),
- info.libraryType,
- partitionToBpOption(info.toPartition))
- return android.GroupFixturePreparers(
- PrepareForTestWithJavaSdkLibraryFiles,
- FixtureWithLastReleaseApis("bar"),
- android.FixtureWithRootAndroidBp(bpFile),
- android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
- variables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
- if info.enforceVendorInterface {
- variables.DeviceVndkVersion = proptools.StringPtr("current")
- }
- variables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
- variables.InterPartitionJavaLibraryAllowList = info.allowList
- }),
- )
- }
- runTest := func(t *testing.T, info testConfigInfo, expectedErrorPattern string) {
- t.Run(fmt.Sprintf("%v", info), func(t *testing.T) {
- errorHandler := android.FixtureExpectsNoErrors
- if expectedErrorPattern != "" {
- errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorPattern)
- }
- prepareForJavaTest.ExtendWithErrorHandler(errorHandler).RunTest(t, createPreparer(info))
- })
- }
- errorMessage := "is not allowed across the partitions"
- runTest(t, testConfigInfo{
- libraryType: "java_library",
- fromPartition: "product",
- toPartition: "system",
- enforceVendorInterface: true,
- enforceProductInterface: true,
- enforceJavaSdkLibraryCheck: false,
- }, "")
- runTest(t, testConfigInfo{
- libraryType: "java_library",
- fromPartition: "product",
- toPartition: "system",
- enforceVendorInterface: true,
- enforceProductInterface: false,
- enforceJavaSdkLibraryCheck: true,
- }, "")
- runTest(t, testConfigInfo{
- libraryType: "java_library",
- fromPartition: "product",
- toPartition: "system",
- enforceVendorInterface: true,
- enforceProductInterface: true,
- enforceJavaSdkLibraryCheck: true,
- }, errorMessage)
- runTest(t, testConfigInfo{
- libraryType: "java_library",
- fromPartition: "vendor",
- toPartition: "system",
- enforceVendorInterface: true,
- enforceProductInterface: true,
- enforceJavaSdkLibraryCheck: true,
- }, errorMessage)
- runTest(t, testConfigInfo{
- libraryType: "java_library",
- fromPartition: "vendor",
- toPartition: "system",
- enforceVendorInterface: true,
- enforceProductInterface: true,
- enforceJavaSdkLibraryCheck: true,
- allowList: []string{"bar"},
- }, "")
- runTest(t, testConfigInfo{
- libraryType: "java_library",
- fromPartition: "vendor",
- toPartition: "product",
- enforceVendorInterface: true,
- enforceProductInterface: true,
- enforceJavaSdkLibraryCheck: true,
- }, errorMessage)
- runTest(t, testConfigInfo{
- libraryType: "java_sdk_library",
- fromPartition: "product",
- toPartition: "system",
- enforceVendorInterface: true,
- enforceProductInterface: true,
- enforceJavaSdkLibraryCheck: true,
- }, "")
- runTest(t, testConfigInfo{
- libraryType: "java_sdk_library",
- fromPartition: "vendor",
- toPartition: "system",
- enforceVendorInterface: true,
- enforceProductInterface: true,
- enforceJavaSdkLibraryCheck: true,
- }, "")
- runTest(t, testConfigInfo{
- libraryType: "java_sdk_library",
- fromPartition: "vendor",
- toPartition: "product",
- enforceVendorInterface: true,
- enforceProductInterface: true,
- enforceJavaSdkLibraryCheck: true,
- }, "")
- }
- func TestDefaults(t *testing.T) {
- ctx, _ := testJava(t, `
- java_defaults {
- name: "defaults",
- srcs: ["a.java"],
- libs: ["bar"],
- static_libs: ["baz"],
- optimize: {enabled: false},
- }
- java_library {
- name: "foo",
- defaults: ["defaults"],
- }
- java_library {
- name: "bar",
- srcs: ["b.java"],
- }
- java_library {
- name: "baz",
- srcs: ["c.java"],
- }
- android_test {
- name: "atestOptimize",
- defaults: ["defaults"],
- optimize: {enabled: true},
- }
- android_test {
- name: "atestNoOptimize",
- defaults: ["defaults"],
- }
- android_test {
- name: "atestDefault",
- srcs: ["a.java"],
- }
- `)
- javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
- combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
- if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
- t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
- }
- barTurbine := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
- if !strings.Contains(javac.Args["classpath"], barTurbine) {
- t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
- }
- baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
- if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
- t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
- }
- atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
- if atestOptimize.Output == nil {
- t.Errorf("atestOptimize should optimize APK")
- }
- atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
- if atestNoOptimize.Output == nil {
- t.Errorf("atestNoOptimize should not optimize APK")
- }
- atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
- if atestDefault.Output == nil {
- t.Errorf("atestDefault should optimize APK")
- }
- }
- func TestResources(t *testing.T) {
- var table = []struct {
- name string
- prop string
- extra string
- args string
- }{
- {
- // Test that a module with java_resource_dirs includes the files
- name: "resource dirs",
- prop: `java_resource_dirs: ["java-res"]`,
- args: "-C java-res -f java-res/a/a -f java-res/b/b",
- },
- {
- // Test that a module with java_resources includes the files
- name: "resource files",
- prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
- args: "-C . -f java-res/a/a -f java-res/b/b",
- },
- {
- // Test that a module with a filegroup in java_resources includes the files with the
- // path prefix
- name: "resource filegroup",
- prop: `java_resources: [":foo-res"]`,
- extra: `
- filegroup {
- name: "foo-res",
- path: "java-res",
- srcs: ["java-res/a/a", "java-res/b/b"],
- }`,
- args: "-C java-res -f java-res/a/a -f java-res/b/b",
- },
- {
- // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
- name: "wildcard dirs",
- prop: `java_resource_dirs: ["java-res/*"]`,
- args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
- },
- {
- // Test that a module exclude_java_resource_dirs excludes the files
- name: "wildcard dirs",
- prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
- args: "-C java-res/a -f java-res/a/a",
- },
- {
- // Test wildcards in java_resources
- name: "wildcard files",
- prop: `java_resources: ["java-res/**/*"]`,
- args: "-C . -f java-res/a/a -f java-res/b/b",
- },
- {
- // Test exclude_java_resources with java_resources
- name: "wildcard files with exclude",
- prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
- args: "-C . -f java-res/a/a",
- },
- {
- // Test exclude_java_resources with java_resource_dirs
- name: "resource dirs with exclude files",
- prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
- args: "-C java-res -f java-res/a/a",
- },
- {
- // Test exclude_java_resource_dirs with java_resource_dirs
- name: "resource dirs with exclude files",
- prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
- args: "-C java-res -f java-res/a/a -f java-res/b/b",
- },
- }
- for _, test := range table {
- t.Run(test.name, func(t *testing.T) {
- ctx, _ := testJavaWithFS(t, `
- java_library {
- name: "foo",
- srcs: [
- "a.java",
- "b.java",
- "c.java",
- ],
- `+test.prop+`,
- }
- `+test.extra,
- map[string][]byte{
- "java-res/a/a": nil,
- "java-res/b/b": nil,
- "java-res2/a": nil,
- },
- )
- foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
- fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
- if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
- t.Errorf("foo combined jars %v does not contain %q",
- foo.Inputs.Strings(), fooRes.Output.String())
- }
- if fooRes.Args["jarArgs"] != test.args {
- t.Errorf("foo resource jar args %q is not %q",
- fooRes.Args["jarArgs"], test.args)
- }
- })
- }
- }
- func TestIncludeSrcs(t *testing.T) {
- ctx, _ := testJavaWithFS(t, `
- java_library {
- name: "foo",
- srcs: [
- "a.java",
- "b.java",
- "c.java",
- ],
- include_srcs: true,
- }
- java_library {
- name: "bar",
- srcs: [
- "a.java",
- "b.java",
- "c.java",
- ],
- java_resource_dirs: ["java-res"],
- include_srcs: true,
- }
- `, map[string][]byte{
- "java-res/a/a": nil,
- "java-res/b/b": nil,
- "java-res2/a": nil,
- })
- // Test a library with include_srcs: true
- foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
- fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
- if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
- t.Errorf("foo combined jars %v does not contain %q", w, g)
- }
- if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
- t.Errorf("foo source jar args %q is not %q", w, g)
- }
- // Test a library with include_srcs: true and resources
- bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
- barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
- barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
- barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
- if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
- t.Errorf("bar combined resource jars %v does not contain %q", w, g)
- }
- if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
- t.Errorf("bar combined resource jars %v does not contain %q", w, g)
- }
- if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
- t.Errorf("bar combined jars %v does not contain %q", w, g)
- }
- if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
- t.Errorf("bar source jar args %q is not %q", w, g)
- }
- if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
- t.Errorf("bar resource jar args %q is not %q", w, g)
- }
- }
- func TestJavaLint(t *testing.T) {
- ctx, _ := testJavaWithFS(t, `
- java_library {
- name: "foo",
- srcs: [
- "a.java",
- "b.java",
- "c.java",
- ],
- min_sdk_version: "29",
- sdk_version: "system_current",
- }
- `, map[string][]byte{
- "lint-baseline.xml": nil,
- })
- foo := ctx.ModuleForTests("foo", "android_common")
- sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
- if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml") {
- t.Error("did not pass --baseline flag")
- }
- }
- func TestJavaLintWithoutBaseline(t *testing.T) {
- ctx, _ := testJavaWithFS(t, `
- java_library {
- name: "foo",
- srcs: [
- "a.java",
- "b.java",
- "c.java",
- ],
- min_sdk_version: "29",
- sdk_version: "system_current",
- }
- `, map[string][]byte{})
- foo := ctx.ModuleForTests("foo", "android_common")
- sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
- if strings.Contains(*sboxProto.Commands[0].Command, "--baseline") {
- t.Error("passed --baseline flag for non existent file")
- }
- }
- func TestJavaLintRequiresCustomLintFileToExist(t *testing.T) {
- android.GroupFixturePreparers(
- PrepareForTestWithJavaDefaultModules,
- android.PrepareForTestDisallowNonExistentPaths,
- ).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern([]string{`source path "mybaseline.xml" does not exist`})).
- RunTestWithBp(t, `
- java_library {
- name: "foo",
- srcs: [
- ],
- min_sdk_version: "29",
- sdk_version: "system_current",
- lint: {
- baseline_filename: "mybaseline.xml",
- },
- }
- `)
- }
- func TestJavaLintUsesCorrectBpConfig(t *testing.T) {
- ctx, _ := testJavaWithFS(t, `
- java_library {
- name: "foo",
- srcs: [
- "a.java",
- "b.java",
- "c.java",
- ],
- min_sdk_version: "29",
- sdk_version: "system_current",
- lint: {
- error_checks: ["SomeCheck"],
- baseline_filename: "mybaseline.xml",
- },
- }
- `, map[string][]byte{
- "mybaseline.xml": nil,
- })
- foo := ctx.ModuleForTests("foo", "android_common")
- sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
- if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline mybaseline.xml") {
- t.Error("did not use the correct file for baseline")
- }
- }
- func TestGeneratedSources(t *testing.T) {
- ctx, _ := testJavaWithFS(t, `
- java_library {
- name: "foo",
- srcs: [
- "a*.java",
- ":gen",
- "b*.java",
- ],
- }
- genrule {
- name: "gen",
- tool_files: ["java-res/a"],
- out: ["gen.java"],
- }
- `, map[string][]byte{
- "a.java": nil,
- "b.java": nil,
- })
- javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
- genrule := ctx.ModuleForTests("gen", "").Rule("generator")
- if filepath.Base(genrule.Output.String()) != "gen.java" {
- t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
- }
- if len(javac.Inputs) != 3 ||
- javac.Inputs[0].String() != "a.java" ||
- javac.Inputs[1].String() != genrule.Output.String() ||
- javac.Inputs[2].String() != "b.java" {
- t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
- }
- }
- func TestTurbine(t *testing.T) {
- result := android.GroupFixturePreparers(
- prepareForJavaTest, FixtureWithPrebuiltApis(map[string][]string{"14": {"foo"}})).
- RunTestWithBp(t, `
- java_library {
- name: "foo",
- srcs: ["a.java"],
- sdk_version: "14",
- }
- java_library {
- name: "bar",
- srcs: ["b.java"],
- static_libs: ["foo"],
- sdk_version: "14",
- }
- java_library {
- name: "baz",
- srcs: ["c.java"],
- libs: ["bar"],
- sdk_version: "14",
- }
- `)
- fooTurbine := result.ModuleForTests("foo", "android_common").Rule("turbine")
- barTurbine := result.ModuleForTests("bar", "android_common").Rule("turbine")
- barJavac := result.ModuleForTests("bar", "android_common").Rule("javac")
- barTurbineCombined := result.ModuleForTests("bar", "android_common").Description("for turbine")
- bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
- android.AssertPathsRelativeToTopEquals(t, "foo inputs", []string{"a.java"}, fooTurbine.Inputs)
- fooHeaderJar := filepath.Join("out", "soong", ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
- barTurbineJar := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine", "bar.jar")
- android.AssertStringDoesContain(t, "bar turbine classpath", barTurbine.Args["classpath"], fooHeaderJar)
- android.AssertStringDoesContain(t, "bar javac classpath", barJavac.Args["classpath"], fooHeaderJar)
- android.AssertPathsRelativeToTopEquals(t, "bar turbine combineJar", []string{barTurbineJar, fooHeaderJar}, barTurbineCombined.Inputs)
- android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar")
- }
- func TestSharding(t *testing.T) {
- ctx, _ := testJava(t, `
- java_library {
- name: "bar",
- srcs: ["a.java","b.java","c.java"],
- javac_shard_size: 1
- }
- `)
- barHeaderJar := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
- for i := 0; i < 3; i++ {
- barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
- if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
- t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
- }
- }
- }
- func TestJarGenrules(t *testing.T) {
- ctx, _ := testJava(t, `
- java_library {
- name: "foo",
- srcs: ["a.java"],
- }
- java_genrule {
- name: "jargen",
- tool_files: ["b.java"],
- cmd: "$(location b.java) $(in) $(out)",
- out: ["jargen.jar"],
- srcs: [":foo"],
- }
- java_library {
- name: "bar",
- static_libs: ["jargen"],
- srcs: ["c.java"],
- }
- java_library {
- name: "baz",
- libs: ["jargen"],
- srcs: ["c.java"],
- }
- `)
- foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
- jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
- bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
- baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
- barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
- if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) {
- t.Errorf("expected jargen inputs [%q], got %q", w, g)
- }
- if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
- t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
- }
- if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
- t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
- }
- if len(barCombined.Inputs) != 2 ||
- barCombined.Inputs[0].String() != bar.Output.String() ||
- barCombined.Inputs[1].String() != jargen.Output.String() {
- t.Errorf("bar combined jar inputs %v is not [%q, %q]",
- barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
- }
- }
- func TestExcludeFileGroupInSrcs(t *testing.T) {
- ctx, _ := testJava(t, `
- java_library {
- name: "foo",
- srcs: ["a.java", ":foo-srcs"],
- exclude_srcs: ["a.java", ":foo-excludes"],
- }
- filegroup {
- name: "foo-srcs",
- srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
- }
- filegroup {
- name: "foo-excludes",
- srcs: ["java-fg/a.java", "java-fg/b.java"],
- }
- `)
- javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
- if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
- t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
- }
- }
- func TestJavaLibrary(t *testing.T) {
- testJavaWithFS(t, "", map[string][]byte{
- "libcore/Android.bp": []byte(`
- java_library {
- name: "core",
- sdk_version: "none",
- system_modules: "none",
- }
- filegroup {
- name: "core-jar",
- srcs: [":core{.jar}"],
- }
- `),
- })
- }
- func TestJavaImport(t *testing.T) {
- testJavaWithFS(t, "", map[string][]byte{
- "libcore/Android.bp": []byte(`
- java_import {
- name: "core",
- sdk_version: "none",
- }
- filegroup {
- name: "core-jar",
- srcs: [":core{.jar}"],
- }
- `),
- })
- }
- func TestJavaSdkLibrary(t *testing.T) {
- result := android.GroupFixturePreparers(
- prepareForJavaTest,
- PrepareForTestWithJavaSdkLibraryFiles,
- FixtureWithPrebuiltApis(map[string][]string{
- "28": {"foo"},
- "29": {"foo"},
- "30": {"bar", "barney", "baz", "betty", "foo", "fred", "quuz", "wilma"},
- }),
- ).RunTestWithBp(t, `
- droiddoc_exported_dir {
- name: "droiddoc-templates-sdk",
- path: ".",
- }
- java_sdk_library {
- name: "foo",
- srcs: ["a.java", "b.java"],
- api_packages: ["foo"],
- }
- java_sdk_library {
- name: "bar",
- srcs: ["a.java", "b.java"],
- api_packages: ["bar"],
- }
- java_library {
- name: "baz",
- srcs: ["c.java"],
- libs: ["foo", "bar.stubs"],
- sdk_version: "system_current",
- }
- java_sdk_library {
- name: "barney",
- srcs: ["c.java"],
- api_only: true,
- }
- java_sdk_library {
- name: "betty",
- srcs: ["c.java"],
- shared_library: false,
- }
- java_sdk_library_import {
- name: "quuz",
- public: {
- jars: ["c.jar"],
- },
- }
- java_sdk_library_import {
- name: "fred",
- public: {
- jars: ["b.jar"],
- },
- }
- java_sdk_library_import {
- name: "wilma",
- public: {
- jars: ["b.jar"],
- },
- shared_library: false,
- }
- java_library {
- name: "qux",
- srcs: ["c.java"],
- libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
- sdk_version: "system_current",
- }
- java_library {
- name: "baz-test",
- srcs: ["c.java"],
- libs: ["foo"],
- sdk_version: "test_current",
- }
- java_library {
- name: "baz-29",
- srcs: ["c.java"],
- libs: ["foo"],
- sdk_version: "system_29",
- }
- java_library {
- name: "baz-module-30",
- srcs: ["c.java"],
- libs: ["foo"],
- sdk_version: "module_30",
- }
- `)
- // check the existence of the internal modules
- result.ModuleForTests("foo", "android_common")
- result.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
- result.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
- result.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
- result.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
- result.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
- result.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
- result.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
- result.ModuleForTests("foo.api.public.28", "")
- result.ModuleForTests("foo.api.system.28", "")
- result.ModuleForTests("foo.api.test.28", "")
- bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
- // tests if baz is actually linked to the stubs lib
- android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.system.jar")
- // ... and not to the impl lib
- android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.jar")
- // test if baz is not linked to the system variant of foo
- android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.jar")
- bazTestJavac := result.ModuleForTests("baz-test", "android_common").Rule("javac")
- // tests if baz-test is actually linked to the test stubs lib
- android.AssertStringDoesContain(t, "baz-test javac classpath", bazTestJavac.Args["classpath"], "foo.stubs.test.jar")
- baz29Javac := result.ModuleForTests("baz-29", "android_common").Rule("javac")
- // tests if baz-29 is actually linked to the system 29 stubs lib
- android.AssertStringDoesContain(t, "baz-29 javac classpath", baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar")
- bazModule30Javac := result.ModuleForTests("baz-module-30", "android_common").Rule("javac")
- // tests if "baz-module-30" is actually linked to the module 30 stubs lib
- android.AssertStringDoesContain(t, "baz-module-30 javac classpath", bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar")
- // test if baz has exported SDK lib names foo and bar to qux
- qux := result.ModuleForTests("qux", "android_common")
- if quxLib, ok := qux.Module().(*Library); ok {
- sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
- android.AssertDeepEquals(t, "qux exports", []string{"foo", "bar", "fred", "quuz"}, sdkLibs)
- }
- }
- func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
- result := android.GroupFixturePreparers(
- prepareForJavaTest,
- PrepareForTestWithJavaSdkLibraryFiles,
- FixtureWithLastReleaseApis("sdklib"),
- ).RunTestWithBp(t, `
- java_sdk_library {
- name: "sdklib",
- srcs: ["a.java"],
- impl_only_libs: ["foo"],
- stub_only_libs: ["bar"],
- }
- java_library {
- name: "foo",
- srcs: ["a.java"],
- sdk_version: "current",
- }
- java_library {
- name: "bar",
- srcs: ["a.java"],
- sdk_version: "current",
- }
- `)
- for _, implName := range []string{"sdklib", "sdklib.impl"} {
- implJavacCp := result.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
- if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
- t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
- }
- }
- stubName := apiScopePublic.stubsLibraryModuleName("sdklib")
- stubsJavacCp := result.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
- if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
- t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
- }
- }
- func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
- result := android.GroupFixturePreparers(
- prepareForJavaTest,
- PrepareForTestWithJavaSdkLibraryFiles,
- FixtureWithLastReleaseApis("foo"),
- ).RunTestWithBp(t, `
- java_sdk_library {
- name: "foo",
- srcs: ["a.java"],
- api_only: true,
- public: {
- enabled: true,
- },
- }
- java_library {
- name: "bar",
- srcs: ["b.java"],
- libs: ["foo"],
- }
- `)
- // The bar library should depend on the stubs jar.
- barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac")
- if expected, actual := `^-classpath .*:out/soong/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
- t.Errorf("expected %q, found %#q", expected, actual)
- }
- }
- func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
- android.GroupFixturePreparers(
- prepareForJavaTest,
- PrepareForTestWithJavaSdkLibraryFiles,
- FixtureWithLastReleaseApis("foo"),
- ).RunTestWithBp(t, `
- java_sdk_library {
- name: "foo",
- srcs: ["a.java"],
- api_packages: ["foo"],
- public: {
- enabled: true,
- },
- }
- java_library {
- name: "bar",
- srcs: ["b.java", ":foo{.public.stubs.source}"],
- }
- `)
- }
- func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
- android.GroupFixturePreparers(
- prepareForJavaTest,
- PrepareForTestWithJavaSdkLibraryFiles,
- FixtureWithLastReleaseApis("foo"),
- ).
- ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"foo" does not provide api scope system`)).
- RunTestWithBp(t, `
- java_sdk_library {
- name: "foo",
- srcs: ["a.java"],
- api_packages: ["foo"],
- public: {
- enabled: true,
- },
- }
- java_library {
- name: "bar",
- srcs: ["b.java", ":foo{.system.stubs.source}"],
- }
- `)
- }
- func TestJavaSdkLibrary_Deps(t *testing.T) {
- result := android.GroupFixturePreparers(
- prepareForJavaTest,
- PrepareForTestWithJavaSdkLibraryFiles,
- FixtureWithLastReleaseApis("sdklib"),
- ).RunTestWithBp(t, `
- java_sdk_library {
- name: "sdklib",
- srcs: ["a.java"],
- sdk_version: "none",
- system_modules: "none",
- public: {
- enabled: true,
- },
- }
- `)
- CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
- `dex2oatd`,
- `sdklib.impl`,
- `sdklib.stubs`,
- `sdklib.stubs.source`,
- `sdklib.xml`,
- })
- }
- func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
- prepareForJavaTest.RunTestWithBp(t, `
- java_sdk_library_import {
- name: "foo",
- public: {
- jars: ["a.jar"],
- stub_srcs: ["a.java"],
- current_api: "api/current.txt",
- removed_api: "api/removed.txt",
- },
- }
- java_library {
- name: "bar",
- srcs: [":foo{.public.stubs.source}"],
- java_resources: [
- ":foo{.public.api.txt}",
- ":foo{.public.removed-api.txt}",
- ],
- }
- `)
- }
- func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
- bp := `
- java_sdk_library_import {
- name: "foo",
- public: {
- jars: ["a.jar"],
- },
- }
- `
- t.Run("stubs.source", func(t *testing.T) {
- prepareForJavaTest.
- ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`stubs.source not available for api scope public`)).
- RunTestWithBp(t, bp+`
- java_library {
- name: "bar",
- srcs: [":foo{.public.stubs.source}"],
- java_resources: [
- ":foo{.public.api.txt}",
- ":foo{.public.removed-api.txt}",
- ],
- }
- `)
- })
- t.Run("api.txt", func(t *testing.T) {
- prepareForJavaTest.
- ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`api.txt not available for api scope public`)).
- RunTestWithBp(t, bp+`
- java_library {
- name: "bar",
- srcs: ["a.java"],
- java_resources: [
- ":foo{.public.api.txt}",
- ],
- }
- `)
- })
- t.Run("removed-api.txt", func(t *testing.T) {
- prepareForJavaTest.
- ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`removed-api.txt not available for api scope public`)).
- RunTestWithBp(t, bp+`
- java_library {
- name: "bar",
- srcs: ["a.java"],
- java_resources: [
- ":foo{.public.removed-api.txt}",
- ],
- }
- `)
- })
- }
- func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
- prepareForJavaTest.
- ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "foo": enabled api scope "system" depends on disabled scope "public"`)).
- RunTestWithBp(t, `
- java_sdk_library {
- name: "foo",
- srcs: ["a.java", "b.java"],
- api_packages: ["foo"],
- // Explicitly disable public to test the check that ensures the set of enabled
- // scopes is consistent.
- public: {
- enabled: false,
- },
- system: {
- enabled: true,
- },
- }
- `)
- }
- func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
- android.GroupFixturePreparers(
- prepareForJavaTest,
- PrepareForTestWithJavaSdkLibraryFiles,
- FixtureWithLastReleaseApis("foo"),
- ).RunTestWithBp(t, `
- java_sdk_library {
- name: "foo",
- srcs: ["a.java", "b.java"],
- api_packages: ["foo"],
- system: {
- enabled: true,
- sdk_version: "module_current",
- },
- }
- `)
- }
- func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
- android.GroupFixturePreparers(
- prepareForJavaTest,
- PrepareForTestWithJavaSdkLibraryFiles,
- FixtureWithLastReleaseApis("foo"),
- ).RunTestWithBp(t, `
- java_sdk_library {
- name: "foo",
- srcs: ["a.java", "b.java"],
- api_packages: ["foo"],
- system: {
- enabled: true,
- },
- module_lib: {
- enabled: true,
- },
- }
- `)
- }
- func TestJavaSdkLibrary_SystemServer(t *testing.T) {
- android.GroupFixturePreparers(
- prepareForJavaTest,
- PrepareForTestWithJavaSdkLibraryFiles,
- FixtureWithLastReleaseApis("foo"),
- ).RunTestWithBp(t, `
- java_sdk_library {
- name: "foo",
- srcs: ["a.java", "b.java"],
- api_packages: ["foo"],
- system: {
- enabled: true,
- },
- system_server: {
- enabled: true,
- },
- }
- `)
- }
- func TestJavaSdkLibrary_MissingScope(t *testing.T) {
- prepareForJavaTest.
- ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`requires api scope module-lib from foo but it only has \[\] available`)).
- RunTestWithBp(t, `
- java_sdk_library {
- name: "foo",
- srcs: ["a.java"],
- public: {
- enabled: false,
- },
- }
- java_library {
- name: "baz",
- srcs: ["a.java"],
- libs: ["foo"],
- sdk_version: "module_current",
- }
- `)
- }
- func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
- android.GroupFixturePreparers(
- prepareForJavaTest,
- PrepareForTestWithJavaSdkLibraryFiles,
- FixtureWithLastReleaseApis("foo"),
- ).RunTestWithBp(t, `
- java_sdk_library {
- name: "foo",
- srcs: ["a.java"],
- system: {
- enabled: true,
- },
- }
- java_library {
- name: "baz",
- srcs: ["a.java"],
- libs: ["foo"],
- // foo does not have module-lib scope so it should fallback to system
- sdk_version: "module_current",
- }
- `)
- }
- func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
- result := android.GroupFixturePreparers(
- prepareForJavaTest,
- PrepareForTestWithJavaSdkLibraryFiles,
- FixtureWithLastReleaseApis("foo"),
- ).RunTestWithBp(t, `
- java_sdk_library {
- name: "foo",
- srcs: ["a.java"],
- system: {
- enabled: true,
- },
- default_to_stubs: true,
- }
- java_library {
- name: "baz",
- srcs: ["a.java"],
- libs: ["foo"],
- // does not have sdk_version set, should fallback to module,
- // which will then fallback to system because the module scope
- // is not enabled.
- }
- `)
- // The baz library should depend on the system stubs jar.
- bazLibrary := result.ModuleForTests("baz", "android_common").Rule("javac")
- if expected, actual := `^-classpath .*:out/soong/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
- t.Errorf("expected %q, found %#q", expected, actual)
- }
- }
- var compilerFlagsTestCases = []struct {
- in string
- out bool
- }{
- {
- in: "a",
- out: false,
- },
- {
- in: "-a",
- out: true,
- },
- {
- in: "-no-jdk",
- out: false,
- },
- {
- in: "-no-stdlib",
- out: false,
- },
- {
- in: "-kotlin-home",
- out: false,
- },
- {
- in: "-kotlin-home /some/path",
- out: false,
- },
- {
- in: "-include-runtime",
- out: false,
- },
- {
- in: "-Xintellij-plugin-root",
- out: false,
- },
- }
- type mockContext struct {
- android.ModuleContext
- result bool
- }
- func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
- // CheckBadCompilerFlags calls this function when the flag should be rejected
- ctx.result = false
- }
- func TestCompilerFlags(t *testing.T) {
- for _, testCase := range compilerFlagsTestCases {
- ctx := &mockContext{result: true}
- CheckKotlincFlags(ctx, []string{testCase.in})
- if ctx.result != testCase.out {
- t.Errorf("incorrect output:")
- t.Errorf(" input: %#v", testCase.in)
- t.Errorf(" expected: %#v", testCase.out)
- t.Errorf(" got: %#v", ctx.result)
- }
- }
- }
- // TODO(jungjw): Consider making this more robust by ignoring path order.
- func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
- variables := ctx.ModuleForTests(moduleName, "android_common").VariablesForTestsRelativeToTop()
- flags := strings.Split(variables["javacFlags"], " ")
- got := ""
- for _, flag := range flags {
- keyEnd := strings.Index(flag, "=")
- if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
- got = flag[keyEnd+1:]
- break
- }
- }
- if expected != android.StringPathRelativeToTop(ctx.Config().BuildDir(), got) {
- t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
- }
- }
- func TestPatchModule(t *testing.T) {
- t.Run("Java language level 8", func(t *testing.T) {
- // Test with legacy javac -source 1.8 -target 1.8
- bp := `
- java_library {
- name: "foo",
- srcs: ["a.java"],
- java_version: "1.8",
- }
- java_library {
- name: "bar",
- srcs: ["b.java"],
- sdk_version: "none",
- system_modules: "none",
- patch_module: "java.base",
- java_version: "1.8",
- }
- java_library {
- name: "baz",
- srcs: ["c.java"],
- patch_module: "java.base",
- java_version: "1.8",
- }
- `
- ctx, _ := testJava(t, bp)
- checkPatchModuleFlag(t, ctx, "foo", "")
- checkPatchModuleFlag(t, ctx, "bar", "")
- checkPatchModuleFlag(t, ctx, "baz", "")
- })
- t.Run("Java language level 9", func(t *testing.T) {
- // Test with default javac -source 9 -target 9
- bp := `
- java_library {
- name: "foo",
- srcs: ["a.java"],
- }
- java_library {
- name: "bar",
- srcs: ["b.java"],
- sdk_version: "none",
- system_modules: "none",
- patch_module: "java.base",
- }
- java_library {
- name: "baz",
- srcs: [
- "c.java",
- // Tests for b/150878007
- "dir/d.java",
- "dir2/e.java",
- "dir2/f.java",
- "nested/dir/g.java"
- ],
- patch_module: "java.base",
- }
- `
- ctx, _ := testJava(t, bp)
- checkPatchModuleFlag(t, ctx, "foo", "")
- expected := "java.base=.:out/soong"
- checkPatchModuleFlag(t, ctx, "bar", expected)
- expected = "java.base=" + strings.Join([]string{
- ".", "out/soong", "dir", "dir2", "nested", defaultModuleToPath("ext"), defaultModuleToPath("framework")}, ":")
- checkPatchModuleFlag(t, ctx, "baz", expected)
- })
- }
- func TestJavaLibraryWithSystemModules(t *testing.T) {
- ctx, _ := testJava(t, `
- java_library {
- name: "lib-with-source-system-modules",
- srcs: [
- "a.java",
- ],
- sdk_version: "none",
- system_modules: "source-system-modules",
- }
- java_library {
- name: "source-jar",
- srcs: [
- "a.java",
- ],
- }
- java_system_modules {
- name: "source-system-modules",
- libs: ["source-jar"],
- }
- java_library {
- name: "lib-with-prebuilt-system-modules",
- srcs: [
- "a.java",
- ],
- sdk_version: "none",
- system_modules: "prebuilt-system-modules",
- }
- java_import {
- name: "prebuilt-jar",
- jars: ["a.jar"],
- }
- java_system_modules_import {
- name: "prebuilt-system-modules",
- libs: ["prebuilt-jar"],
- }
- `)
- checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
- checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
- }
- func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
- javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
- bootClasspath := javacRule.Args["bootClasspath"]
- if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
- t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
- }
- }
- func TestAidlExportIncludeDirsFromImports(t *testing.T) {
- ctx, _ := testJava(t, `
- java_library {
- name: "foo",
- srcs: ["aidl/foo/IFoo.aidl"],
- libs: ["bar"],
- }
- java_import {
- name: "bar",
- jars: ["a.jar"],
- aidl: {
- export_include_dirs: ["aidl/bar"],
- },
- }
- `)
- aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
- expectedAidlFlag := "-Iaidl/bar"
- if !strings.Contains(aidlCommand, expectedAidlFlag) {
- t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
- }
- }
- func TestAidlFlagsArePassedToTheAidlCompiler(t *testing.T) {
- ctx, _ := testJava(t, `
- java_library {
- name: "foo",
- srcs: ["aidl/foo/IFoo.aidl"],
- aidl: { flags: ["-Werror"], },
- }
- `)
- aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
- expectedAidlFlag := "-Werror"
- if !strings.Contains(aidlCommand, expectedAidlFlag) {
- t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
- }
- }
- func TestDataNativeBinaries(t *testing.T) {
- ctx, _ := testJava(t, `
- java_test_host {
- name: "foo",
- srcs: ["a.java"],
- data_native_bins: ["bin"]
- }
- python_binary_host {
- name: "bin",
- srcs: ["bin.py"],
- }
- `)
- buildOS := android.BuildOs.String()
- test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
- entries := android.AndroidMkEntriesForTest(t, ctx, test)[0]
- expected := []string{"out/soong/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
- actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
- android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_COMPATIBILITY_SUPPORT_FILES", ctx.Config(), expected, actual)
- }
- func TestDefaultInstallable(t *testing.T) {
- ctx, _ := testJava(t, `
- java_test_host {
- name: "foo"
- }
- `)
- buildOS := android.BuildOs.String()
- module := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
- assertDeepEquals(t, "Default installable value should be true.", proptools.BoolPtr(true),
- module.properties.Installable)
- }
|