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
3 // found in the LICENSE file.
5 package org.chromium.content.browser;
7 import android.content.Context;
8 import android.util.DisplayMetrics;
9 import android.view.WindowManager;
11 import org.chromium.base.test.util.DisabledTest;
12 import org.chromium.content.browser.test.util.JavaScriptUtils;
15 * Test suite for viewport-related properties.
17 public class ViewportTest extends ContentViewTestBase {
19 protected String evaluateStringValue(String expression) throws Throwable {
20 return JavaScriptUtils.executeJavaScriptAndWaitForResult(getWebContents(),
24 protected float evaluateFloatValue(String expression) throws Throwable {
25 return Float.valueOf(evaluateStringValue(expression));
28 protected int evaluateIntegerValue(String expression) throws Throwable {
29 return Integer.parseInt(evaluateStringValue(expression));
34 @Feature({"Viewport", "InitialViewportSize"})
35 https://bugs.webkit.org/show_bug.cgi?id=107424
38 public void testDefaultViewportSize() throws Throwable {
39 launchContentShellWithUrl("about:blank");
40 waitForActiveShellToBeDoneLoading();
42 Context context = getInstrumentation().getTargetContext();
43 WindowManager winManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
44 DisplayMetrics metrics = new DisplayMetrics();
45 winManager.getDefaultDisplay().getMetrics(metrics);
47 // window.devicePixelRatio should match the default display. Only check to 1 decimal place
48 // to allow for rounding.
49 assertEquals(metrics.density, evaluateFloatValue("window.devicePixelRatio"), 0.1);
51 // Check that the viewport width is vaguely sensible.
52 int viewportWidth = evaluateIntegerValue("document.documentElement.clientWidth");
53 assertTrue(Math.abs(evaluateIntegerValue("window.innerWidth") - viewportWidth) <= 1);
54 assertTrue(viewportWidth >= 979);
55 assertTrue(viewportWidth <= Math.max(981, metrics.widthPixels / metrics.density + 1));