• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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.layoutlib.bridge.android;
18 
19 import com.android.layoutlib.bridge.impl.ParserFactory;
20 
21 import org.junit.AfterClass;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.kxml2.io.KXmlParser;
25 import org.w3c.dom.Node;
26 import org.xmlpull.v1.XmlPullParser;
27 import org.xmlpull.v1.XmlPullParserException;
28 
29 import android.annotation.NonNull;
30 
31 import static org.junit.Assert.assertEquals;
32 
33 public class BridgeXmlBlockParserTest {
34 
35     @BeforeClass
setUp()36     public static void setUp() {
37         ParserFactory.setParserFactory(new ParserFactoryImpl());
38     }
39 
40     @Test
testXmlBlockParser()41     public void testXmlBlockParser() throws Exception {
42 
43         XmlPullParser parser = ParserFactory.create(
44                 getClass().getResourceAsStream("/com/android/layoutlib/testdata/layout1.xml"),
45                         "layout1.xml");
46 
47         parser = new BridgeXmlBlockParser(parser, null, false /* platformResourceFlag */);
48 
49         assertEquals(XmlPullParser.START_DOCUMENT, parser.next());
50 
51         assertEquals(XmlPullParser.START_TAG, parser.next());
52         assertEquals("LinearLayout", parser.getName());
53 
54         assertEquals(XmlPullParser.TEXT, parser.next());
55 
56         assertEquals(XmlPullParser.START_TAG, parser.next());
57         assertEquals("Button", parser.getName());
58         assertEquals(XmlPullParser.TEXT, parser.next());
59         assertEquals(XmlPullParser.END_TAG, parser.next());
60 
61         assertEquals(XmlPullParser.TEXT, parser.next());
62 
63         assertEquals(XmlPullParser.START_TAG, parser.next());
64         assertEquals("View", parser.getName());
65         assertEquals(XmlPullParser.END_TAG, parser.next());
66 
67         assertEquals(XmlPullParser.TEXT, parser.next());
68 
69         assertEquals(XmlPullParser.START_TAG, parser.next());
70         assertEquals("TextView", parser.getName());
71         assertEquals(XmlPullParser.END_TAG, parser.next());
72 
73         assertEquals(XmlPullParser.TEXT, parser.next());
74 
75         assertEquals(XmlPullParser.END_TAG, parser.next());
76         assertEquals(XmlPullParser.END_DOCUMENT, parser.next());
77     }
78 
79     //------------
80 
81     /**
82      * Quick 'n' dirty debug helper that dumps an XML structure to stdout.
83      */
84     @SuppressWarnings("unused")
dump(Node node, String prefix)85     private void dump(Node node, String prefix) {
86         Node n;
87 
88         String[] types = {
89                 "unknown",
90                 "ELEMENT_NODE",
91                 "ATTRIBUTE_NODE",
92                 "TEXT_NODE",
93                 "CDATA_SECTION_NODE",
94                 "ENTITY_REFERENCE_NODE",
95                 "ENTITY_NODE",
96                 "PROCESSING_INSTRUCTION_NODE",
97                 "COMMENT_NODE",
98                 "DOCUMENT_NODE",
99                 "DOCUMENT_TYPE_NODE",
100                 "DOCUMENT_FRAGMENT_NODE",
101                 "NOTATION_NODE"
102         };
103 
104         String s = String.format("%s<%s> %s %s",
105                 prefix,
106                 types[node.getNodeType()],
107                 node.getNodeName(),
108                 node.getNodeValue() == null ? "" : node.getNodeValue().trim());
109 
110         System.out.println(s);
111 
112         n = node.getFirstChild();
113         if (n != null) {
114             dump(n, prefix + "- ");
115         }
116 
117         n = node.getNextSibling();
118         if (n != null) {
119             dump(n, prefix);
120         }
121     }
122 
123     @AfterClass
tearDown()124     public static void tearDown() {
125         ParserFactory.setParserFactory(null);
126     }
127 
128     private static class ParserFactoryImpl
129             extends com.android.ide.common.rendering.api.ParserFactory {
130 
131         @NonNull
132         @Override
createParser(String displayName)133         public XmlPullParser createParser(String displayName) throws XmlPullParserException {
134             return new KXmlParser();
135         }
136     }
137 }
138