1 package org.robolectric.shadows; 2 3 import static android.os.Build.VERSION_CODES.O; 4 import static com.google.common.truth.Truth.assertThat; 5 import static org.robolectric.Shadows.shadowOf; 6 7 import android.content.Context; 8 import android.graphics.PixelFormat; 9 import android.view.View; 10 import android.view.ViewGroup; 11 import android.view.WindowManager; 12 import android.view.WindowManager.LayoutParams; 13 import androidx.test.core.app.ApplicationProvider; 14 import androidx.test.ext.junit.runners.AndroidJUnit4; 15 import java.util.List; 16 import org.junit.Before; 17 import org.junit.Test; 18 import org.junit.runner.RunWith; 19 import org.robolectric.annotation.Config; 20 21 /** Unit test for {@link ShadowWindowManagerImpl}. */ 22 @RunWith(AndroidJUnit4.class) 23 @Config(minSdk = O) 24 public class ShadowWindowManagerImplTest { 25 26 private View view; 27 private LayoutParams layoutParams; 28 private WindowManager windowManager; 29 30 @Before setUp()31 public void setUp() { 32 Context context = ApplicationProvider.getApplicationContext(); 33 view = new View(context); 34 windowManager = context.getSystemService(WindowManager.class); 35 layoutParams = 36 new LayoutParams( 37 /*w=*/ ViewGroup.LayoutParams.MATCH_PARENT, 38 /*h=*/ ViewGroup.LayoutParams.MATCH_PARENT, 39 LayoutParams.TYPE_APPLICATION_OVERLAY, 40 LayoutParams.FLAG_LAYOUT_IN_SCREEN, 41 PixelFormat.TRANSLUCENT); 42 } 43 44 @Test getViews_isInitiallyEmpty()45 public void getViews_isInitiallyEmpty() { 46 List<View> views = ((ShadowWindowManagerImpl) shadowOf(windowManager)).getViews(); 47 48 assertThat(views).isEmpty(); 49 } 50 51 @Test getViews_returnsAnAddedView()52 public void getViews_returnsAnAddedView() { 53 windowManager.addView(view, layoutParams); 54 55 List<View> views = ((ShadowWindowManagerImpl) shadowOf(windowManager)).getViews(); 56 57 assertThat(views).hasSize(1); 58 assertThat(views.get(0)).isSameInstanceAs(view); 59 } 60 61 @Test getViews_doesNotReturnARemovedView()62 public void getViews_doesNotReturnARemovedView() { 63 windowManager.addView(view, layoutParams); 64 windowManager.removeView(view); 65 66 List<View> views = ((ShadowWindowManagerImpl) shadowOf(windowManager)).getViews(); 67 68 assertThat(views).isEmpty(); 69 } 70 } 71