1 /*
2  * Copyright (C) 2017 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 android.util;
18 
19 import com.android.ide.common.rendering.api.RenderResources;
20 import com.android.layoutlib.bridge.BridgeConstants;
21 import com.android.layoutlib.bridge.android.BridgeContext;
22 
23 import org.junit.Test;
24 import org.xmlpull.v1.XmlPullParser;
25 
26 import com.google.common.collect.ImmutableMap;
27 
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
32 
33 public class BridgeXmlPullAttributesTest {
34     @Test
testGetAttributeIntValueForEnums()35     public void testGetAttributeIntValueForEnums() {
36         RenderResources renderResources = new RenderResources();
37 
38         XmlPullParser parser = mock(XmlPullParser.class);
39         when(parser.getAttributeValue(BridgeConstants.NS_RESOURCES, "layout_width"))
40                 .thenReturn("match_parent");
41         when(parser.getAttributeName(0)).thenReturn("layout_width");
42         when(parser.getAttributeNamespace(0)).thenReturn(BridgeConstants.NS_RESOURCES);
43         // Return every value twice since there is one test using name and other using index
44         when(parser.getAttributeValue("http://custom", "my_custom_attr"))
45                 .thenReturn("a", "a", "b", "b", "invalid", "invalid");
46         when(parser.getAttributeName(1)).thenReturn("my_custom_attr");
47         when(parser.getAttributeNamespace(1)).thenReturn("http://custom");
48 
49         BridgeContext context = mock(BridgeContext.class);
50         when(context.getRenderResources()).thenReturn(renderResources);
51 
52         BridgeXmlPullAttributes attributes = new BridgeXmlPullAttributes(
53                 parser,
54                 context,
55                 false,
56                 attrName -> {
57                     if ("layout_width".equals(attrName)) {
58                         return ImmutableMap.of(
59                                 "match_parent", 123);
60                     }
61                     return ImmutableMap.of();
62                 },
63                 attrName -> {
64                     if ("my_custom_attr".equals(attrName)) {
65                         return ImmutableMap.of(
66                                 "a", 1,
67                                 "b", 2
68                         );
69                     }
70                     return ImmutableMap.of();
71                 });
72 
73         // Test a framework defined enum attribute
74         assertEquals(123, attributes.getAttributeIntValue(BridgeConstants.NS_RESOURCES,
75                 "layout_width", 500));
76         assertEquals(123, attributes.getAttributeIntValue(0, 500));
77         // Test non existing attribute (it should return the default value)
78         assertEquals(500, attributes.getAttributeIntValue(BridgeConstants.NS_RESOURCES,
79                 "layout_height", 500));
80         assertEquals(500, attributes.getAttributeIntValue(2, 500));
81 
82         // Test project defined enum attribute
83         assertEquals(1, attributes.getAttributeIntValue("http://custom",
84                 "my_custom_attr", 500));
85         assertEquals(1, attributes.getAttributeIntValue(1, 500));
86         assertEquals(2, attributes.getAttributeIntValue("http://custom",
87                 "my_custom_attr", 500));
88         assertEquals(2, attributes.getAttributeIntValue(1, 500));
89         // Test an invalid enum
90         boolean exception = false;
91         try {
92             attributes.getAttributeIntValue("http://custom", "my_custom_attr", 500);
93         } catch(NumberFormatException e) {
94             exception = true;
95         }
96         assertTrue(exception);
97         exception = false;
98         try {
99             attributes.getAttributeIntValue(1, 500);
100         } catch(NumberFormatException e) {
101             exception = true;
102         }
103         assertTrue(exception);
104 
105         // Test non existing project attribute
106         assertEquals(500, attributes.getAttributeIntValue("http://custom",
107                 "my_other_attr", 500));
108     }
109 
110 }