diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2009-03-03 19:31:44 -0800 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2009-03-03 19:31:44 -0800 |
commit | 9066cfe9886ac131c34d59ed0e2d287b0e3c0087 (patch) | |
tree | d88beb88001f2482911e3d28e43833b50e4b4e97 /tools/layoutlib/bridge/tests | |
parent | d83a98f4ce9cfa908f5c54bbd70f03eec07e7553 (diff) | |
download | frameworks_base-9066cfe9886ac131c34d59ed0e2d287b0e3c0087.zip frameworks_base-9066cfe9886ac131c34d59ed0e2d287b0e3c0087.tar.gz frameworks_base-9066cfe9886ac131c34d59ed0e2d287b0e3c0087.tar.bz2 |
auto import from //depot/cupcake/@135843
Diffstat (limited to 'tools/layoutlib/bridge/tests')
7 files changed, 596 insertions, 0 deletions
diff --git a/tools/layoutlib/bridge/tests/com/android/layoutlib/bridge/AndroidGraphicsTests.java b/tools/layoutlib/bridge/tests/com/android/layoutlib/bridge/AndroidGraphicsTests.java new file mode 100644 index 0000000..ac144e7 --- /dev/null +++ b/tools/layoutlib/bridge/tests/com/android/layoutlib/bridge/AndroidGraphicsTests.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * 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.android.layoutlib.bridge; + +import android.graphics.Matrix; +import android.graphics.Paint; +import android.graphics._Original_Paint; +import android.text.TextPaint; + +import junit.framework.TestCase; + +/** + * + */ +public class AndroidGraphicsTests extends TestCase { + + @Override + protected void setUp() throws Exception { + super.setUp(); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testMatrix() { + Matrix m1 = new Matrix(); + + assertFalse(m1.isIdentity()); + + m1.setValues(new float[] { 1,0,0, 0,1,0, 0,0,1 }); + assertTrue(m1.isIdentity()); + + Matrix m2 = new Matrix(m1); + + float[] v1 = new float[9]; + float[] v2 = new float[9]; + m1.getValues(v1); + m2.getValues(v2); + + for (int i = 0 ; i < 9; i++) { + assertEquals(v1[i], v2[i]); + } + } + + public void testPaint() { + _Original_Paint o = new _Original_Paint(); + assertNotNull(o); + + Paint p = new Paint(); + assertNotNull(p); + } + + public void textTextPaint() { + TextPaint p = new TextPaint(); + assertNotNull(p); + } +} diff --git a/tools/layoutlib/bridge/tests/com/android/layoutlib/bridge/BridgeTest.java b/tools/layoutlib/bridge/tests/com/android/layoutlib/bridge/BridgeTest.java new file mode 100644 index 0000000..e424f1d --- /dev/null +++ b/tools/layoutlib/bridge/tests/com/android/layoutlib/bridge/BridgeTest.java @@ -0,0 +1,229 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * 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.android.layoutlib.bridge; + +import com.android.layoutlib.api.ILayoutResult; +import com.android.layoutlib.api.IResourceValue; +import com.android.layoutlib.api.IStyleResourceValue; +import com.android.layoutlib.api.IXmlPullParser; +import com.android.layoutlib.api.ILayoutResult.ILayoutViewInfo; + +import org.kxml2.io.KXmlParser; +import org.xmlpull.v1.XmlPullParser; + +import java.io.File; +import java.io.FileReader; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + +import junit.framework.TestCase; + +public class BridgeTest extends TestCase { + + /** the class being tested */ + private Bridge mBridge; + /** the path to the sample layout.xml file */ + private String mLayoutXml1Path; + private String mTextOnlyXmlPath; + + @Override + protected void setUp() throws Exception { + super.setUp(); + + mBridge = new Bridge(); + + // FIXME: need some fonts somewhere. + mBridge.init(null /* fontOsLocation */, getAttributeValues()); + + URL url = this.getClass().getClassLoader().getResource("data/layout1.xml"); + mLayoutXml1Path = url.getFile(); + + url = this.getClass().getClassLoader().getResource("data/textonly.xml"); + mTextOnlyXmlPath = url.getFile(); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + } + + // --------------- + + /** + * Test parser that implements {@link IXmlPullParser}. + */ + private static class TestParser extends KXmlParser implements IXmlPullParser { + public Object getViewKey() { + return null; + } + } + + public void testComputeLayout() throws Exception { + + TestParser parser = new TestParser(); + parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); + parser.setInput(new FileReader(new File(mLayoutXml1Path))); + + Map<String, Map<String, IResourceValue>> projectResources = getProjectResources(); + + Map<String, Map<String, IResourceValue>> frameworkResources = getFrameworkResources(); + + int screenWidth = 320; + int screenHeight = 480; + + // FIXME need a dummy font for the tests! + ILayoutResult result = mBridge.computeLayout(parser, new Integer(1) /* projectKey */, + screenWidth, screenHeight, + "Theme", projectResources, frameworkResources, null, null); + + display(result.getRootView(), ""); + } + + private Map<String, Map<String, Integer>> getAttributeValues() { + Map<String, Map<String, Integer>> attributeValues = + new HashMap<String, Map<String,Integer>>(); + + // lets create a map for the orientation attribute + Map<String, Integer> attributeMap = new HashMap<String, Integer>(); + + attributeMap.put("horizontal", Integer.valueOf(0)); + attributeMap.put("vertical", Integer.valueOf(1)); + + attributeValues.put("orientation", attributeMap); + + return attributeValues; + } + + private Map<String, Map<String, IResourceValue>> getFrameworkResources() { + Map<String, Map<String, IResourceValue>> frameworkResources = + new HashMap<String, Map<String, IResourceValue>>(); + + // create the style map + Map<String, IResourceValue> styleMap = new HashMap<String, IResourceValue>(); + frameworkResources.put("style", styleMap); + + // create a button style. + IStyleResourceValue style = createStyle("Widget.Button", + "background", "@android:drawable/something", + "focusable", "true", + "clickable", "true", + "textAppearance", "?android:attr/textAppearanceSmallInverse", + "textColor", "?android:attr/textColorBrightInverseNoDisable", + "gravity", "center_vertical|center_horizontal" + ); + styleMap.put(style.getName(), style); + + // create the parent style of button style + style = createStyle("Widget", + "textAppearance", "?textAppearance"); + styleMap.put(style.getName(), style); + + // link the buttonStyle info in the default theme. + style = createStyle("Theme", + BridgeConstants.RES_STYLE, "buttonStyle", "@android:style/Widget.Button", + BridgeConstants.RES_STYLE, "textAppearance", "@android:style/TextAppearance", + BridgeConstants.RES_STYLE, "textAppearanceSmallInverse", "@android:style/TextAppearance.Small.Inverse", + BridgeConstants.RES_COLOR, "textColorBrightInverseNoDisable", "@android:color/bright_text_light_nodisable" + ); + styleMap.put(style.getName(), style); + + // create a dummy drawable to go with it + Map<String, IResourceValue> drawableMap = new HashMap<String, IResourceValue>(); + frameworkResources.put("drawable", drawableMap); + + // get the 9 patch test location + URL url = this.getClass().getClassLoader().getResource("data/button.9.png"); + + IResourceValue drawable = new ResourceValue(BridgeConstants.RES_DRAWABLE, "something", + url.getPath()); + drawableMap.put(drawable.getName(), drawable); + return frameworkResources; + } + + private Map<String, Map<String, IResourceValue>> getProjectResources() { + Map<String, Map<String, IResourceValue>> projectResources = + new HashMap<String, Map<String, IResourceValue>>(); + + // create the style map (even empty there should be one) + Map<String, IResourceValue> styleMap = new HashMap<String, IResourceValue>(); + projectResources.put("style", styleMap); + + return projectResources; + } + + + private void display(ILayoutViewInfo result, String offset) { + + String msg = String.format("%s%s L:%d T:%d R:%d B:%d", + offset, + result.getName(), + result.getLeft(), result.getTop(), result.getRight(), result.getBottom()); + + System.out.println(msg); + ILayoutViewInfo[] children = result.getChildren(); + if (children != null) { + offset += "+-"; + for (ILayoutViewInfo child : children) { + display(child, offset); + } + } + } + + /** + * Creates a {@link IStyleResourceValue} based on the given values. + * @param styleName the name of the style. + * @param items An array of Strings. Even indices contain a style item name, and odd indices + * a style item value. If the number of string in the array is not even, an exception is thrown. + */ + private IStyleResourceValue createStyle(String styleName, String... items) { + StyleResourceValue value = new StyleResourceValue(styleName); + + if (items.length % 3 == 0) { + for (int i = 0 ; i < items.length;) { + value.addItem(new ResourceValue(items[i++], items[i++], items[i++])); + } + } else { + throw new IllegalArgumentException("Need a multiple of 3 for the number of strings"); + } + + return value; + } + + // --------------- + + public void testTextLayout() throws Exception { + + TestParser parser = new TestParser(); + parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); + parser.setInput(new FileReader(new File(mTextOnlyXmlPath))); + + Map<String, Map<String, IResourceValue>> projectResources = getProjectResources(); + Map<String, Map<String, IResourceValue>> frameworkResources = getFrameworkResources(); + + int screenWidth = 320; + int screenHeight = 480; + + // FIXME need a dummy font for the tests! + ILayoutResult result = mBridge.computeLayout(parser, new Integer(1) /* projectKey */, + screenWidth, screenHeight, + "Theme", projectResources, frameworkResources, null, null); + + display(result.getRootView(), ""); + } + +} diff --git a/tools/layoutlib/bridge/tests/com/android/layoutlib/bridge/BridgeXmlBlockParserTest.java b/tools/layoutlib/bridge/tests/com/android/layoutlib/bridge/BridgeXmlBlockParserTest.java new file mode 100644 index 0000000..cac1f95 --- /dev/null +++ b/tools/layoutlib/bridge/tests/com/android/layoutlib/bridge/BridgeXmlBlockParserTest.java @@ -0,0 +1,150 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * 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.android.layoutlib.bridge; + +import org.kxml2.io.KXmlParser; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.xml.sax.SAXException; +import org.xmlpull.v1.XmlPullParser; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.net.URL; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import junit.framework.TestCase; + +public class BridgeXmlBlockParserTest extends TestCase { + + private String mXmlPath; + private Document mDoc; + + @Override + protected void setUp() throws Exception { + super.setUp(); + URL url = this.getClass().getClassLoader().getResource("data/layout1.xml"); + mXmlPath = url.getFile(); + mDoc = getXmlDocument(mXmlPath); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testXmlBlockParser() throws Exception { + XmlPullParser parser = new KXmlParser(); + parser = new BridgeXmlBlockParser(parser, null, false /* platformResourceFlag */); + parser.setInput(new FileReader(new File(mXmlPath))); + + assertEquals(XmlPullParser.START_DOCUMENT, parser.next()); + + assertEquals(XmlPullParser.START_TAG, parser.next()); + assertEquals("LinearLayout", parser.getName()); + + assertEquals(XmlPullParser.TEXT, parser.next()); + + assertEquals(XmlPullParser.START_TAG, parser.next()); + assertEquals("Button", parser.getName()); + assertEquals(XmlPullParser.TEXT, parser.next()); + assertEquals(XmlPullParser.END_TAG, parser.next()); + + assertEquals(XmlPullParser.TEXT, parser.next()); + + assertEquals(XmlPullParser.START_TAG, parser.next()); + assertEquals("View", parser.getName()); + assertEquals(XmlPullParser.END_TAG, parser.next()); + + assertEquals(XmlPullParser.TEXT, parser.next()); + + assertEquals(XmlPullParser.START_TAG, parser.next()); + assertEquals("TextView", parser.getName()); + assertEquals(XmlPullParser.END_TAG, parser.next()); + + assertEquals(XmlPullParser.TEXT, parser.next()); + + assertEquals(XmlPullParser.END_TAG, parser.next()); + assertEquals(XmlPullParser.END_DOCUMENT, parser.next()); + } + + //------------ + + private Document getXmlDocument(String xmlFilePath) + throws ParserConfigurationException, SAXException, IOException { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + + // keep comments + factory.setIgnoringComments(false); + // don't validate our bogus DTD + factory.setValidating(false); + // we want namespaces + factory.setNamespaceAware(true); + + DocumentBuilder builder = factory.newDocumentBuilder(); + return builder.parse(new File(xmlFilePath)); + } + + + /** + * Quick'n'dirty debug helper that dumps an XML structure to stdout. + */ + @SuppressWarnings("unused") + private void dump(Node node, String prefix) { + Node n; + + String[] types = { + "unknown", + "ELEMENT_NODE", + "ATTRIBUTE_NODE", + "TEXT_NODE", + "CDATA_SECTION_NODE", + "ENTITY_REFERENCE_NODE", + "ENTITY_NODE", + "PROCESSING_INSTRUCTION_NODE", + "COMMENT_NODE", + "DOCUMENT_NODE", + "DOCUMENT_TYPE_NODE", + "DOCUMENT_FRAGMENT_NODE", + "NOTATION_NODE" + }; + + String s = String.format("%s<%s> %s %s", + prefix, + types[node.getNodeType()], + node.getNodeName(), + node.getNodeValue() == null ? "" : node.getNodeValue().trim()); + + System.out.println(s); + + n = node.getFirstChild(); + if (n != null) { + dump(n, prefix + "- "); + } + + n = node.getNextSibling(); + if (n != null) { + dump(n, prefix); + } + + } + +} diff --git a/tools/layoutlib/bridge/tests/com/android/layoutlib/bridge/NinePatchTest.java b/tools/layoutlib/bridge/tests/com/android/layoutlib/bridge/NinePatchTest.java new file mode 100644 index 0000000..67ec5e1 --- /dev/null +++ b/tools/layoutlib/bridge/tests/com/android/layoutlib/bridge/NinePatchTest.java @@ -0,0 +1,35 @@ +package com.android.layoutlib.bridge; + +import com.android.ninepatch.NinePatch; + +import java.net.URL; + +import junit.framework.TestCase; + +public class NinePatchTest extends TestCase { + + private NinePatch mPatch; + + @Override + protected void setUp() throws Exception { + URL url = this.getClass().getClassLoader().getResource("data/button.9.png"); + + mPatch = NinePatch.load(url, false /* convert */); + } + + public void test9PatchLoad() throws Exception { + assertNotNull(mPatch); + } + + public void test9PatchMinSize() { + int[] padding = new int[4]; + mPatch.getPadding(padding); + assertEquals(13, padding[0]); + assertEquals(3, padding[1]); + assertEquals(13, padding[2]); + assertEquals(4, padding[3]); + assertEquals(38, mPatch.getWidth()); + assertEquals(27, mPatch.getHeight()); + } + +} diff --git a/tools/layoutlib/bridge/tests/com/android/layoutlib/bridge/StyleResourceValue.java b/tools/layoutlib/bridge/tests/com/android/layoutlib/bridge/StyleResourceValue.java new file mode 100644 index 0000000..84bdc2f --- /dev/null +++ b/tools/layoutlib/bridge/tests/com/android/layoutlib/bridge/StyleResourceValue.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * 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.android.layoutlib.bridge; + +import com.android.layoutlib.api.IResourceValue; +import com.android.layoutlib.api.IStyleResourceValue; + +import java.util.HashMap; + +class StyleResourceValue extends ResourceValue implements IStyleResourceValue { + + private String mParentStyle = null; + private HashMap<String, IResourceValue> mItems = new HashMap<String, IResourceValue>(); + + StyleResourceValue(String name) { + super(name); + } + + StyleResourceValue(String name, String parentStyle) { + super(name); + mParentStyle = parentStyle; + } + + public String getParentStyle() { + return mParentStyle; + } + + public IResourceValue findItem(String name) { + return mItems.get(name); + } + + public void addItem(IResourceValue value) { + mItems.put(value.getName(), value); + } + + @Override + public void replaceWith(ResourceValue value) { + super.replaceWith(value); + + if (value instanceof StyleResourceValue) { + mItems.clear(); + mItems.putAll(((StyleResourceValue)value).mItems); + } + } + +} diff --git a/tools/layoutlib/bridge/tests/data/button.9.png b/tools/layoutlib/bridge/tests/data/button.9.png Binary files differnew file mode 100644 index 0000000..9d52f40 --- /dev/null +++ b/tools/layoutlib/bridge/tests/data/button.9.png diff --git a/tools/layoutlib/bridge/tests/data/layout1.xml b/tools/layoutlib/bridge/tests/data/layout1.xml new file mode 100644 index 0000000..554f541 --- /dev/null +++ b/tools/layoutlib/bridge/tests/data/layout1.xml @@ -0,0 +1,49 @@ +<?xml version="1.0" encoding="utf-8"?> + +<!-- + Copyright (C) 2008 The Android Open Source Project + + 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. +--> + +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + android:orientation="vertical" +> + <Button + android:id="@+id/bouton" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_weight="1" + android:text="My Button Text" + > + </Button> + <View + android:id="@+id/surface" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + android:layout_weight="2" + /> + <TextView + android:id="@+id/status" + android:paddingLeft="2dip" + android:layout_weight="0" + android:background="@drawable/black" + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:lines="1" + android:gravity="center_vertical|center_horizontal" + android:text="My TextView Text" + /> +</LinearLayout> |