Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import static android.os.Build.VERSION_CODES.LOLLIPOP_MR1;
      4 import static android.os.Build.VERSION_CODES.M;
      5 import static org.assertj.core.api.Assertions.assertThat;
      6 import static org.robolectric.Shadows.shadowOf;
      7 
      8 import android.R;
      9 import android.app.Activity;
     10 import android.os.Bundle;
     11 import android.view.View;
     12 import android.view.Window;
     13 import android.view.WindowManager;
     14 import android.widget.LinearLayout;
     15 import android.widget.ProgressBar;
     16 import org.junit.Test;
     17 import org.junit.runner.RunWith;
     18 import org.robolectric.Robolectric;
     19 import org.robolectric.RobolectricTestRunner;
     20 import org.robolectric.RuntimeEnvironment;
     21 import org.robolectric.android.controller.ActivityController;
     22 import org.robolectric.annotation.Config;
     23 
     24 @RunWith(RobolectricTestRunner.class)
     25 public class ShadowWindowTest {
     26   @Test
     27   public void getFlag_shouldReturnWindowFlags() throws Exception {
     28     Activity activity = Robolectric.buildActivity(Activity.class).create().get();
     29     Window window = activity.getWindow();
     30 
     31     assertThat(shadowOf(window).getFlag(WindowManager.LayoutParams.FLAG_FULLSCREEN)).isFalse();
     32     window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
     33     assertThat(shadowOf(window).getFlag(WindowManager.LayoutParams.FLAG_FULLSCREEN)).isTrue();
     34     window.setFlags(WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON, WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
     35     assertThat(shadowOf(window).getFlag(WindowManager.LayoutParams.FLAG_FULLSCREEN)).isTrue();
     36   }
     37 
     38   @Test
     39   public void getTitle_shouldReturnWindowTitle() throws Exception {
     40     Activity activity = Robolectric.buildActivity(Activity.class).create().get();
     41     Window window = activity.getWindow();
     42     window.setTitle("My Window Title");
     43     assertThat(shadowOf(window).getTitle()).isEqualTo("My Window Title");
     44   }
     45 
     46   @Test
     47   public void getBackgroundDrawable_returnsSetDrawable() throws Exception {
     48     Activity activity = Robolectric.buildActivity(Activity.class).create().get();
     49     Window window = activity.getWindow();
     50     ShadowWindow shadowWindow = shadowOf(window);
     51 
     52     assertThat(shadowWindow.getBackgroundDrawable()).isNull();
     53 
     54     window.setBackgroundDrawableResource(R.drawable.btn_star);
     55     assertThat(shadowOf(shadowWindow.getBackgroundDrawable()).createdFromResId).isEqualTo(R.drawable.btn_star);
     56   }
     57 
     58   @Test
     59   public void getSoftInputMode_returnsSoftInputMode() throws Exception {
     60     TestActivity activity = Robolectric.buildActivity(TestActivity.class).create().get();
     61     Window window = activity.getWindow();
     62     ShadowWindow shadowWindow = shadowOf(window);
     63 
     64     window.setSoftInputMode(7);
     65 
     66     assertThat(shadowWindow.getSoftInputMode()).isEqualTo(7);
     67   }
     68 
     69   @Test
     70   public void getProgressBar_returnsTheProgressBar() {
     71     Activity activity = Robolectric.buildActivity(TestActivity.class).create().get();
     72 
     73     ProgressBar progress = shadowOf(activity.getWindow()).getProgressBar();
     74 
     75     assertThat(progress.getVisibility()).isEqualTo(View.INVISIBLE);
     76     activity.setProgressBarVisibility(true);
     77     assertThat(progress.getVisibility()).isEqualTo(View.VISIBLE);
     78     activity.setProgressBarVisibility(false);
     79     assertThat(progress.getVisibility()).isEqualTo(View.GONE);
     80   }
     81 
     82   @Test
     83   public void getIndeterminateProgressBar_returnsTheIndeterminateProgressBar() {
     84     ActivityController<TestActivity> testActivityActivityController = Robolectric.buildActivity(TestActivity.class);
     85     TestActivity activity = testActivityActivityController.get();
     86     activity.requestFeature = Window.FEATURE_INDETERMINATE_PROGRESS;
     87     testActivityActivityController.create();
     88 
     89     ProgressBar indeterminate = shadowOf(activity.getWindow()).getIndeterminateProgressBar();
     90 
     91     assertThat(indeterminate.getVisibility()).isEqualTo(View.INVISIBLE);
     92     activity.setProgressBarIndeterminateVisibility(true);
     93     assertThat(indeterminate.getVisibility()).isEqualTo(View.VISIBLE);
     94     activity.setProgressBarIndeterminateVisibility(false);
     95     assertThat(indeterminate.getVisibility()).isEqualTo(View.GONE);
     96   }
     97 
     98   @Test @Config(maxSdk = LOLLIPOP_MR1)
     99   public void forPreM_create_shouldCreateImplPhoneWindow() throws Exception {
    100     assertThat(ShadowWindow.create(RuntimeEnvironment.application).getClass().getName())
    101         .isEqualTo("com.android.internal.policy.impl.PhoneWindow");
    102   }
    103 
    104   @Test @Config(minSdk = M)
    105   public void forM_create_shouldCreatePhoneWindow() throws Exception {
    106     assertThat(ShadowWindow.create(RuntimeEnvironment.application).getClass().getName())
    107         .isEqualTo("com.android.internal.policy.PhoneWindow");
    108   }
    109 
    110   public static class TestActivity extends Activity {
    111     public int requestFeature = Window.FEATURE_PROGRESS;
    112 
    113     @Override
    114     protected void onCreate(Bundle savedInstanceState) {
    115       super.onCreate(savedInstanceState);
    116       setTheme(R.style.Theme_Holo_Light);
    117       getWindow().requestFeature(requestFeature);
    118       setContentView(new LinearLayout(this));
    119       getActionBar().setIcon(R.drawable.ic_lock_power_off);
    120     }
    121   }
    122 }
    123