Exemplo n.º 1
0
  /**
   * Tests an android_binary with zero dependent android_library rules that contains an assets
   * directory.
   */
  @Test
  public void testCreateAllAssetsDirectoryWithZeroAssetsDirectories() throws IOException {
    BuildRuleResolver ruleResolver = new BuildRuleResolver();

    // Two android_library deps, neither with an assets directory.
    JavaLibraryRule libraryOne =
        createAndroidLibraryRule(
            "//java/src/com/facebook/base:libraryOne",
            ruleResolver,
            null, /* resDirectory */
            null, /* assetDirectory */
            null /* nativeLibsDirectory */);
    JavaLibraryRule libraryTwo =
        createAndroidLibraryRule(
            "//java/src/com/facebook/base:libraryTwo",
            ruleResolver,
            null, /* resDirectory */
            null, /* assetDirectory */
            null /* nativeLibsDirectory */);

    // One android_binary rule that depends on the two android_library rules.
    BuildTarget binaryBuildTarget =
        BuildTargetFactory.newInstance("//java/src/com/facebook/base:apk");
    AndroidBinaryRule androidBinary =
        ruleResolver.buildAndAddToIndex(
            AndroidBinaryRule.newAndroidBinaryRuleBuilder(new FakeAbstractBuildRuleBuilderParams())
                .setBuildTarget(binaryBuildTarget)
                .addClasspathDep(libraryOne.getBuildTarget())
                .addClasspathDep(libraryTwo.getBuildTarget())
                .setManifest("java/src/com/facebook/base/AndroidManifest.xml")
                .setTarget("Google Inc.:Google APIs:16")
                .setKeystore(addKeystoreRule(ruleResolver))
                .setPackageType("debug"));

    // Build up the parameters needed to invoke createAllAssetsDirectory().
    Set<String> assetsDirectories = ImmutableSet.of();
    ImmutableList.Builder<Step> commands = ImmutableList.builder();
    DirectoryTraverser traverser =
        new DirectoryTraverser() {
          @Override
          public void traverse(DirectoryTraversal traversal) {
            throw new RuntimeException("Unexpected: no assets directories to traverse!");
          }
        };

    // Invoke createAllAssetsDirectory(), the method under test.
    Optional<String> allAssetsDirectory =
        androidBinary.createAllAssetsDirectory(
            assetsDirectories, ImmutableMap.<String, File>of(), commands, traverser);

    // Verify that no assets/ directory is used.
    assertFalse(
        "There should not be an assets/ directory to pass to aapt.",
        allAssetsDirectory.isPresent());
    assertTrue(
        "There should not be any commands to build up an assets/ directory.",
        commands.build().isEmpty());
  }
Exemplo n.º 2
0
  private JavaBinaryRule createSampleJavaBinaryRule(BuildRuleResolver ruleResolver) {
    // Create a java_binary that depends on a java_library so it is possible to create a
    // java_binary rule with a classpath entry and a main class.
    JavaLibraryRule javaLibrary =
        ruleResolver.buildAndAddToIndex(
            DefaultJavaLibraryRule.newJavaLibraryRuleBuilder(
                    new FakeAbstractBuildRuleBuilderParams())
                .setBuildTarget(BuildTargetFactory.newInstance("//java/com/facebook/util:util"))
                .addVisibilityPattern(BuildTargetPattern.MATCH_ALL)
                .addSrc("java/com/facebook/util/ManifestGenerator.java"));

    JavaBinaryRule javaBinary =
        ruleResolver.buildAndAddToIndex(
            JavaBinaryRule.newJavaBinaryRuleBuilder(new FakeAbstractBuildRuleBuilderParams())
                .setBuildTarget(
                    BuildTargetFactory.newInstance("//java/com/facebook/util:ManifestGenerator"))
                .setMainClass("com.facebook.util.ManifestGenerator")
                .addDep(javaLibrary.getBuildTarget()));

    return javaBinary;
  }
