diff options
author | Tor Norbye <tnorbye@google.com> | 2012-11-07 16:23:19 -0800 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2012-11-07 16:23:19 -0800 |
commit | 922316822c7dbb9ad26f2cd2da858b38ab06e894 (patch) | |
tree | 186f827dbb84c41f8de42881ac5ae5c13a1c1294 /lint/libs | |
parent | cd42b54e35adc35dde7e8a02b1daa8f8aefd412e (diff) | |
parent | 991cc009e4ee9bc4d4f698894a64b01d9348b51e (diff) | |
download | sdk-922316822c7dbb9ad26f2cd2da858b38ab06e894.zip sdk-922316822c7dbb9ad26f2cd2da858b38ab06e894.tar.gz sdk-922316822c7dbb9ad26f2cd2da858b38ab06e894.tar.bz2 |
Merge "Make hardcoded text detector also check menu files"
Diffstat (limited to 'lint/libs')
2 files changed, 37 insertions, 4 deletions
diff --git a/lint/libs/lint_checks/src/com/android/tools/lint/checks/HardcodedValuesDetector.java b/lint/libs/lint_checks/src/com/android/tools/lint/checks/HardcodedValuesDetector.java index b7f60df..74522ba 100644 --- a/lint/libs/lint_checks/src/com/android/tools/lint/checks/HardcodedValuesDetector.java +++ b/lint/libs/lint_checks/src/com/android/tools/lint/checks/HardcodedValuesDetector.java @@ -22,8 +22,10 @@ import static com.android.SdkConstants.ATTR_HINT; import static com.android.SdkConstants.ATTR_LABEL; import static com.android.SdkConstants.ATTR_PROMPT; import static com.android.SdkConstants.ATTR_TEXT; +import static com.android.SdkConstants.ATTR_TITLE; import com.android.annotations.NonNull; +import com.android.resources.ResourceFolderType; import com.android.tools.lint.detector.api.Category; import com.android.tools.lint.detector.api.Issue; import com.android.tools.lint.detector.api.LayoutDetector; @@ -52,7 +54,10 @@ public class HardcodedValuesDetector extends LayoutDetector { "you have to repeat the actual text (and keep it up to date when making changes)\n" + "\n" + "* The application cannot be translated to other languages by just adding new " + - "translations for existing string resources.", + "translations for existing string resources.\n" + + "\n" + + "In Eclipse there is a quickfix to automatically extract this hardcoded string into " + + "a resource lookup.", Category.I18N, 5, @@ -74,15 +79,24 @@ public class HardcodedValuesDetector extends LayoutDetector { @Override public Collection<String> getApplicableAttributes() { return Arrays.asList( + // Layouts ATTR_TEXT, ATTR_CONTENT_DESCRIPTION, ATTR_HINT, ATTR_LABEL, - ATTR_PROMPT + ATTR_PROMPT, + + // Menus + ATTR_TITLE ); } @Override + public boolean appliesTo(@NonNull ResourceFolderType folderType) { + return folderType == ResourceFolderType.LAYOUT || folderType == ResourceFolderType.MENU; + } + + @Override public void visitAttribute(@NonNull XmlContext context, @NonNull Attr attribute) { String value = attribute.getValue(); if (value.length() > 0 && (value.charAt(0) != '@' && value.charAt(0) != '?')) { diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/HardcodedValuesDetectorTest.java b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/HardcodedValuesDetectorTest.java index 6bef3b7..b326fd4 100644 --- a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/HardcodedValuesDetectorTest.java +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/HardcodedValuesDetectorTest.java @@ -33,11 +33,30 @@ public class HardcodedValuesDetectorTest extends AbstractCheckTest { "res/layout/accessibility.xml:6: Warning: [I18N] Hardcoded string \"Button\", should use @string resource [HardcodedText]\n" + " <Button android:text=\"Button\" android:id=\"@+id/button2\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\"></Button>\n" + " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" + - "0 errors, 2 warnings\n" + - "", + "0 errors, 2 warnings\n", + lintFiles("res/layout/accessibility.xml")); } + public void testMenus() throws Exception { + assertEquals( + "res/menu/menu.xml:7: Warning: [I18N] Hardcoded string \"My title 1\", should use @string resource [HardcodedText]\n" + + " android:title=\"My title 1\">\n" + + " ~~~~~~~~~~~~~~~~~~~~~~~~~~\n" + + "res/menu/menu.xml:13: Warning: [I18N] Hardcoded string \"My title 2\", should use @string resource [HardcodedText]\n" + + " android:title=\"My title 2\">\n" + + " ~~~~~~~~~~~~~~~~~~~~~~~~~~\n" + + "0 errors, 2 warnings\n", + + lintFiles("res/menu/menu.xml")); + } + + public void testMenusOk() throws Exception { + assertEquals( + "No warnings.", + lintFiles("res/menu/titles.xml")); + } + public void testSuppress() throws Exception { // All but one errors in the file contain ignore attributes - direct, inherited // and lists |