Search in sources :

Example 11 with FakeDevice

use of com.android.tools.build.bundletool.testing.FakeDevice in project bundletool by google.

the class InstallApksCommandTest method bundleWithDeviceTierTargeting_noDeviceTierSpecified_usesDefaultValue.

@Test
public void bundleWithDeviceTierTargeting_noDeviceTierSpecified_usesDefaultValue() throws Exception {
    ZipPath baseMasterApk = ZipPath.create("base-master.apk");
    ZipPath baseLowApk = ZipPath.create("base-tier_0.apk");
    ZipPath baseHighApk = ZipPath.create("base-tier_1.apk");
    BuildApksResult buildApksResult = BuildApksResult.newBuilder().setPackageName(PKG_NAME).setBundletool(Bundletool.newBuilder().setVersion(BundleToolVersion.getCurrentVersion().toString())).addVariant(createVariant(variantSdkTargeting(sdkVersionFrom(21), ImmutableSet.of(SdkVersion.getDefaultInstance())), createSplitApkSet("base", createMasterApkDescription(ApkTargeting.getDefaultInstance(), baseMasterApk), splitApkDescription(apkDeviceTierTargeting(deviceTierTargeting(/* value= */
    0, /* alternatives= */
    ImmutableList.of(1))), baseLowApk), splitApkDescription(apkDeviceTierTargeting(deviceTierTargeting(/* value= */
    1, /* alternatives= */
    ImmutableList.of(0))), baseHighApk)))).addDefaultTargetingValue(DefaultTargetingValue.newBuilder().setDimension(Value.DEVICE_TIER).setDefaultValue("1")).build();
    Path apksFile = createApksArchiveFile(buildApksResult, tmpDir.resolve("bundle.apks"));
    List<Path> installedApks = new ArrayList<>();
    FakeDevice fakeDevice = FakeDevice.fromDeviceSpec(DEVICE_ID, DeviceState.ONLINE, lDeviceWithLocales("en-US"));
    fakeDevice.setInstallApksSideEffect((apks, installOptions) -> installedApks.addAll(apks));
    AdbServer adbServer = new FakeAdbServer(/* hasInitialDeviceList= */
    true, ImmutableList.of(fakeDevice));
    InstallApksCommand.builder().setApksArchivePath(apksFile).setAdbPath(adbPath).setAdbServer(adbServer).build().execute();
    // Base only, the on demand asset is not installed. Low tier splits are filtered out.
    assertThat(getFileNames(installedApks)).containsExactly(baseMasterApk.toString(), baseHighApk.toString());
}
Also used : Path(java.nio.file.Path) ZipPath(com.android.tools.build.bundletool.model.ZipPath) BuildApksResult(com.android.bundle.Commands.BuildApksResult) ArrayList(java.util.ArrayList) ZipPath(com.android.tools.build.bundletool.model.ZipPath) FakeDevice(com.android.tools.build.bundletool.testing.FakeDevice) AdbServer(com.android.tools.build.bundletool.device.AdbServer) FakeAdbServer(com.android.tools.build.bundletool.testing.FakeAdbServer) FakeAdbServer(com.android.tools.build.bundletool.testing.FakeAdbServer) Test(org.junit.Test)

Example 12 with FakeDevice

use of com.android.tools.build.bundletool.testing.FakeDevice in project bundletool by google.

the class InstallApksCommandTest method installAssetModules.

