Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MikhailSuendukov committed Nov 27, 2023
1 parent 007b053 commit f302029
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static com.microsoft.appcenter.distribute.DistributeConstants.LOG_TAG;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
Expand All @@ -31,8 +32,14 @@ class InstallStatusReceiver extends BroadcastReceiver {

@VisibleForTesting
static final String INSTALL_STATUS_ACTION = "com.microsoft.appcenter.action.INSTALL_STATUS";

/**
* Raw value of PendingIntent.FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT which will appear only in
* Android target SDK 34.
*/
@VisibleForTesting
static final String FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT_NAME = "FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT";
private static final int FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT_VALUE = 16777216;


static IntentFilter getInstallerReceiverFilter() {
IntentFilter installerReceiverFilter = new IntentFilter();
Expand All @@ -52,10 +59,12 @@ static IntentSender getInstallStatusIntentSender(Context context, int requestCod
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
broadcastFlags = PendingIntent.FLAG_MUTABLE;
if (Build.VERSION.SDK_INT >= 34) {
broadcastFlags |= PendingIntent.class.getDeclaredField(FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT_NAME).getInt(PendingIntent.class);
broadcastFlags |= FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT_VALUE;
}
}
PendingIntent pendingIntent = PendingIntent.getBroadcast(
// We use PendingIntent.FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT on target SDK < 34.
// Suppress warning because there is no such flag in SDK < 34.
@SuppressLint("WrongConstant") PendingIntent pendingIntent = PendingIntent.getBroadcast(
context,
requestCode,
new Intent(INSTALL_STATUS_ACTION),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
public class InstallStatusReceiverTest {

private static final int SESSION_ID = 42;
private static final int FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT_VALUE = 16777216;

@Rule
public PowerMockRule mRule = new PowerMockRule();
Expand Down Expand Up @@ -216,6 +217,14 @@ public void installerReceiverFilter() throws Exception {
verify(filter).addAction(INSTALL_STATUS_ACTION);
}

@Test
public void createIntentSenderOnAndroid34() throws NoSuchFieldException, IllegalAccessException {

/* Mock SDK_INT to 34 target SDK. */
Whitebox.setInternalState(Build.VERSION.class, "SDK_INT", 34);
createIntentSender(PendingIntent.FLAG_MUTABLE | FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT_VALUE);
}

@Test
public void createIntentSenderOnAndroidS() throws NoSuchFieldException, IllegalAccessException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import android.content.pm.PackageInstaller;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.ParcelFileDescriptor;

Expand All @@ -41,12 +42,14 @@
import org.junit.Rule;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.powermock.reflect.Whitebox;

import java.io.FileInputStream;
import java.io.IOException;
Expand All @@ -58,7 +61,7 @@
InstallStatusReceiver.class,
PackageInstallerListener.class,
ReleaseInstallerActivity.class,
SessionReleaseInstaller.class
SessionReleaseInstaller.class,
})
public class SessionReleaseInstallerTest {

Expand Down Expand Up @@ -153,7 +156,9 @@ public Boolean answer(InvocationOnMock invocation) {
}

@Test
public void installSuccess() throws IOException {
public void installSuccessForSV2() throws IOException {
Whitebox.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.S_V2);

Uri uri = mock(Uri.class);
mInstaller.install(uri);

Expand All @@ -177,6 +182,33 @@ public void installSuccess() throws IOException {
verify(mPackageInstaller).abandonSession(eq(SESSION_ID));
}

@Test
public void installSuccessForTiramisu() throws IOException {
Whitebox.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU);

Uri uri = mock(Uri.class);
mInstaller.install(uri);

/* Verify that all required things called. */
verify(mHandler).post(any(Runnable.class));
verify(mContext).registerReceiver(eq(mInstallStatusReceiver), any(), anyInt());
verify(mPackageInstaller).registerSessionCallback(eq(mPackageInstallerListener));
verify(mInputStream).close();
verify(mOutputStream).close();
verify(mSession).commit(any(IntentSender.class));
verify(mSession, never()).abandon();
verify(mSession).close();
verifyNoInteractions(mListener);

/* Try to star install second time. It's valid case if something goes wrong with previous try. */
mInstaller.install(uri);

/* Cancel previous session and re-use callbacks. */
verify(mContext).registerReceiver(eq(mInstallStatusReceiver), any(), anyInt());
verify(mPackageInstaller).registerSessionCallback(eq(mPackageInstallerListener));
verify(mPackageInstaller).abandonSession(eq(SESSION_ID));
}

@Test
public void throwIOExceptionWhenTryToOpenWriteSession() throws IOException {

Expand Down Expand Up @@ -246,7 +278,7 @@ public void clear() {
mInstaller.install(uri);

/* Registering callbacks. */
verify(mContext).registerReceiver(eq(mInstallStatusReceiver), any());
verify(mContext).registerReceiver(eq(mInstallStatusReceiver), any(), anyInt());
verify(mPackageInstaller).registerSessionCallback(eq(mPackageInstallerListener));

/* Clear after start should clear registered callbacks and abandon the session. */
Expand Down

0 comments on commit f302029

Please sign in to comment.