|
16 | 16 |
|
17 | 17 | package dagger.internal.codegen;
|
18 | 18 |
|
| 19 | +import static com.google.common.truth.Truth.assertThat; |
19 | 20 | import static com.google.testing.compile.CompilationSubject.assertThat;
|
| 21 | +import static java.nio.charset.StandardCharsets.UTF_8; |
20 | 22 |
|
21 | 23 | import androidx.room.compiler.processing.util.Source;
|
| 24 | +import com.google.common.truth.PrimitiveByteArraySubject; |
| 25 | +import com.google.common.truth.StringSubject; |
| 26 | +import com.google.common.truth.Subject; |
22 | 27 | import com.google.testing.compile.Compilation;
|
23 | 28 | import com.google.testing.compile.JavaFileObjects;
|
24 | 29 | import dagger.testing.compile.CompilerTests;
|
25 | 30 | import dagger.testing.golden.GoldenFileRule;
|
| 31 | +import java.lang.reflect.Method; |
26 | 32 | import java.util.Collection;
|
27 | 33 | import javax.tools.JavaFileObject;
|
28 | 34 | import org.junit.Rule;
|
@@ -276,4 +282,213 @@ public void scopedLazyClassKeyProvider_compilesSuccessfully() throws Exception {
|
276 | 282 | .withProcessingOptions(compilerMode.processorOptions())
|
277 | 283 | .compile(subject -> subject.hasErrorCount(0));
|
278 | 284 | }
|
| 285 | + |
| 286 | + @Test |
| 287 | + public void testProguardFile() throws Exception { |
| 288 | + Source fooKey = |
| 289 | + CompilerTests.javaSource( |
| 290 | + "test.FooKey", |
| 291 | + "package test;", |
| 292 | + "", |
| 293 | + "interface FooKey {}"); |
| 294 | + Source fooKeyModule = |
| 295 | + CompilerTests.javaSource( |
| 296 | + "test.FooKeyModule", |
| 297 | + "package test;", |
| 298 | + "", |
| 299 | + "import dagger.Module;", |
| 300 | + "import dagger.Provides;", |
| 301 | + "import dagger.multibindings.LazyClassKey;", |
| 302 | + "import dagger.multibindings.IntoMap;", |
| 303 | + "", |
| 304 | + "@Module", |
| 305 | + "public interface FooKeyModule {", |
| 306 | + " @Provides", |
| 307 | + " @IntoMap", |
| 308 | + " @LazyClassKey(FooKey.class)", |
| 309 | + " static String provideString() { return \"\"; }", |
| 310 | + "}"); |
| 311 | + CompilerTests.daggerCompiler(fooKey, fooKeyModule) |
| 312 | + .withProcessingOptions(compilerMode.processorOptions()) |
| 313 | + .compile( |
| 314 | + subject -> { |
| 315 | + subject.hasErrorCount(0); |
| 316 | + PrimitiveByteArraySubject proguardFile = |
| 317 | + subject.generatedResourceFileWithPath( |
| 318 | + "META-INF/proguard/test_FooKeyModule_LazyClassKeys.pro"); |
| 319 | + assertThatContentAsUtf8String(proguardFile) |
| 320 | + .isEqualTo("-keep,allowobfuscation,allowshrinking class test.FooKey"); |
| 321 | + }); |
| 322 | + } |
| 323 | + |
| 324 | + @Test |
| 325 | + public void testProguardFile_nestedModule() throws Exception { |
| 326 | + Source fooKey = |
| 327 | + CompilerTests.javaSource( |
| 328 | + "test.FooKey", |
| 329 | + "package test;", |
| 330 | + "", |
| 331 | + "interface FooKey {}"); |
| 332 | + Source outerClass = |
| 333 | + CompilerTests.javaSource( |
| 334 | + "test.OuterClass", |
| 335 | + "package test;", |
| 336 | + "", |
| 337 | + "import dagger.Module;", |
| 338 | + "import dagger.Provides;", |
| 339 | + "import dagger.multibindings.LazyClassKey;", |
| 340 | + "import dagger.multibindings.IntoMap;", |
| 341 | + "", |
| 342 | + "public interface OuterClass {", |
| 343 | + " @Module", |
| 344 | + " public interface FooKeyModule {", |
| 345 | + " @Provides", |
| 346 | + " @IntoMap", |
| 347 | + " @LazyClassKey(FooKey.class)", |
| 348 | + " static String provideString() { return \"\"; }", |
| 349 | + " }", |
| 350 | + "}"); |
| 351 | + CompilerTests.daggerCompiler(fooKey, outerClass) |
| 352 | + .withProcessingOptions(compilerMode.processorOptions()) |
| 353 | + .compile( |
| 354 | + subject -> { |
| 355 | + subject.hasErrorCount(0); |
| 356 | + PrimitiveByteArraySubject proguardFile = |
| 357 | + subject.generatedResourceFileWithPath( |
| 358 | + "META-INF/proguard/test_OuterClass_FooKeyModule_LazyClassKeys.pro"); |
| 359 | + assertThatContentAsUtf8String(proguardFile) |
| 360 | + .isEqualTo("-keep,allowobfuscation,allowshrinking class test.FooKey"); |
| 361 | + }); |
| 362 | + } |
| 363 | + |
| 364 | + @Test |
| 365 | + public void testProguardFile_multipleModules() throws Exception { |
| 366 | + Source fooKey = |
| 367 | + CompilerTests.javaSource( |
| 368 | + "test.FooKey", |
| 369 | + "package test;", |
| 370 | + "", |
| 371 | + "interface FooKey {}"); |
| 372 | + Source barKey = |
| 373 | + CompilerTests.javaSource( |
| 374 | + "test.BarKey", |
| 375 | + "package test;", |
| 376 | + "", |
| 377 | + "interface BarKey {}"); |
| 378 | + Source fooKeyModule = |
| 379 | + CompilerTests.javaSource( |
| 380 | + "test.FooKeyModule", |
| 381 | + "package test;", |
| 382 | + "", |
| 383 | + "import dagger.Module;", |
| 384 | + "import dagger.Provides;", |
| 385 | + "import dagger.multibindings.LazyClassKey;", |
| 386 | + "import dagger.multibindings.IntoMap;", |
| 387 | + "", |
| 388 | + "@Module", |
| 389 | + "public interface FooKeyModule {", |
| 390 | + " @Provides", |
| 391 | + " @IntoMap", |
| 392 | + " @LazyClassKey(FooKey.class)", |
| 393 | + " static String provideString() { return \"\"; }", |
| 394 | + "}"); |
| 395 | + Source barKeyModule = |
| 396 | + CompilerTests.javaSource( |
| 397 | + "test.BarKeyModule", |
| 398 | + "package test;", |
| 399 | + "", |
| 400 | + "import dagger.Module;", |
| 401 | + "import dagger.Provides;", |
| 402 | + "import dagger.multibindings.LazyClassKey;", |
| 403 | + "import dagger.multibindings.IntoMap;", |
| 404 | + "", |
| 405 | + "@Module", |
| 406 | + "public interface BarKeyModule {", |
| 407 | + " @Provides", |
| 408 | + " @IntoMap", |
| 409 | + " @LazyClassKey(BarKey.class)", |
| 410 | + " static String provideString() { return \"\"; }", |
| 411 | + "}"); |
| 412 | + CompilerTests.daggerCompiler(fooKey, fooKeyModule, barKey, barKeyModule) |
| 413 | + .withProcessingOptions(compilerMode.processorOptions()) |
| 414 | + .compile( |
| 415 | + subject -> { |
| 416 | + subject.hasErrorCount(0); |
| 417 | + PrimitiveByteArraySubject fooKeyModuleProguardFile = |
| 418 | + subject.generatedResourceFileWithPath( |
| 419 | + "META-INF/proguard/test_FooKeyModule_LazyClassKeys.pro"); |
| 420 | + assertThatContentAsUtf8String(fooKeyModuleProguardFile) |
| 421 | + .isEqualTo("-keep,allowobfuscation,allowshrinking class test.FooKey"); |
| 422 | + |
| 423 | + PrimitiveByteArraySubject barKeyModuleProguardFile = |
| 424 | + subject.generatedResourceFileWithPath( |
| 425 | + "META-INF/proguard/test_BarKeyModule_LazyClassKeys.pro"); |
| 426 | + assertThatContentAsUtf8String(barKeyModuleProguardFile) |
| 427 | + .isEqualTo("-keep,allowobfuscation,allowshrinking class test.BarKey"); |
| 428 | + }); |
| 429 | + } |
| 430 | + |
| 431 | + @Test |
| 432 | + public void testProguardFile_multipleKeys() throws Exception { |
| 433 | + Source fooKey = |
| 434 | + CompilerTests.javaSource( |
| 435 | + "test.FooKey", |
| 436 | + "package test;", |
| 437 | + "", |
| 438 | + "interface FooKey {}"); |
| 439 | + Source barKey = |
| 440 | + CompilerTests.javaSource( |
| 441 | + "test.BarKey", |
| 442 | + "package test;", |
| 443 | + "", |
| 444 | + "interface BarKey {}"); |
| 445 | + Source fooKeyAndBarKeyModule = |
| 446 | + CompilerTests.javaSource( |
| 447 | + "test.FooKeyAndBarKeyModule", |
| 448 | + "package test;", |
| 449 | + "", |
| 450 | + "import dagger.Module;", |
| 451 | + "import dagger.Provides;", |
| 452 | + "import dagger.multibindings.LazyClassKey;", |
| 453 | + "import dagger.multibindings.IntoMap;", |
| 454 | + "", |
| 455 | + "@Module", |
| 456 | + "public interface FooKeyAndBarKeyModule {", |
| 457 | + " @Provides", |
| 458 | + " @IntoMap", |
| 459 | + " @LazyClassKey(FooKey.class)", |
| 460 | + " static String provideFooKeyString() { return \"\"; }", |
| 461 | + "", |
| 462 | + " @Provides", |
| 463 | + " @IntoMap", |
| 464 | + " @LazyClassKey(BarKey.class)", |
| 465 | + " static String provideBarKeyString() { return \"\"; }", |
| 466 | + "}"); |
| 467 | + CompilerTests.daggerCompiler(fooKey, barKey, fooKeyAndBarKeyModule) |
| 468 | + .withProcessingOptions(compilerMode.processorOptions()) |
| 469 | + .compile( |
| 470 | + subject -> { |
| 471 | + subject.hasErrorCount(0); |
| 472 | + PrimitiveByteArraySubject proguardFile = |
| 473 | + subject.generatedResourceFileWithPath( |
| 474 | + "META-INF/proguard/test_FooKeyAndBarKeyModule_LazyClassKeys.pro"); |
| 475 | + assertThatContentAsUtf8String(proguardFile) |
| 476 | + .isEqualTo( |
| 477 | + "-keep,allowobfuscation,allowshrinking class test.FooKey\n" |
| 478 | + + "-keep,allowobfuscation,allowshrinking class test.BarKey"); |
| 479 | + }); |
| 480 | + } |
| 481 | + |
| 482 | + // TODO(b/386213524): Add support for getting a resource file as a StringSubject. |
| 483 | + // Use reflection to get the subject's byte array and then convert it to a StringSubject. |
| 484 | + private static StringSubject assertThatContentAsUtf8String(PrimitiveByteArraySubject subject) { |
| 485 | + try { |
| 486 | + Method protectedActualMethod = Subject.class.getDeclaredMethod("actual"); |
| 487 | + protectedActualMethod.setAccessible(true); |
| 488 | + byte[] actualBytes = (byte[]) protectedActualMethod.invoke(subject); |
| 489 | + return assertThat(new String(actualBytes, UTF_8)); |
| 490 | + } catch (Exception e) { |
| 491 | + throw new RuntimeException(e); |
| 492 | + } |
| 493 | + } |
279 | 494 | }
|
0 commit comments