| 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.PlaceholderHandler.KeyBasedValueResolver; |
| 20 |
import static org.mockito.Matchers.any; |
| 21 |
import static org.mockito.Matchers.anyString; |
| 22 |
import static org.mockito.Matchers.eq; |
| 23 |
import static org.mockito.Mockito.times; |
| 24 |
import static org.mockito.Mockito.verify; |
| 25 |
import static org.mockito.Mockito.when; |
| 26 |
|
| 27 |
import com.android.annotations.NonNull; |
| 28 |
import com.android.ide.common.blame.SourceFilePosition; |
| 29 |
import com.android.ide.common.blame.SourcePosition; |
| 30 |
import com.android.sdklib.mock.MockLog; |
| 31 |
import com.google.common.base.Optional; |
| 32 |
|
| 33 |
import junit.framework.TestCase; |
| 34 |
|
| 35 |
import org.mockito.Mock; |
| 36 |
import org.mockito.MockitoAnnotations; |
| 37 |
import org.xml.sax.SAXException; |
| 38 |
|
| 39 |
import java.io.IOException; |
| 40 |
|
| 41 |
import javax.xml.parsers.ParserConfigurationException; |
| 42 |
|
| 43 |
/** |
| 44 |
* Tests for the {@link com.android.manifmerger.PlaceholderHandler} |
| 45 |
*/ |
| 46 |
public class PlaceholderHandlerTest extends TestCase { |
| 47 |
|
| 48 |
@Mock |
| 49 |
ActionRecorder mActionRecorder; |
| 50 |
|
| 51 |
@Mock |
| 52 |
MergingReport.Builder mBuilder; |
| 53 |
|
| 54 |
MockLog mMockLog = new MockLog(); |
| 55 |
|
| 56 |
KeyBasedValueResolver<String> nullResolver = new KeyBasedValueResolver<String>() { |
| 57 |
@Override |
| 58 |
public String getValue(@NonNull String key) { |
| 59 |
// not provided a placeholder value should generate an error. |
| 60 |
return null; |
| 61 |
} |
| 62 |
}; |
| 63 |
|
| 64 |
@Override |
| 65 |
protected void setUp() throws Exception { |
| 66 |
super.setUp(); |
| 67 |
MockitoAnnotations.initMocks(this); |
| 68 |
when(mBuilder.getLogger()).thenReturn(mMockLog); |
| 69 |
when(mBuilder.getActionRecorder()).thenReturn(mActionRecorder); |
| 70 |
} |
| 71 |
|
| 72 |
public void testPlaceholders() throws ParserConfigurationException, SAXException, IOException { |
| 73 |
|
| 74 |
String xml = "" |
| 75 |
+ "<manifest\n" |
| 76 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\">\n" |
| 77 |
+ " <activity android:name=\"activityOne\"\n" |
| 78 |
+ " android:attr1=\"${landscapePH}\"\n" |
| 79 |
+ " android:attr2=\"prefix.${landscapePH}\"\n" |
| 80 |
+ " android:attr3=\"${landscapePH}.suffix\"\n" |
| 81 |
+ " android:attr4=\"prefix${landscapePH}suffix\">\n" |
| 82 |
+ " </activity>\n" |
| 83 |
+ "</manifest>"; |
| 84 |
|
| 85 |
XmlDocument refDocument = TestUtils.xmlDocumentFromString( |
| 86 |
TestUtils.sourceFile(getClass(), "testPlaceholders#xml"), xml); |
| 87 |
|
| 88 |
PlaceholderHandler.visit( |
| 89 |
ManifestMerger2.MergeType.APPLICATION, |
| 90 |
refDocument, new KeyBasedValueResolver<String>() { |
| 91 |
@Override |
| 92 |
public String getValue(@NonNull String key) { |
| 93 |
return "newValue"; |
| 94 |
} |
| 95 |
}, mBuilder); |
| 96 |
|
| 97 |
Optional<XmlElement> activityOne = refDocument.getRootNode() |
| 98 |
.getNodeByTypeAndKey(ManifestModel.NodeTypes.ACTIVITY, ".activityOne"); |
| 99 |
assertTrue(activityOne.isPresent()); |
| 100 |
assertEquals(5, activityOne.get().getAttributes().size()); |
| 101 |
// check substitution. |
| 102 |
assertEquals("newValue", |
| 103 |
activityOne.get().getAttribute( |
| 104 |
XmlNode.fromXmlName("android:attr1")).get().getValue()); |
| 105 |
assertEquals("prefix.newValue", |
| 106 |
activityOne.get().getAttribute( |
| 107 |
XmlNode.fromXmlName("android:attr2")).get().getValue()); |
| 108 |
assertEquals("newValue.suffix", |
| 109 |
activityOne.get().getAttribute( |
| 110 |
XmlNode.fromXmlName("android:attr3")).get().getValue()); |
| 111 |
assertEquals("prefixnewValuesuffix", |
| 112 |
activityOne.get().getAttribute( |
| 113 |
XmlNode.fromXmlName("android:attr4")).get().getValue()); |
| 114 |
|
| 115 |
for (XmlAttribute xmlAttribute : activityOne.get().getAttributes()) { |
| 116 |
// any attribute other than android:name should have been injected. |
| 117 |
if (!xmlAttribute.getName().toString().contains("name")) { |
| 118 |
verify(mActionRecorder).recordAttributeAction( |
| 119 |
xmlAttribute, |
| 120 |
SourcePosition.UNKNOWN, |
| 121 |
Actions.ActionType.INJECTED, |
| 122 |
null); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
public void testSeveralPlaceholders() throws ParserConfigurationException, SAXException, IOException { |
| 128 |
|
| 129 |
String xml = "" |
| 130 |
+ "<manifest\n" |
| 131 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\">\n" |
| 132 |
+ " <activity android:name=\"activityOne\"\n" |
| 133 |
+ " android:attr1=\"prefix${first}${second}\"\n" |
| 134 |
+ " android:attr2=\"${first}${second}suffix\"\n" |
| 135 |
+ " android:attr3=\"prefix${first}.${second}suffix\"\n" |
| 136 |
+ " android:attr4=\"${first}.${second}\"/>\n" |
| 137 |
+ "</manifest>"; |
| 138 |
|
| 139 |
XmlDocument refDocument = TestUtils.xmlDocumentFromString( |
| 140 |
TestUtils.sourceFile(getClass(), "testPlaceholders#xml"), xml); |
| 141 |
|
| 142 |
PlaceholderHandler.visit( |
| 143 |
ManifestMerger2.MergeType.APPLICATION, |
| 144 |
refDocument, new KeyBasedValueResolver<String>() { |
| 145 |
@Override |
| 146 |
public String getValue(@NonNull String key) { |
| 147 |
if (key.equals("first")) { |
| 148 |
return "firstValue"; |
| 149 |
} else { |
| 150 |
return "secondValue"; |
| 151 |
} |
| 152 |
} |
| 153 |
}, mBuilder); |
| 154 |
|
| 155 |
Optional<XmlElement> activityOne = refDocument.getRootNode() |
| 156 |
.getNodeByTypeAndKey(ManifestModel.NodeTypes.ACTIVITY, ".activityOne"); |
| 157 |
assertTrue(activityOne.isPresent()); |
| 158 |
assertEquals(5, activityOne.get().getAttributes().size()); |
| 159 |
// check substitution. |
| 160 |
|
| 161 |
assertEquals("prefixfirstValuesecondValue", |
| 162 |
activityOne.get().getAttribute( |
| 163 |
XmlNode.fromXmlName("android:attr1")).get().getValue()); |
| 164 |
|
| 165 |
assertEquals("firstValuesecondValuesuffix", |
| 166 |
activityOne.get().getAttribute( |
| 167 |
XmlNode.fromXmlName("android:attr2")).get().getValue()); |
| 168 |
|
| 169 |
assertEquals("prefixfirstValue.secondValuesuffix", |
| 170 |
activityOne.get().getAttribute( |
| 171 |
XmlNode.fromXmlName("android:attr3")).get().getValue()); |
| 172 |
|
| 173 |
assertEquals("firstValue.secondValue", |
| 174 |
activityOne.get().getAttribute( |
| 175 |
XmlNode.fromXmlName("android:attr4")).get().getValue()); |
| 176 |
|
| 177 |
for (XmlAttribute xmlAttribute : activityOne.get().getAttributes()) { |
| 178 |
// any attribute other than android:name should have been injected. |
| 179 |
if (!xmlAttribute.getName().toString().contains("name")) { |
| 180 |
verify(mActionRecorder, times(2)).recordAttributeAction( |
| 181 |
xmlAttribute, |
| 182 |
SourcePosition.UNKNOWN, |
| 183 |
Actions.ActionType.INJECTED, |
| 184 |
null); |
| 185 |
|
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
public void testPlaceHolder_notProvided() |
| 191 |
throws ParserConfigurationException, SAXException, IOException { |
| 192 |
String xml = "" |
| 193 |
+ "<manifest\n" |
| 194 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\">\n" |
| 195 |
+ " <activity android:name=\"activityOne\"\n" |
| 196 |
+ " android:attr1=\"${landscapePH}\"/>\n" |
| 197 |
+ "</manifest>"; |
| 198 |
|
| 199 |
XmlDocument refDocument = TestUtils.xmlDocumentFromString( |
| 200 |
TestUtils.sourceFile(getClass(), "testPlaceholders#xml"), xml); |
| 201 |
|
| 202 |
PlaceholderHandler |
| 203 |
.visit(ManifestMerger2.MergeType.APPLICATION, refDocument, nullResolver, mBuilder); |
| 204 |
// verify the error was recorded. |
| 205 |
verify(mBuilder).addMessage( |
| 206 |
any(SourceFilePosition.class), |
| 207 |
eq(MergingReport.Record.Severity.ERROR), anyString()); |
| 208 |
} |
| 209 |
|
| 210 |
public void testPlaceHolder_notProvided_inLibrary() |
| 211 |
throws ParserConfigurationException, SAXException, IOException { |
| 212 |
String xml = "" |
| 213 |
+ "<manifest\n" |
| 214 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\">\n" |
| 215 |
+ " <activity android:name=\"activityOne\"\n" |
| 216 |
+ " android:attr1=\"${landscapePH}\"/>\n" |
| 217 |
+ "</manifest>"; |
| 218 |
|
| 219 |
XmlDocument refDocument = TestUtils.xmlDocumentFromString( |
| 220 |
TestUtils.sourceFile(getClass(), "testPlaceholders#xml"), xml); |
| 221 |
|
| 222 |
PlaceholderHandler |
| 223 |
.visit(ManifestMerger2.MergeType.LIBRARY, refDocument, nullResolver, mBuilder); |
| 224 |
// verify the error was recorded. |
| 225 |
verify(mBuilder).addMessage( |
| 226 |
any(SourceFilePosition.class), |
| 227 |
eq(MergingReport.Record.Severity.INFO), anyString()); |
| 228 |
} |
| 229 |
} |