OSDN Git Service

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