diff options
author | Tor Norbye <tnorbye@google.com> | 2012-04-28 08:50:50 -0700 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2012-04-28 08:50:50 -0700 |
commit | 69b77687c995b37b4dce05fc7d9b64e2faae6891 (patch) | |
tree | c3575c49989d248fe0230f5b8d5f0f916e69facd /lint/libs | |
parent | 169b1b34c6d359994486bf99645bcc262a568dfb (diff) | |
download | sdk-69b77687c995b37b4dce05fc7d9b64e2faae6891.zip sdk-69b77687c995b37b4dce05fc7d9b64e2faae6891.tar.gz sdk-69b77687c995b37b4dce05fc7d9b64e2faae6891.tar.bz2 |
Fix nested linear layout weights lint check
See issue
http://code.google.com/p/android/issues/detail?id=22889
Change-Id: Ifd27764c0bb3360e52abb3334bdbfef820d231f7
Diffstat (limited to 'lint/libs')
4 files changed, 53 insertions, 11 deletions
diff --git a/lint/libs/lint_checks/src/com/android/tools/lint/checks/InefficientWeightDetector.java b/lint/libs/lint_checks/src/com/android/tools/lint/checks/InefficientWeightDetector.java index 04cb9b6..84216d1 100644 --- a/lint/libs/lint_checks/src/com/android/tools/lint/checks/InefficientWeightDetector.java +++ b/lint/libs/lint_checks/src/com/android/tools/lint/checks/InefficientWeightDetector.java @@ -40,7 +40,7 @@ import org.w3c.dom.Node; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; +import java.util.IdentityHashMap; import java.util.List; import java.util.Map; @@ -93,7 +93,7 @@ public class InefficientWeightDetector extends LayoutDetector { * Map from element to whether that element has a non-zero linear layout * weight or has an ancestor which does */ - private Map<Node, Boolean> mInsideWeight = new HashMap<Node, Boolean>(); + private Map<Node, Boolean> mInsideWeight = new IdentityHashMap<Node, Boolean>(); /** Constructs a new {@link InefficientWeightDetector} */ public InefficientWeightDetector() { @@ -116,7 +116,6 @@ public class InefficientWeightDetector extends LayoutDetector { boolean multipleWeights = false; Element weightChild = null; boolean checkNesting = context.isEnabled(NESTED_WEIGHTS); - Node parent = element.getParentNode(); for (Element child : children) { if (child.hasAttributeNS(ANDROID_URI, ATTR_LAYOUT_WEIGHT)) { if (weightChild != null) { @@ -127,11 +126,11 @@ public class InefficientWeightDetector extends LayoutDetector { } if (checkNesting) { - mInsideWeight.put(element, Boolean.TRUE); + mInsideWeight.put(child, Boolean.TRUE); - Boolean inside = mInsideWeight.get(parent); + Boolean inside = mInsideWeight.get(element); if (inside == null) { - mInsideWeight.put(parent, Boolean.FALSE); + mInsideWeight.put(element, Boolean.FALSE); } else if (inside) { Attr sizeNode = child.getAttributeNodeNS(ANDROID_URI, ATTR_LAYOUT_WEIGHT); context.report(NESTED_WEIGHTS, sizeNode, diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/InefficientWeightDetectorTest.java b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/InefficientWeightDetectorTest.java index ed820c6..c48658f 100644 --- a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/InefficientWeightDetectorTest.java +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/InefficientWeightDetectorTest.java @@ -28,16 +28,13 @@ public class InefficientWeightDetectorTest extends AbstractCheckTest { public void testWeights() throws Exception { assertEquals( "inefficient_weight.xml:10: Warning: Use a layout_width of 0dip instead of match_parent for better performance\n" + - "inefficient_weight.xml:24: Warning: Use a layout_height of 0dip instead of wrap_content for better performance\n" + - "inefficient_weight.xml:25: Warning: Nested weights are bad for performance\n" + - "inefficient_weight.xml:40: Warning: Nested weights are bad for performance", + "inefficient_weight.xml:24: Warning: Use a layout_height of 0dip instead of wrap_content for better performance", lintFiles("res/layout/inefficient_weight.xml")); } public void testWeights2() throws Exception { assertEquals( - "nested_weights.xml:22: Warning: Nested weights are bad for performance\n" + - "nested_weights.xml:28: Warning: Use a layout_width of 0dip instead of match_parent for better performance", + "nested_weights.xml:23: Warning: Nested weights are bad for performance", lintFiles("res/layout/nested_weights.xml")); } @@ -68,5 +65,12 @@ public class InefficientWeightDetectorTest extends AbstractCheckTest { lintFiles("res/layout/inefficient_weight2.xml")); } + public void testNestedWeights() throws Exception { + // Regression test for http://code.google.com/p/android/issues/detail?id=22889 + // (Comment 8) + assertEquals( + "No warnings.", + lintFiles("res/layout/nested_weights2.xml")); + } } diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/nested_weights.xml b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/nested_weights.xml index fda1e3e..a375e06 100644 --- a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/nested_weights.xml +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/nested_weights.xml @@ -12,6 +12,7 @@ <LinearLayout android:id="@+id/linearLayout1" + android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" > diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/nested_weights2.xml b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/nested_weights2.xml new file mode 100644 index 0000000..7002b89 --- /dev/null +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/nested_weights2.xml @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="utf-8"?> +<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="fill_parent" + android:layout_height="fill_parent" > + + <LinearLayout + android:layout_width="fill_parent" + android:layout_height="fill_parent" + android:orientation="vertical" > + + <LinearLayout + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:orientation="horizontal" > + + <ImageView + android:layout_width="32dp" + android:layout_height="32dp" + android:layout_gravity="center_vertical" + android:src="@drawable/launcher_icon" /> + + <TextView + android:layout_width="0dp" + android:layout_height="fill_parent" + android:layout_gravity="center_vertical" + android:layout_weight="1" + android:text="test" /> + </LinearLayout> + + <LinearLayout + android:layout_width="fill_parent" + android:layout_weight="1" + android:layout_height="0dp" + android:orientation="vertical" > + </LinearLayout> + </LinearLayout> + +</FrameLayout> |