/** * If {@link #setScaledDensity(float)} has been called, {@link DisplayMetrics#scaledDensity} will * be modified to reflect the value specified. Note that this is not a realistic state. * * @deprecated This behavior is deprecated and will be removed in Robolectric 3.7. */ @Deprecated @Implementation protected void getRealMetrics(DisplayMetrics outMetrics) { if (isJB()) { getMetrics(outMetrics); outMetrics.widthPixels = realWidth; outMetrics.heightPixels = realHeight; } else { directlyOn(realObject, Display.class).getRealMetrics(outMetrics); if (scaledDensity != null) { outMetrics.scaledDensity = scaledDensity; } } }
@Test @Config(minSdk = JELLY_BEAN_MR1) public void changeDisplay_shouldUpdateSmallestAndLargestNominalWidthAndHeight() throws Exception { Point smallest = new Point(); Point largest = new Point(); ShadowDisplay.getDefaultDisplay().getCurrentSizeRange(smallest, largest); assertThat(smallest).isEqualTo(new Point(320, 320)); assertThat(largest).isEqualTo(new Point(470, 470)); Display display = ShadowDisplay.getDefaultDisplay(); ShadowDisplay shadowDisplay = Shadow.extract(display); shadowDisplay.setWidth(display.getWidth() - 10); shadowDisplay.setHeight(display.getHeight() - 10); ShadowDisplay.getDefaultDisplay().getCurrentSizeRange(smallest, largest); assertThat(smallest).isEqualTo(new Point(310, 310)); assertThat(largest).isEqualTo(new Point(460, 460)); }
/** * Changes the density for this display. * * Any registered {@link android.hardware.display.DisplayManager.DisplayListener}s will be * notified of the change. */ public void setDensity(float density) { setDensityDpi((int) (density * DisplayMetrics.DENSITY_DEFAULT)); }
private void setDisplayDimens(Integer width, Integer height) { WindowManager windowManager = (WindowManager) RuntimeEnvironment.application.getSystemService(Context.WINDOW_SERVICE); Display display = Preconditions.checkNotNull(windowManager).getDefaultDisplay(); if (width != null) { Shadows.shadowOf(display).setWidth(width); } if (height != null) { Shadows.shadowOf(display).setHeight(height); } }
@Test public void shouldProvideDisplayMetrics() throws Exception { shadow.setDensity(1.5f); shadow.setDensityDpi(DisplayMetrics.DENSITY_HIGH); shadow.setScaledDensity(1.6f); shadow.setWidth(1024); shadow.setHeight(600); shadow.setRealWidth(1400); shadow.setRealHeight(900); shadow.setXdpi(183.0f); shadow.setYdpi(184.0f); shadow.setRefreshRate(123f);
@Test public void shouldProvideDisplaySize() throws Exception { Point outSmallestSize = new Point(); Point outLargestSize = new Point(); Point outSize = new Point(); Rect outRect = new Rect(); shadow.setWidth(400); shadow.setHeight(600); shadow.setRealWidth(480); shadow.setRealHeight(800); display.getCurrentSizeRange(outSmallestSize, outLargestSize); assertEquals(400, outSmallestSize.x); assertEquals(400, outSmallestSize.y); assertEquals(600, outLargestSize.x); assertEquals(600, outLargestSize.y); display.getSize(outSize); assertEquals(400, outSize.x); assertEquals(600, outSize.y); display.getRectSize(outRect); assertEquals(400, outRect.width()); assertEquals(600, outRect.height()); display.getRealSize(outSize); assertEquals(480, outSize.x); assertEquals(800, outSize.y); }
@Test public void changedStateShouldApplyToOtherInstancesOfSameDisplay() throws Exception { shadow.setName("another name"); shadow.setWidth(1024); shadow.setHeight(600); display = DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY); assertEquals(1024, display.getWidth()); assertEquals(600, display.getHeight()); assertEquals("another name", display.getName()); }
/** * Changes the horizontal DPI for this display. * * Any registered {@link android.hardware.display.DisplayManager.DisplayListener}s will be * notified of the change. */ public void setXdpi(float xdpi) { if (isJB()) { this.xdpi = xdpi; } else { ShadowDisplayManager.changeDisplay(realObject.getDisplayId(), di -> di.physicalXDpi = xdpi); } }
@Implementation public void getRealMetrics(DisplayMetrics outMetrics) { getMetrics(outMetrics); outMetrics.widthPixels = realWidth; outMetrics.heightPixels = realHeight; }
@Implementation public int getOrientation() { return getRotation(); }
.getSystemService(Context.WINDOW_SERVICE)) .getDefaultDisplay()) .setHeight(0); // no preload shadowOf((ConnectivityManager) RuntimeEnvironment.application .getSystemService(Context.CONNECTIVITY_SERVICE))
@Test @Config(sdk = KITKAT) public void shouldLoadDisplay() throws Exception { ReflectionHelpers.callInstanceMethod( Display.class, ShadowDisplay.getDefaultDisplay(), "getDisplayAdjustments"); }
/** internal only */ public static void configureDefaultDisplayForJBOnly( Configuration configuration, DisplayMetrics displayMetrics) { Class<?> arg2Type = ReflectionHelpers.loadClass(ShadowWindowManagerImpl.class.getClassLoader(), "android.view.CompatibilityInfoHolder"); defaultDisplayJB = ReflectionHelpers.callConstructor(Display.class, ClassParameter.from(int.class, 0), ClassParameter.from(arg2Type, null)); ShadowDisplay shadowDisplay = Shadow.extract(defaultDisplayJB); shadowDisplay.configureForJBOnly(configuration, displayMetrics); }
private void setDisplayDimens(Integer width, Integer height) { WindowManager windowManager = (WindowManager) RuntimeEnvironment.application.getSystemService(Context.WINDOW_SERVICE); Display display = Preconditions.checkNotNull(windowManager).getDefaultDisplay(); if (width != null) { Shadows.shadowOf(display).setWidth(width); } if (height != null) { Shadows.shadowOf(display).setHeight(height); } }
/** * Changes the vertical DPI for this display. * * Any registered {@link android.hardware.display.DisplayManager.DisplayListener}s will be * notified of the change. */ public void setYdpi(float ydpi) { if (isJB()) { this.ydpi = ydpi; } else { ShadowDisplayManager.changeDisplay(realObject.getDisplayId(), di -> di.physicalYDpi = ydpi); } }
@Implementation public void getRealMetrics(DisplayMetrics outMetrics) { getMetrics(outMetrics); outMetrics.widthPixels = realWidth; outMetrics.heightPixels = realHeight; }
@Implementation public int getOrientation() { return getRotation(); }
@Test @Config(minSdk = JELLY_BEAN_MR1) public void withQualifiers_changeDisplay_shouldUpdateSmallestAndLargestNominalWidthAndHeight() throws Exception { Point smallest = new Point(); Point largest = new Point(); Display display = ShadowDisplay.getDefaultDisplay(); display.getCurrentSizeRange(smallest, largest); assertThat(smallest).isEqualTo(new Point(320, 320)); assertThat(largest).isEqualTo(new Point(470, 470)); ShadowDisplayManager.changeDisplay(display.getDisplayId(), "w310dp-h460dp"); display.getCurrentSizeRange(smallest, largest); assertThat(smallest).isEqualTo(new Point(310, 310)); assertThat(largest).isEqualTo(new Point(460, 460)); }
/** internal only */ public static void configureDefaultDisplayForJBOnly( Configuration configuration, DisplayMetrics displayMetrics) { Class<?> arg2Type = ReflectionHelpers.loadClass(ShadowWindowManagerImpl.class.getClassLoader(), "android.view.CompatibilityInfoHolder"); defaultDisplayJB = ReflectionHelpers.callConstructor(Display.class, ClassParameter.from(int.class, 0), ClassParameter.from(arg2Type, null)); ShadowDisplay shadowDisplay = Shadow.extract(defaultDisplayJB); shadowDisplay.configureForJBOnly(configuration, displayMetrics); }
/** * If {@link #setScaledDensity(float)} has been called, {@link DisplayMetrics#scaledDensity} will * be modified to reflect the value specified. Note that this is not a realistic state. * * @deprecated This behavior is deprecated and will be removed in Robolectric 3.7. */ @Deprecated @Implementation protected void getRealMetrics(DisplayMetrics outMetrics) { if (isJB()) { getMetrics(outMetrics); outMetrics.widthPixels = realWidth; outMetrics.heightPixels = realHeight; } else { directlyOn(realObject, Display.class).getRealMetrics(outMetrics); if (scaledDensity != null) { outMetrics.scaledDensity = scaledDensity; } } }