Exemplo n.º 3
0
  @Test
  public void testAndroidBinaryNoDx() {
    BuildRuleResolver ruleResolver = new BuildRuleResolver();

    // Two android_library deps, neither with an assets directory.
    JavaLibraryRule libraryOne =
        createAndroidLibraryRule(
            "//java/src/com/facebook/base:libraryOne",
            ruleResolver,
            null, /* resDirectory */
            null, /* assetDirectory */
            null /* nativeLibsDirectory */);
    JavaLibraryRule libraryTwo =
        createAndroidLibraryRule(
            "//java/src/com/facebook/base:libraryTwo",
            ruleResolver,
            null, /* resDirectory */
            null, /* assetDirectory */
            null /* nativeLibsDirectory */);

    // One android_binary rule that depends on the two android_library rules.
    BuildTarget binaryBuildTarget =
        BuildTargetFactory.newInstance("//java/src/com/facebook/base:apk");
    AndroidBinaryRule androidBinary =
        ruleResolver.buildAndAddToIndex(
            AndroidBinaryRule.newAndroidBinaryRuleBuilder(new FakeAbstractBuildRuleBuilderParams())
                .setBuildTarget(binaryBuildTarget)
                .addClasspathDep(libraryOne.getBuildTarget())
                .addClasspathDep(libraryTwo.getBuildTarget())
                .addBuildRuleToExcludeFromDex(
                    BuildTargetFactory.newInstance("//java/src/com/facebook/base:libraryTwo"))
                .setManifest("java/src/com/facebook/base/AndroidManifest.xml")
                .setTarget("Google Inc.:Google APIs:16")
                .setKeystore(addKeystoreRule(ruleResolver))
                .setPackageType("debug"));

    DependencyGraph graph = RuleMap.createGraphFromBuildRules(ruleResolver);
    AndroidTransitiveDependencies transitiveDependencies =
        androidBinary.findTransitiveDependencies(graph);
    AndroidDexTransitiveDependencies dexTransitiveDependencies =
        androidBinary.findDexTransitiveDependencies(graph);
    ImmutableList.Builder<Step> commands = ImmutableList.builder();

    BuildContext context = createMock(BuildContext.class);
    replay(context);
    androidBinary.addProguardCommands(
        context,
        dexTransitiveDependencies.classpathEntriesToDex,
        transitiveDependencies.proguardConfigs,
        commands,
        ImmutableSet.<String>of());
    verify(context);

    MakeCleanDirectoryStep expectedClean =
        new MakeCleanDirectoryStep("buck-out/gen/java/src/com/facebook/base/.proguard/apk");

    GenProGuardConfigStep expectedGenProguard =
        new GenProGuardConfigStep(
            "buck-out/bin/java/src/com/facebook/base/__manifest_apk__/AndroidManifest.xml",
            ImmutableSet.<String>of(),
            "buck-out/gen/java/src/com/facebook/base/.proguard/apk/proguard.txt");

    ProGuardObfuscateStep expectedObfuscation =
        new ProGuardObfuscateStep(
            "buck-out/gen/java/src/com/facebook/base/.proguard/apk/proguard.txt",
            ImmutableSet.<String>of(),
            false,
            ImmutableMap.of(
                "buck-out/gen/java/src/com/facebook/base/lib__libraryOne__output/libraryOne.jar",
                "buck-out/gen/java/src/com/facebook/base/.proguard/apk/buck-out/gen/java/src/com/"
                    + "facebook/base/lib__libraryOne__output/libraryOne-obfuscated.jar"),
            ImmutableSet.of(
                "buck-out/gen/java/src/com/facebook/base/lib__libraryTwo__output/libraryTwo.jar"),
            "buck-out/gen/java/src/com/facebook/base/.proguard/apk");

    assertEquals(
        ImmutableList.of(expectedClean, expectedGenProguard, expectedObfuscation),
        commands.build());
  }
