// Copyright (c) 2012 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.content.browser;

import android.test.FlakyTest;

import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.UrlUtils;
import org.chromium.content_shell.ContentShellTestBase;

/*
 * Tests that we can scroll and fling a ContentView running inside ContentShell.
 */
public class ContentViewScrollingTest extends ContentShellTestBase {

    private static final String LARGE_PAGE = UrlUtils.encodeHtmlDataUri(
            "<html>" +
            "<head><style>body { width: 5000px; height: 5000px; }</style></head>" +
            "<body>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</body>" +
            "</html>");

    /*
     * @SmallTest
     * @Feature({"Android-WebView"})
     * BUG 162967
     */
    @FlakyTest
    public void testFling() throws Throwable {
        launchContentShellWithUrl(LARGE_PAGE);
        waitForActiveShellToBeDoneLoading();
        final ContentView view = getActivity().getActiveContentView();

        assertEquals(0, view.getContentViewCore().getNativeScrollXForTest());
        assertEquals(0, view.getContentViewCore().getNativeScrollYForTest());

        // Vertical fling
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                view.fling(System.currentTimeMillis(), 0, 0, 0, -1000);
            }
        });

        // There's no end-of-fling notification so we need to busy-wait.
        for (int i = 0; i < 500; ++i) {
          if (view.getContentViewCore().getNativeScrollYForTest() > 100) {
            break;
          }
          Thread.sleep(10);
        }

        assertEquals(0, view.getContentViewCore().getNativeScrollXForTest());
        assertTrue(view.getContentViewCore().getNativeScrollYForTest() > 100);

        // Horizontal fling
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                view.fling(System.currentTimeMillis(), 0, 0, -1000, 0);
            }
        });

        // There's no end-of-fling notification so we need to busy-wait.
        for (int i = 0; i < 500; ++i) {
          if (view.getContentViewCore().getNativeScrollXForTest() > 100) {
            break;
          }
          Thread.sleep(10);
        }

        assertTrue(view.getContentViewCore().getNativeScrollXForTest() > 100);
        assertTrue(view.getContentViewCore().getNativeScrollYForTest() > 100);
    }
}

@@ -222,21 +222,45 @@
 id.test.
-Flaky
+suitebuilder.annotation.Small
 Test;%0A%0Ai
@@ -347,16 +347,134 @@
 lUtils;%0A
+import org.chromium.content.browser.test.util.Criteria;%0Aimport org.chromium.content.browser.test.util.CriteriaHelper;%0A
 import o
@@ -772,16 +772,22 @@
  %22%3Chtml%3E
+%3Chead%3E
 %22 +%0A    
@@ -796,21 +796,144 @@
       %22%3C
-head%3E
+meta name=%5C%22viewport%5C%22 content=%5C%22width=device-width, %22 +%0A            %22initial-scale=1.0, maximum-scale=1.0%5C%22 /%3E%22 +%0A            %22
 %3Cstyle%3Eb
@@ -1108,106 +1108,378 @@
     
-/*%0A     * @SmallTest%0A     * @Feature(%7B%22Android-WebView%22%7D)%0A     * BUG 162967%0A     */%0A    @FlakyTest
+private void assertWaitForPageScaleFactor(final float scale) throws InterruptedException %7B%0A        assertTrue(CriteriaHelper.pollForCriteria(new Criteria() %7B%0A            @Override%0A            public boolean isSatisfied() %7B%0A                return getContentViewCore().getScale() == scale;%0A            %7D%0A        %7D));%0A    %7D%0A%0A    @SmallTest%0A    @Feature(%7B%22Android-WebView%22%7D)
 %0A   
@@ -1576,16 +1576,50 @@
 %0A       
+ assertTrue(%22Page failed to load%22,
  waitFor
@@ -1646,17 +1646,63 @@
 oading()
-;
+);%0A        assertWaitForPageScaleFactor(1.0f);%0A
 %0A       

Fix ContentViewScrollingTest