@Test
@Theory
public void installAssetModules(@FromDataPoints("apksInDirectory") boolean apksInDirectory) throws Exception {
    String installTimeModule1 = "installtime_assetmodule1";
    String installTimeModule2 = "installtime_assetmodule2";
    String onDemandModule = "ondemand_assetmodule";
    ZipPath installTimeMasterApk1 = ZipPath.create(installTimeModule1 + "-master.apk");
    ZipPath installTimeEnApk1 = ZipPath.create(installTimeModule1 + "-en.apk");
    ZipPath installTimeMasterApk2 = ZipPath.create(installTimeModule2 + "-master.apk");
    ZipPath installTimeEnApk2 = ZipPath.create(installTimeModule2 + "-en.apk");
    ZipPath onDemandMasterApk = ZipPath.create(onDemandModule + "-master.apk");
    ZipPath baseApk = ZipPath.create("base-master.apk");
    BuildApksResult tableOfContent = BuildApksResult.newBuilder().setBundletool(Bundletool.newBuilder().setVersion(BundleToolVersion.getCurrentVersion().toString())).addVariant(createVariant(VariantTargeting.getDefaultInstance(), createSplitApkSet("base", createMasterApkDescription(ApkTargeting.getDefaultInstance(), baseApk)))).addAssetSliceSet(AssetSliceSet.newBuilder().setAssetModuleMetadata(AssetModuleMetadata.newBuilder().setName(installTimeModule1).setDeliveryType(DeliveryType.INSTALL_TIME)).addApkDescription(splitApkDescription(ApkTargeting.getDefaultInstance(), installTimeMasterApk1)).addApkDescription(splitApkDescription(apkLanguageTargeting("en"), installTimeEnApk1))).addAssetSliceSet(AssetSliceSet.newBuilder().setAssetModuleMetadata(AssetModuleMetadata.newBuilder().setName(installTimeModule2).setDeliveryType(DeliveryType.INSTALL_TIME)).addApkDescription(splitApkDescription(ApkTargeting.getDefaultInstance(), installTimeMasterApk2)).addApkDescription(splitApkDescription(apkLanguageTargeting("en"), installTimeEnApk2))).addAssetSliceSet(AssetSliceSet.newBuilder().setAssetModuleMetadata(AssetModuleMetadata.newBuilder().setName(onDemandModule).setDeliveryType(DeliveryType.ON_DEMAND)).addApkDescription(splitApkDescription(ApkTargeting.getDefaultInstance(), onDemandMasterApk))).build();
    Path apksFile = createApks(tableOfContent, apksInDirectory);
    List<Path> installedApks = new ArrayList<>();
    FakeDevice fakeDevice = FakeDevice.fromDeviceSpec(DEVICE_ID, DeviceState.ONLINE, lDeviceWithLocales("en-US"));
    AdbServer adbServer = new FakeAdbServer(/* hasInitialDeviceList= */
    true, ImmutableList.of(fakeDevice));
    fakeDevice.setInstallApksSideEffect((apks, installOptions) -> installedApks.addAll(apks));
    InstallApksCommand.builder().setApksArchivePath(apksFile).setAdbPath(adbPath).setAdbServer(adbServer).setModules(ImmutableSet.of(installTimeModule1)).build().execute();
    assertThat(getFileNames(installedApks)).containsExactly(baseApk.toString(), installTimeMasterApk1.toString(), installTimeEnApk1.toString());
}
Also used : Path(java.nio.file.Path) ZipPath(com.android.tools.build.bundletool.model.ZipPath) BuildApksResult(com.android.bundle.Commands.BuildApksResult) ArrayList(java.util.ArrayList) ZipPath(com.android.tools.build.bundletool.model.ZipPath) FakeDevice(com.android.tools.build.bundletool.testing.FakeDevice) AdbServer(com.android.tools.build.bundletool.device.AdbServer) FakeAdbServer(com.android.tools.build.bundletool.testing.FakeAdbServer) FakeAdbServer(com.android.tools.build.bundletool.testing.FakeAdbServer) Test(org.junit.Test) Theory(org.junit.experimental.theories.Theory)

Example 13 with FakeDevice

use of com.android.tools.build.bundletool.testing.FakeDevice in project bundletool by google.

the class AdbRunnerTest method installApks_allowingDowngrade.

@Test
public void installApks_allowingDowngrade() {
    FakeDevice fakeDevice = FakeDevice.fromDeviceSpec("device1", DeviceState.ONLINE, lDeviceWithLocales("en-US"));
    AdbServer testAdbServer = new FakeAdbServer(/* hasInitialDeviceList= */
    true, ImmutableList.of(fakeDevice));
    testAdbServer.init(Paths.get("/test/adb"));
    AdbRunner adbRunner = new AdbRunner(testAdbServer);
    fakeDevice.setInstallApksSideEffect((apks, installOptions) -> {
        if (!installOptions.getAllowDowngrade()) {
            throw new RuntimeException("Downgrade disallowed.");
        }
    });
    adbRunner.run(device -> device.installApks(ImmutableList.of(apkPath), InstallOptions.builder().setAllowDowngrade(true).build()));
}
Also used : FakeDevice(com.android.tools.build.bundletool.testing.FakeDevice) FakeAdbServer(com.android.tools.build.bundletool.testing.FakeAdbServer) FakeAdbServer(com.android.tools.build.bundletool.testing.FakeAdbServer) Test(org.junit.Test)

Example 14 with FakeDevice

use of com.android.tools.build.bundletool.testing.FakeDevice in project bundletool by google.

the class AdbRunnerTest method installApks_withDeviceId_disconnectedDevice_throws.

