@SmallTest
  @Feature({"AndroidWebView"})
  public void testJsScrollFromBody() throws Throwable {
    final TestAwContentsClient contentsClient = new TestAwContentsClient();
    final ScrollTestContainerView testContainerView =
        (ScrollTestContainerView) createAwTestContainerViewOnMainSync(contentsClient);
    enableJavaScriptOnUiThread(testContainerView.getAwContents());

    final double deviceDIPScale =
        DeviceDisplayInfo.create(testContainerView.getContext()).getDIPScale();
    final int targetScrollXCss = 132;
    final int targetScrollYCss = 243;
    final int targetScrollXPix = (int) Math.floor(targetScrollXCss * deviceDIPScale);
    final int targetScrollYPix = (int) Math.floor(targetScrollYCss * deviceDIPScale);

    final String scrollFromBodyScript =
        "<script> "
            + "  window.scrollTo("
            + targetScrollXCss
            + ", "
            + targetScrollYCss
            + "); "
            + "</script> ";

    final CallbackHelper onScrollToCallbackHelper = testContainerView.getOnScrollToCallbackHelper();
    final int scrollToCallCount = onScrollToCallbackHelper.getCallCount();
    loadDataAsync(
        testContainerView.getAwContents(),
        makeTestPage(null, null, scrollFromBodyScript),
        "text/html",
        false);
    onScrollToCallbackHelper.waitForCallback(scrollToCallCount);

    assertScrollOnMainSync(testContainerView, targetScrollXPix, targetScrollYPix);
  }
  @SmallTest
  @Feature({"AndroidWebView"})
  public void testJsScrollReflectedInUi() throws Throwable {
    final TestAwContentsClient contentsClient = new TestAwContentsClient();
    final ScrollTestContainerView testContainerView =
        (ScrollTestContainerView) createAwTestContainerViewOnMainSync(contentsClient);
    enableJavaScriptOnUiThread(testContainerView.getAwContents());

    final double deviceDIPScale =
        DeviceDisplayInfo.create(testContainerView.getContext()).getDIPScale();
    final int targetScrollXCss = 132;
    final int targetScrollYCss = 243;
    final int targetScrollXPix = (int) Math.floor(targetScrollXCss * deviceDIPScale);
    final int targetScrollYPix = (int) Math.floor(targetScrollYCss * deviceDIPScale);

    loadDataSync(
        testContainerView.getAwContents(),
        contentsClient.getOnPageFinishedHelper(),
        makeTestPage(null, null, ""),
        "text/html",
        false);

    final CallbackHelper onScrollToCallbackHelper = testContainerView.getOnScrollToCallbackHelper();
    final int scrollToCallCount = onScrollToCallbackHelper.getCallCount();
    executeJavaScriptAndWaitForResult(
        testContainerView.getAwContents(),
        contentsClient,
        String.format("window.scrollTo(%d, %d);", targetScrollXCss, targetScrollYCss));
    onScrollToCallbackHelper.waitForCallback(scrollToCallCount);

    assertScrollOnMainSync(testContainerView, targetScrollXPix, targetScrollYPix);
  }
  @SmallTest
  @Feature({"AndroidWebView"})
  public void testUiScrollReflectedInJs() throws Throwable {
    final TestAwContentsClient contentsClient = new TestAwContentsClient();
    final ScrollTestContainerView testContainerView =
        (ScrollTestContainerView) createAwTestContainerViewOnMainSync(contentsClient);
    enableJavaScriptOnUiThread(testContainerView.getAwContents());

    final double deviceDIPScale =
        DeviceDisplayInfo.create(testContainerView.getContext()).getDIPScale();
    final int targetScrollXCss = 233;
    final int targetScrollYCss = 322;
    final int targetScrollXPix = (int) Math.ceil(targetScrollXCss * deviceDIPScale);
    final int targetScrollYPix = (int) Math.ceil(targetScrollYCss * deviceDIPScale);
    final JavascriptEventObserver onscrollObserver = new JavascriptEventObserver();

    getInstrumentation()
        .runOnMainSync(
            new Runnable() {
              @Override
              public void run() {
                onscrollObserver.register(
                    testContainerView.getContentViewCore(), "onscrollObserver");
              }
            });

    loadTestPageAndWaitForFirstFrame(testContainerView, contentsClient, "onscrollObserver", "");

    scrollToOnMainSync(testContainerView, targetScrollXPix, targetScrollYPix);

    onscrollObserver.waitForEvent(WAIT_TIMEOUT_MS);
    assertScrollInJs(
        testContainerView.getAwContents(), contentsClient, targetScrollXCss, targetScrollYCss);
  }
  @SmallTest
  @Feature({"AndroidWebView"})
  public void testTouchScrollCanBeAlteredByUi() throws Throwable {
    final TestAwContentsClient contentsClient = new TestAwContentsClient();
    final ScrollTestContainerView testContainerView =
        (ScrollTestContainerView) createAwTestContainerViewOnMainSync(contentsClient);
    enableJavaScriptOnUiThread(testContainerView.getAwContents());

    final int dragSteps = 10;
    final int dragStepSize = 24;
    // Watch out when modifying - if the y or x delta aren't big enough vertical or horizontal
    // scroll snapping will kick in.
    final int targetScrollXPix = dragStepSize * dragSteps;
    final int targetScrollYPix = dragStepSize * dragSteps;

    final double deviceDIPScale =
        DeviceDisplayInfo.create(testContainerView.getContext()).getDIPScale();
    final int maxScrollXPix = 101;
    final int maxScrollYPix = 211;
    // Make sure we can't hit these values simply as a result of scrolling.
    assert (maxScrollXPix % dragStepSize) != 0;
    assert (maxScrollYPix % dragStepSize) != 0;
    final int maxScrollXCss = (int) Math.floor(maxScrollXPix / deviceDIPScale);
    final int maxScrollYCss = (int) Math.floor(maxScrollYPix / deviceDIPScale);

    setMaxScrollOnMainSync(testContainerView, maxScrollXPix, maxScrollYPix);

    loadTestPageAndWaitForFirstFrame(testContainerView, contentsClient, null, "");

    final CallbackHelper onScrollToCallbackHelper = testContainerView.getOnScrollToCallbackHelper();
    final int scrollToCallCount = onScrollToCallbackHelper.getCallCount();
    AwTestTouchUtils.dragCompleteView(
        testContainerView,
        0,
        -targetScrollXPix, // these need to be negative as we're scrolling down.
        0,
        -targetScrollYPix,
        dragSteps,
        null /* completionLatch */);

    for (int i = 1; i <= dragSteps; ++i) {
      onScrollToCallbackHelper.waitForCallback(scrollToCallCount, i);
      if (checkScrollOnMainSync(testContainerView, maxScrollXPix, maxScrollYPix)) break;
    }

    assertScrollOnMainSync(testContainerView, maxScrollXPix, maxScrollYPix);
    assertScrollInJs(
        testContainerView.getAwContents(), contentsClient, maxScrollXCss, maxScrollYCss);
  }
  @SmallTest
  @Feature({"AndroidWebView"})
  public void testPageUp() throws Throwable {
    final TestAwContentsClient contentsClient = new TestAwContentsClient();
    final ScrollTestContainerView testContainerView =
        (ScrollTestContainerView) createAwTestContainerViewOnMainSync(contentsClient);
    enableJavaScriptOnUiThread(testContainerView.getAwContents());

    final double deviceDIPScale =
        DeviceDisplayInfo.create(testContainerView.getContext()).getDIPScale();
    final int targetScrollYCss = 243;
    final int targetScrollYPix = (int) Math.ceil(targetScrollYCss * deviceDIPScale);

    loadTestPageAndWaitForFirstFrame(testContainerView, contentsClient, null, "");

    assertScrollOnMainSync(testContainerView, 0, 0);

    scrollToOnMainSync(testContainerView, 0, targetScrollYPix);

    final CallbackHelper onScrollToCallbackHelper = testContainerView.getOnScrollToCallbackHelper();
    final int scrollToCallCount = onScrollToCallbackHelper.getCallCount();

    getInstrumentation()
        .runOnMainSync(
            new Runnable() {
              @Override
              public void run() {
                testContainerView.getAwContents().pageUp(true);
              }
            });

    // Wait for the animation to hit the bottom of the page.
    for (int i = 1; ; ++i) {
      onScrollToCallbackHelper.waitForCallback(scrollToCallCount, i);
      if (checkScrollOnMainSync(testContainerView, 0, 0)) break;
    }
  }
Пример #6
0
 private static void updateDeviceDisplayInfo() {
   DeviceDisplayInfo.create(getContext()).updateNativeSharedDisplayInfo();
 }