Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "reflect" |
| 20 | "testing" |
| 21 | ) |
| 22 | |
| 23 | var ( |
| 24 | resourceFiles = []string{ |
| 25 | "res/layout/layout.xml", |
| 26 | "res/values/strings.xml", |
| 27 | "res/values-en-rUS/strings.xml", |
| 28 | } |
| 29 | |
| 30 | compiledResourceFiles = []string{ |
| 31 | "aapt2/res/layout_layout.xml.flat", |
| 32 | "aapt2/res/values_strings.arsc.flat", |
| 33 | "aapt2/res/values-en-rUS_strings.arsc.flat", |
| 34 | } |
| 35 | ) |
| 36 | |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame^] | 37 | func testAppContext(config android.Config, bp string, fs map[string][]byte) *android.TestContext { |
| 38 | appFS := map[string][]byte{} |
| 39 | for k, v := range fs { |
| 40 | appFS[k] = v |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame^] | 43 | for _, file := range resourceFiles { |
| 44 | appFS[file] = nil |
| 45 | } |
| 46 | |
| 47 | return testContext(config, bp, appFS) |
| 48 | } |
| 49 | |
| 50 | func testApp(t *testing.T, bp string) *android.TestContext { |
| 51 | config := testConfig(nil) |
| 52 | |
| 53 | ctx := testAppContext(config, bp, nil) |
| 54 | |
| 55 | run(t, ctx, config) |
| 56 | |
| 57 | return ctx |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | func TestApp(t *testing.T) { |
| 61 | ctx := testApp(t, ` |
| 62 | android_app { |
| 63 | name: "foo", |
| 64 | srcs: ["a.java"], |
| 65 | } |
| 66 | `) |
| 67 | |
| 68 | foo := ctx.ModuleForTests("foo", "android_common") |
| 69 | |
| 70 | expectedLinkImplicits := []string{"AndroidManifest.xml"} |
| 71 | |
| 72 | frameworkRes := ctx.ModuleForTests("framework-res", "android_common") |
| 73 | expectedLinkImplicits = append(expectedLinkImplicits, |
| 74 | frameworkRes.Output("package-res.apk").Output.String()) |
| 75 | |
| 76 | // Test the mapping from input files to compiled output file names |
| 77 | compile := foo.Output(compiledResourceFiles[0]) |
| 78 | if !reflect.DeepEqual(resourceFiles, compile.Inputs.Strings()) { |
| 79 | t.Errorf("expected aapt2 compile inputs expected:\n %#v\n got:\n %#v", |
| 80 | resourceFiles, compile.Inputs.Strings()) |
| 81 | } |
| 82 | expectedLinkImplicits = append(expectedLinkImplicits, compile.Outputs.Strings()...) |
| 83 | |
| 84 | list := foo.Output("aapt2/res.list") |
| 85 | expectedLinkImplicits = append(expectedLinkImplicits, list.Output.String()) |
| 86 | |
| 87 | // Check that the link rule uses |
| 88 | res := ctx.ModuleForTests("foo", "android_common").Output("package-res.apk") |
| 89 | if !reflect.DeepEqual(expectedLinkImplicits, res.Implicits.Strings()) { |
| 90 | t.Errorf("expected aapt2 link implicits expected:\n %#v\n got:\n %#v", |
| 91 | expectedLinkImplicits, res.Implicits.Strings()) |
| 92 | } |
| 93 | } |