@Test
public void installApks_withDeviceId_disconnectedDevice_throws() {
    FakeDevice disconnectedDevice = FakeDevice.inDisconnectedState("device2", DeviceState.UNAUTHORIZED);
    AdbServer testAdbServer = new FakeAdbServer(/* hasInitialDeviceList= */
    true, ImmutableList.of(FakeDevice.fromDeviceSpec("device1", DeviceState.ONLINE, lDeviceWithLocales("en-US")), disconnectedDevice));
    testAdbServer.init(Paths.get("/test/adb"));
    AdbRunner adbRunner = new AdbRunner(testAdbServer);
    disconnectedDevice.setInstallApksSideEffect((apks, installOptions) -> {
        throw CommandExecutionException.builder().withInternalMessage("Enable USB debugging on the connected device.").build();
    });
    Throwable exception = assertThrows(CommandExecutionException.class, () -> adbRunner.run(device -> device.installApks(ImmutableList.of(apkPath), DEFAULT_INSTALL_OPTIONS), "device2"));
    assertThat(exception).hasMessageThat().contains("Enable USB debugging on the connected device.");
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Files(java.nio.file.Files) DeviceState(com.android.ddmlib.IDevice.DeviceState) CommandExecutionException(com.android.tools.build.bundletool.model.exceptions.CommandExecutionException) DeviceFactory.lDeviceWithLocales(com.android.tools.build.bundletool.testing.DeviceFactory.lDeviceWithLocales) InstallOptions(com.android.tools.build.bundletool.device.Device.InstallOptions) FakeDevice(com.android.tools.build.bundletool.testing.FakeDevice) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) Rule(org.junit.Rule) ImmutableList(com.google.common.collect.ImmutableList) Paths(java.nio.file.Paths) FakeAdbServer(com.android.tools.build.bundletool.testing.FakeAdbServer) Duration(java.time.Duration) Path(java.nio.file.Path) TemporaryFolder(org.junit.rules.TemporaryFolder) Before(org.junit.Before) FakeDevice(com.android.tools.build.bundletool.testing.FakeDevice) FakeAdbServer(com.android.tools.build.bundletool.testing.FakeAdbServer) FakeAdbServer(com.android.tools.build.bundletool.testing.FakeAdbServer) Test(org.junit.Test)

Example 15 with FakeDevice

use of com.android.tools.build.bundletool.testing.FakeDevice in project bundletool by google.

the class AdbShellCommandTaskTest method commandExecution_outputMultiLine.

@Test
public void commandExecution_outputMultiLine() {
    FakeDevice fakeDevice = FakeDevice.fromDeviceSpec("id1", DeviceState.ONLINE, lDeviceWithLocales("en-US"));
    fakeDevice.injectShellCommandOutput("getprop", () -> "test output\nnew line");
    AdbShellCommandTask task = new AdbShellCommandTask(fakeDevice, "getprop");
    assertThat(task.execute()).containsExactly("test output", "new line").inOrder();
}
Also used : FakeDevice(com.android.tools.build.bundletool.testing.FakeDevice) Test(org.junit.Test)

Aggregations

FakeDevice (com.android.tools.build.bundletool.testing.FakeDevice)32 Test (org.junit.Test)32 FakeAdbServer (com.android.tools.build.bundletool.testing.FakeAdbServer)24 AdbServer (com.android.tools.build.bundletool.device.AdbServer)17 Path (java.nio.file.Path)17 ZipPath (com.android.tools.build.bundletool.model.ZipPath)16 BuildApksResult (com.android.bundle.Commands.BuildApksResult)13 DeviceSpec (com.android.bundle.Devices.DeviceSpec)10 ArrayList (java.util.ArrayList)10 Theory (org.junit.experimental.theories.Theory)9 Duration (java.time.Duration)2 AdbCommandRejectedException (com.android.ddmlib.AdbCommandRejectedException)1 DeviceState (com.android.ddmlib.IDevice.DeviceState)1 ShellCommandUnresponsiveException (com.android.ddmlib.ShellCommandUnresponsiveException)1 TimeoutException (com.android.ddmlib.TimeoutException)1 InstallOptions (com.android.tools.build.bundletool.device.Device.InstallOptions)1 CommandExecutionException (com.android.tools.build.bundletool.model.exceptions.CommandExecutionException)1 DeviceFactory.lDeviceWithLocales (com.android.tools.build.bundletool.testing.DeviceFactory.lDeviceWithLocales)1 ImmutableList (com.google.common.collect.ImmutableList)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1