| 1 | // Copyright (c) 2013 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.android_webview.test; |
| 6 | |
| 7 | import android.test.suitebuilder.annotation.MediumTest; |
| 8 | import org.chromium.android_webview.AwContents; |
| 9 | import org.chromium.android_webview.AwSettings; |
| 10 | import org.chromium.base.test.util.Feature; |
| 11 | import org.chromium.content.browser.test.util.CallbackHelper; |
| 12 | import org.chromium.ui.gfx.DeviceDisplayInfo; |
| 13 | |
| 14 | public class AwTargetDensityDpiTest extends AwTestBase { |
| 15 | |
| 16 | @MediumTest |
| 17 | @Feature({"AndroidWebView"}) |
| 18 | public void testTargetDensityDpi() throws Throwable { |
| 19 | final TestAwContentsClient contentClient = new TestAwContentsClient(); |
| 20 | final AwTestContainerView testContainerView = |
| 21 | createAwTestContainerViewOnMainSync(contentClient); |
| 22 | final AwContents awContents = testContainerView.getAwContents(); |
| 23 | AwSettings settings = getAwSettingsOnUiThread(awContents); |
| 24 | CallbackHelper onPageFinishedHelper = contentClient.getOnPageFinishedHelper(); |
| 25 | |
| 26 | final String pageTemplate = "<html><head>" + |
| 27 | "<meta name='viewport' content='width=device-width, target-densityDpi=%s' />" + |
| 28 | "</head><body onload='document.title=document.body.clientWidth'></body></html>"; |
| 29 | final String pageDeviceDpi = String.format(pageTemplate, "device-dpi"); |
| 30 | final String pageHighDpi = String.format(pageTemplate, "high-dpi"); |
| 31 | final String pageDpi100 = String.format(pageTemplate, "100"); |
| 32 | |
| 33 | settings.setJavaScriptEnabled(true); |
| 34 | |
| 35 | DeviceDisplayInfo deviceInfo = |
| 36 | DeviceDisplayInfo.create(getInstrumentation().getTargetContext()); |
| 37 | loadDataSync(awContents, onPageFinishedHelper, pageDeviceDpi, "text/html", false); |
| 38 | int actualWidth = Integer.parseInt(getTitleOnUiThread(awContents)); |
| 39 | assertEquals((float)deviceInfo.getDisplayWidth(), (float)actualWidth, 10f); |
| 40 | |
| 41 | float displayWidth = (float)(deviceInfo.getDisplayWidth()); |
| 42 | float deviceDpi = (float)(160f * deviceInfo.getDIPScale()); |
| 43 | |
| 44 | loadDataSync(awContents, onPageFinishedHelper, pageHighDpi, "text/html", false); |
| 45 | actualWidth = Integer.parseInt(getTitleOnUiThread(awContents)); |
| 46 | assertEquals(displayWidth * (240f / deviceDpi), (float)actualWidth, 10f); |
| 47 | |
| 48 | loadDataSync(awContents, onPageFinishedHelper, pageDpi100, "text/html", false); |
| 49 | actualWidth = Integer.parseInt(getTitleOnUiThread(awContents)); |
| 50 | assertEquals(displayWidth * (100f / deviceDpi), (float)actualWidth, 10f); |
| 51 | } |
| 52 | } |