- add sources.
[platform/framework/web/crosswalk.git] / src / content / public / android / javatests / src / org / chromium / content / browser / ViewportTest.java
1 // Copyright (c) 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.
4
5 package org.chromium.content.browser;
6
7 import android.content.Context;
8 import android.test.suitebuilder.annotation.MediumTest;
9 import android.util.DisplayMetrics;
10 import android.view.Display;
11 import android.view.WindowManager;
12
13 import org.chromium.base.test.util.DisabledTest;
14 import org.chromium.base.test.util.Feature;
15 import org.chromium.content.browser.test.util.JavaScriptUtils;
16 import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
17
18 /**
19  * Test suite for viewport-related properties.
20  */
21 public class ViewportTest extends ContentViewTestBase {
22
23     private TestCallbackHelperContainer mCallbackHelper;
24
25     /**
26      * Returns the TestCallbackHelperContainer associated with this ContentView,
27      * or creates it lazily.
28      */
29     protected TestCallbackHelperContainer getTestCallbackHelperContainer() {
30         if (mCallbackHelper == null) {
31             mCallbackHelper = new TestCallbackHelperContainer(getContentView());
32         }
33         return mCallbackHelper;
34     }
35
36     protected String evaluateStringValue(String expression) throws Throwable {
37         return JavaScriptUtils.executeJavaScriptAndWaitForResult(getContentView(),
38                 getTestCallbackHelperContainer(), expression);
39     }
40
41     protected float evaluateFloatValue(String expression) throws Throwable {
42         return Float.valueOf(evaluateStringValue(expression));
43     }
44
45     protected int evaluateIntegerValue(String expression) throws Throwable {
46         return Integer.valueOf(evaluateStringValue(expression));
47     }
48
49     /*
50     @MediumTest
51     @Feature({"Viewport", "InitialViewportSize"})
52     https://bugs.webkit.org/show_bug.cgi?id=107424
53     */
54     @DisabledTest
55     public void testDefaultViewportSize() throws Throwable {
56         launchContentShellWithUrl("about:blank");
57         waitForActiveShellToBeDoneLoading();
58
59         Context context = getInstrumentation().getTargetContext();
60         WindowManager winManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
61         DisplayMetrics metrics = new DisplayMetrics();
62         winManager.getDefaultDisplay().getMetrics(metrics);
63
64         // window.devicePixelRatio should match the default display. Only check to 1 decimal place
65         // to allow for rounding.
66         assertEquals(String.format("%.1g", metrics.density),
67                 String.format("%.1g", evaluateFloatValue("window.devicePixelRatio")));
68
69         // Check that the viewport width is vaguely sensible.
70         int viewportWidth = evaluateIntegerValue("document.documentElement.clientWidth");
71         assertTrue(Math.abs(evaluateIntegerValue("window.innerWidth") - viewportWidth) <= 1);
72         assertTrue(viewportWidth >= 979);
73         assertTrue(viewportWidth <= Math.max(981, metrics.widthPixels / metrics.density + 1));
74     }
75 }