xref: /aosp_15_r20/external/bazelbuild-rules_android/src/tools/ak/res/struct_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	"fmt"
19	"strings"
20	"testing"
21)
22
23func TestKinds(t *testing.T) {
24	tests := []struct {
25		t Type
26		k Kind
27	}{
28		{t: String, k: Value},
29		{t: XML, k: NonValue},
30		{t: Drawable, k: NonValue},
31		{t: Color, k: Both},
32		{t: Menu, k: NonValue},
33		{t: Dimen, k: Value},
34		{t: UnknownType, k: Unknown},
35	}
36	for _, tc := range tests {
37		if tc.t.Kind() != tc.k {
38			t.Errorf("%v.Kind() = %v want: %v", tc.t, tc.t.Kind(), tc.k)
39		}
40	}
41	for _, at := range AllTypes {
42		if at == UnknownType {
43			continue
44		}
45		if at.Kind() == Unknown {
46			t.Errorf("%v.Kind() = %v - wanting anything else but that", at, Unknown)
47		}
48	}
49}
50
51func TestTypes(t *testing.T) {
52	tests := []struct {
53		t Type
54		s string
55	}{
56		{t: String, s: "string"},
57		{t: XML, s: "xml"},
58		{t: Drawable, s: "drawable"},
59		{t: Color, s: "color"},
60		{t: Menu, s: "menu"},
61		{t: Dimen, s: "dimen"},
62	}
63
64	for _, tc := range tests {
65		pt, err := ParseType(tc.s)
66		if tc.t != pt || err != nil {
67			t.Errorf("ParseType(%s): %v, %v want: %v", tc.s, pt, err, tc.t)
68		}
69	}
70}
71
72func TestDensities(t *testing.T) {
73	tests := []struct {
74		arg  string
75		want Density
76		err  error
77	}{
78		{arg: "tvdpi", want: TVDPI},
79		{arg: "hdpi", want: HDPI},
80		{arg: "320dpi", want: 320},
81		{arg: "nodpi", want: NoDPI},
82		{arg: "en-US", want: UnspecifiedDensity},
83		{arg: "12000000dpi", err: fmt.Errorf("%ddpi: unparsable", 12000000)},
84	}
85
86	for _, tc := range tests {
87		got, err := ParseDensity(tc.arg)
88		if tc.err == nil && err != nil {
89			t.Errorf("ParseDensity(%s): got err: %s", tc.arg, err)
90		}
91		if tc.err != nil && err != nil && !strings.HasPrefix(err.Error(), tc.err.Error()) {
92			t.Errorf("ParseDensity(%s): got err: %v want err: %v", tc.arg, err, tc.err)
93		}
94
95		if got != tc.want {
96			t.Errorf("ParseDensity(%s): Got: %v want: %v", tc.arg, got, tc.want)
97		}
98	}
99}
100