• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.google.common.truth.Truth.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 androidx.test.core.app.ApplicationProvider;
17 import androidx.test.ext.junit.runners.AndroidJUnit4;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.robolectric.Robolectric;
21 import org.robolectric.android.controller.ActivityController;
22 import org.robolectric.annotation.Config;
23 
24 @RunWith(AndroidJUnit4.class)
25 public class ShadowWindowTest {
26   @Test
getFlag_shouldReturnWindowFlags()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
getTitle_shouldReturnWindowTitle()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
getBackgroundDrawable_returnsSetDrawable()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
getSoftInputMode_returnsSoftInputMode()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
getProgressBar_returnsTheProgressBar()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
getIndeterminateProgressBar_returnsTheIndeterminateProgressBar()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)
forPreM_create_shouldCreateImplPhoneWindow()99   public void forPreM_create_shouldCreateImplPhoneWindow() throws Exception {
100     assertThat(
101             ShadowWindow.create(ApplicationProvider.getApplicationContext()).getClass().getName())
102         .isEqualTo("com.android.internal.policy.impl.PhoneWindow");
103   }
104 
105   @Test @Config(minSdk = M)
forM_create_shouldCreatePhoneWindow()106   public void forM_create_shouldCreatePhoneWindow() throws Exception {
107     assertThat(
108             ShadowWindow.create(ApplicationProvider.getApplicationContext()).getClass().getName())
109         .isEqualTo("com.android.internal.policy.PhoneWindow");
110   }
111 
112   public static class TestActivity extends Activity {
113     public int requestFeature = Window.FEATURE_PROGRESS;
114 
115     @Override
onCreate(Bundle savedInstanceState)116     protected void onCreate(Bundle savedInstanceState) {
117       super.onCreate(savedInstanceState);
118       setTheme(R.style.Theme_Holo_Light);
119       getWindow().requestFeature(requestFeature);
120       setContentView(new LinearLayout(this));
121       getActionBar().setIcon(R.drawable.ic_lock_power_off);
122     }
123   }
124 }
125