Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e870e97c authored by Mehdi Alizadeh's avatar Mehdi Alizadeh
Browse files

Adds tests for AppPrediction service availability check

Bug: 138595943
Test: atest ChooserActivityTest
Change-Id: I7760f37d3f9fee00648cea5c70e744f1da2bdeca
parent 06955f6f
Loading
Loading
Loading
Loading
+8 −3
Original line number Diff line number Diff line
@@ -729,7 +729,13 @@ public class ChooserActivity extends ResolverActivity {
    /**
     * Returns true if app prediction service is defined and the component exists on device.
     */
    private boolean isAppPredictionServiceAvailable() {
    @VisibleForTesting
    public boolean isAppPredictionServiceAvailable() {
        if (getPackageManager().getAppPredictionServicePackageName() == null) {
            // Default AppPredictionService is not defined.
            return false;
        }

        final String appPredictionServiceName =
                getString(R.string.config_defaultAppPredictionService);
        if (appPredictionServiceName == null) {
@@ -1747,8 +1753,7 @@ public class ChooserActivity extends ResolverActivity {
        if (!mIsAppPredictorComponentAvailable) {
            return null;
        }
        if (mAppPredictor == null
                    && getPackageManager().getAppPredictionServicePackageName() != null) {
        if (mAppPredictor == null) {
            final IntentFilter filter = getTargetIntentFilter();
            Bundle extras = new Bundle();
            extras.putParcelable(APP_PREDICTION_INTENT_FILTER_KEY, filter);
+30 −0
Original line number Diff line number Diff line
@@ -807,6 +807,36 @@ public class ChooserActivityTest {
                is(testBaseScore * SHORTCUT_TARGET_SCORE_BOOST));
    }

    /**
     * The case when AppPrediction service is not defined in PackageManager is already covered
     * as a test parameter {@link ChooserActivityTest#packageManagers}. This test is checking the
     * case when the prediction service is defined but the component is not available on the device.
     */
    @Test
    public void testIsAppPredictionServiceAvailable() {
        Intent sendIntent = createSendTextIntent();
        List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
        when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
                Mockito.anyBoolean(),
                Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);

        final ChooserWrapperActivity activity = mActivityRule
                .launchActivity(Intent.createChooser(sendIntent, null));
        waitForIdle();

        if (activity.getPackageManager().getAppPredictionServicePackageName() == null) {
            assertThat(activity.isAppPredictionServiceAvailable(), is(false));
        } else {
            assertThat(activity.isAppPredictionServiceAvailable(), is(true));

            sOverrides.resources = Mockito.spy(activity.getResources());
            when(sOverrides.resources.getString(R.string.config_defaultAppPredictionService))
                    .thenReturn("ComponentNameThatDoesNotExist");

            assertThat(activity.isAppPredictionServiceAvailable(), is(false));
        }
    }

    // This test is too long and too slow and should not be taken as an example for future tests.
    @Test
    public void testDirectTargetSelectionLogging() throws InterruptedException {
+11 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
@@ -84,6 +85,14 @@ public class ChooserWrapperActivity extends ChooserActivity {
        return super.getPackageManager();
    }

    @Override
    public Resources getResources() {
        if (sOverrides.resources != null) {
            return sOverrides.resources;
        }
        return super.getResources();
    }

    @Override
    protected Bitmap loadThumbnail(Uri uri, Size size) {
        if (sOverrides.previewThumbnail != null) {
@@ -145,6 +154,7 @@ public class ChooserWrapperActivity extends ChooserActivity {
        public Bitmap previewThumbnail;
        public MetricsLogger metricsLogger;
        public int alternateProfileSetting;
        public Resources resources;

        public void reset() {
            onSafelyStartCallback = null;
@@ -157,6 +167,7 @@ public class ChooserWrapperActivity extends ChooserActivity {
            resolverListController = mock(ResolverListController.class);
            metricsLogger = mock(MetricsLogger.class);
            alternateProfileSetting = 0;
            resources = null;
        }
    }
}