// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.android_webview.test;

import android.test.suitebuilder.annotation.MediumTest;
import org.chromium.android_webview.AwContents;
import org.chromium.android_webview.AwSettings;
import org.chromium.base.test.util.Feature;
import org.chromium.content.browser.test.util.CallbackHelper;
import org.chromium.ui.gfx.DeviceDisplayInfo;

public class AwTargetDensityDpiTest extends AwTestBase {

    @MediumTest
    @Feature({"AndroidWebView"})
    public void testTargetDensityDpi() throws Throwable {
        final TestAwContentsClient contentClient = new TestAwContentsClient();
        final AwTestContainerView testContainerView =
                createAwTestContainerViewOnMainSync(contentClient);
        final AwContents awContents = testContainerView.getAwContents();
        AwSettings settings = getAwSettingsOnUiThread(awContents);
        CallbackHelper onPageFinishedHelper = contentClient.getOnPageFinishedHelper();

        final String pageTemplate = "<html><head>" +
                "<meta name='viewport' content='width=device-width, target-densityDpi=%s' />" +
                "</head><body onload='document.title=document.body.clientWidth'></body></html>";
        final String pageDeviceDpi = String.format(pageTemplate, "device-dpi");
        final String pageHighDpi = String.format(pageTemplate, "high-dpi");
        final String pageDpi100 = String.format(pageTemplate, "100");

        settings.setJavaScriptEnabled(true);

        DeviceDisplayInfo deviceInfo =
                DeviceDisplayInfo.create(getInstrumentation().getTargetContext());
        loadDataSync(awContents, onPageFinishedHelper, pageDeviceDpi, "text/html", false);
        int actualWidth = Integer.parseInt(getTitleOnUiThread(awContents));
        assertEquals((float)deviceInfo.getDisplayWidth(), (float)actualWidth, 10f);

        float displayWidth = (float)(deviceInfo.getDisplayWidth() / deviceInfo.getDIPScale());
        float deviceDpi = (float)(120f * deviceInfo.getDIPScale());
        loadDataSync(awContents, onPageFinishedHelper, pageHighDpi, "text/html", false);
        actualWidth = Integer.parseInt(getTitleOnUiThread(awContents));
        assertEquals(displayWidth * (240f / deviceDpi), (float)actualWidth, 10f);

        loadDataSync(awContents, onPageFinishedHelper, pageDpi100, "text/html", false);
        actualWidth = Integer.parseInt(getTitleOnUiThread(awContents));
        assertEquals(displayWidth * (100f / deviceDpi), (float)actualWidth, 10f);
    }
}

@@ -359,16 +359,65 @@
 ttings;%0A
+import org.chromium.base.test.util.DisabledTest;%0A
 import o
@@ -452,16 +452,16 @@
 eature;%0A
-
 import o
@@ -561,16 +561,17 @@
 yInfo;%0A%0A
+%0A
 public c
@@ -623,16 +623,28 @@
 e %7B%0A%0A   
+ /**%0A      *
  @Medium
@@ -651,16 +651,20 @@
 Test%0A   
+   *
  @Featur
@@ -685,16 +685,182 @@
 View%22%7D)%0A
+      *%0A      * TODO(mkosiba): This is disabled only temporarily to enable the WebKit roll that contains%0A      * http://crrev.com/22574004%0A      */%0A    @DisabledTest%0A
     publ

Disable testTargetDensityDpi.