Debian Codegraph @

sdk-common/src/test/java/com/android/ide/common/res2/PartialFileResourceNameValidatorTest.java

raw
1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.ide.common.res2;
18
19 import com.android.resources.ResourceType;
20
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.junit.runners.Parameterized;
24
25 import java.util.Arrays;
26 import java.util.Collection;
27
28 @RunWith(Parameterized.class)
29 public class PartialFileResourceNameValidatorTest {
30
31 @Parameterized.Parameters(name = "file={0}, resourceType={1} gives error {2}")
32 public static Collection<Object[]> expected() {
33 final String IS_NOT_A_VALID_ETC =
34 " is not a valid file-based resource name character: File-based resource names "
35 + "must contain only lowercase a-z, 0-9, or underscore";
36 final String THE_FILE_NAME_MUST_END_WITH_XML_OR_PNG =
37 "The file name must end with .xml or .png";
38 return Arrays.asList(new Object[][]{
39 //{ resourceName, resourceType, sourceFile, expectedException }
40 {"", ResourceType.ANIMATOR, "Resource must have a name"},
41 {"foo.png", ResourceType.DRAWABLE, null},
42 {"foo.xml", ResourceType.DRAWABLE, null},
43 {"foo.9.png", ResourceType.DRAWABLE, null},
44 {"foo.other.png", ResourceType.DRAWABLE, "'.'" + IS_NOT_A_VALID_ETC},
45 {"foo.xml", ResourceType.XML, null},
46 {"foo.png", ResourceType.XML, "The file name must end with .xml"},
47 {"foo.8.xml", ResourceType.DRAWABLE, "'.'" + IS_NOT_A_VALID_ETC},
48 {"foo", ResourceType.DRAWABLE, null},
49 {"foo.txt", ResourceType.RAW, null},
50 {"foo", ResourceType.RAW, null},
51 {"foo", ResourceType.RAW, null},
52 {"foo.txt", ResourceType.DRAWABLE, THE_FILE_NAME_MUST_END_WITH_XML_OR_PNG},
53 {"Foo.png", ResourceType.DRAWABLE, "'F'" + IS_NOT_A_VALID_ETC},
54 {"foo$.png", ResourceType.DRAWABLE, "'$'" + IS_NOT_A_VALID_ETC},
55 {"enum.png", ResourceType.DRAWABLE,
56 "enum is not a valid resource name (reserved Java keyword)"},
57 });
58 }
59
60 @Parameterized.Parameter(value = 0)
61 public String mSourceFileName;
62
63 @Parameterized.Parameter(value = 1)
64 public ResourceType mResourceType;
65
66 @Parameterized.Parameter(value = 2)
67 public String mExpectedErrorMessage;
68
69
70 @Test
71 public void validate() {
72 String actual =
73 FileResourceNameValidator.getErrorTextForPartialName(mSourceFileName, mResourceType);
74 FileResourceNameValidatorTest.assertErrorMessageCorrect(mExpectedErrorMessage, actual);
75 }
76 }