Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import static android.os.Build.VERSION_CODES.LOLLIPOP;
      4 import static com.google.common.truth.Truth.assertThat;
      5 import static org.robolectric.Shadows.shadowOf;
      6 
      7 import android.app.Activity;
      8 import android.app.ActivityManager.AppTask;
      9 import android.app.ActivityManager.RecentTaskInfo;
     10 import android.content.Intent;
     11 import androidx.test.ext.junit.runners.AndroidJUnit4;
     12 import org.junit.Test;
     13 import org.junit.runner.RunWith;
     14 import org.robolectric.Robolectric;
     15 import org.robolectric.annotation.Config;
     16 
     17 @RunWith(AndroidJUnit4.class)
     18 @Config(minSdk = LOLLIPOP)
     19 public class ShadowAppTaskTest {
     20 
     21   @Test
     22   public void finishAndRemoveTask_marksTaskFinished() {
     23     final AppTask appTask = ShadowAppTask.newInstance();
     24 
     25     appTask.finishAndRemoveTask();
     26 
     27     assertThat(shadowOf(appTask).isFinishedAndRemoved()).isTrue();
     28   }
     29 
     30   @Test
     31   public void taskIsNotFinishedInitially() {
     32     assertThat(shadowOf(ShadowAppTask.newInstance()).isFinishedAndRemoved()).isFalse();
     33   }
     34 
     35   @Test
     36   public void getTaskInfo_returnsNullInitially() {
     37     assertThat(ShadowAppTask.newInstance().getTaskInfo()).isNull();
     38   }
     39 
     40   @Test
     41   public void getTaskInfo_returnsCorrectValue() {
     42     final AppTask appTask = ShadowAppTask.newInstance();
     43     final RecentTaskInfo recentTaskInfo = new RecentTaskInfo();
     44     recentTaskInfo.description = "com.google.test";
     45 
     46     shadowOf(appTask).setTaskInfo(recentTaskInfo);
     47 
     48     assertThat(appTask.getTaskInfo()).isSameAs(recentTaskInfo);
     49   }
     50 
     51   @Test
     52   public void moveToFront_movesTaskToFront() {
     53     final AppTask appTask = ShadowAppTask.newInstance();
     54 
     55     appTask.moveToFront();
     56 
     57     assertThat(shadowOf(appTask).hasMovedToFront()).isTrue();
     58   }
     59 
     60   @Test
     61   public void taskIsNotMovedToFrontInitially() {
     62     assertThat(shadowOf(ShadowAppTask.newInstance()).hasMovedToFront()).isFalse();
     63   }
     64 
     65   @Test
     66   public void startActivity() {
     67     final AppTask appTask = ShadowAppTask.newInstance();
     68     final Activity activity = Robolectric.setupActivity(Activity.class);
     69     final Intent intent = new Intent(Intent.ACTION_VIEW);
     70 
     71     appTask.startActivity(activity, intent, null);
     72 
     73     assertThat(shadowOf(activity).peekNextStartedActivity()).isNotNull();
     74     assertThat(shadowOf(activity).peekNextStartedActivity().getAction())
     75         .isEqualTo(Intent.ACTION_VIEW);
     76   }
     77 
     78   @Test
     79   public void setExcludeFromRecents_excludesFromRecents() {
     80     final AppTask appTask = ShadowAppTask.newInstance();
     81 
     82     appTask.setExcludeFromRecents(true);
     83 
     84     assertThat(shadowOf(appTask).isExcludedFromRecents()).isTrue();
     85   }
     86 
     87   @Test
     88   public void taskIsNotExcludedFromRecentsInitially() {
     89     assertThat(shadowOf(ShadowAppTask.newInstance()).isExcludedFromRecents()).isFalse();
     90   }
     91 }
     92