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