| Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 1 | // Copyright 2020 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 android | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "reflect" | 
|  | 19 | "testing" | 
|  | 20 | ) | 
|  | 21 |  | 
|  | 22 | func Test_mergeApexVariations(t *testing.T) { | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 23 | const ( | 
|  | 24 | ForPrebuiltApex    = true | 
|  | 25 | NotForPrebuiltApex = false | 
|  | 26 | ) | 
| Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 27 | tests := []struct { | 
|  | 28 | name        string | 
|  | 29 | in          []ApexInfo | 
|  | 30 | wantMerged  []ApexInfo | 
|  | 31 | wantAliases [][2]string | 
|  | 32 | }{ | 
|  | 33 | { | 
|  | 34 | name: "single", | 
|  | 35 | in: []ApexInfo{ | 
| Spandan Das | e8173a8 | 2023-04-12 17:14:11 +0000 | [diff] [blame] | 36 | {"foo", FutureApiLevel, false, false, []string{"foo"}, []string{"foo"}, nil, NotForPrebuiltApex, nil}, | 
| Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 37 | }, | 
|  | 38 | wantMerged: []ApexInfo{ | 
| Spandan Das | e8173a8 | 2023-04-12 17:14:11 +0000 | [diff] [blame] | 39 | {"apex10000", FutureApiLevel, false, false, []string{"foo"}, []string{"foo"}, nil, NotForPrebuiltApex, nil}, | 
| Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 40 | }, | 
|  | 41 | wantAliases: [][2]string{ | 
|  | 42 | {"foo", "apex10000"}, | 
|  | 43 | }, | 
|  | 44 | }, | 
|  | 45 | { | 
|  | 46 | name: "merge", | 
|  | 47 | in: []ApexInfo{ | 
| Spandan Das | e8173a8 | 2023-04-12 17:14:11 +0000 | [diff] [blame] | 48 | {"foo", FutureApiLevel, false, false, []string{"foo"}, []string{"foo"}, nil, NotForPrebuiltApex, nil}, | 
|  | 49 | {"bar", FutureApiLevel, false, false, []string{"bar"}, []string{"bar"}, nil, NotForPrebuiltApex, nil}, | 
| Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 50 | }, | 
|  | 51 | wantMerged: []ApexInfo{ | 
| Spandan Das | e8173a8 | 2023-04-12 17:14:11 +0000 | [diff] [blame] | 52 | {"apex10000", FutureApiLevel, false, false, []string{"bar", "foo"}, []string{"bar", "foo"}, nil, false, nil}}, | 
| Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 53 | wantAliases: [][2]string{ | 
| Liz Kammer | 7eed538 | 2022-04-21 11:13:45 -0400 | [diff] [blame] | 54 | {"bar", "apex10000"}, | 
|  | 55 | {"foo", "apex10000"}, | 
| Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 56 | }, | 
|  | 57 | }, | 
|  | 58 | { | 
|  | 59 | name: "don't merge version", | 
|  | 60 | in: []ApexInfo{ | 
| Spandan Das | e8173a8 | 2023-04-12 17:14:11 +0000 | [diff] [blame] | 61 | {"foo", FutureApiLevel, false, false, []string{"foo"}, []string{"foo"}, nil, NotForPrebuiltApex, nil}, | 
|  | 62 | {"bar", uncheckedFinalApiLevel(30), false, false, []string{"bar"}, []string{"bar"}, nil, NotForPrebuiltApex, nil}, | 
| Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 63 | }, | 
|  | 64 | wantMerged: []ApexInfo{ | 
| Spandan Das | e8173a8 | 2023-04-12 17:14:11 +0000 | [diff] [blame] | 65 | {"apex30", uncheckedFinalApiLevel(30), false, false, []string{"bar"}, []string{"bar"}, nil, NotForPrebuiltApex, nil}, | 
|  | 66 | {"apex10000", FutureApiLevel, false, false, []string{"foo"}, []string{"foo"}, nil, NotForPrebuiltApex, nil}, | 
| Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 67 | }, | 
|  | 68 | wantAliases: [][2]string{ | 
|  | 69 | {"bar", "apex30"}, | 
|  | 70 | {"foo", "apex10000"}, | 
|  | 71 | }, | 
|  | 72 | }, | 
|  | 73 | { | 
|  | 74 | name: "merge updatable", | 
|  | 75 | in: []ApexInfo{ | 
| Spandan Das | e8173a8 | 2023-04-12 17:14:11 +0000 | [diff] [blame] | 76 | {"foo", FutureApiLevel, false, false, []string{"foo"}, []string{"foo"}, nil, NotForPrebuiltApex, nil}, | 
|  | 77 | {"bar", FutureApiLevel, true, false, []string{"bar"}, []string{"bar"}, nil, NotForPrebuiltApex, nil}, | 
| Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 78 | }, | 
|  | 79 | wantMerged: []ApexInfo{ | 
| Spandan Das | e8173a8 | 2023-04-12 17:14:11 +0000 | [diff] [blame] | 80 | {"apex10000", FutureApiLevel, true, false, []string{"bar", "foo"}, []string{"bar", "foo"}, nil, NotForPrebuiltApex, nil}, | 
| Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 81 | }, | 
|  | 82 | wantAliases: [][2]string{ | 
|  | 83 | {"bar", "apex10000"}, | 
|  | 84 | {"foo", "apex10000"}, | 
|  | 85 | }, | 
|  | 86 | }, | 
|  | 87 | { | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 88 | name: "don't merge when for prebuilt_apex", | 
|  | 89 | in: []ApexInfo{ | 
| Spandan Das | e8173a8 | 2023-04-12 17:14:11 +0000 | [diff] [blame] | 90 | {"foo", FutureApiLevel, false, false, []string{"foo"}, []string{"foo"}, nil, NotForPrebuiltApex, nil}, | 
|  | 91 | {"bar", FutureApiLevel, true, false, []string{"bar"}, []string{"bar"}, nil, NotForPrebuiltApex, nil}, | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 92 | // This one should not be merged in with the others because it is for | 
|  | 93 | // a prebuilt_apex. | 
| Spandan Das | e8173a8 | 2023-04-12 17:14:11 +0000 | [diff] [blame] | 94 | {"baz", FutureApiLevel, true, false, []string{"baz"}, []string{"baz"}, nil, ForPrebuiltApex, nil}, | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 95 | }, | 
|  | 96 | wantMerged: []ApexInfo{ | 
| Spandan Das | e8173a8 | 2023-04-12 17:14:11 +0000 | [diff] [blame] | 97 | {"apex10000", FutureApiLevel, true, false, []string{"bar", "foo"}, []string{"bar", "foo"}, nil, NotForPrebuiltApex, nil}, | 
|  | 98 | {"baz", FutureApiLevel, true, false, []string{"baz"}, []string{"baz"}, nil, ForPrebuiltApex, nil}, | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 99 | }, | 
|  | 100 | wantAliases: [][2]string{ | 
|  | 101 | {"bar", "apex10000"}, | 
|  | 102 | {"foo", "apex10000"}, | 
|  | 103 | }, | 
|  | 104 | }, | 
| Jiyong Park | 9477c26 | 2021-06-22 20:23:05 +0900 | [diff] [blame] | 105 | { | 
| Jiyong Park | d4a0063 | 2022-04-12 12:23:20 +0900 | [diff] [blame] | 106 | name: "merge different UsePlatformApis but don't allow using platform api", | 
| Jiyong Park | 9477c26 | 2021-06-22 20:23:05 +0900 | [diff] [blame] | 107 | in: []ApexInfo{ | 
| Spandan Das | e8173a8 | 2023-04-12 17:14:11 +0000 | [diff] [blame] | 108 | {"foo", FutureApiLevel, false, false, []string{"foo"}, []string{"foo"}, nil, NotForPrebuiltApex, nil}, | 
|  | 109 | {"bar", FutureApiLevel, false, true, []string{"bar"}, []string{"bar"}, nil, NotForPrebuiltApex, nil}, | 
| Jiyong Park | 9477c26 | 2021-06-22 20:23:05 +0900 | [diff] [blame] | 110 | }, | 
|  | 111 | wantMerged: []ApexInfo{ | 
| Spandan Das | e8173a8 | 2023-04-12 17:14:11 +0000 | [diff] [blame] | 112 | {"apex10000", FutureApiLevel, false, false, []string{"bar", "foo"}, []string{"bar", "foo"}, nil, NotForPrebuiltApex, nil}, | 
| Jiyong Park | 9477c26 | 2021-06-22 20:23:05 +0900 | [diff] [blame] | 113 | }, | 
|  | 114 | wantAliases: [][2]string{ | 
| Jiyong Park | d4a0063 | 2022-04-12 12:23:20 +0900 | [diff] [blame] | 115 | {"bar", "apex10000"}, | 
|  | 116 | {"foo", "apex10000"}, | 
|  | 117 | }, | 
|  | 118 | }, | 
|  | 119 | { | 
|  | 120 | name: "merge same UsePlatformApis and allow using platform api", | 
|  | 121 | in: []ApexInfo{ | 
| Spandan Das | e8173a8 | 2023-04-12 17:14:11 +0000 | [diff] [blame] | 122 | {"foo", FutureApiLevel, false, true, []string{"foo"}, []string{"foo"}, nil, NotForPrebuiltApex, nil}, | 
|  | 123 | {"bar", FutureApiLevel, false, true, []string{"bar"}, []string{"bar"}, nil, NotForPrebuiltApex, nil}, | 
| Jiyong Park | d4a0063 | 2022-04-12 12:23:20 +0900 | [diff] [blame] | 124 | }, | 
|  | 125 | wantMerged: []ApexInfo{ | 
| Spandan Das | e8173a8 | 2023-04-12 17:14:11 +0000 | [diff] [blame] | 126 | {"apex10000", FutureApiLevel, false, true, []string{"bar", "foo"}, []string{"bar", "foo"}, nil, NotForPrebuiltApex, nil}, | 
| Jiyong Park | d4a0063 | 2022-04-12 12:23:20 +0900 | [diff] [blame] | 127 | }, | 
|  | 128 | wantAliases: [][2]string{ | 
|  | 129 | {"bar", "apex10000"}, | 
| Jiyong Park | 9477c26 | 2021-06-22 20:23:05 +0900 | [diff] [blame] | 130 | {"foo", "apex10000"}, | 
|  | 131 | }, | 
|  | 132 | }, | 
| Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 133 | } | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 134 |  | 
| Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 135 | for _, tt := range tests { | 
|  | 136 | t.Run(tt.name, func(t *testing.T) { | 
| Paul Duffin | d210afa | 2021-03-16 17:31:20 +0000 | [diff] [blame] | 137 | config := TestConfig(t.TempDir(), nil, "", nil) | 
| Colin Cross | 9f720ce | 2020-10-02 10:26:04 -0700 | [diff] [blame] | 138 | ctx := &configErrorWrapper{config: config} | 
|  | 139 | gotMerged, gotAliases := mergeApexVariations(ctx, tt.in) | 
| Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 140 | if !reflect.DeepEqual(gotMerged, tt.wantMerged) { | 
|  | 141 | t.Errorf("mergeApexVariations() gotMerged = %v, want %v", gotMerged, tt.wantMerged) | 
|  | 142 | } | 
|  | 143 | if !reflect.DeepEqual(gotAliases, tt.wantAliases) { | 
|  | 144 | t.Errorf("mergeApexVariations() gotAliases = %v, want %v", gotAliases, tt.wantAliases) | 
|  | 145 | } | 
|  | 146 | }) | 
|  | 147 | } | 
|  | 148 | } |