Exemplo n.º 4
0
  /**
   * Tests an android_binary with multiple dependent android_library rules, each with its own assets
   * directory.
   */
  @Test
  public void testCreateAllAssetsDirectoryWithMultipleAssetsDirectories() throws IOException {
    BuildRuleResolver ruleResolver = new BuildRuleResolver();

    // Two android_library deps, each with an assets directory.
    JavaLibraryRule libraryOne =
        createAndroidLibraryRule(
            "//java/src/com/facebook/base:libraryOne",
            ruleResolver,
            null, /* resDirectory */
            "java/src/com/facebook/base/assets1",
            null /* nativeLibsDirectory */);
    JavaLibraryRule libraryTwo =
        createAndroidLibraryRule(
            "//java/src/com/facebook/base:libraryTwo",
            ruleResolver,
            null, /* resDirectory */
            "java/src/com/facebook/base/assets2",
            null /* nativeLibsDirectory */);

    // One android_binary rule that depends on the two android_library rules.
    BuildTarget binaryBuildTarget =
        BuildTargetFactory.newInstance("//java/src/com/facebook/base:apk");
    AndroidBinaryRule androidBinary =
        ruleResolver.buildAndAddToIndex(
            AndroidBinaryRule.newAndroidBinaryRuleBuilder(new FakeAbstractBuildRuleBuilderParams())
                .setBuildTarget(binaryBuildTarget)
                .addClasspathDep(libraryOne.getBuildTarget())
                .addClasspathDep(libraryTwo.getBuildTarget())
                .setManifest("java/src/com/facebook/base/AndroidManifest.xml")
                .setTarget("Google Inc.:Google APIs:16")
                .setKeystore(addKeystoreRule(ruleResolver))
                .setPackageType("debug"));

    AndroidResourceRule resourceOne =
        (AndroidResourceRule)
            ruleResolver.get(
                BuildTargetFactory.newInstance(
                    "//java/src/com/facebook/base:libraryOne_resources"));
    AndroidResourceRule resourceTwo =
        (AndroidResourceRule)
            ruleResolver.get(
                BuildTargetFactory.newInstance(
                    "//java/src/com/facebook/base:libraryTwo_resources"));

    // Build up the parameters needed to invoke createAllAssetsDirectory().
    Set<String> assetsDirectories =
        ImmutableSet.of(resourceOne.getAssets(), resourceTwo.getAssets());
    ImmutableList.Builder<Step> commands = ImmutableList.builder();
    DirectoryTraverser traverser =
        new DirectoryTraverser() {
          @Override
          public void traverse(DirectoryTraversal traversal) throws IOException {
            String rootPath = Paths.normalizePathSeparator(traversal.getRoot().getPath());
            if ("java/src/com/facebook/base/assets1".equals(rootPath)) {
              traversal.visit(
                  new File("java/src/com/facebook/base/assets1", "guava-10.0.1-fork.dex.1.jar"),
                  "guava-10.0.1-fork.dex.1.jar");
            } else if ("java/src/com/facebook/base/assets2".equals(rootPath)) {
              traversal.visit(
                  new File("java/src/com/facebook/base/assets2", "fonts/Theinhardt-Medium.otf"),
                  "fonts/Theinhardt-Medium.otf");
              traversal.visit(
                  new File("java/src/com/facebook/base/assets2", "fonts/Theinhardt-Regular.otf"),
                  "fonts/Theinhardt-Regular.otf");
            } else {
              throw new RuntimeException("Unexpected path: " + rootPath);
            }
          }
        };

    // Invoke createAllAssetsDirectory(), the method under test.
    Optional<String> allAssetsDirectory =
        androidBinary.createAllAssetsDirectory(
            assetsDirectories, ImmutableMap.<String, File>of(), commands, traverser);

    // Verify that an assets/ directory will be created and passed to aapt.
    assertTrue(allAssetsDirectory.isPresent());
    assertEquals(BIN_DIR + "/java/src/com/facebook/base/__assets_apk__", allAssetsDirectory.get());
    List<? extends Step> expectedCommands =
        ImmutableList.of(
            new MakeCleanDirectoryStep(BIN_DIR + "/java/src/com/facebook/base/__assets_apk__"),
            new MkdirAndSymlinkFileStep(
                "java/src/com/facebook/base/assets1/guava-10.0.1-fork.dex.1.jar",
                BIN_DIR + "/java/src/com/facebook/base/__assets_apk__/guava-10.0.1-fork.dex.1.jar"),
            new MkdirAndSymlinkFileStep(
                "java/src/com/facebook/base/assets2/fonts/Theinhardt-Medium.otf",
                BIN_DIR + "/java/src/com/facebook/base/__assets_apk__/fonts/Theinhardt-Medium.otf"),
            new MkdirAndSymlinkFileStep(
                "java/src/com/facebook/base/assets2/fonts/Theinhardt-Regular.otf",
                BIN_DIR
                    + "/java/src/com/facebook/base/__assets_apk__/fonts/Theinhardt-Regular.otf"));
    MoreAsserts.assertListEquals(expectedCommands, commands.build());
  }
