Home | History | Annotate | Download | only in descriptors
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.eclipse.org/org/documents/epl-v10.php
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 
     18 package com.android.ide.eclipse.adt.internal.editors.descriptors;
     19 
     20 import com.android.ide.eclipse.adt.internal.editors.layout.descriptors.ViewElementDescriptor;
     21 import com.android.ide.eclipse.adt.internal.editors.uimodel.UiDocumentNode;
     22 import com.android.ide.eclipse.adt.internal.editors.uimodel.UiElementNode;
     23 
     24 import junit.framework.TestCase;
     25 
     26 /**
     27  * Unit tests for DescriptorsUtils in the editors plugin
     28  */
     29 @SuppressWarnings("javadoc")
     30 public class DescriptorsUtilsTest extends TestCase {
     31 
     32     @Override
     33     protected void setUp() throws Exception {
     34         super.setUp();
     35     }
     36 
     37     @Override
     38     protected void tearDown() throws Exception {
     39         super.tearDown();
     40     }
     41 
     42     public void testPrettyAttributeUiName() {
     43         assertEquals("", DescriptorsUtils.prettyAttributeUiName(""));
     44 
     45         assertEquals("Max width for view",
     46                 DescriptorsUtils.prettyAttributeUiName("maxWidthForView"));
     47 
     48         assertEquals("Layout width",
     49                 DescriptorsUtils.prettyAttributeUiName("layout_width"));
     50 
     51         // X Y and Z are capitalized when used as single words (so "T" becomes "t")
     52         assertEquals("Axis X", DescriptorsUtils.prettyAttributeUiName("axisX"));
     53         assertEquals("Axis Y", DescriptorsUtils.prettyAttributeUiName("axisY"));
     54         assertEquals("Axis Z", DescriptorsUtils.prettyAttributeUiName("axisZ"));
     55         assertEquals("Axis t", DescriptorsUtils.prettyAttributeUiName("axisT"));
     56 
     57         assertEquals("The X axis", DescriptorsUtils.prettyAttributeUiName("theXAxis"));
     58         assertEquals("The Y axis", DescriptorsUtils.prettyAttributeUiName("theYAxis"));
     59         assertEquals("The Z axis", DescriptorsUtils.prettyAttributeUiName("theZAxis"));
     60         assertEquals("The t axis", DescriptorsUtils.prettyAttributeUiName("theTAxis"));
     61 
     62         // Special cases for "uri" and "sdk" etc
     63         assertEquals("Grant URI permission",
     64                 DescriptorsUtils.prettyAttributeUiName("grantUriPermission"));
     65         assertEquals("URI permission",
     66                 DescriptorsUtils.prettyAttributeUiName("uriPermission"));
     67         assertEquals("Min SDK version", DescriptorsUtils.prettyAttributeUiName("minSdkVersion"));
     68         assertEquals("SDK version", DescriptorsUtils.prettyAttributeUiName("sdkVersion"));
     69         assertEquals("IME action method",
     70                 DescriptorsUtils.prettyAttributeUiName("imeActionMethod"));
     71         assertEquals("VM safe mode", DescriptorsUtils.prettyAttributeUiName("vmSafeMode"));
     72         assertEquals("UI options", DescriptorsUtils.prettyAttributeUiName("uiOptions"));
     73     }
     74 
     75     public void testCapitalize() {
     76         assertEquals("", DescriptorsUtils.capitalize(""));
     77 
     78         assertEquals("Max Width For View",
     79                 DescriptorsUtils.capitalize("maxWidthForView"));
     80 
     81         assertEquals("Layout Width",
     82                 DescriptorsUtils.capitalize("layout_width"));
     83 
     84         assertEquals("Axis X", DescriptorsUtils.capitalize("axisX"));
     85         assertEquals("Axis Y", DescriptorsUtils.capitalize("axisY"));
     86         assertEquals("Axis Z", DescriptorsUtils.capitalize("axisZ"));
     87         assertEquals("Axis T", DescriptorsUtils.capitalize("axisT"));
     88 
     89         assertEquals("The X Axis", DescriptorsUtils.capitalize("theXAxis"));
     90         assertEquals("The Y Axis", DescriptorsUtils.capitalize("theYAxis"));
     91         assertEquals("The Z Axis", DescriptorsUtils.capitalize("theZAxis"));
     92         assertEquals("The T Axis", DescriptorsUtils.capitalize("theTAxis"));
     93 
     94         // Special cases for "uri" and "sdk" etc
     95         assertEquals("Grant URI Permission", DescriptorsUtils.capitalize("grantUriPermission"));
     96         assertEquals("Min SDK Version", DescriptorsUtils.capitalize("minSdkVersion"));
     97         assertEquals("IME Action Method", DescriptorsUtils.capitalize("imeActionMethod"));
     98         assertEquals("URI Permission", DescriptorsUtils.capitalize("uriPermission"));
     99         assertEquals("SDK Version", DescriptorsUtils.capitalize("sdkVersion"));
    100         assertEquals("Grant IME", DescriptorsUtils.capitalize("GrantIme"));
    101         assertEquals("VM Safe Mode", DescriptorsUtils.capitalize("vmSafeMode"));
    102         assertEquals("UI Options", DescriptorsUtils.capitalize("uiOptions"));
    103     }
    104 
    105     public void testFormatTooltip() {
    106         assertEquals("", DescriptorsUtils.formatTooltip(""));
    107 
    108         assertEquals("\"application\"",
    109                 DescriptorsUtils.formatTooltip(
    110                         "<code>application</code>"));
    111 
    112         assertEquals("android.content.Intent",
    113                 DescriptorsUtils.formatTooltip(
    114                         "{@link android.content.Intent}"));
    115 
    116         assertEquals("FLAG_ACTIVITY_SINGLE_TOP",
    117                 DescriptorsUtils.formatTooltip(
    118                         "{@link android.content.Intent#FLAG_ACTIVITY_SINGLE_TOP}"));
    119 
    120         assertEquals("activity-alias",
    121                 DescriptorsUtils.formatTooltip(
    122                         "{@link \t  #AndroidManifestActivityAlias  \tactivity-alias }"));
    123 
    124         assertEquals("\"permission\"",
    125                 DescriptorsUtils.formatTooltip(
    126                         "{@link #AndroidManifestPermission &lt;permission&gt;}"));
    127 
    128         assertEquals("and etc.",
    129                 DescriptorsUtils.formatTooltip(
    130                         "{@link #IntentCategory <category> and etc. }"));
    131 
    132         assertEquals("Activity.onNewIntent()",
    133                 DescriptorsUtils.formatTooltip(
    134                         "{@link android.app.Activity#onNewIntent Activity.onNewIntent()}"));
    135     }
    136 
    137     public void testFormatFormText() {
    138         ElementDescriptor desc = new ElementDescriptor("application");
    139         desc.setSdkUrl(DescriptorsUtils.MANIFEST_SDK_URL + "TagApplication");
    140         String docBaseUrl = "http://base";
    141         assertEquals("<form><li style=\"image\" value=\"image\"></li></form>", DescriptorsUtils.formatFormText("", desc, docBaseUrl));
    142 
    143         assertEquals("<form><li style=\"image\" value=\"image\"><a href=\"http://base/reference/android/R.styleable.html#TagApplication\">application</a></li></form>",
    144                 DescriptorsUtils.formatFormText(
    145                         "<code>application</code>",
    146                         desc, docBaseUrl));
    147 
    148         assertEquals("<form><li style=\"image\" value=\"image\"><b>android.content.Intent</b></li></form>",
    149                 DescriptorsUtils.formatFormText(
    150                         "{@link android.content.Intent}",
    151                         desc, docBaseUrl));
    152 
    153         assertEquals("<form><li style=\"image\" value=\"image\"><a href=\"http://base/reference/android/R.styleable.html#AndroidManifestPermission\">AndroidManifestPermission</a></li></form>",
    154                 DescriptorsUtils.formatFormText(
    155                         "{@link #AndroidManifestPermission}",
    156                         desc, docBaseUrl));
    157 
    158         assertEquals("<form><li style=\"image\" value=\"image\"><a href=\"http://base/reference/android/R.styleable.html#AndroidManifestPermission\">\"permission\"</a></li></form>",
    159                 DescriptorsUtils.formatFormText(
    160                         "{@link #AndroidManifestPermission &lt;permission&gt;}",
    161                         desc, docBaseUrl));
    162     }
    163 
    164     public void testGetFreeWidgetId() throws Exception {
    165         DocumentDescriptor documentDescriptor =
    166             new DocumentDescriptor("layout_doc", null); //$NON-NLS-1$
    167         UiDocumentNode model = new UiDocumentNode(documentDescriptor);
    168         UiElementNode uiRoot = model.getUiRoot();
    169 
    170         assertEquals("@+id/button1", DescriptorsUtils.getFreeWidgetId(uiRoot, "Button"));
    171         assertEquals("@+id/linearLayout1",
    172                 DescriptorsUtils.getFreeWidgetId(uiRoot, "LinearLayout"));
    173     }
    174 
    175     public void testNeedsDefaultId() throws Exception {
    176         assertTrue(DescriptorsUtils.needsDefaultId(new ElementDescriptor("Button")));
    177         assertTrue(DescriptorsUtils.needsDefaultId(new ElementDescriptor("EditText")));
    178         assertTrue(DescriptorsUtils.needsDefaultId(new ElementDescriptor("TextView")));
    179 
    180         assertFalse(DescriptorsUtils.needsDefaultId(new ElementDescriptor("LinearLayout")));
    181         assertFalse(DescriptorsUtils.needsDefaultId(new ElementDescriptor("GridLayout")));
    182         assertFalse(DescriptorsUtils.needsDefaultId(new ElementDescriptor("RelativeLayout")));
    183         assertFalse(DescriptorsUtils.needsDefaultId(new ElementDescriptor("include")));
    184         assertFalse(DescriptorsUtils.needsDefaultId(new ElementDescriptor("merge")));
    185         assertFalse(DescriptorsUtils.needsDefaultId(new ElementDescriptor("fragment")));
    186         assertFalse(DescriptorsUtils.needsDefaultId(new ElementDescriptor("Space")));
    187     }
    188 
    189     private static ViewElementDescriptor createDesc(String name, String fqn, boolean hasChildren) {
    190         if (hasChildren) {
    191             return new ViewElementDescriptor(name, name, fqn, "", "", new AttributeDescriptor[0],
    192                     new AttributeDescriptor[0], new ElementDescriptor[1], false);
    193         } else {
    194             return new ViewElementDescriptor(name, fqn);
    195         }
    196     }
    197 
    198     public void testCanInsertChildren() throws Exception {
    199         assertFalse(DescriptorsUtils.canInsertChildren(createDesc("android:Button",
    200                 "android.widget.Button", false), null));
    201         assertTrue(DescriptorsUtils.canInsertChildren(createDesc("android:LinearLayout",
    202                 "android.view.LinearLayout", true), null));
    203         assertFalse(DescriptorsUtils.canInsertChildren(createDesc("android:ListView",
    204                 "android.widget.ListView", true), null));
    205         assertFalse(DescriptorsUtils.canInsertChildren(createDesc("android:ExpandableListView",
    206                 "android.widget.ExpandableListView", true), null));
    207         assertFalse(DescriptorsUtils.canInsertChildren(createDesc("android:Gallery",
    208                 "android.widget.Gallery", true), null));
    209         assertFalse(DescriptorsUtils.canInsertChildren(createDesc("android:GridView",
    210                 "android.widget.GridView", true), null));
    211 
    212         // This isn't the Android one (missing android: namespace prefix):
    213         // This test is disabled since I had to remove the namespace enforcement
    214         // (see namespace-related comment in canInsertChildren)
    215         //assertTrue(DescriptorsUtils.canInsertChildren(createDesc("mynamespace:ListView",
    216         //        "android.widget.ListView", true), null));
    217 
    218         // Custom view without known view object
    219         assertTrue(DescriptorsUtils.canInsertChildren(createDesc("MyView",
    220                 "foo.bar.MyView", true), null));
    221 
    222         // Custom view with known view object that extends AdapterView
    223         Object view = new MyClassLoader().findClass("foo.bar.MyView").newInstance();
    224         assertFalse(DescriptorsUtils.canInsertChildren(createDesc("MyView",
    225                 "foo.bar.MyView", true), view));
    226     }
    227 
    228     /** Test class loader which finds foo.bar.MyView extends android.widget.AdapterView */
    229     private static class MyClassLoader extends ClassLoader {
    230         public MyClassLoader() {
    231             super(null);
    232         }
    233 
    234         @Override
    235         protected Class<?> findClass(String name) throws ClassNotFoundException {
    236             if (name.equals("foo.bar.MyView")) {
    237                 // Simple class stub compiled by javac and dumped as bytes:
    238                 //package foo.bar;
    239                 //public class MyView extends android.widget.AdapterView {
    240                 //    public MyView() {
    241                 //        super(null);
    242                 //    }
    243                 //}
    244                 byte[] classData = new byte[] {
    245                         -54,-2,-70,-66,0,0,0,49,0,17,10,0,3,0,13,7,0,14,7,0,15,1,0,6,60,105,110,
    246                         105,116,62,1,0,3,40,41,86,1,0,4,67,111,100,101,1,0,15,76,105,110,101,78,
    247                         117,109,98,101,114,84,97,98,108,101,1,0,18,76,111,99,97,108,86,97,114,
    248                         105,97,98,108,101,84,97,98,108,101,1,0,4,116,104,105,115,1,0,16,76,102,
    249                         111,111,47,98,97,114,47,77,121,86,105,101,119,59,1,0,10,83,111,117,114,
    250                         99,101,70,105,108,101,1,0,11,77,121,86,105,101,119,46,106,97,118,97,12,
    251                         0,4,0,16,1,0,14,102,111,111,47,98,97,114,47,77,121,86,105,101,119,1,0,
    252                         26,97,110,100,114,111,105,100,47,119,105,100,103,101,116,47,65,100,97,
    253                         112,116,101,114,86,105,101,119,1,0,28,40,76,97,110,100,114,111,105,100,
    254                         47,99,111,110,116,101,110,116,47,67,111,110,116,101,120,116,59,41,86,0,
    255                         33,0,2,0,3,0,0,0,0,0,1,0,1,0,4,0,5,0,1,0,6,0,0,0,52,0,2,0,1,0,0,0,6,42,
    256                         1,-73,0,1,-79,0,0,0,2,0,7,0,0,0,10,0,2,0,0,0,9,0,5,0,10,0,8,0,0,0,12,0,
    257                         1,0,0,0,6,0,9,0,10,0,0,0,1,0,11,0,0,0,2,0,12
    258                 };
    259                 return defineClass("foo.bar.MyView", classData, 0, classData.length);
    260             }
    261             if (name.equals("android.widget.AdapterView")) {
    262                 // Simple class stub compiled by javac and dumped as bytes:
    263                 //package android.widget;
    264                 //public class AdapterView {
    265                 //    public AdapterView(android.content.Context context) { }
    266                 //}
    267                 byte[] classData = new byte[] {
    268                         -54,-2,-70,-66,0,0,0,49,0,19,10,0,3,0,15,7,0,16,7,0,17,1,0,6,60,105,110,
    269                         105,116,62,1,0,28,40,76,97,110,100,114,111,105,100,47,99,111,110,116,101,
    270                         110,116,47,67,111,110,116,101,120,116,59,41,86,1,0,4,67,111,100,101,1,0,
    271                         15,76,105,110,101,78,117,109,98,101,114,84,97,98,108,101,1,0,18,76,111,
    272                         99,97,108,86,97,114,105,97,98,108,101,84,97,98,108,101,1,0,4,116,104,105,
    273                         115,1,0,28,76,97,110,100,114,111,105,100,47,119,105,100,103,101,116,47,
    274                         65,100,97,112,116,101,114,86,105,101,119,59,1,0,7,99,111,110,116,101,120,
    275                         116,1,0,25,76,97,110,100,114,111,105,100,47,99,111,110,116,101,110,116,
    276                         47,67,111,110,116,101,120,116,59,1,0,10,83,111,117,114,99,101,70,105,108,
    277                         101,1,0,16,65,100,97,112,116,101,114,86,105,101,119,46,106,97,118,97,12,
    278                         0,4,0,18,1,0,26,97,110,100,114,111,105,100,47,119,105,100,103,101,116,
    279                         47,65,100,97,112,116,101,114,86,105,101,119,1,0,16,106,97,118,97,47,108,
    280                         97,110,103,47,79,98,106,101,99,116,1,0,3,40,41,86,0,33,0,2,0,3,0,0,0,0,0,
    281                         1,0,1,0,4,0,5,0,1,0,6,0,0,0,57,0,1,0,2,0,0,0,5,42,-73,0,1,-79,0,0,0,2,0,
    282                         7,0,0,0,6,0,1,0,0,0,8,0,8,0,0,0,22,0,2,0,0,0,5,0,9,0,10,0,0,0,0,0,5,0,11,
    283                         0,12,0,1,0,1,0,13,0,0,0,2,0,14
    284                 };
    285                 return defineClass("android.widget.AdapterView", classData, 0, classData.length);
    286             }
    287 
    288             return super.findClass(name);
    289         }
    290     }
    291 
    292     public void testGetBasename() {
    293         assertEquals("Foo", DescriptorsUtils.getBasename("Foo"));
    294         assertEquals("Foo", DescriptorsUtils.getBasename("foo.Foo"));
    295         assertEquals("String", DescriptorsUtils.getBasename("java.util.String"));
    296     }
    297 }
    298