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