Exemplo n.º 5
0
  /**
   * Tests an android_binary with one dependent android_library rule that contains an assets
   * directory.
   */
  @Test
  public void testCreateAllAssetsDirectoryWithOneAssetsDirectory() throws IOException {
    BuildRuleResolver ruleResolver = new BuildRuleResolver();

    // Two android_library deps, one of which has an assets directory.
    JavaLibraryRule libraryOne =
        createAndroidLibraryRule(
            "//java/src/com/facebook/base:libraryOne",
            ruleResolver,
            null, /* resDirectory */
            null, /* assetDirectory */
            null /* nativeLibsDirectory */);
    JavaLibraryRule libraryTwo =
        createAndroidLibraryRule(
            "//java/src/com/facebook/base:libraryTwo",
            ruleResolver,
            null, /* resDirectory */
            "java/src/com/facebook/base/assets2",
            null /* nativeLibsDirectory */);

    AndroidResourceRule resourceOne =
        (AndroidResourceRule)
            ruleResolver.get(
                BuildTargetFactory.newInstance(
                    "//java/src/com/facebook/base:libraryTwo_resources"));

    // One android_binary rule that depends on the two android_library rules.
    BuildTarget binaryBuildTarget =
        BuildTargetFactory.newInstance("//java/src/com/facebook/base:apk");
    AndroidBinaryRule androidBinary =
        ruleResolver.buildAndAddToIndex(
            AndroidBinaryRule.newAndroidBinaryRuleBuilder(new FakeAbstractBuildRuleBuilderParams())
                .setBuildTarget(binaryBuildTarget)
                .addClasspathDep(libraryOne.getBuildTarget())
                .addClasspathDep(libraryTwo.getBuildTarget())
                .setManifest("java/src/com/facebook/base/AndroidManifest.xml")
                .setTarget("Google Inc.:Google APIs:16")
                .setKeystore(addKeystoreRule(ruleResolver))
                .setPackageType("debug"));

    // Build up the parameters needed to invoke createAllAssetsDirectory().
    Set<String> assetsDirectories = ImmutableSet.of(resourceOne.getAssets());
    ImmutableList.Builder<Step> commands = ImmutableList.builder();
    DirectoryTraverser traverser =
        new DirectoryTraverser() {
          @Override
          public void traverse(DirectoryTraversal traversal) throws IOException {
            String rootPath = Paths.normalizePathSeparator(traversal.getRoot().getPath());
            if ("java/src/com/facebook/base/assets2".equals(rootPath)) {
              traversal.visit(
                  new File("java/src/com/facebook/base/assets2", "fonts/Theinhardt-Medium.otf"),
                  "fonts/Theinhardt-Medium.otf");
              traversal.visit(
                  new File("java/src/com/facebook/base/assets2", "fonts/Theinhardt-Regular.otf"),
                  "fonts/Theinhardt-Regular.otf");
            } else {
              throw new RuntimeException("Unexpected path: " + rootPath);
            }
          }
        };

    // Invoke createAllAssetsDirectory(), the method under test.
    Optional<String> allAssetsDirectory =
        androidBinary.createAllAssetsDirectory(
            assetsDirectories, ImmutableMap.<String, File>of(), commands, traverser);

    // Verify that the existing assets/ directory will be passed to aapt.
    assertTrue(allAssetsDirectory.isPresent());
    assertEquals(
        "Even though there is only one assets directory, the one in "
            + BIN_DIR
            + " should be used.",
        androidBinary.getPathToAllAssetsDirectory(),
        allAssetsDirectory.get());
  }