Adds parser for TextAlignConstant, which accepts both "friendly" names and the UGLY_ONES used in code. Horiz and Vert constant parsers retrofitted with the same friendliness. Patch by konstantin.scheglov@gmail.com Review by rjrjr@google.com http://gwt-code-reviews.appspot.com/612803 Review by: jgw@google.com git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@8347 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/uibinder/attributeparsers/AttributeParsers.java b/user/src/com/google/gwt/uibinder/attributeparsers/AttributeParsers.java index 5b0c9c3..fceb458 100644 --- a/user/src/com/google/gwt/uibinder/attributeparsers/AttributeParsers.java +++ b/user/src/com/google/gwt/uibinder/attributeparsers/AttributeParsers.java
@@ -19,8 +19,12 @@ import com.google.gwt.core.ext.typeinfo.JType; import com.google.gwt.core.ext.typeinfo.TypeOracle; import com.google.gwt.core.ext.typeinfo.TypeOracleException; +import com.google.gwt.dom.client.Style.Unit; import com.google.gwt.uibinder.rebind.FieldManager; import com.google.gwt.uibinder.rebind.MortalLogger; +import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant; +import com.google.gwt.user.client.ui.HasVerticalAlignment.VerticalAlignmentConstant; +import com.google.gwt.user.client.ui.TextBoxBase.TextAlignConstant; import java.util.HashMap; import java.util.Map; @@ -29,15 +33,14 @@ * Managers access to all implementations of {@link AttributeParser}. */ public class AttributeParsers { - private static final String VERT_CONSTANT = "com.google.gwt.user.client.ui.HasVerticalAlignment." - + "VerticalAlignmentConstant"; - private static final String HORIZ_CONSTANT = "com.google.gwt.user.client.ui.HasHorizontalAlignment." - + "HorizontalAlignmentConstant"; + private static final String HORIZ_CONSTANT = HorizontalAlignmentConstant.class.getCanonicalName(); + private static final String VERT_CONSTANT = VerticalAlignmentConstant.class.getCanonicalName(); + private static final String TEXT_ALIGN_CONSTANT = TextAlignConstant.class.getCanonicalName(); private static final String INT = "int"; private static final String STRING = String.class.getCanonicalName(); private static final String DOUBLE = "double"; private static final String BOOLEAN = "boolean"; - private static final String UNIT = "com.google.gwt.dom.client.Style.Unit"; + private static final String UNIT = Unit.class.getCanonicalName(); private final MortalLogger logger; private final FieldReferenceConverter converter; @@ -58,26 +61,29 @@ types.parse(BOOLEAN), logger); addAttributeParser(BOOLEAN, boolParser); addAttributeParser(Boolean.class.getCanonicalName(), boolParser); - + IntAttributeParser intParser = new IntAttributeParser(converter, types.parse(INT), logger); addAttributeParser(INT, intParser); addAttributeParser(Integer.class.getCanonicalName(), intParser); - + DoubleAttributeParser doubleParser = new DoubleAttributeParser(converter, types.parse(DOUBLE), logger); addAttributeParser(DOUBLE, doubleParser); addAttributeParser(Double.class.getCanonicalName(), doubleParser); - - addAttributeParser("int,int", new IntPairAttributeParser(intParser, logger)); - + + addAttributeParser("int,int", new IntPairAttributeParser(intParser, + logger)); + addAttributeParser(HORIZ_CONSTANT, new HorizontalAlignmentConstantParser( converter, types.parse(HORIZ_CONSTANT), logger)); addAttributeParser(VERT_CONSTANT, new VerticalAlignmentConstantParser( converter, types.parse(VERT_CONSTANT), logger)); - - addAttributeParser(STRING, new StringAttributeParser(converter, - types.parse(STRING))); + addAttributeParser(TEXT_ALIGN_CONSTANT, new TextAlignConstantParser( + converter, types.parse(TEXT_ALIGN_CONSTANT), logger)); + + addAttributeParser(STRING, + new StringAttributeParser(converter, types.parse(STRING))); EnumAttributeParser unitParser = new EnumAttributeParser(converter, (JEnumType) types.parse(UNIT), logger);
diff --git a/user/src/com/google/gwt/uibinder/attributeparsers/HorizontalAlignmentConstantParser.java b/user/src/com/google/gwt/uibinder/attributeparsers/HorizontalAlignmentConstantParser.java index dfa8a80..82b7056 100644 --- a/user/src/com/google/gwt/uibinder/attributeparsers/HorizontalAlignmentConstantParser.java +++ b/user/src/com/google/gwt/uibinder/attributeparsers/HorizontalAlignmentConstantParser.java
@@ -18,6 +18,7 @@ import com.google.gwt.core.ext.UnableToCompleteException; import com.google.gwt.core.ext.typeinfo.JType; import com.google.gwt.uibinder.rebind.MortalLogger; +import com.google.gwt.user.client.ui.HasHorizontalAlignment; import java.util.HashMap; @@ -28,15 +29,17 @@ */ class HorizontalAlignmentConstantParser extends StrictAttributeParser { + private static final String PREFIX = HasHorizontalAlignment.class.getCanonicalName() + + ".ALIGN_"; private static final HashMap<String, String> values = new HashMap<String, String>(); static { - values.put("ALIGN_LEFT", - "com.google.gwt.user.client.ui.HasHorizontalAlignment.ALIGN_LEFT"); - values.put("ALIGN_RIGHT", - "com.google.gwt.user.client.ui.HasHorizontalAlignment.ALIGN_RIGHT"); - values.put("ALIGN_CENTER", - "com.google.gwt.user.client.ui.HasHorizontalAlignment.ALIGN_CENTER"); + values.put("LEFT", PREFIX + "LEFT"); + values.put("CENTER", PREFIX + "CENTER"); + values.put("RIGHT", PREFIX + "RIGHT"); + values.put("ALIGN_LEFT", PREFIX + "LEFT"); + values.put("ALIGN_CENTER", PREFIX + "CENTER"); + values.put("ALIGN_RIGHT", PREFIX + "RIGHT"); } HorizontalAlignmentConstantParser(FieldReferenceConverter converter, @@ -44,8 +47,9 @@ super(converter, type, logger); } + @Override public String parse(String value) throws UnableToCompleteException { - String translated = values.get(value); + String translated = values.get(value.toUpperCase()); if (translated != null) { return translated; }
diff --git a/user/src/com/google/gwt/uibinder/attributeparsers/LengthAttributeParser.java b/user/src/com/google/gwt/uibinder/attributeparsers/LengthAttributeParser.java index 21743df..2e788dd 100644 --- a/user/src/com/google/gwt/uibinder/attributeparsers/LengthAttributeParser.java +++ b/user/src/com/google/gwt/uibinder/attributeparsers/LengthAttributeParser.java
@@ -16,6 +16,7 @@ package com.google.gwt.uibinder.attributeparsers; import com.google.gwt.core.ext.UnableToCompleteException; +import com.google.gwt.dom.client.Style.Unit; import com.google.gwt.uibinder.rebind.MortalLogger; import java.util.regex.Matcher; @@ -27,13 +28,12 @@ */ public class LengthAttributeParser implements AttributeParser { - static final String UNIT = "com.google.gwt.dom.client.Style.Unit"; + static final String UNIT = Unit.class.getCanonicalName(); // This regular expression matches CSS length patterns of the form // (value)(unit), where the two may be separated by whitespace. Either part // can be a {class.method} expression. - private static final Pattern pattern = Pattern.compile( - "((?:\\{[\\w\\.]+\\})|[\\d\\.]+)\\s*(\\{?[\\w\\.\\%]*\\}?)?"); + private static final Pattern pattern = Pattern.compile("((?:\\{[\\w\\.]+\\})|[\\d\\.]+)\\s*(\\{?[\\w\\.\\%]*\\}?)?"); private final MortalLogger logger; private final DoubleAttributeParser doubleParser;
diff --git a/user/src/com/google/gwt/uibinder/attributeparsers/TextAlignConstantParser.java b/user/src/com/google/gwt/uibinder/attributeparsers/TextAlignConstantParser.java new file mode 100644 index 0000000..735ef74 --- /dev/null +++ b/user/src/com/google/gwt/uibinder/attributeparsers/TextAlignConstantParser.java
@@ -0,0 +1,58 @@ +/* + * Copyright 2007 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.gwt.uibinder.attributeparsers; + +import com.google.gwt.core.ext.UnableToCompleteException; +import com.google.gwt.core.ext.typeinfo.JType; +import com.google.gwt.uibinder.rebind.MortalLogger; +import com.google.gwt.user.client.ui.TextBoxBase; + +import java.util.HashMap; + +/** + * Parses a {@link com.google.gwt.user.client.ui.TextBoxBase.TextAlignConstant}. + */ +class TextAlignConstantParser extends StrictAttributeParser { + + private static final String PREFIX = TextBoxBase.class.getCanonicalName() + + ".ALIGN_"; + private static final HashMap<String, String> values = new HashMap<String, String>(); + + static { + values.put("LEFT", PREFIX + "LEFT"); + values.put("CENTER", PREFIX + "CENTER"); + values.put("RIGHT", PREFIX + "RIGHT"); + values.put("JUSTIFY", PREFIX + "JUSTIFY"); + values.put("ALIGN_LEFT", PREFIX + "LEFT"); + values.put("ALIGN_CENTER", PREFIX + "CENTER"); + values.put("ALIGN_RIGHT", PREFIX + "RIGHT"); + values.put("ALIGN_JUSTIFY", PREFIX + "JUSTIFY"); + } + + TextAlignConstantParser(FieldReferenceConverter converter, JType type, + MortalLogger logger) { + super(converter, type, logger); + } + + @Override + public String parse(String value) throws UnableToCompleteException { + String translated = values.get(value.toUpperCase()); + if (translated != null) { + return translated; + } + return super.parse(value); + } +}
diff --git a/user/src/com/google/gwt/uibinder/attributeparsers/VerticalAlignmentConstantParser.java b/user/src/com/google/gwt/uibinder/attributeparsers/VerticalAlignmentConstantParser.java index 1beb2a6..66c3a21 100644 --- a/user/src/com/google/gwt/uibinder/attributeparsers/VerticalAlignmentConstantParser.java +++ b/user/src/com/google/gwt/uibinder/attributeparsers/VerticalAlignmentConstantParser.java
@@ -18,6 +18,7 @@ import com.google.gwt.core.ext.UnableToCompleteException; import com.google.gwt.core.ext.typeinfo.JType; import com.google.gwt.uibinder.rebind.MortalLogger; +import com.google.gwt.user.client.ui.HasVerticalAlignment; import java.util.HashMap; @@ -28,15 +29,17 @@ */ class VerticalAlignmentConstantParser extends StrictAttributeParser { + private static final String PREFIX = HasVerticalAlignment.class.getCanonicalName() + + ".ALIGN_"; private static final HashMap<String, String> values = new HashMap<String, String>(); static { - values.put("ALIGN_TOP", - "com.google.gwt.user.client.ui.HasVerticalAlignment.ALIGN_TOP"); - values.put("ALIGN_MIDDLE", - "com.google.gwt.user.client.ui.HasVerticalAlignment.ALIGN_MIDDLE"); - values.put("ALIGN_BOTTOM", - "com.google.gwt.user.client.ui.HasVerticalAlignment.ALIGN_BOTTOM"); + values.put("TOP", PREFIX + "TOP"); + values.put("MIDDLE", PREFIX + "MIDDLE"); + values.put("BOTTOM", PREFIX + "BOTTOM"); + values.put("ALIGN_TOP", PREFIX + "TOP"); + values.put("ALIGN_MIDDLE", PREFIX + "MIDDLE"); + values.put("ALIGN_BOTTOM", PREFIX + "BOTTOM"); } VerticalAlignmentConstantParser(FieldReferenceConverter converter, @@ -44,8 +47,9 @@ super(converter, type, logger); } + @Override public String parse(String value) throws UnableToCompleteException { - String translated = values.get(value); + String translated = values.get(value.toUpperCase()); if (translated != null) { return translated; }
diff --git a/user/src/com/google/gwt/user/client/ui/HasHorizontalAlignment.java b/user/src/com/google/gwt/user/client/ui/HasHorizontalAlignment.java index 04f3ba4..f0f54b3 100644 --- a/user/src/com/google/gwt/user/client/ui/HasHorizontalAlignment.java +++ b/user/src/com/google/gwt/user/client/ui/HasHorizontalAlignment.java
@@ -25,13 +25,18 @@ * <h3>Use in UiBinder Templates</h3> * * <p> - * The names of the static members of {@link HorizontalAlignmentConstant} - * can be used as values for a <code>horizontalAlignment</code> attribute - * of any widget that implements this interface. (In fact, this will work - * for any widget method that takes a single HorizontalAlignmentConstant value.) + * The names of the static members of {@link HorizontalAlignmentConstant}, as + * well as simple alignment names (<code>left</code>, <code>center</code>, + * <code>right</code>), can be used as values for a + * <code>horizontalAlignment</code> attribute of any widget that implements this + * interface. (In fact, this will work for any widget method that takes a single + * HorizontalAlignmentConstant value.) * <p> - * For example,<pre> + * For example, + * + * <pre> * <g:Label horizontalAlignment='ALIGN_RIGHT'>Hi there.</g:Label> + * <g:Label horizontalAlignment='right'>Hi there.</g:Label> * </pre> */ public interface HasHorizontalAlignment { @@ -40,7 +45,7 @@ * Horizontal alignment constant. */ public static class HorizontalAlignmentConstant { - private String textAlignString; + private final String textAlignString; private HorizontalAlignmentConstant(String textAlignString) { this.textAlignString = textAlignString; @@ -75,14 +80,13 @@ "right"); /** - * In a RTL layout, specifies that the widget's contents should be aligned - * to the right. In a LTR layout, specifies that the widget's constants - * should be aligned to the left. + * In a RTL layout, specifies that the widget's contents should be aligned to + * the right. In a LTR layout, specifies that the widget's constants should be + * aligned to the left. */ - HorizontalAlignmentConstant ALIGN_DEFAULT = - (GWT.isClient() && (LocaleInfo.getCurrentLocale().isRTL())) - ? ALIGN_RIGHT : ALIGN_LEFT; - + HorizontalAlignmentConstant ALIGN_DEFAULT = GWT.isClient() + && LocaleInfo.getCurrentLocale().isRTL() ? ALIGN_RIGHT : ALIGN_LEFT; + /** * Gets the horizontal alignment. *
diff --git a/user/src/com/google/gwt/user/client/ui/HasVerticalAlignment.java b/user/src/com/google/gwt/user/client/ui/HasVerticalAlignment.java index 430c393..5bc1545 100644 --- a/user/src/com/google/gwt/user/client/ui/HasVerticalAlignment.java +++ b/user/src/com/google/gwt/user/client/ui/HasVerticalAlignment.java
@@ -22,15 +22,18 @@ * <h3>Use in UiBinder Templates</h3> * * <p> - * The names of the static members of {@link VerticalAlignmentConstant} can - * be used as values for a <code>verticalAlignment</code> attribute of any - * widget that implements this interface. (In fact, this will work for any - * widget method that takes a single VerticalAlignmentConstant value.) + * The names of the static members of {@link VerticalAlignmentConstant}, as well + * as simple alignment names (<code>top</code>, <code>middle</code>, + * <code>bottom</code>), can be used as values for a + * <code>verticalAlignment</code> attribute of any widget that implements this + * interface. (In fact, this will work for any widget method that takes a single + * VerticalAlignmentConstant value.) * <p> * For example, * * <pre> * <g:VerticalPanel verticalAlignment='ALIGN_BOTTOM' /> + * <g:VerticalPanel verticalAlignment='bottom' /> * </pre> */ public interface HasVerticalAlignment { @@ -39,7 +42,7 @@ * Horizontal alignment constant. */ public static class VerticalAlignmentConstant { - private String verticalAlignString; + private final String verticalAlignString; private VerticalAlignmentConstant(String verticalAlignString) { this.verticalAlignString = verticalAlignString; @@ -59,19 +62,18 @@ * Specifies that the widget's contents should be aligned to the bottom. */ VerticalAlignmentConstant ALIGN_BOTTOM = new VerticalAlignmentConstant( - "bottom"); + "bottom"); /** * Specifies that the widget's contents should be aligned in the middle. */ VerticalAlignmentConstant ALIGN_MIDDLE = new VerticalAlignmentConstant( - "middle"); + "middle"); /** * Specifies that the widget's contents should be aligned to the top. */ - VerticalAlignmentConstant ALIGN_TOP = new VerticalAlignmentConstant( - "top"); + VerticalAlignmentConstant ALIGN_TOP = new VerticalAlignmentConstant("top"); /** * Gets the vertical alignment.
diff --git a/user/src/com/google/gwt/user/client/ui/ValueBoxBase.java b/user/src/com/google/gwt/user/client/ui/ValueBoxBase.java index a3445fd..413601c 100644 --- a/user/src/com/google/gwt/user/client/ui/ValueBoxBase.java +++ b/user/src/com/google/gwt/user/client/ui/ValueBoxBase.java
@@ -45,6 +45,21 @@ * <p> * Abstract base class for all text entry widgets. * + * <h3>Use in UiBinder Templates</h3> + * + * <p> + * The names of the static members of {@link TextBoxBase}, as well as simple + * alignment names (<code>left</code>, <code>center</code>, <code>right</code>, + * <code>justify</code>), can be used as values for a <code>textAlignment</code> + * attribute. + * <p> + * For example, + * + * <pre> + * <g:TextBox textAlignment='ALIGN_RIGHT'/> + * <g:TextBox textAlignment='right'/> + * </pre> + * * @param <T> the value type */ @SuppressWarnings("deprecation") @@ -54,7 +69,7 @@ private static TextBoxImpl impl = GWT.create(TextBoxImpl.class); - private AutoDirectionHandler autoDirHandler; + private final AutoDirectionHandler autoDirHandler; private final Parser<T> parser; private final Renderer<T> renderer; @@ -125,14 +140,14 @@ public Direction getDirection() { return BidiUtils.getDirectionOnElement(getElement()); } - + /** * Gets the direction estimation model of the auto-dir handler. */ public DirectionEstimator getDirectionEstimator() { return autoDirHandler.getDirectionEstimator(); } - + public String getName() { return DOM.getElementProperty(getElement(), "name"); } @@ -182,7 +197,7 @@ */ public T getValueOrThrow() throws ParseException { String text = getText().trim(); - + if ("".equals(text)) { return null; } @@ -254,21 +269,21 @@ public void setDirection(Direction direction) { BidiUtils.setDirectionOnElement(getElement(), direction); } - + /** * Toggles on / off direction estimation. */ public void setDirectionEstimator(boolean enabled) { autoDirHandler.setDirectionEstimator(enabled); } - + /** * Sets the direction estimation model of the auto-dir handler. */ public void setDirectionEstimator(DirectionEstimator directionEstimator) { autoDirHandler.setDirectionEstimator(directionEstimator); } - + /** * If a keyboard event is currently being handled by the text box, this method * replaces the unicode character or key code associated with it. This allows @@ -324,7 +339,7 @@ throw new IndexOutOfBoundsException( "Length must be a positive integer. Length: " + length); } - if ((pos < 0) || (length + pos > getText().length())) { + if (pos < 0 || length + pos > getText().length()) { throw new IndexOutOfBoundsException("From Index: " + pos + " To Index: " + (pos + length) + " Text Length: " + getText().length()); } @@ -371,7 +386,7 @@ protected TextBoxImpl getImpl() { return impl; } - + @Override protected void onLoad() { super.onLoad();
diff --git a/user/test/com/google/gwt/uibinder/UiBinderJreSuite.java b/user/test/com/google/gwt/uibinder/UiBinderJreSuite.java index 9058362..38e8cfd 100644 --- a/user/test/com/google/gwt/uibinder/UiBinderJreSuite.java +++ b/user/test/com/google/gwt/uibinder/UiBinderJreSuite.java
@@ -17,13 +17,14 @@ import com.google.gwt.uibinder.attributeparsers.CssNameConverterTest; import com.google.gwt.uibinder.attributeparsers.FieldReferenceConverterTest; -import com.google.gwt.uibinder.attributeparsers.HorizontalAlignmentConstantParser_Test; +import com.google.gwt.uibinder.attributeparsers.HorizontalAlignmentConstantParserTest; import com.google.gwt.uibinder.attributeparsers.IntAttributeParserTest; import com.google.gwt.uibinder.attributeparsers.IntPairAttributeParserTest; import com.google.gwt.uibinder.attributeparsers.LengthAttributeParserTest; import com.google.gwt.uibinder.attributeparsers.StrictAttributeParserTest; import com.google.gwt.uibinder.attributeparsers.StringAttributeParserTest; -import com.google.gwt.uibinder.attributeparsers.VerticalAlignmentConstantParser_Test; +import com.google.gwt.uibinder.attributeparsers.TextAlignConstantParserTest; +import com.google.gwt.uibinder.attributeparsers.VerticalAlignmentConstantParserTest; import com.google.gwt.uibinder.elementparsers.DialogBoxParserTest; import com.google.gwt.uibinder.elementparsers.DockLayoutPanelParserTest; import com.google.gwt.uibinder.elementparsers.ImageParserTest; @@ -72,8 +73,9 @@ suite.addTestSuite(StrictAttributeParserTest.class); suite.addTestSuite(StringAttributeParserTest.class); suite.addTestSuite(LengthAttributeParserTest.class); - suite.addTestSuite(HorizontalAlignmentConstantParser_Test.class); - suite.addTestSuite(VerticalAlignmentConstantParser_Test.class); + suite.addTestSuite(HorizontalAlignmentConstantParserTest.class); + suite.addTestSuite(VerticalAlignmentConstantParserTest.class); + suite.addTestSuite(TextAlignConstantParserTest.class); // elementparsers suite.addTestSuite(DialogBoxParserTest.class);
diff --git a/user/test/com/google/gwt/uibinder/attributeparsers/HorizontalAlignmentConstantParserTest.java b/user/test/com/google/gwt/uibinder/attributeparsers/HorizontalAlignmentConstantParserTest.java new file mode 100644 index 0000000..005c5cc --- /dev/null +++ b/user/test/com/google/gwt/uibinder/attributeparsers/HorizontalAlignmentConstantParserTest.java
@@ -0,0 +1,76 @@ +/* + * Copyright 2009 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.gwt.uibinder.attributeparsers; + +import com.google.gwt.core.ext.TreeLogger; +import com.google.gwt.core.ext.UnableToCompleteException; +import com.google.gwt.core.ext.typeinfo.TypeOracle; +import com.google.gwt.dev.javac.CompilationState; +import com.google.gwt.dev.javac.CompilationStateBuilder; +import com.google.gwt.uibinder.rebind.MortalLogger; +import com.google.gwt.uibinder.test.UiJavaResources; +import com.google.gwt.user.client.ui.HasHorizontalAlignment; +import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant; + +import junit.framework.TestCase; + +/** + * Test for {@link HorizontalAlignmentConstantParser}. + */ +public class HorizontalAlignmentConstantParserTest extends TestCase { + private static final String HHA = HasHorizontalAlignment.class.getCanonicalName(); + private static final String HAC = HorizontalAlignmentConstant.class.getCanonicalName(); + private HorizontalAlignmentConstantParser parser; + + @Override + public void setUp() throws Exception { + super.setUp(); + CompilationState state = CompilationStateBuilder.buildFrom(TreeLogger.NULL, + UiJavaResources.getUiResources()); + TypeOracle types = state.getTypeOracle(); + parser = new HorizontalAlignmentConstantParser(new FieldReferenceConverter( + null), types.parse(HAC), MortalLogger.NULL); + } + + public void testFriendlyNames() throws UnableToCompleteException { + assertEquals(HHA + ".ALIGN_LEFT", parser.parse("left")); + assertEquals(HHA + ".ALIGN_CENTER", parser.parse("center")); + assertEquals(HHA + ".ALIGN_RIGHT", parser.parse("right")); + // capitalized + assertEquals(HHA + ".ALIGN_LEFT", parser.parse("Left")); + assertEquals(HHA + ".ALIGN_CENTER", parser.parse("Center")); + assertEquals(HHA + ".ALIGN_RIGHT", parser.parse("Right")); + } + + public void testUglyNames() throws UnableToCompleteException { + assertEquals(HHA + ".ALIGN_LEFT", parser.parse("ALIGN_LEFT")); + assertEquals(HHA + ".ALIGN_CENTER", parser.parse("ALIGN_CENTER")); + assertEquals(HHA + ".ALIGN_RIGHT", parser.parse("ALIGN_RIGHT")); + } + + public void testBad() { + try { + parser.parse("fnord"); + fail("Expected UnableToCompleteException"); + } catch (UnableToCompleteException e) { + /* pass */ + } + } + + public void testFieldRef() throws UnableToCompleteException { + assertEquals("foo.bar().baz()", parser.parse("{foo.bar.baz}")); + } +}
diff --git a/user/test/com/google/gwt/uibinder/attributeparsers/TextAlignConstantParserTest.java b/user/test/com/google/gwt/uibinder/attributeparsers/TextAlignConstantParserTest.java new file mode 100644 index 0000000..a2b0d4a --- /dev/null +++ b/user/test/com/google/gwt/uibinder/attributeparsers/TextAlignConstantParserTest.java
@@ -0,0 +1,79 @@ +/* + * Copyright 2009 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.gwt.uibinder.attributeparsers; + +import com.google.gwt.core.ext.TreeLogger; +import com.google.gwt.core.ext.UnableToCompleteException; +import com.google.gwt.core.ext.typeinfo.TypeOracle; +import com.google.gwt.dev.javac.CompilationState; +import com.google.gwt.dev.javac.CompilationStateBuilder; +import com.google.gwt.uibinder.rebind.MortalLogger; +import com.google.gwt.uibinder.test.UiJavaResources; +import com.google.gwt.user.client.ui.TextBoxBase; +import com.google.gwt.user.client.ui.TextBoxBase.TextAlignConstant; + +import junit.framework.TestCase; + +/** + * Test for {@link TextAlignConstantParser}. + */ +public class TextAlignConstantParserTest extends TestCase { + private static final String TBB = TextBoxBase.class.getCanonicalName(); + private static final String TAC = TextAlignConstant.class.getCanonicalName(); + private TextAlignConstantParser parser; + + @Override + public void setUp() throws Exception { + super.setUp(); + CompilationState state = CompilationStateBuilder.buildFrom(TreeLogger.NULL, + UiJavaResources.getUiResources()); + TypeOracle types = state.getTypeOracle(); + parser = new TextAlignConstantParser(new FieldReferenceConverter(null), + types.parse(TAC), MortalLogger.NULL); + } + + public void testFriendlyNames() throws UnableToCompleteException { + assertEquals(TBB + ".ALIGN_LEFT", parser.parse("left")); + assertEquals(TBB + ".ALIGN_CENTER", parser.parse("center")); + assertEquals(TBB + ".ALIGN_RIGHT", parser.parse("right")); + assertEquals(TBB + ".ALIGN_JUSTIFY", parser.parse("justify")); + // capitalized + assertEquals(TBB + ".ALIGN_LEFT", parser.parse("Left")); + assertEquals(TBB + ".ALIGN_CENTER", parser.parse("Center")); + assertEquals(TBB + ".ALIGN_RIGHT", parser.parse("Right")); + assertEquals(TBB + ".ALIGN_JUSTIFY", parser.parse("Justify")); + } + + public void testUglyNames() throws UnableToCompleteException { + assertEquals(TBB + ".ALIGN_LEFT", parser.parse("ALIGN_LEFT")); + assertEquals(TBB + ".ALIGN_CENTER", parser.parse("ALIGN_CENTER")); + assertEquals(TBB + ".ALIGN_RIGHT", parser.parse("ALIGN_RIGHT")); + assertEquals(TBB + ".ALIGN_JUSTIFY", parser.parse("ALIGN_JUSTIFY")); + } + + public void testBad() { + try { + parser.parse("fnord"); + fail("Expected UnableToCompleteException"); + } catch (UnableToCompleteException e) { + /* pass */ + } + } + + public void testFieldRef() throws UnableToCompleteException { + assertEquals("foo.bar().baz()", parser.parse("{foo.bar.baz}")); + } +}
diff --git a/user/test/com/google/gwt/uibinder/attributeparsers/VerticalAlignmentConstantParserTest.java b/user/test/com/google/gwt/uibinder/attributeparsers/VerticalAlignmentConstantParserTest.java new file mode 100644 index 0000000..f4e9402 --- /dev/null +++ b/user/test/com/google/gwt/uibinder/attributeparsers/VerticalAlignmentConstantParserTest.java
@@ -0,0 +1,76 @@ +/* + * Copyright 2010 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.gwt.uibinder.attributeparsers; + +import com.google.gwt.core.ext.TreeLogger; +import com.google.gwt.core.ext.UnableToCompleteException; +import com.google.gwt.core.ext.typeinfo.TypeOracle; +import com.google.gwt.dev.javac.CompilationState; +import com.google.gwt.dev.javac.CompilationStateBuilder; +import com.google.gwt.uibinder.rebind.MortalLogger; +import com.google.gwt.uibinder.test.UiJavaResources; +import com.google.gwt.user.client.ui.HasVerticalAlignment; +import com.google.gwt.user.client.ui.HasVerticalAlignment.VerticalAlignmentConstant; + +import junit.framework.TestCase; + +/** + * Test for {@link VerticalAlignmentConstantParser}. + */ +public class VerticalAlignmentConstantParserTest extends TestCase { + private static final String HVA = HasVerticalAlignment.class.getCanonicalName(); + private static final String VAC = VerticalAlignmentConstant.class.getCanonicalName(); + private VerticalAlignmentConstantParser parser; + + @Override + public void setUp() throws Exception { + super.setUp(); + CompilationState state = CompilationStateBuilder.buildFrom(TreeLogger.NULL, + UiJavaResources.getUiResources()); + TypeOracle types = state.getTypeOracle(); + parser = new VerticalAlignmentConstantParser(new FieldReferenceConverter( + null), types.parse(VAC), MortalLogger.NULL); + } + + public void testFriendlyNames() throws UnableToCompleteException { + assertEquals(HVA + ".ALIGN_TOP", parser.parse("top")); + assertEquals(HVA + ".ALIGN_MIDDLE", parser.parse("middle")); + assertEquals(HVA + ".ALIGN_BOTTOM", parser.parse("bottom")); + // capitalized + assertEquals(HVA + ".ALIGN_TOP", parser.parse("Top")); + assertEquals(HVA + ".ALIGN_MIDDLE", parser.parse("Middle")); + assertEquals(HVA + ".ALIGN_BOTTOM", parser.parse("Bottom")); + } + + public void testUglyNames() throws UnableToCompleteException { + assertEquals(HVA + ".ALIGN_TOP", parser.parse("ALIGN_TOP")); + assertEquals(HVA + ".ALIGN_MIDDLE", parser.parse("ALIGN_MIDDLE")); + assertEquals(HVA + ".ALIGN_BOTTOM", parser.parse("ALIGN_BOTTOM")); + } + + public void testBad() { + try { + parser.parse("fnord"); + fail("Expected UnableToCompleteException"); + } catch (UnableToCompleteException e) { + /* pass */ + } + } + + public void testFieldRef() throws UnableToCompleteException { + assertEquals("foo.bar().baz()", parser.parse("{foo.bar.baz}")); + } +}
diff --git a/user/test/com/google/gwt/uibinder/test/UiJavaResources.java b/user/test/com/google/gwt/uibinder/test/UiJavaResources.java index c9e5c0e..c832935 100644 --- a/user/test/com/google/gwt/uibinder/test/UiJavaResources.java +++ b/user/test/com/google/gwt/uibinder/test/UiJavaResources.java
@@ -304,6 +304,19 @@ return code; } }; + public static final MockJavaResource TEXT_BOX_BASE = new MockJavaResource( + "com.google.gwt.user.client.ui.TextBoxBase") { + @Override + protected CharSequence getContent() { + StringBuffer code = new StringBuffer(); + code.append("package com.google.gwt.user.client.ui;\n"); + code.append("public class TextBoxBase {\n"); + code.append(" public static class TextAlignConstant {\n"); + code.append(" }\n"); + code.append("}\n"); + return code; + } + }; public static final MockJavaResource UI_BINDER = new MockJavaResource( "com.google.gwt.uibinder.client.UiBinder") { @Override @@ -380,6 +393,7 @@ rtn.add(STACK_LAYOUT_PANEL); rtn.add(STYLE); rtn.add(TAB_LAYOUT_PANEL); + rtn.add(TEXT_BOX_BASE); rtn.add(UI_OBJECT); rtn.add(UI_BINDER); rtn.add(UI_FACTORY);