diff options
author | Tor Norbye <tnorbye@google.com> | 2012-07-27 12:24:56 -0700 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2012-07-27 12:24:56 -0700 |
commit | 2a65086ed8cc1fdace4f98e0931893e5cdce171d (patch) | |
tree | a3e0c90b65a262fa63db0f05b48c2d28be2c4ff8 /eclipse/plugins | |
parent | 3d1fd6f05a58463a8d56f7ba374240a093a061d0 (diff) | |
download | sdk-2a65086ed8cc1fdace4f98e0931893e5cdce171d.zip sdk-2a65086ed8cc1fdace4f98e0931893e5cdce171d.tar.gz sdk-2a65086ed8cc1fdace4f98e0931893e5cdce171d.tar.bz2 |
26501: Handle padding between image and text
The visual refactoring for converting a LinearLayout with a <TextView>
and an <ImageView> adjacent into a single <TextView> using a compound
drawable, needed to properly handle spacing between the two
widgets. This changeset updates it to convert margins between the
views into a single drawablePadding attribute.
Change-Id: Idb077e3324c279d41fdc0baa68008fe862d8181a
Diffstat (limited to 'eclipse/plugins')
13 files changed, 258 insertions, 49 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/LayoutConstants.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/LayoutConstants.java index 7801fbd..ec9c435 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/LayoutConstants.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/LayoutConstants.java @@ -86,6 +86,7 @@ public class LayoutConstants { public static final String ATTR_DRAWABLE_LEFT = "drawableLeft"; //$NON-NLS-1$ public static final String ATTR_DRAWABLE_BOTTOM = "drawableBottom"; //$NON-NLS-1$ public static final String ATTR_DRAWABLE_TOP = "drawableTop"; //$NON-NLS-1$ + public static final String ATTR_DRAWABLE_PADDING = "drawablePadding"; //$NON-NLS-1$ // RelativeLayout layout params: public static final String ATTR_LAYOUT_ALIGN_LEFT = "layout_alignLeft"; //$NON-NLS-1$ diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/LinearLayoutRule.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/LinearLayoutRule.java index a1571e2..0df2a09 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/LinearLayoutRule.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/LinearLayoutRule.java @@ -28,11 +28,11 @@ import static com.android.ide.common.layout.LayoutConstants.VALUE_HORIZONTAL; import static com.android.ide.common.layout.LayoutConstants.VALUE_VERTICAL; import static com.android.ide.common.layout.LayoutConstants.VALUE_WRAP_CONTENT; import static com.android.ide.common.layout.LayoutConstants.VALUE_ZERO_DP; +import static com.android.ide.eclipse.adt.AdtUtils.formatFloatAttribute; import static com.android.util.XmlUtils.ANDROID_URI; import com.android.annotations.NonNull; import com.android.annotations.Nullable; -import com.android.annotations.VisibleForTesting; import com.android.ide.common.api.DrawingStyle; import com.android.ide.common.api.DropFeedback; import com.android.ide.common.api.IClientRulesEngine; @@ -59,7 +59,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.Locale; import java.util.Map; /** @@ -1087,15 +1086,4 @@ public class LinearLayoutRule extends BaseLayoutRule { return sum; } - - @VisibleForTesting - static String formatFloatAttribute(float value) { - if (value != (int) value) { - // Run String.format without a locale, because we don't want locale-specific - // conversions here like separating the decimal part with a comma instead of a dot! - return String.format((Locale) null, "%.2f", value); //$NON-NLS-1$ - } else { - return Integer.toString((int) value); - } - } } diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/AdtUtils.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/AdtUtils.java index 2e58310..020ec82 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/AdtUtils.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/AdtUtils.java @@ -79,6 +79,7 @@ import java.net.URL; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Locale; /** Utility methods for ADT */ @@ -1059,4 +1060,21 @@ public class AdtUtils { folder.create(false, false, null); } } + + /** + * Format the given floating value into an XML string, omitting decimals if + * 0 + * + * @param value the value to be formatted + * @return the corresponding XML string for the value + */ + public static String formatFloatAttribute(float value) { + if (value != (int) value) { + // Run String.format without a locale, because we don't want locale-specific + // conversions here like separating the decimal part with a comma instead of a dot! + return String.format((Locale) null, "%.2f", value); //$NON-NLS-1$ + } else { + return Integer.toString((int) value); + } + } } diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/UseCompoundDrawableRefactoring.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/UseCompoundDrawableRefactoring.java index 453daa8..b169b19 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/UseCompoundDrawableRefactoring.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/UseCompoundDrawableRefactoring.java @@ -15,26 +15,34 @@ */ package com.android.ide.eclipse.adt.internal.editors.layout.refactoring; -import static com.android.util.XmlUtils.ANDROID_URI; import static com.android.ide.common.layout.LayoutConstants.ATTR_DRAWABLE_BOTTOM; import static com.android.ide.common.layout.LayoutConstants.ATTR_DRAWABLE_LEFT; +import static com.android.ide.common.layout.LayoutConstants.ATTR_DRAWABLE_PADDING; import static com.android.ide.common.layout.LayoutConstants.ATTR_DRAWABLE_RIGHT; import static com.android.ide.common.layout.LayoutConstants.ATTR_DRAWABLE_TOP; import static com.android.ide.common.layout.LayoutConstants.ATTR_GRAVITY; import static com.android.ide.common.layout.LayoutConstants.ATTR_ID; import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_HEIGHT; +import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_MARGIN_BOTTOM; +import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_MARGIN_LEFT; +import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_MARGIN_RIGHT; +import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_MARGIN_TOP; +import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_PREFIX; import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_WIDTH; import static com.android.ide.common.layout.LayoutConstants.ATTR_ORIENTATION; import static com.android.ide.common.layout.LayoutConstants.ATTR_SRC; -import static com.android.ide.common.layout.LayoutConstants.LAYOUT_PREFIX; import static com.android.ide.common.layout.LayoutConstants.LINEAR_LAYOUT; import static com.android.ide.common.layout.LayoutConstants.VALUE_VERTICAL; +import static com.android.ide.common.resources.ResourceResolver.PREFIX_RESOURCE_REF; import static com.android.ide.eclipse.adt.AdtConstants.EXT_XML; import static com.android.tools.lint.detector.api.LintConstants.IMAGE_VIEW; import static com.android.tools.lint.detector.api.LintConstants.TEXT_VIEW; +import static com.android.util.XmlUtils.ANDROID_URI; import com.android.annotations.NonNull; +import com.android.annotations.Nullable; import com.android.annotations.VisibleForTesting; +import com.android.ide.eclipse.adt.AdtUtils; import com.android.ide.eclipse.adt.internal.editors.formatting.XmlFormatPreferences; import com.android.ide.eclipse.adt.internal.editors.formatting.XmlFormatStyle; import com.android.ide.eclipse.adt.internal.editors.formatting.XmlPrettyPrinter; @@ -67,6 +75,8 @@ import org.w3c.dom.NamedNodeMap; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Converts a LinearLayout with exactly a TextView child and an ImageView child into @@ -234,14 +244,19 @@ public class UseCompoundDrawableRefactoring extends VisualRefactoring { // Luckily we just need to clone a single element, not a nested structure, so it's // easy enough to do this manually: Document tempDocument = DomUtilities.createEmptyDocument(); + if (tempDocument == null) { + return changes; + } Element newTextElement = tempDocument.createElement(text.getTagName()); tempDocument.appendChild(newTextElement); NamedNodeMap attributes = text.getAttributes(); for (int i = 0, n = attributes.getLength(); i < n; i++) { Attr attribute = (Attr) attributes.item(i); - if (attribute.getLocalName().startsWith(LAYOUT_PREFIX) - && ANDROID_URI.equals(attribute.getNamespaceURI())) { + String name = attribute.getLocalName(); + if (name.startsWith(ATTR_LAYOUT_PREFIX) + && ANDROID_URI.equals(attribute.getNamespaceURI()) + && !(name.equals(ATTR_LAYOUT_WIDTH) || name.equals(ATTR_LAYOUT_HEIGHT))) { // Ignore layout params: the parent layout is going away } else { newTextElement.setAttribute(attribute.getName(), attribute.getValue()); @@ -269,22 +284,40 @@ public class UseCompoundDrawableRefactoring extends VisualRefactoring { // Set the drawable String drawableAttribute; + // The space between the image and the text can have margins/padding, both + // from the text's perspective and from the image's perspective. We need to + // combine these. + String padding1 = null; + String padding2 = null; if (isVertical) { if (first == image) { drawableAttribute = ATTR_DRAWABLE_TOP; + padding1 = getPadding(image, ATTR_LAYOUT_MARGIN_BOTTOM); + padding2 = getPadding(text, ATTR_LAYOUT_MARGIN_TOP); } else { drawableAttribute = ATTR_DRAWABLE_BOTTOM; + padding1 = getPadding(text, ATTR_LAYOUT_MARGIN_BOTTOM); + padding2 = getPadding(image, ATTR_LAYOUT_MARGIN_TOP); } } else { if (first == image) { drawableAttribute = ATTR_DRAWABLE_LEFT; + padding1 = getPadding(image, ATTR_LAYOUT_MARGIN_RIGHT); + padding2 = getPadding(text, ATTR_LAYOUT_MARGIN_LEFT); } else { drawableAttribute = ATTR_DRAWABLE_RIGHT; + padding1 = getPadding(text, ATTR_LAYOUT_MARGIN_RIGHT); + padding2 = getPadding(image, ATTR_LAYOUT_MARGIN_LEFT); } } setAndroidAttribute(newTextElement, androidNsPrefix, drawableAttribute, src); + String padding = combine(padding1, padding2); + if (padding != null) { + setAndroidAttribute(newTextElement, androidNsPrefix, ATTR_DRAWABLE_PADDING, padding); + } + // If the removed LinearLayout is the root container, transfer its namespace // declaration to the TextView if (layout.getParentNode() instanceof Document) { @@ -343,6 +376,53 @@ public class UseCompoundDrawableRefactoring extends VisualRefactoring { return changes; } + @Nullable + private static String getPadding(@NonNull Element element, @NonNull String attribute) { + String padding = element.getAttributeNS(ANDROID_URI, attribute); + if (padding != null && padding.isEmpty()) { + padding = null; + } + return padding; + } + + @VisibleForTesting + @Nullable + static String combine(@Nullable String dimension1, @Nullable String dimension2) { + if (dimension1 == null || dimension1.isEmpty()) { + if (dimension2 != null && dimension2.isEmpty()) { + return null; + } + return dimension2; + } else if (dimension2 == null || dimension2.isEmpty()) { + if (dimension1 != null && dimension1.isEmpty()) { + return null; + } + return dimension1; + } else { + // Two dimensions are specified (e.g. marginRight for the left one and marginLeft + // for the right one); we have to add these together. We can only do that if + // they use the same units, and do not use resources. + if (dimension1.startsWith(PREFIX_RESOURCE_REF) + || dimension2.startsWith(PREFIX_RESOURCE_REF)) { + return null; + } + + Pattern p = Pattern.compile("([\\d\\.]+)(.+)"); //$NON-NLS-1$ + Matcher matcher1 = p.matcher(dimension1); + Matcher matcher2 = p.matcher(dimension2); + if (matcher1.matches() && matcher2.matches()) { + String unit = matcher1.group(2); + if (unit.equals(matcher2.group(2))) { + float value1 = Float.parseFloat(matcher1.group(1)); + float value2 = Float.parseFloat(matcher2.group(1)); + return AdtUtils.formatFloatAttribute(value1 + value2) + unit; + } + } + } + + return null; + } + /** * Sets an Android attribute (in the Android namespace) on an element * without a given namespace prefix. This is done when building a new Element diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/UseCompoundDrawableRefactoringTest.java b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/UseCompoundDrawableRefactoringTest.java index c1a5ca4..e946327 100644 --- a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/UseCompoundDrawableRefactoringTest.java +++ b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/UseCompoundDrawableRefactoringTest.java @@ -15,6 +15,8 @@ */ package com.android.ide.eclipse.adt.internal.editors.layout.refactoring; +import static com.android.ide.eclipse.adt.internal.editors.layout.refactoring.UseCompoundDrawableRefactoring.combine; + import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.ltk.core.refactoring.Change; @@ -24,6 +26,22 @@ import java.util.List; @SuppressWarnings("javadoc") public class UseCompoundDrawableRefactoringTest extends RefactoringTest { + public void testCombine() throws Exception { + assertNull(combine(null, null)); + assertNull(combine("@dimen/foo", "@dimen/bar")); + assertNull(combine("@dimen/foo", "@dimen/bar")); + assertNull(combine("1sp", "@dimen/bar")); + assertNull(combine("1sp", "2dp")); + assertNull(combine(null, "")); + assertNull(combine("", null)); + + assertEquals("@dimen/foo", combine(null, "@dimen/foo")); + assertEquals("@dimen/foo", combine("@dimen/foo", null)); + assertEquals("5sp", combine("5sp", null)); + + assertEquals("10sp", combine("8sp", "2sp")); + assertEquals("50dp", combine("30dp", "20dp")); + } public void test1() throws Exception { // Test converting an image above a text view @@ -62,6 +80,16 @@ public class UseCompoundDrawableRefactoringTest extends RefactoringTest { checkRefactoring("refactoring/usecompound/compound5.xml", "@+id/layout"); } + public void test8() throws Exception { + // Test padding handling + checkRefactoring("refactoring/usecompound/compound6.xml", "@+id/layout1"); + } + + public void test9() throws Exception { + // Test margin combination + checkRefactoring("refactoring/usecompound/compound7.xml", "@+id/layout1"); + } + private void checkRefactoring(String basename, String id) throws Exception { IFile file = getLayoutFile(getProject(), basename); TestContext info = setupTestContext(file, basename); diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/refactoring/usecompound/compound6-expected-8.xml b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/refactoring/usecompound/compound6-expected-8.xml new file mode 100644 index 0000000..1ef5d78 --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/refactoring/usecompound/compound6-expected-8.xml @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + android:orientation="vertical" > + + <TextView + android:id="@+id/TextView1" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:drawableLeft="@drawable/ic_launcher" + android:drawablePadding="@android:dimen/thumbnail_width" + android:gravity="center" + android:text="Hello World" > + </TextView> + +</LinearLayout>
\ No newline at end of file diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/refactoring/usecompound/compound6.info b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/refactoring/usecompound/compound6.info new file mode 100644 index 0000000..4f0aea5 --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/refactoring/usecompound/compound6.info @@ -0,0 +1,4 @@ +android.widget.LinearLayout [0,121,800,480] <LinearLayout> + android.widget.LinearLayout [0,0,109,97] <LinearLayout> + android.widget.ImageView [18,0,90,72] <ImageView> + android.widget.TextView [0,72,109,97] <TextView> diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/refactoring/usecompound/compound6.xml b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/refactoring/usecompound/compound6.xml new file mode 100644 index 0000000..94a363b --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/refactoring/usecompound/compound6.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + android:orientation="vertical" > + + <LinearLayout + android:id="@+id/layout1" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:gravity="center" > + + <ImageView + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginRight="@android:dimen/thumbnail_width" + android:src="@drawable/ic_launcher" /> + + <TextView + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="Hello World" /> + </LinearLayout> + +</LinearLayout> diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/refactoring/usecompound/compound7-expected-9.xml b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/refactoring/usecompound/compound7-expected-9.xml new file mode 100644 index 0000000..1b2a864 --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/refactoring/usecompound/compound7-expected-9.xml @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + android:orientation="vertical" > + + <TextView + android:id="@+id/TextView1" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:drawablePadding="35dp" + android:drawableTop="@drawable/ic_launcher" + android:gravity="center" + android:text="Hello World" > + </TextView> + +</LinearLayout>
\ No newline at end of file diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/refactoring/usecompound/compound7.info b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/refactoring/usecompound/compound7.info new file mode 100644 index 0000000..4f0aea5 --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/refactoring/usecompound/compound7.info @@ -0,0 +1,4 @@ +android.widget.LinearLayout [0,121,800,480] <LinearLayout> + android.widget.LinearLayout [0,0,109,97] <LinearLayout> + android.widget.ImageView [18,0,90,72] <ImageView> + android.widget.TextView [0,72,109,97] <TextView> diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/refactoring/usecompound/compound7.xml b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/refactoring/usecompound/compound7.xml new file mode 100644 index 0000000..46393fe --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/refactoring/usecompound/compound7.xml @@ -0,0 +1,27 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + android:orientation="vertical" > + + <LinearLayout + android:id="@+id/layout1" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:gravity="center" + android:orientation="vertical" > + + <ImageView + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginBottom="10dp" + android:src="@drawable/ic_launcher" /> + + <TextView + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginTop="25dp" + android:text="Hello World" /> + </LinearLayout> + +</LinearLayout> diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/common/layout/LinearLayoutRuleTest.java b/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/common/layout/LinearLayoutRuleTest.java index 638a384..cfccb70 100644 --- a/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/common/layout/LinearLayoutRuleTest.java +++ b/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/common/layout/LinearLayoutRuleTest.java @@ -16,13 +16,13 @@ package com.android.ide.common.layout; -import static com.android.util.XmlUtils.ANDROID_URI; import static com.android.ide.common.layout.LayoutConstants.ATTR_ID; import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_HEIGHT; import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_WIDTH; import static com.android.ide.common.layout.LayoutConstants.ATTR_ORIENTATION; import static com.android.ide.common.layout.LayoutConstants.VALUE_HORIZONTAL; import static com.android.ide.common.layout.LayoutConstants.VALUE_VERTICAL; +import static com.android.util.XmlUtils.ANDROID_URI; import com.android.ide.common.api.DropFeedback; import com.android.ide.common.api.IAttributeInfo.Format; @@ -39,7 +39,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.Locale; /** Test the {@link LinearLayoutRule} */ public class LinearLayoutRuleTest extends LayoutTestBase { @@ -461,36 +460,6 @@ public class LinearLayoutRuleTest extends LayoutTestBase { "useStyle(DROP_PREVIEW), drawRect(0,381,100,461)"); } - public void testFormatFloatValue() throws Exception { - assertEquals("1", LinearLayoutRule.formatFloatAttribute(1.0f)); - assertEquals("2", LinearLayoutRule.formatFloatAttribute(2.0f)); - assertEquals("1.50", LinearLayoutRule.formatFloatAttribute(1.5f)); - assertEquals("1.50", LinearLayoutRule.formatFloatAttribute(1.50f)); - assertEquals("1.51", LinearLayoutRule.formatFloatAttribute(1.51f)); - assertEquals("1.51", LinearLayoutRule.formatFloatAttribute(1.514542f)); - assertEquals("1.52", LinearLayoutRule.formatFloatAttribute(1.516542f)); - assertEquals("-1.51", LinearLayoutRule.formatFloatAttribute(-1.51f)); - assertEquals("-1", LinearLayoutRule.formatFloatAttribute(-1f)); - } - - public void testFormatFloatValueLocale() throws Exception { - // Ensure that the layout float values aren't affected by - // locale settings, like using commas instead of of periods - Locale originalDefaultLocale = Locale.getDefault(); - - try { - Locale.setDefault(Locale.FRENCH); - - // Ensure that this is a locale which uses a comma instead of a period: - assertEquals("5,24", String.format("%.2f", 5.236f)); - - // Ensure that the formatFloatAttribute is immune - assertEquals("1.50", LinearLayoutRule.formatFloatAttribute(1.5f)); - } finally { - Locale.setDefault(originalDefaultLocale); - } - } - // Left to test: // Check inserting at last pos with multiple children // Check inserting with no bounds rectangle for dragged element diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/AdtUtilsTest.java b/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/AdtUtilsTest.java index b89d56a..b2b6787 100644 --- a/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/AdtUtilsTest.java +++ b/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/AdtUtilsTest.java @@ -18,6 +18,7 @@ package com.android.ide.eclipse.adt; import com.google.common.collect.Iterables; import java.util.Arrays; +import java.util.Locale; import junit.framework.TestCase; @@ -171,4 +172,34 @@ public class AdtUtilsTest extends TestCase { assertTrue(Arrays.equals(new String[] { "C:\\foo", "/bar" }, Iterables.toArray(AdtUtils.splitPath("C:\\foo:/bar"), String.class))); } + + public void testFormatFloatValue() throws Exception { + assertEquals("1", AdtUtils.formatFloatAttribute(1.0f)); + assertEquals("2", AdtUtils.formatFloatAttribute(2.0f)); + assertEquals("1.50", AdtUtils.formatFloatAttribute(1.5f)); + assertEquals("1.50", AdtUtils.formatFloatAttribute(1.50f)); + assertEquals("1.51", AdtUtils.formatFloatAttribute(1.51f)); + assertEquals("1.51", AdtUtils.formatFloatAttribute(1.514542f)); + assertEquals("1.52", AdtUtils.formatFloatAttribute(1.516542f)); + assertEquals("-1.51", AdtUtils.formatFloatAttribute(-1.51f)); + assertEquals("-1", AdtUtils.formatFloatAttribute(-1f)); + } + + public void testFormatFloatValueLocale() throws Exception { + // Ensure that the layout float values aren't affected by + // locale settings, like using commas instead of of periods + Locale originalDefaultLocale = Locale.getDefault(); + + try { + Locale.setDefault(Locale.FRENCH); + + // Ensure that this is a locale which uses a comma instead of a period: + assertEquals("5,24", String.format("%.2f", 5.236f)); + + // Ensure that the formatFloatAttribute is immune + assertEquals("1.50", AdtUtils.formatFloatAttribute(1.5f)); + } finally { + Locale.setDefault(originalDefaultLocale); + } + } } |