| 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.io.File; |
| 26 |
import java.util.Arrays; |
| 27 |
import java.util.Collection; |
| 28 |
|
| 29 |
@RunWith(Parameterized.class) |
| 30 |
public class ValueResourceNameValidatorTest { |
| 31 |
|
| 32 |
public static File DUMMY_FILE = new File("DUMMY_FILE"); |
| 33 |
|
| 34 |
@Parameterized.Parameters(name="name=\"{0}\", resourceType={1}, file={2} gives error {3}") |
| 35 |
public static Collection<Object[]> expected() { |
| 36 |
return Arrays.asList(new Object[][] { |
| 37 |
//{ resourceName, resourceType, sourceFile, expectedException } |
| 38 |
{ "foo.png", ResourceType.DRAWABLE, DUMMY_FILE, null}, |
| 39 |
{ "foo.xml", ResourceType.DRAWABLE, DUMMY_FILE, null}, |
| 40 |
{ "foo.9.xml", ResourceType.DRAWABLE, DUMMY_FILE, null}, |
| 41 |
{ "foo", ResourceType.DRAWABLE, DUMMY_FILE, null}, |
| 42 |
{ "foo.png", ResourceType.DRAWABLE, DUMMY_FILE, null}, |
| 43 |
{ "foo.txt", ResourceType.RAW, DUMMY_FILE, null}, |
| 44 |
{ "foo.txt", ResourceType.DRAWABLE, DUMMY_FILE, null}, |
| 45 |
{ "foo.other.png", ResourceType.DRAWABLE, DUMMY_FILE, null}, |
| 46 |
{ "android:q", ResourceType.STRING, DUMMY_FILE, "':' is not a valid resource name character"}, |
| 47 |
{ "android:q", ResourceType.STRING, null, "':' is not a valid resource name character"}, |
| 48 |
{ "android:q", ResourceType.ATTR, DUMMY_FILE, null}, |
| 49 |
{ "foo.s_3", ResourceType.STRING, null, null}, |
| 50 |
{ "FOO$", ResourceType.STRING, null, null}, |
| 51 |
{ "1st", ResourceType.STRING, null, "The resource name must start with a letter"}, |
| 52 |
{ "Foo#", ResourceType.STRING, null, "'#' is not a valid resource name character"}, |
| 53 |
{ "void", ResourceType.STRING, null, "void is not a valid resource name (reserved Java keyword)"}, |
| 54 |
{ "", ResourceType.STRING, null, "The resource name shouldn't be empty" } |
| 55 |
}); |
| 56 |
} |
| 57 |
|
| 58 |
@Parameterized.Parameter |
| 59 |
public String mResourceName; |
| 60 |
|
| 61 |
@Parameterized.Parameter(value=1) |
| 62 |
public ResourceType mResourceType; |
| 63 |
|
| 64 |
@Parameterized.Parameter(value=2) |
| 65 |
public File mSourceFile; |
| 66 |
|
| 67 |
@Parameterized.Parameter(value=3) |
| 68 |
public String mExpectedErrorMessage; |
| 69 |
|
| 70 |
|
| 71 |
@Test |
| 72 |
public void validate() { |
| 73 |
String errorMessage = null; |
| 74 |
try { |
| 75 |
ValueResourceNameValidator.validate(mResourceName, mResourceType, mSourceFile); |
| 76 |
} catch (MergingException e) { |
| 77 |
errorMessage = e.getMessage(); |
| 78 |
} |
| 79 |
FileResourceNameValidatorTest.assertErrorMessageCorrect( |
| 80 |
mExpectedErrorMessage, errorMessage, mSourceFile); |
| 81 |
} |
| 82 |
|
| 83 |
} |