Debian Codegraph @

build-system/manifest-merger/src/test/java/com/android/manifmerger/ActionsTest.java

raw
1 /*
2 * Copyright (C) 2014 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.manifmerger;
18
19 import static com.android.manifmerger.Actions.DecisionTreeRecord;
20 import static com.android.manifmerger.XmlNode.NodeKey;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.when;
23
24 import com.android.SdkConstants;
25 import com.android.ide.common.blame.SourceFile;
26 import com.android.ide.common.blame.SourceFilePosition;
27 import com.android.ide.common.blame.SourcePosition;
28 import com.android.sdklib.mock.MockLog;
29 import com.android.utils.ILogger;
30 import com.android.utils.StdLogger;
31 import com.google.common.base.Optional;
32 import com.google.common.collect.ImmutableList;
33 import com.google.common.collect.ImmutableMap;
34
35 import junit.framework.TestCase;
36
37 import org.w3c.dom.Element;
38 import org.xml.sax.SAXException;
39
40 import java.io.IOException;
41 import java.util.List;
42
43 import javax.xml.parsers.ParserConfigurationException;
44
45 /**
46 * Tests for {@link Actions} class
47 */
48 public class ActionsTest extends TestCase {
49
50 public void testGetNodeKeys() {
51 Element xmlElement = mock(Element.class);
52 when(xmlElement.getNodeName()).thenReturn("activity");
53 when(xmlElement.getAttributeNS(SdkConstants.ANDROID_URI, "name")).thenReturn("keyOne");
54 NodeKey nodeKey = NodeKey.fromXml(xmlElement);
55 assertNotNull(nodeKey);
56
57 ImmutableMap.Builder<NodeKey, DecisionTreeRecord> records = ImmutableMap.builder();
58 DecisionTreeRecord activityDecisionTree = new DecisionTreeRecord();
59 records.put(nodeKey, activityDecisionTree);
60 Actions actions = new Actions(records.build());
61
62 assertEquals(1, actions.getNodeKeys().size());
63 assertTrue(actions.getNodeKeys().contains(nodeKey));
64 }
65
66 public void testGetNodeRecords() {
67 Element xmlElement = mock(Element.class);
68 when(xmlElement.getNodeName()).thenReturn("activity");
69 when(xmlElement.getAttributeNS(SdkConstants.ANDROID_URI, "name")).thenReturn("keyOne");
70 NodeKey nodeKey = NodeKey.fromXml(xmlElement);
71 assertNotNull(nodeKey);
72
73 ImmutableMap.Builder<NodeKey, DecisionTreeRecord> records = ImmutableMap.builder();
74 DecisionTreeRecord activityDecisionTree = new DecisionTreeRecord();
75 activityDecisionTree.addNodeRecord(new Actions.NodeRecord(
76 Actions.ActionType.ADDED,
77 new SourceFilePosition(new SourceFile("file"), new SourcePosition(1, 2, -1)),
78 new NodeKey("nodeKey"),
79 null, /* reason */
80 NodeOperationType.MERGE));
81 records.put(nodeKey, activityDecisionTree);
82 Actions actions = new Actions(records.build());
83
84 // lookup using key
85 ImmutableList<Actions.NodeRecord> nodeRecords = actions.getNodeRecords(nodeKey);
86 assertNotNull(nodeRecords);
87 assertEquals(1, nodeRecords.size());
88 }
89
90 public void testGetAttributesRecords() {
91 Element xmlElement = mock(Element.class);
92 when(xmlElement.getNodeName()).thenReturn("activity");
93 when(xmlElement.getAttributeNS(SdkConstants.ANDROID_URI, "name")).thenReturn("keyOne");
94 NodeKey nodeKey = NodeKey.fromXml(xmlElement);
95 assertNotNull(nodeKey);
96
97 ImmutableMap.Builder<NodeKey, DecisionTreeRecord> records = ImmutableMap.builder();
98 DecisionTreeRecord activityDecisionTree = new DecisionTreeRecord();
99 activityDecisionTree.addNodeRecord(new Actions.NodeRecord(
100 Actions.ActionType.ADDED,
101 new SourceFilePosition(new SourceFile("file"), new SourcePosition(1, 2, -1)),
102 new NodeKey("nodeKey"),
103 null, /* reason */
104 NodeOperationType.MERGE));
105 XmlNode.NodeName attributeName = XmlNode.fromXmlName("android:name");
106 activityDecisionTree.mAttributeRecords.put(attributeName,
107 ImmutableList.of(
108 new Actions.AttributeRecord(Actions.ActionType.INJECTED,
109 new SourceFilePosition(
110 new SourceFile("file"), new SourcePosition(1, 2, -1)),
111 new NodeKey("nodeKey"),
112 null, /* reason */
113 AttributeOperationType.STRICT)));
114 records.put(nodeKey, activityDecisionTree);
115 Actions actions = new Actions(records.build());
116
117 ImmutableList<XmlNode.NodeName> recordedAttributeNames = actions
118 .getRecordedAttributeNames(nodeKey);
119 assertTrue(recordedAttributeNames.contains(attributeName));
120
121 ImmutableList<Actions.AttributeRecord> attributeRecords = actions
122 .getAttributeRecords(nodeKey, attributeName);
123 assertEquals(1, attributeRecords.size());
124 }
125
126
127 /**
128 * test tools:node="removeAll" with several target elements to be removed.
129 */
130 public void testActionsPersistenceAndLoading()
131 throws ParserConfigurationException, SAXException, IOException {
132 String higherPriority = ""
133 + "<manifest\n" // 1
134 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" // 2
135 + " xmlns:tools=\"http://schemas.android.com/tools\"\n" // 3
136 + " package=\"com.example.lib3\">\n" // 4
137 + "\n" // 5
138 + " <permission\n" // 6
139 + " android:name=\"permissionOne\"\n" // 7
140 + " tools:node=\"remove\">\n" // 8
141 + " </permission>\n" // 10
142 + " <permission \n" // 11
143 + " tools:node=\"removeAll\"\n" // 12
144 + " tools:selector=\"com.example.lib3\">\n" // 13
145 + " </permission>\n" // 14
146 + " <permission\n" // 15
147 + " android:name=\"permissionThree\"\n" // 16
148 + " android:protectionLevel=\"signature\"\n" // 17
149 + " tools:node=\"replace\">\n" // 18
150 + " </permission>\n" // 19
151 + "\n" // 20
152 + "</manifest>"; // 21
153
154 String lowerPriorityOne = ""
155 + "<manifest\n" // 1
156 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" // 2
157 + " xmlns:tools=\"http://schemas.android.com/tools\"\n" // 3
158 + " package=\"com.example.lib1\">\n" // 4
159 + "\n" // 5
160 + " <permission android:name=\"permissionOne\"\n" // 6
161 + " android:protectionLevel=\"signature\">\n" // 7
162 + " </permission>\n" // 8
163 + " <permission android:name=\"permissionTwo\"\n" // 9
164 + " android:protectionLevel=\"signature\">\n" // 10
165 + " </permission>\n" // 11
166 + "\n" // 12
167 + "</manifest>"; // 13
168
169 String lowerPriorityTwo = ""
170 + "<manifest\n" // 1
171 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" // 2
172 + " xmlns:tools=\"http://schemas.android.com/tools\"\n" // 3
173 + " package=\"com.example.lib2\">\n" // 4
174 + "\n" // 5
175 + " <permission android:name=\"permissionThree\"\n" // 6
176 + " android:protectionLevel=\"normal\">\n" // 7
177 + " </permission>\n" // 8
178 + " <permission android:name=\"permissionFour\"\n" // 9
179 + " android:protectionLevel=\"normal\">\n" // 10
180 + " </permission>\n" // 11
181 + "\n" // 12
182 + "</manifest>"; // 13
183
184 XmlDocument refDocument = TestUtils.xmlDocumentFromString(
185 TestUtils.sourceFile(getClass(), "higherPriority"), higherPriority);
186 XmlDocument firstLibrary = TestUtils.xmlLibraryFromString(
187 TestUtils.sourceFile(getClass(), "lowerPriorityOne"), lowerPriorityOne);
188 XmlDocument secondLibrary = TestUtils.xmlLibraryFromString(
189 TestUtils.sourceFile(getClass(), "lowerPriorityTwo"), lowerPriorityTwo);
190
191 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(
192 new StdLogger(StdLogger.Level.VERBOSE));
193 Optional<XmlDocument> result = refDocument.merge(firstLibrary, mergingReportBuilder);
194 assertTrue(result.isPresent());
195 result = result.get().merge(secondLibrary, mergingReportBuilder);
196 assertTrue(result.isPresent());
197
198 ILogger logger = new MockLog();
199 XmlDocument cleanedDocument =
200 ToolsInstructionsCleaner.cleanToolsReferences(
201 ManifestMerger2.MergeType.APPLICATION, result.get(), logger);
202 assertNotNull(cleanedDocument);
203
204 Actions actions = mergingReportBuilder.getActionRecorder().build();
205 String expectedMappings = "1<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
206 + "2<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
207 + "3 package=\"com.example.lib3\" >\n"
208 + "4\n"
209 + "5 <permission\n"
210 + "5-->ActionsTest#higherPriority:14:5-18:18\n"
211 + "6 android:name=\"permissionThree\"\n"
212 + "6-->ActionsTest#higherPriority:15:14-44\n"
213 + "7 android:protectionLevel=\"signature\" >\n"
214 + "7-->ActionsTest#higherPriority:16:14-49\n"
215 + "8 </permission>\n"
216 + "9 <permission\n"
217 + "9-->ActionsTest#lowerPriorityOne:9:5-11:18\n"
218 + "10 android:name=\"permissionTwo\"\n"
219 + "10-->ActionsTest#lowerPriorityOne:9:17-45\n"
220 + "11 android:protectionLevel=\"signature\" >\n"
221 + "11-->ActionsTest#lowerPriorityOne:10:14-49\n"
222 + "12 </permission>\n"
223 + "13 <permission\n"
224 + "13-->ActionsTest#lowerPriorityTwo:9:5-11:18\n"
225 + "14 android:name=\"permissionFour\"\n"
226 + "14-->ActionsTest#lowerPriorityTwo:9:17-46\n"
227 + "15 android:protectionLevel=\"normal\" >\n"
228 + "15-->ActionsTest#lowerPriorityTwo:10:14-46\n"
229 + "16 </permission>\n"
230 + "17\n"
231 + "18</manifest>\n";
232 assertEquals(expectedMappings, actions.blame(cleanedDocument));
233
234 // persist the records
235 String persistedMappings = actions.persist();
236 System.out.println(persistedMappings);
237
238 // and reload them from the persisted media.
239 Actions newActions = Actions.load(persistedMappings);
240 assertNotNull(newActions);
241
242 // check equality.
243 for (NodeKey nodeKey : actions.getNodeKeys()) {
244
245 ImmutableList<Actions.NodeRecord> expectedNodeRecords = actions.getNodeRecords(nodeKey);
246 assertNotNull(expectedNodeRecords);
247 assertEquals(expectedNodeRecords.size(), newActions.getNodeRecords(nodeKey).size());
248
249 for (Actions.NodeRecord nodeRecord : newActions.getNodeRecords(nodeKey)) {
250 assertTrue("Cannot find node=" + nodeKey + "record=" + nodeRecord,
251 findNodeRecordInList(nodeRecord, expectedNodeRecords));
252 }
253
254 for (XmlNode.NodeName nodeName : actions
255 .getRecordedAttributeNames(nodeKey)) {
256 ImmutableList<Actions.AttributeRecord> expectedAttributeRecords =
257 actions.getAttributeRecords(nodeKey, nodeName);
258 ImmutableList<Actions.AttributeRecord> actualAttributeRecords =
259 newActions.getAttributeRecords(nodeKey, nodeName);
260 assertEquals(expectedAttributeRecords.size(), actualAttributeRecords.size());
261 for (Actions.AttributeRecord expectedAttributeRecord : expectedAttributeRecords) {
262 assertTrue("Cannot find attribute=" + nodeName
263 + " record=" + expectedAttributeRecord,
264 findAttributeRecordInList(
265 expectedAttributeRecord, actualAttributeRecords)
266 );
267 }
268 }
269
270 }
271 }
272
273 private static boolean findNodeRecordInList(
274 Actions.NodeRecord nodeRecord,
275 List<Actions.NodeRecord> nodeRecordList) {
276 for (Actions.NodeRecord record : nodeRecordList) {
277 if (record.getActionLocation().equals(nodeRecord.getActionLocation())
278 && record.getActionType() == nodeRecord.getActionType()) {
279 return true;
280 }
281 }
282 return false;
283 }
284
285 private static boolean findAttributeRecordInList(
286 Actions.AttributeRecord attributeRecord,
287 List<Actions.AttributeRecord> attributeRecordList) {
288
289 for (Actions.AttributeRecord record : attributeRecordList) {
290 if (record.getOperationType() == attributeRecord.getOperationType()
291 && record.getActionType() == attributeRecord.getActionType()
292 && record.getActionLocation().equals(attributeRecord.getActionLocation())) {
293 return true;
294 }
295 }
296 return false;
297 }
298 }