xref: /aosp_15_r20/external/bazelbuild-rules_android/src/tools/ak/res/path_test.go (revision 9e965d6fece27a77de5377433c2f7e6999b8cc0b)
1// Copyright 2018 The Bazel Authors. 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
15package res
16
17import (
18	"reflect"
19	"testing"
20)
21
22func TestParsePath(t *testing.T) {
23	tests := []struct {
24		arg  string
25		want PathInfo
26	}{
27		{
28			"/tmp/foobar/values/strings.xml",
29			PathInfo{
30				Path:    "/tmp/foobar/values/strings.xml",
31				Type:    ValueType,
32				TypeDir: "values",
33				ResDir:  "/tmp/foobar",
34			},
35		},
36		{
37			"/tmp/foobar/values-v19/strings.xml",
38			PathInfo{
39				Path:      "/tmp/foobar/values-v19/strings.xml",
40				Type:      ValueType,
41				TypeDir:   "values-v19",
42				ResDir:    "/tmp/foobar",
43				Qualifier: "v19",
44			},
45		},
46		{
47			"/tmp/baz/foobar/layout-es-419/main_activity.xml",
48			PathInfo{
49				Path:      "/tmp/baz/foobar/layout-es-419/main_activity.xml",
50				Type:      Layout,
51				TypeDir:   "layout-es-419",
52				ResDir:    "/tmp/baz/foobar",
53				Qualifier: "es-419",
54			},
55		},
56		{
57			"/tmp/baz/foobar/menu/menu_data.xml",
58			PathInfo{
59				Path:    "/tmp/baz/foobar/menu/menu_data.xml",
60				Type:    Menu,
61				TypeDir: "menu",
62				ResDir:  "/tmp/baz/foobar",
63			},
64		},
65		{
66			"tmp/baz/foobar/drawable-en-ldpi/mercury.png",
67			PathInfo{
68				Path:      "tmp/baz/foobar/drawable-en-ldpi/mercury.png",
69				Type:      Drawable,
70				TypeDir:   "drawable-en-ldpi",
71				ResDir:    "tmp/baz/foobar",
72				Qualifier: "en-ldpi",
73				Density:   120,
74			},
75		},
76		{
77			"tmp/baz/foobar/drawable-fr-mdpi-nokeys/mars.xml",
78			PathInfo{
79				Path:      "tmp/baz/foobar/drawable-fr-mdpi-nokeys/mars.xml",
80				Type:      Drawable,
81				TypeDir:   "drawable-fr-mdpi-nokeys",
82				ResDir:    "tmp/baz/foobar",
83				Qualifier: "fr-mdpi-nokeys",
84				Density:   160,
85			},
86		},
87
88		{
89			"tmp/baz/foobar/drawable-mcc310-en-rUS-tvdpi/venus.jpg",
90			PathInfo{
91				Path:      "tmp/baz/foobar/drawable-mcc310-en-rUS-tvdpi/venus.jpg",
92				Type:      Drawable,
93				TypeDir:   "drawable-mcc310-en-rUS-tvdpi",
94				ResDir:    "tmp/baz/foobar",
95				Qualifier: "mcc310-en-rUS-tvdpi",
96				Density:   213,
97			},
98		},
99		{
100			"tmp/baz/foobar/drawable-mcc208-mnc00-fr-rCA-hdpi-12key-dpad/earth.gif",
101			PathInfo{
102				Path:      "tmp/baz/foobar/drawable-mcc208-mnc00-fr-rCA-hdpi-12key-dpad/earth.gif",
103				Type:      Drawable,
104				TypeDir:   "drawable-mcc208-mnc00-fr-rCA-hdpi-12key-dpad",
105				ResDir:    "tmp/baz/foobar",
106				Qualifier: "mcc208-mnc00-fr-rCA-hdpi-12key-dpad",
107				Density:   240,
108			},
109		},
110		{
111			"tmp/baz/foobar/drawable-xhdpi/neptune.jpg",
112			PathInfo{
113				Path:      "tmp/baz/foobar/drawable-xhdpi/neptune.jpg",
114				Type:      Drawable,
115				TypeDir:   "drawable-xhdpi",
116				ResDir:    "tmp/baz/foobar",
117				Qualifier: "xhdpi",
118				Density:   320,
119			},
120		},
121		{
122			"tmp/baz/foobar/drawable-xxhdpi/uranus.png",
123			PathInfo{
124				Path:      "tmp/baz/foobar/drawable-xxhdpi/uranus.png",
125				Type:      Drawable,
126				TypeDir:   "drawable-xxhdpi",
127				ResDir:    "tmp/baz/foobar",
128				Qualifier: "xxhdpi",
129				Density:   480,
130			},
131		},
132		{
133			"tmp/baz/foobar/drawable-xxxhdpi/saturn.xml",
134			PathInfo{
135				Path:      "tmp/baz/foobar/drawable-xxxhdpi/saturn.xml",
136				Type:      Drawable,
137				TypeDir:   "drawable-xxxhdpi",
138				ResDir:    "tmp/baz/foobar",
139				Qualifier: "xxxhdpi",
140				Density:   640,
141			},
142		},
143		{
144			"tmp/baz/foobar/drawable-anydpi/jupiter.png",
145			PathInfo{
146				Path:      "tmp/baz/foobar/drawable-anydpi/jupiter.png",
147				Type:      Drawable,
148				TypeDir:   "drawable-anydpi",
149				ResDir:    "tmp/baz/foobar",
150				Qualifier: "anydpi",
151				Density:   AnyDPI,
152			},
153		},
154		{
155			"tmp/baz/foobar/drawable-nodpi/sun.gif",
156			PathInfo{
157				Path:      "tmp/baz/foobar/drawable-nodpi/sun.gif",
158				Type:      Drawable,
159				TypeDir:   "drawable-nodpi",
160				ResDir:    "tmp/baz/foobar",
161				Qualifier: "nodpi",
162				Density:   NoDPI,
163			},
164		},
165		{
166			"tmp/baz/foobar/drawable-120dpi/moon.xml",
167			PathInfo{
168				Path:      "tmp/baz/foobar/drawable-120dpi/moon.xml",
169				Type:      Drawable,
170				TypeDir:   "drawable-120dpi",
171				ResDir:    "tmp/baz/foobar",
172				Qualifier: "120dpi",
173				Density:   120,
174			},
175		},
176	}
177	for _, tc := range tests {
178		got, err := ParsePath(tc.arg)
179		if err != nil {
180			t.Errorf("ParsePath(%s): got err: %s", tc.arg, err)
181			continue
182		}
183		if !reflect.DeepEqual(got, tc.want) {
184			t.Errorf("ParsePath(%s): got %+v want: %+v", tc.arg, got, tc.want)
185		}
186	}
187}
188
189func TestParsePath_NegativeCases(t *testing.T) {
190	tests := []struct {
191		arg string
192		err error
193	}{
194		{"/foo/bar/baz/strings.xml", ErrNotResPath},
195		{"strings.xml", ErrNotResPath},
196	}
197	for _, tc := range tests {
198		got, err := ParsePath(tc.arg)
199		if err == nil {
200			t.Errorf("ParsePath(%s): got: %+v and nil err, want err: %v", tc.arg, got, tc.err)
201		}
202		if err != tc.err {
203			t.Errorf("ParsePath(%s): got err: %v want err: %v", tc.arg, err, tc.err)
204		}
205	}
206}
207
208func TestMakePathInfo(t *testing.T) {
209	paths := []string{
210		"/tmp/foobar/values/strings.xml",
211		"/tmp/foobar/values-v19/strings.xml",
212		"/tmp/foobar/values-v19/.skip_me.xml",
213		"/tmp/baz/foobar/menu/menu_data.xml",
214		"tmp/baz/foobar/drawable-en-ldpi/mercury.png",
215		"/tmp/foobar/values-v19/.skip_me_as_well.xml",
216	}
217	want := []*PathInfo{
218		&PathInfo{
219			Path:    "/tmp/foobar/values/strings.xml",
220			Type:    ValueType,
221			TypeDir: "values",
222			ResDir:  "/tmp/foobar"},
223		&PathInfo{
224			Path:      "/tmp/foobar/values-v19/strings.xml",
225			Type:      ValueType,
226			TypeDir:   "values-v19",
227			ResDir:    "/tmp/foobar",
228			Qualifier: "v19"},
229		&PathInfo{
230			Path:    "/tmp/baz/foobar/menu/menu_data.xml",
231			Type:    Menu,
232			TypeDir: "menu",
233			ResDir:  "/tmp/baz/foobar"},
234		&PathInfo{
235			Path:      "tmp/baz/foobar/drawable-en-ldpi/mercury.png",
236			Type:      Drawable,
237			TypeDir:   "drawable-en-ldpi",
238			ResDir:    "tmp/baz/foobar",
239			Qualifier: "en-ldpi",
240			Density:   120},
241	}
242	pInfos, err := MakePathInfos(paths)
243	if err != nil {
244		t.Fatalf("MakePathInfos unexpected error: %v", err)
245	}
246	if !reflect.DeepEqual(pInfos, want) {
247		t.Errorf("MakePathInfos: got %+v want: %+v", pInfos, want)
248	}
249}
250