Commit 9745d1e6 authored by Luca Hunkeler's avatar Luca Hunkeler Committed by Commit Bot

[Autofill Assistant] Add bottomsheet integration tests

Add integration test to check that the bottomsheet behaves correctly when minimized and restored.
Also verify that the page is showed as intended when the visual or layout viewport is resized.

Bug: b/143265578
Change-Id: If79e40b28ed885b70f353d787e05fb4b1a62f5fb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1890333
Commit-Queue: Luca Hunkeler <hluca@google.com>
Reviewed-by: default avatarClemens Arbesser <arbesser@google.com>
Cr-Commit-Position: refs/heads/master@{#712540}
parent b86d3c32
......@@ -205,6 +205,7 @@ android_library("test_java") {
java_files = [
"javatests/src/org/chromium/chrome/browser/autofill_assistant/AutofillAssistantActionsCarouselUiTest.java",
"javatests/src/org/chromium/chrome/browser/autofill_assistant/AutofillAssistantAutostartTest.java",
"javatests/src/org/chromium/chrome/browser/autofill_assistant/AutofillAssistantBottomsheetTest.java",
"javatests/src/org/chromium/chrome/browser/autofill_assistant/AutofillAssistantDetailsUiTest.java",
"javatests/src/org/chromium/chrome/browser/autofill_assistant/AutofillAssistantDirectActionHandlerTest.java",
"javatests/src/org/chromium/chrome/browser/autofill_assistant/AutofillAssistantHeaderUiTest.java",
......@@ -241,6 +242,7 @@ android_library("test_java") {
"//third_party/android_deps:com_google_protobuf_protobuf_lite_java",
"//third_party/android_support_test_runner:runner_java",
"//third_party/espresso:espresso_all_java",
"//third_party/feed:feed_lib_java",
"//third_party/hamcrest:hamcrest_java",
"//third_party/junit",
"//third_party/mockito:mockito_java",
......
......@@ -8,6 +8,7 @@ import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.support.design.widget.CoordinatorLayout;
import android.support.test.InstrumentationRegistry;
......@@ -28,6 +29,7 @@ import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.hamcrest.TypeSafeMatcher;
import org.json.JSONArray;
import org.chromium.base.Callback;
import org.chromium.base.Supplier;
......@@ -41,8 +43,10 @@ import org.chromium.chrome.browser.image_fetcher.ImageFetcher;
import org.chromium.chrome.browser.image_fetcher.ImageFetcherConfig;
import org.chromium.chrome.browser.widget.bottomsheet.BottomSheet;
import org.chromium.chrome.browser.widget.bottomsheet.BottomSheetController;
import org.chromium.content_public.browser.WebContents;
import org.chromium.content_public.browser.test.util.Criteria;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.TestCallbackHelperContainer;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import java.util.ArrayList;
......@@ -255,4 +259,84 @@ class AutofillAssistantUiTestUtil {
testService.scheduleForInjection();
TestThreadUtils.runOnUiThreadBlocking(() -> AutofillAssistantFacade.start(activity));
}
/** Computes the bounding rectangle of the specified DOM element in absolute screen space. */
public static Rect getAbsoluteBoundingRect(String elementId, CustomTabActivityTestRule testRule)
throws Exception {
// Get bounding rectangle in viewport space.
Rect elementRect = getBoundingRectForElement(elementId, testRule.getWebContents());
/*
* Conversion from viewport space to screen space is done in two steps:
* - First, convert viewport to compositor space (scrolling offset, multiply with factor).
* - Then, convert compositor space to screen space (add content offset).
*/
Rect viewport = getViewport(testRule.getWebContents());
float cssToPysicalPixels =
(((float) testRule.getActivity().getCompositorViewHolder().getWidth()
/ (float) viewport.width()));
int[] compositorLocation = new int[2];
testRule.getActivity().getCompositorViewHolder().getLocationOnScreen(compositorLocation);
int offsetY = compositorLocation[1]
+ testRule.getActivity().getFullscreenManager().getContentOffset();
return new Rect((int) ((elementRect.left - viewport.left) * cssToPysicalPixels),
(int) ((elementRect.top - viewport.top) * cssToPysicalPixels + offsetY),
(int) ((elementRect.right - viewport.left) * cssToPysicalPixels),
(int) ((elementRect.bottom - viewport.top) * cssToPysicalPixels + offsetY));
}
/**
* Retrieves the bounding rectangle for the specified element in the DOM tree in CSS pixel
* coordinates.
*/
public static Rect getBoundingRectForElement(String elementId, WebContents webContents)
throws Exception {
if (!checkElementExists(elementId, webContents)) {
throw new IllegalArgumentException(elementId + " does not exist");
}
TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper javascriptHelper =
new TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper();
javascriptHelper.evaluateJavaScriptForTests(webContents,
"(function() {"
+ " rect = document.getElementById('" + elementId
+ "').getBoundingClientRect();"
+ " return [window.scrollX + rect.left, window.scrollY + rect.top, "
+ " window.scrollX + rect.right, window.scrollY + rect.bottom];"
+ "})()");
javascriptHelper.waitUntilHasValue();
JSONArray rectJson = new JSONArray(javascriptHelper.getJsonResultAndClear());
return new Rect(
rectJson.getInt(0), rectJson.getInt(1), rectJson.getInt(2), rectJson.getInt(3));
}
/** Checks whether the specified element exists in the DOM tree. */
public static boolean checkElementExists(String elementId, WebContents webContents)
throws Exception {
TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper javascriptHelper =
new TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper();
javascriptHelper.evaluateJavaScriptForTests(webContents,
"(function() {"
+ " return [document.getElementById('" + elementId + "') != null]; "
+ "})()");
javascriptHelper.waitUntilHasValue();
JSONArray result = new JSONArray(javascriptHelper.getJsonResultAndClear());
return result.getBoolean(0);
}
/**
* Retrieves the visual viewport of the webpage in CSS pixel coordinates.
*/
public static Rect getViewport(WebContents webContents) throws Exception {
TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper javascriptHelper =
new TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper();
javascriptHelper.evaluateJavaScriptForTests(webContents,
"(function() {"
+ " const v = window.visualViewport;"
+ " return [v.pageLeft, v.pageTop, v.width, v.height]"
+ "})()");
javascriptHelper.waitUntilHasValue();
JSONArray values = new JSONArray(javascriptHelper.getJsonResultAndClear());
return new Rect(values.getInt(0), values.getInt(1), values.getInt(2), values.getInt(3));
}
}
<!DOCTYPE html>
<!--
Copyright 2019 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.
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bottomsheet Behaviour Test</title>
<style>
body {
margin: 0;
padding: 0;
}
nav {
width: 100%;
position: fixed;
height: 2em;
background-color: lightgreen;
}
p {
height: 5em;
}
.top {
background-color: lightblue;
}
.bottom {
background-color: yellow;
}
.cast {
height: 100px;
margin: 20px;
border: 2px solid black;
border-radius: 15px;
}
.blank {
height: 100em;
}
</style>
</head>
<body>
<nav>Navbar</nav>
<p class="top">Top text hidden</p>
<div class="cast"></div>
<div class="blank">Blank</div>
<p id="bottom" class="bottom">Bottom text</p>
</body>
</html>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment