Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
      4 import static android.os.Build.VERSION_CODES.KITKAT;
      5 import static android.os.Build.VERSION_CODES.LOLLIPOP;
      6 import static android.os.Build.VERSION_CODES.N;
      7 import static com.google.common.truth.Truth.assertThat;
      8 import static org.robolectric.Shadows.shadowOf;
      9 
     10 import android.app.Application;
     11 import android.app.PendingIntent;
     12 import android.app.WallpaperManager;
     13 import android.bluetooth.BluetoothManager;
     14 import android.content.ComponentName;
     15 import android.content.Context;
     16 import android.content.Intent;
     17 import android.content.IntentSender;
     18 import android.content.ServiceConnection;
     19 import android.os.IBinder;
     20 import android.os.Process;
     21 import android.view.LayoutInflater;
     22 import android.widget.FrameLayout;
     23 import android.widget.RemoteViews;
     24 import androidx.test.core.app.ApplicationProvider;
     25 import androidx.test.ext.junit.runners.AndroidJUnit4;
     26 import java.io.File;
     27 import org.junit.Test;
     28 import org.junit.runner.RunWith;
     29 import org.robolectric.R;
     30 import org.robolectric.annotation.Config;
     31 import org.robolectric.shadow.api.Shadow;
     32 
     33 @RunWith(AndroidJUnit4.class)
     34 public class ShadowContextImplTest {
     35   private final Application context = ApplicationProvider.getApplicationContext();
     36   private final ShadowContextImpl shadowContext = Shadow.extract(context.getBaseContext());
     37 
     38   @Test
     39   @Config(minSdk = N)
     40   public void deviceProtectedContext() {
     41     // Regular context should be credential protected, not device protected.
     42     assertThat(context.isDeviceProtectedStorage()).isFalse();
     43     assertThat(context.isCredentialProtectedStorage()).isFalse();
     44 
     45     // Device protected storage context should have device protected rather than credential
     46     // protected storage.
     47     Context deviceProtectedStorageContext = context.createDeviceProtectedStorageContext();
     48     assertThat(deviceProtectedStorageContext.isDeviceProtectedStorage()).isTrue();
     49     assertThat(deviceProtectedStorageContext.isCredentialProtectedStorage()).isFalse();
     50 
     51     // Data dirs of these two contexts must be different locations.
     52     assertThat(context.getDataDir()).isNotEqualTo(deviceProtectedStorageContext.getDataDir());
     53   }
     54 
     55   @Test
     56   @Config(minSdk = N)
     57   public void testMoveSharedPreferencesFrom() throws Exception {
     58     String PREFS = "PREFS";
     59     String PREF_NAME = "TOKEN_PREF";
     60 
     61     context
     62         .getSharedPreferences(PREFS, Context.MODE_PRIVATE)
     63         .edit()
     64         .putString(PREF_NAME, "token")
     65         .commit();
     66 
     67     Context dpContext = context.createDeviceProtectedStorageContext();
     68 
     69     assertThat(dpContext.getSharedPreferences(PREFS, Context.MODE_PRIVATE).contains(PREF_NAME))
     70         .isFalse();
     71     assertThat(context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).contains(PREF_NAME))
     72         .isTrue();
     73 
     74     assertThat(dpContext.moveSharedPreferencesFrom(context, PREFS)).isTrue();
     75 
     76     assertThat(dpContext.getSharedPreferences(PREFS, Context.MODE_PRIVATE).contains(PREF_NAME))
     77         .isTrue();
     78     assertThat(context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).contains(PREF_NAME))
     79         .isFalse();
     80   }
     81 
     82   @Config(minSdk = KITKAT)
     83   @Test
     84   public void getExternalFilesDirs() {
     85     File[] dirs = context.getExternalFilesDirs("something");
     86     assertThat(dirs).asList().hasSize(1);
     87     assertThat(dirs[0].isDirectory()).isTrue();
     88     assertThat(dirs[0].canWrite()).isTrue();
     89     assertThat(dirs[0].getName()).isEqualTo("something");
     90   }
     91 
     92   @Test
     93   @Config(minSdk = JELLY_BEAN_MR2)
     94   public void getSystemService_shouldReturnBluetoothAdapter() {
     95     assertThat(context.getSystemService(Context.BLUETOOTH_SERVICE))
     96         .isInstanceOf(BluetoothManager.class);
     97   }
     98 
     99   @Test
    100   public void getSystemService_shouldReturnWallpaperManager() {
    101     assertThat(context.getSystemService(Context.WALLPAPER_SERVICE))
    102         .isInstanceOf(WallpaperManager.class);
    103   }
    104 
    105   @Test
    106   public void removeSystemService_getSystemServiceReturnsNull() {
    107     shadowContext.removeSystemService(Context.WALLPAPER_SERVICE);
    108     assertThat(context.getSystemService(Context.WALLPAPER_SERVICE)).isNull();
    109   }
    110 
    111   @Test
    112   public void startIntentSender_activityIntent() throws IntentSender.SendIntentException {
    113     PendingIntent intent =
    114         PendingIntent.getActivity(
    115             context,
    116             0,
    117             new Intent()
    118                 .setClassName(context, "ActivityIntent")
    119                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
    120             PendingIntent.FLAG_UPDATE_CURRENT);
    121 
    122     context.startIntentSender(intent.getIntentSender(), null, 0, 0, 0);
    123 
    124     assertThat(shadowOf(context).getNextStartedActivity().getComponent().getClassName())
    125         .isEqualTo("ActivityIntent");
    126   }
    127 
    128   @Test
    129   public void startIntentSender_broadcastIntent() throws IntentSender.SendIntentException {
    130     PendingIntent intent =
    131         PendingIntent.getBroadcast(
    132             context,
    133             0,
    134             new Intent().setClassName(context, "BroadcastIntent"),
    135             PendingIntent.FLAG_UPDATE_CURRENT);
    136 
    137     context.startIntentSender(intent.getIntentSender(), null, 0, 0, 0);
    138 
    139     assertThat(shadowOf(context).getBroadcastIntents().get(0).getComponent().getClassName())
    140         .isEqualTo("BroadcastIntent");
    141   }
    142 
    143   @Test
    144   public void startIntentSender_serviceIntent() throws IntentSender.SendIntentException {
    145     PendingIntent intent =
    146         PendingIntent.getService(
    147             context,
    148             0,
    149             new Intent().setClassName(context, "ServiceIntent"),
    150             PendingIntent.FLAG_UPDATE_CURRENT);
    151 
    152     context.startIntentSender(intent.getIntentSender(), null, 0, 0, 0);
    153 
    154     assertThat(shadowOf(context).getNextStartedService().getComponent().getClassName())
    155         .isEqualTo("ServiceIntent");
    156   }
    157 
    158   @Test
    159   public void createPackageContext() throws Exception {
    160     Context packageContext = context.createPackageContext(context.getPackageName(), 0);
    161 
    162     LayoutInflater inflater =
    163         (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    164     inflater.cloneInContext(packageContext);
    165 
    166     inflater.inflate(R.layout.remote_views, new FrameLayout(context), false);
    167   }
    168 
    169   @Test
    170   public void createPackageContextRemoteViews() throws Exception {
    171     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.remote_views);
    172     remoteViews.apply(context, new FrameLayout(context));
    173   }
    174 
    175   @Test
    176   @Config(minSdk = LOLLIPOP)
    177   public void bindServiceAsUser() throws Exception {
    178     Intent serviceIntent = new Intent();
    179     ServiceConnection serviceConnection = buildServiceConnection();
    180     int flags = 0;
    181 
    182     assertThat(
    183             context.bindServiceAsUser(
    184                 serviceIntent, serviceConnection, flags, Process.myUserHandle()))
    185         .isTrue();
    186 
    187     assertThat(shadowOf(context).getBoundServiceConnections()).hasSize(1);
    188   }
    189 
    190   @Test
    191   public void bindService() throws Exception {
    192     Intent serviceIntent = new Intent();
    193     ServiceConnection serviceConnection = buildServiceConnection();
    194     int flags = 0;
    195 
    196     assertThat(context.bindService(serviceIntent, serviceConnection, flags)).isTrue();
    197 
    198     assertThat(shadowOf(context).getBoundServiceConnections()).hasSize(1);
    199   }
    200 
    201   @Test
    202   public void bindService_unbindable() throws Exception {
    203     String action = "foo-action";
    204     Intent serviceIntent = new Intent(action);
    205     ServiceConnection serviceConnection = buildServiceConnection();
    206     int flags = 0;
    207     shadowOf(context).declareActionUnbindable(action);
    208 
    209     assertThat(context.bindService(serviceIntent, serviceConnection, flags)).isFalse();
    210   }
    211 
    212   private ServiceConnection buildServiceConnection() {
    213     return new ServiceConnection() {
    214       @Override
    215       public void onServiceConnected(ComponentName name, IBinder service) {}
    216 
    217       @Override
    218       public void onServiceDisconnected(ComponentName name) {}
    219     };
    220   }
    221 }
    222