1515
1616import static com .google .common .truth .Truth .assertThat ;
1717import static java .nio .charset .StandardCharsets .UTF_8 ;
18+ import static org .junit .Assert .assertThrows ;
1819
1920import com .google .common .base .Joiner ;
2021import com .google .common .base .Splitter ;
@@ -92,6 +93,42 @@ private static Path rlocation(String path) throws IOException {
9293 working .toFile ().deleteOnExit ();
9394 }
9495
96+ @ Test
97+ public void testMergeManifestWithBrokenManifestSyntax () throws Exception {
98+ String dataDir =
99+ Paths .get (System .getenv ("TEST_WORKSPACE" ), System .getenv ("TEST_BINARY" ))
100+ .resolveSibling ("testing/manifestmerge" )
101+ .toString ()
102+ .replace ('\\' , '/' );
103+ Files .createDirectories (working .resolve ("output" ));
104+ final Path mergedManifest = working .resolve ("output/mergedManifest.xml" );
105+ final Path brokenMergerManifest = rlocation (dataDir + "/brokenManifest/AndroidManifest.xml" );
106+ assertThat (brokenMergerManifest .toFile ().exists ()).isTrue ();
107+
108+ AndroidManifestProcessor .ManifestProcessingException e =
109+ assertThrows (
110+ AndroidManifestProcessor .ManifestProcessingException .class ,
111+ () -> {
112+ ManifestMergerAction .main (
113+ generateArgs (
114+ brokenMergerManifest ,
115+ ImmutableMap .of (),
116+ false , /* isLibrary */
117+ ImmutableMap .of ("applicationId" , "com.google.android.apps.testapp" ),
118+ "" , /* custom_package */
119+ mergedManifest ,
120+ false /* mergeManifestPermissions */ )
121+ .toArray (new String [0 ]));
122+ });
123+ assertThat (e )
124+ .hasMessageThat ()
125+ .contains (
126+ "com.android.manifmerger.ManifestMerger2$MergeFailureException: "
127+ + "org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 6; "
128+ + "The markup in the document following the root element must be well-formed." );
129+ assertThat (mergedManifest .toFile ().exists ()).isFalse ();
130+ }
131+
95132 @ Test
96133 public void testMerge_GenerateDummyManifest () throws Exception {
97134 Files .createDirectories (working .resolve ("output" ));
@@ -159,7 +196,8 @@ public void testMerge_GenerateDummyManifest() throws Exception {
159196 false , /* isLibrary */
160197 ImmutableMap .of ("applicationId" , "com.google.android.apps.testapp" ),
161198 "" , /* custom_package */
162- mergedManifest );
199+ mergedManifest ,
200+ /* mergeManifestPermissions */ false );
163201 ManifestMergerAction .main (args .toArray (new String [0 ]));
164202
165203 assertThat (
@@ -174,6 +212,64 @@ public void testMerge_GenerateDummyManifest() throws Exception {
174212 .trim ());
175213 }
176214
215+ @ Test
216+ public void testMergeWithMergePermissionsEnabled () throws Exception {
217+ // Largely copied from testMerge() above. Perhaps worth combining the two test methods into one
218+ // method in the future?
219+ String dataDir =
220+ Paths .get (System .getenv ("TEST_WORKSPACE" ), System .getenv ("TEST_BINARY" ))
221+ .resolveSibling ("testing/manifestmerge" )
222+ .toString ()
223+ .replace ("\\ " , "/" );
224+
225+ final Path mergerManifest = rlocation (dataDir + "/merger/AndroidManifest.xml" );
226+ final Path mergeeManifestOne = rlocation (dataDir + "/mergeeOne/AndroidManifest.xml" );
227+ final Path mergeeManifestTwo = rlocation (dataDir + "/mergeeTwo/AndroidManifest.xml" );
228+ assertThat (mergerManifest .toFile ().exists ()).isTrue ();
229+ assertThat (mergeeManifestOne .toFile ().exists ()).isTrue ();
230+ assertThat (mergeeManifestTwo .toFile ().exists ()).isTrue ();
231+
232+ // The following code retrieves the path of the only AndroidManifest.xml in the
233+ // expected-merged-permission/manifests directory. Unfortunately, this test runs
234+ // internally and externally and the files have different names.
235+ final File expectedManifestDirectory =
236+ mergerManifest .getParent ().resolveSibling ("expected-merged-permissions" ).toFile ();
237+ assertThat (expectedManifestDirectory .exists ()).isTrue ();
238+ final String [] debug =
239+ expectedManifestDirectory .list (new PatternFilenameFilter (".*AndroidManifest\\ .xml$" ));
240+ assertThat (debug ).isNotNull ();
241+ final File [] expectedManifestDirectoryManifests =
242+ expectedManifestDirectory .listFiles ((File dir , String name ) -> true );
243+ assertThat (expectedManifestDirectoryManifests ).isNotNull ();
244+ assertThat (expectedManifestDirectoryManifests ).hasLength (1 );
245+ final Path expectedManifest = expectedManifestDirectoryManifests [0 ].toPath ();
246+
247+ Files .createDirectories (working .resolve ("output" ));
248+ final Path mergedManifest = working .resolve ("output/mergedManifest.xml" );
249+
250+ List <String > args =
251+ generateArgs (
252+ mergerManifest ,
253+ ImmutableMap .of (mergeeManifestOne , "mergeeOne" , mergeeManifestTwo , "mergeeTwo" ),
254+ false , /* isLibrary */
255+ ImmutableMap .of ("applicationId" , "com.google.android.apps.testapp" ),
256+ "" , /* custom_package */
257+ mergedManifest ,
258+ /* mergeManifestPermissions */ true );
259+ ManifestMergerAction .main (args .toArray (new String [0 ]));
260+
261+ assertThat (
262+ Joiner .on (" " )
263+ .join (Files .readAllLines (mergedManifest , UTF_8 ))
264+ .replaceAll ("\\ s+" , " " )
265+ .trim ())
266+ .isEqualTo (
267+ Joiner .on (" " )
268+ .join (Files .readAllLines (expectedManifest , UTF_8 ))
269+ .replaceAll ("\\ s+" , " " )
270+ .trim ());
271+ }
272+
177273 @ Test public void fullIntegration () throws Exception {
178274 Files .createDirectories (working .resolve ("output" ));
179275 final Path binaryOutput = working .resolve ("output/binaryManifest.xml" );
@@ -198,8 +294,15 @@ public void testMerge_GenerateDummyManifest() throws Exception {
198294 .getManifest ();
199295
200296 // libFoo manifest merging
201- List <String > args = generateArgs (libFooManifest , ImmutableMap .<Path , String >of (), true ,
202- ImmutableMap .<String , String >of (), "" , libFooOutput );
297+ List <String > args =
298+ generateArgs (
299+ libFooManifest ,
300+ ImmutableMap .<Path , String >of (),
301+ true ,
302+ ImmutableMap .<String , String >of (),
303+ "" ,
304+ libFooOutput ,
305+ false );
203306 ManifestMergerAction .main (args .toArray (new String [0 ]));
204307 assertThat (Joiner .on (" " )
205308 .join (Files .readAllLines (libFooOutput , UTF_8 ))
@@ -211,8 +314,15 @@ public void testMerge_GenerateDummyManifest() throws Exception {
211314 + "</manifest>" );
212315
213316 // libBar manifest merging
214- args = generateArgs (libBarManifest , ImmutableMap .<Path , String >of (), true ,
215- ImmutableMap .<String , String >of (), "com.google.libbar" , libBarOutput );
317+ args =
318+ generateArgs (
319+ libBarManifest ,
320+ ImmutableMap .<Path , String >of (),
321+ true ,
322+ ImmutableMap .<String , String >of (),
323+ "com.google.libbar" ,
324+ libBarOutput ,
325+ false );
216326 ManifestMergerAction .main (args .toArray (new String [0 ]));
217327 assertThat (Joiner .on (" " )
218328 .join (Files .readAllLines (libBarOutput , UTF_8 ))
@@ -235,7 +345,8 @@ public void testMerge_GenerateDummyManifest() throws Exception {
235345 "applicationId" , "com.google.android.app" ,
236346 "foo" , "this \\ \\ : is \" a, \" bad string" ),
237347 /* customPackage= */ "" ,
238- binaryOutput );
348+ binaryOutput ,
349+ /* mergeManifestPermissions */ false );
239350 ManifestMergerAction .main (args .toArray (new String [0 ]));
240351 assertThat (Joiner .on (" " )
241352 .join (Files .readAllLines (binaryOutput , UTF_8 ))
@@ -255,14 +366,26 @@ private List<String> generateArgs(
255366 boolean library ,
256367 Map <String , String > manifestValues ,
257368 String customPackage ,
258- Path manifestOutput ) {
259- return ImmutableList .of (
369+ Path manifestOutput ,
370+ boolean mergeManifestPermissions ) {
371+ ImmutableList .Builder <String > builder = ImmutableList .builder ();
372+ builder .add (
260373 "--manifest" , manifest .toString (),
261- "--mergeeManifests" , mapToDictionaryString (mergeeManifests ),
262- "--mergeType" , library ? "LIBRARY" : "APPLICATION" ,
263- "--manifestValues" , mapToDictionaryString (manifestValues ),
264- "--customPackage" , customPackage ,
265- "--manifestOutput" , manifestOutput .toString ());
374+ "--mergeeManifests" , mapToDictionaryString (mergeeManifests ));
375+ if (mergeManifestPermissions ) {
376+ builder .add ("--mergeManifestPermissions" );
377+ }
378+
379+ builder .add (
380+ "--mergeType" ,
381+ library ? "LIBRARY" : "APPLICATION" ,
382+ "--manifestValues" ,
383+ mapToDictionaryString (manifestValues ),
384+ "--customPackage" ,
385+ customPackage ,
386+ "--manifestOutput" ,
387+ manifestOutput .toString ());
388+ return builder .build ();
266389 }
267390
268391 private <K , V > String mapToDictionaryString (Map <K , V > map ) {
0 commit comments