| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.support.test.InstrumentationRegistry; |
| 8 import android.support.test.filters.MediumTest; | 9 import android.support.test.filters.MediumTest; |
| 9 import android.util.DisplayMetrics; | 10 import android.util.DisplayMetrics; |
| 10 import android.view.WindowManager; | 11 import android.view.WindowManager; |
| 11 | 12 |
| 13 import org.junit.Assert; |
| 14 import org.junit.Rule; |
| 15 import org.junit.Test; |
| 16 import org.junit.runner.RunWith; |
| 17 |
| 18 import org.chromium.base.test.BaseJUnit4ClassRunner; |
| 12 import org.chromium.base.test.util.Feature; | 19 import org.chromium.base.test.util.Feature; |
| 13 import org.chromium.content.browser.test.util.JavaScriptUtils; | 20 import org.chromium.content.browser.test.util.JavaScriptUtils; |
| 14 import org.chromium.content_shell_apk.ContentShellTestBase; | 21 import org.chromium.content_shell_apk.ContentShellActivityTestRule; |
| 15 | 22 |
| 16 /** | 23 /** |
| 17 * Test suite for viewport-related properties. | 24 * Test suite for viewport-related properties. |
| 18 */ | 25 */ |
| 19 public class ViewportTest extends ContentShellTestBase { | 26 @RunWith(BaseJUnit4ClassRunner.class) |
| 27 public class ViewportTest { |
| 28 @Rule |
| 29 public ContentShellActivityTestRule mActivityTestRule = new ContentShellActi
vityTestRule(); |
| 20 | 30 |
| 21 protected String evaluateStringValue(String expression) throws Throwable { | 31 protected String evaluateStringValue(String expression) throws Throwable { |
| 22 return JavaScriptUtils.executeJavaScriptAndWaitForResult(getWebContents(
), | 32 return JavaScriptUtils.executeJavaScriptAndWaitForResult( |
| 23 expression); | 33 mActivityTestRule.getWebContents(), expression); |
| 24 } | 34 } |
| 25 | 35 |
| 26 protected float evaluateFloatValue(String expression) throws Throwable { | 36 protected float evaluateFloatValue(String expression) throws Throwable { |
| 27 return Float.valueOf(evaluateStringValue(expression)); | 37 return Float.valueOf(evaluateStringValue(expression)); |
| 28 } | 38 } |
| 29 | 39 |
| 30 protected int evaluateIntegerValue(String expression) throws Throwable { | 40 protected int evaluateIntegerValue(String expression) throws Throwable { |
| 31 return Integer.parseInt(evaluateStringValue(expression)); | 41 return Integer.parseInt(evaluateStringValue(expression)); |
| 32 } | 42 } |
| 33 | 43 |
| 44 @Test |
| 34 @MediumTest | 45 @MediumTest |
| 35 @Feature({"Viewport", "InitialViewportSize"}) | 46 @Feature({"Viewport", "InitialViewportSize"}) |
| 36 public void testDefaultViewportSize() throws Throwable { | 47 public void testDefaultViewportSize() throws Throwable { |
| 37 launchContentShellWithUrl("about:blank"); | 48 mActivityTestRule.launchContentShellWithUrl("about:blank"); |
| 38 waitForActiveShellToBeDoneLoading(); | 49 mActivityTestRule.waitForActiveShellToBeDoneLoading(); |
| 39 | 50 |
| 40 Context context = getInstrumentation().getTargetContext(); | 51 Context context = InstrumentationRegistry.getInstrumentation().getTarget
Context(); |
| 41 WindowManager winManager = (WindowManager) context.getSystemService(Cont
ext.WINDOW_SERVICE); | 52 WindowManager winManager = (WindowManager) context.getSystemService(Cont
ext.WINDOW_SERVICE); |
| 42 DisplayMetrics metrics = new DisplayMetrics(); | 53 DisplayMetrics metrics = new DisplayMetrics(); |
| 43 winManager.getDefaultDisplay().getMetrics(metrics); | 54 winManager.getDefaultDisplay().getMetrics(metrics); |
| 44 | 55 |
| 45 // window.devicePixelRatio should match the default display. Only check
to 1 decimal place | 56 // window.devicePixelRatio should match the default display. Only check
to 1 decimal place |
| 46 // to allow for rounding. | 57 // to allow for rounding. |
| 47 assertEquals(metrics.density, evaluateFloatValue("window.devicePixelRati
o"), 0.1); | 58 Assert.assertEquals(metrics.density, evaluateFloatValue("window.devicePi
xelRatio"), 0.1); |
| 48 | 59 |
| 49 // Check that the viewport width is vaguely sensible. | 60 // Check that the viewport width is vaguely sensible. |
| 50 int viewportWidth = evaluateIntegerValue("document.documentElement.clien
tWidth"); | 61 int viewportWidth = evaluateIntegerValue("document.documentElement.clien
tWidth"); |
| 51 assertTrue(Math.abs(evaluateIntegerValue("window.innerWidth") - viewport
Width) <= 1); | 62 Assert.assertTrue(Math.abs(evaluateIntegerValue("window.innerWidth") - v
iewportWidth) <= 1); |
| 52 assertTrue(viewportWidth >= 979); | 63 Assert.assertTrue(viewportWidth >= 979); |
| 53 assertTrue(viewportWidth <= Math.max(981, metrics.widthPixels / metrics.
density + 1)); | 64 Assert.assertTrue( |
| 65 viewportWidth <= Math.max(981, metrics.widthPixels / metrics.den
sity + 1)); |
| 54 } | 66 } |
| 55 } | 67 } |
| OLD | NEW |