OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / sdk / eclipse / plugins / com.android.ide.eclipse.tests / unittests / com / android / ide / eclipse / adt / internal / resources / AttrsXmlParserTest.java
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 package com.android.ide.eclipse.adt.internal.resources;
18
19
20 import com.android.ide.eclipse.adt.editors.layout.gscripts.IAttributeInfo.Format;
21 import com.android.ide.eclipse.tests.AdtTestData;
22
23 import org.w3c.dom.Document;
24
25 import java.lang.reflect.Method;
26 import java.util.Map;
27
28 import junit.framework.TestCase;
29
30 public class AttrsXmlParserTest extends TestCase {
31
32     private AttrsXmlParser mParser;
33     private String mFilePath;
34
35     private static final String MOCK_DATA_PATH =
36         "com/android/ide/eclipse/testdata/mock_attrs.xml"; //$NON-NLS-1$
37
38     @Override
39     public void setUp() throws Exception {
40         mFilePath = AdtTestData.getInstance().getTestFilePath(MOCK_DATA_PATH); //$NON-NLS-1$
41         mParser = new AttrsXmlParser(mFilePath);
42     }
43
44     @Override
45     public void tearDown() throws Exception {
46     }
47
48     public final void testGetDocument() throws Exception {
49         assertNotNull(_getDocument());
50     }
51
52     public void testGetOsAttrsXmlPath() throws Exception {
53         assertEquals(mFilePath, mParser.getOsAttrsXmlPath());
54     }
55
56     public final void testPreload() throws Exception {
57         assertSame(mParser, mParser.preload());
58     }
59
60
61     public final void testLoadViewAttributes() throws Exception {
62         mParser.preload();
63         ViewClassInfo info = new ViewClassInfo(
64                 false /* isLayout */,
65                 "mock_android.something.Theme",      //$NON-NLS-1$
66                 "Theme");                            //$NON-NLS-1$
67         mParser.loadViewAttributes(info);
68
69         assertEquals("These are the standard attributes that make up a complete theme.", //$NON-NLS-1$
70                 info.getJavaDoc());
71         AttributeInfo[] attrs = info.getAttributes();
72         assertEquals(1, attrs.length);
73         assertEquals("scrollbarSize", info.getAttributes()[0].getName());
74         assertEquals(1, info.getAttributes()[0].getFormats().length);
75         assertEquals(Format.DIMENSION, info.getAttributes()[0].getFormats()[0]);
76     }
77
78     public final void testEnumFlagValues() throws Exception {
79         /* The XML being read contains:
80             <!-- Standard orientation constant. -->
81             <attr name="orientation">
82                 <!-- Defines an horizontal widget. -->
83                 <enum name="horizontal" value="0" />
84                 <!-- Defines a vertical widget. -->
85                 <enum name="vertical" value="1" />
86             </attr>
87          */
88
89         mParser.preload();
90         Map<String, Map<String, Integer>> attrMap = mParser.getEnumFlagValues();
91         assertTrue(attrMap.containsKey("orientation"));
92
93         Map<String, Integer> valueMap = attrMap.get("orientation");
94         assertTrue(valueMap.containsKey("horizontal"));
95         assertTrue(valueMap.containsKey("vertical"));
96         assertEquals(Integer.valueOf(0), valueMap.get("horizontal"));
97         assertEquals(Integer.valueOf(1), valueMap.get("vertical"));
98     }
99
100     public final void testDeprecated() throws Exception {
101         mParser.preload();
102
103         DeclareStyleableInfo dep = mParser.getDeclareStyleableList().get("DeprecatedTest");
104         assertNotNull(dep);
105
106         AttributeInfo[] attrs = dep.getAttributes();
107         assertEquals(4, attrs.length);
108
109         assertEquals("deprecated-inline", attrs[0].getName());
110         assertEquals("In-line deprecated.", attrs[0].getDeprecatedDoc());
111         assertEquals("Deprecated comments using delimiters.", attrs[0].getJavaDoc());
112
113         assertEquals("deprecated-multiline", attrs[1].getName());
114         assertEquals("Multi-line version of deprecated that works till the next tag.",
115                 attrs[1].getDeprecatedDoc());
116         assertEquals("Deprecated comments on their own line.", attrs[1].getJavaDoc());
117
118         assertEquals("deprecated-not", attrs[2].getName());
119         assertEquals(null, attrs[2].getDeprecatedDoc());
120         assertEquals("This attribute is not deprecated.", attrs[2].getJavaDoc());
121
122         assertEquals("deprecated-no-javadoc", attrs[3].getName());
123         assertEquals("There is no other javadoc here.", attrs[3].getDeprecatedDoc());
124         assertEquals("", attrs[3].getJavaDoc());
125     }
126
127     //---- access to private methods
128
129     private Document _getDocument() throws Exception {
130         Method method = AttrsXmlParser.class.getDeclaredMethod("getDocument"); //$NON-NLS-1$
131         method.setAccessible(true);
132         return (Document) method.invoke(mParser);
133     }
134 }