aboutsummaryrefslogtreecommitdiffstats
path: root/lint/libs
diff options
context:
space:
mode:
authorTor Norbye <tnorbye@google.com>2012-11-07 16:02:37 -0800
committerTor Norbye <tnorbye@google.com>2012-11-07 16:03:55 -0800
commit991cc009e4ee9bc4d4f698894a64b01d9348b51e (patch)
tree983ba9d6f72fd6f8722c4571ad9498103cd9b91c /lint/libs
parente3023aa8af4e93f3ecc4c929153721c185551829 (diff)
downloadsdk-991cc009e4ee9bc4d4f698894a64b01d9348b51e.zip
sdk-991cc009e4ee9bc4d4f698894a64b01d9348b51e.tar.gz
sdk-991cc009e4ee9bc4d4f698894a64b01d9348b51e.tar.bz2
Make hardcoded text detector also check menu files
Change-Id: I1b11d5e6650434fd5e57af05ff86b2d66a0a84db
Diffstat (limited to 'lint/libs')
-rw-r--r--lint/libs/lint_checks/src/com/android/tools/lint/checks/HardcodedValuesDetector.java18
-rw-r--r--lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/HardcodedValuesDetectorTest.java23
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