Commit 323bf3a2 authored by Clemens Arbesser's avatar Clemens Arbesser Committed by Commit Bot

[Autofill Assistant] Test refactoring and cleanup.

Bug: b/133725664
Change-Id: Ifc03bfc11dec8f6cc24456a4fca71b89d8471acc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1687398
Commit-Queue: Clemens Arbesser <arbesser@google.com>
Reviewed-by: default avatarStephane Zermatten <szermatt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#676030}
parent dce57dae
......@@ -16,12 +16,9 @@ import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import android.support.design.widget.CoordinatorLayout;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest;
import android.support.v7.widget.DefaultItemAnimator;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import org.junit.Before;
......@@ -29,86 +26,71 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.chrome.browser.ChromeSwitches;
import org.chromium.chrome.browser.autofill_assistant.carousel.AssistantActionsCarouselCoordinator;
import org.chromium.chrome.browser.autofill_assistant.carousel.AssistantCarouselModel;
import org.chromium.chrome.browser.autofill_assistant.carousel.AssistantChip;
import org.chromium.chrome.browser.customtabs.CustomTabActivity;
import org.chromium.chrome.browser.customtabs.CustomTabActivityTestRule;
import org.chromium.chrome.browser.customtabs.CustomTabsTestUtils;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import java.util.concurrent.ExecutionException;
/**
* Tests for the autofill assistant actions carousel.
*/
@RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
@RunWith(ChromeJUnit4ClassRunner.class)
public class AutofillAssistantActionsCarouselUiTest {
@Rule
public CustomTabActivityTestRule mCustomTabActivityTestRule = new CustomTabActivityTestRule();
@Before
public void setUp() throws Exception {
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(
CustomTabsTestUtils.createMinimalCustomTabIntent(
InstrumentationRegistry.getTargetContext(), "about:blank"));
}
private CustomTabActivity getActivity() {
return mCustomTabActivityTestRule.getActivity();
}
public CustomTabActivityTestRule mTestRule = new CustomTabActivityTestRule();
/** Creates a coordinator for use in UI tests, and adds it to the global view hierarchy. */
private AssistantActionsCarouselCoordinator createCoordinator(AssistantCarouselModel model) {
ThreadUtils.assertOnUiThread();
AssistantActionsCarouselCoordinator coordinator = new AssistantActionsCarouselCoordinator(
InstrumentationRegistry.getTargetContext(), model);
private AssistantActionsCarouselCoordinator createCoordinator(AssistantCarouselModel model)
throws Exception {
AssistantActionsCarouselCoordinator coordinator = TestThreadUtils.runOnUiThreadBlocking(
()
-> new AssistantActionsCarouselCoordinator(
InstrumentationRegistry.getTargetContext(), model));
TestThreadUtils.runOnUiThreadBlocking(() -> {
// Note: apparently, we need an intermediate container for this coordinator's view,
// otherwise the view will be invisible.
// @TODO(crbug.com/806868) figure out why this is the case.
LinearLayout container = new LinearLayout(InstrumentationRegistry.getTargetContext());
container.addView(coordinator.getView());
CoordinatorLayout.LayoutParams lp = new CoordinatorLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.BOTTOM;
ViewGroup chromeCoordinatorView =
getActivity().findViewById(org.chromium.chrome.autofill_assistant.R.id.coordinator);
chromeCoordinatorView.addView(container, lp);
AutofillAssistantUiTestUtil.attachToCoordinator(mTestRule.getActivity(), container);
});
return coordinator;
}
@Before
public void setUp() throws Exception {
AutofillAssistantUiTestUtil.startOnBlankPage(mTestRule);
}
/** Tests assumptions about the initial state of the carousel. */
@Test
@MediumTest
public void testInitialState() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
public void testInitialState() throws Exception {
AssistantCarouselModel model = new AssistantCarouselModel();
AssistantActionsCarouselCoordinator coordinator = createCoordinator(model);
TestThreadUtils.runOnUiThreadBlocking(() -> {
assertThat(((DefaultItemAnimator) coordinator.getView().getItemAnimator())
.getSupportsChangeAnimations(),
is(false));
});
assertThat(model.getChipsModel().size(), is(0));
assertThat(coordinator.getView().getAdapter().getItemCount(), is(0));
});
}
/** Adds a single chip and tests assumptions about the view state after the change. */
@Test
@MediumTest
public void testAddSingleChip() throws ExecutionException {
public void testAddSingleChip() throws Exception {
AssistantCarouselModel model = new AssistantCarouselModel();
AssistantActionsCarouselCoordinator coordinator =
TestThreadUtils.runOnUiThreadBlocking(() -> createCoordinator(model));
AssistantActionsCarouselCoordinator coordinator = createCoordinator(model);
TestThreadUtils.runOnUiThreadBlocking(
()
......@@ -126,10 +108,9 @@ public class AutofillAssistantActionsCarouselUiTest {
/** Adds multiple chips and tests assumptions about the view state after the change. */
@Test
@MediumTest
public void testAddMultipleChips() throws ExecutionException {
public void testAddMultipleChips() throws Exception {
AssistantCarouselModel model = new AssistantCarouselModel();
AssistantActionsCarouselCoordinator coordinator =
TestThreadUtils.runOnUiThreadBlocking(() -> createCoordinator(model));
AssistantActionsCarouselCoordinator coordinator = createCoordinator(model);
// Note: this should be a small number that fits on screen without scrolling.
int numChips = 3;
......@@ -154,10 +135,9 @@ public class AutofillAssistantActionsCarouselUiTest {
/** Adds many chips and tests that the cancel chip is always visible. */
@Test
@MediumTest
public void testCancelChipAlwaysVisible() throws ExecutionException {
public void testCancelChipAlwaysVisible() throws Exception {
AssistantCarouselModel model = new AssistantCarouselModel();
AssistantActionsCarouselCoordinator coordinator =
TestThreadUtils.runOnUiThreadBlocking(() -> createCoordinator(model));
AssistantActionsCarouselCoordinator coordinator = createCoordinator(model);
// Note: this should be a large number that does not fit on screen without scrolling.
int numChips = 30;
......
......@@ -21,12 +21,9 @@ import static org.chromium.content_public.browser.test.util.TestThreadUtils.runO
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.design.widget.CoordinatorLayout;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
......@@ -42,18 +39,19 @@ import org.chromium.chrome.browser.ChromeSwitches;
import org.chromium.chrome.browser.autofill_assistant.details.AssistantDetails;
import org.chromium.chrome.browser.autofill_assistant.details.AssistantDetailsCoordinator;
import org.chromium.chrome.browser.autofill_assistant.details.AssistantDetailsModel;
import org.chromium.chrome.browser.customtabs.CustomTabActivity;
import org.chromium.chrome.browser.customtabs.CustomTabActivityTestRule;
import org.chromium.chrome.browser.customtabs.CustomTabsTestUtils;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import java.util.Calendar;
import java.util.Locale;
/** Tests for the Autofill Assistant details. */
@RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
@RunWith(ChromeJUnit4ClassRunner.class)
public class AutofillAssistantDetailsUiTest {
@Rule
public CustomTabActivityTestRule mTestRule = new CustomTabActivityTestRule();
private static class ViewHolder {
final ImageView mImageView;
final TextView mTitleView;
......@@ -78,20 +76,6 @@ public class AutofillAssistantDetailsUiTest {
}
}
@Rule
public CustomTabActivityTestRule mCustomTabActivityTestRule = new CustomTabActivityTestRule();
@Before
public void setUp() throws Exception {
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(
CustomTabsTestUtils.createMinimalCustomTabIntent(
InstrumentationRegistry.getTargetContext(), "about:blank"));
}
private CustomTabActivity getActivity() {
return mCustomTabActivityTestRule.getActivity();
}
private AssistantDetailsCoordinator createCoordinator(AssistantDetailsModel model)
throws Exception {
return createCoordinator(model, Locale.getDefault());
......@@ -102,25 +86,24 @@ public class AutofillAssistantDetailsUiTest {
AssistantDetailsModel model, Locale locale) throws Exception {
AssistantDetailsCoordinator coordinator = runOnUiThreadBlocking(() -> {
Bitmap testImage = BitmapFactory.decodeResource(
getActivity().getResources(), R.drawable.btn_close);
mTestRule.getActivity().getResources(), R.drawable.btn_close);
return new AssistantDetailsCoordinator(InstrumentationRegistry.getTargetContext(),
locale, model,
new AutofillAssistantUiTestUtil.MockImageFetcher(testImage, null));
});
runOnUiThreadBlocking(() -> {
CoordinatorLayout.LayoutParams lp = new CoordinatorLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.BOTTOM;
ViewGroup chromeCoordinatorView = getActivity().findViewById(R.id.coordinator);
chromeCoordinatorView.addView(coordinator.getView(), lp);
});
runOnUiThreadBlocking(()
-> AutofillAssistantUiTestUtil.attachToCoordinator(
mTestRule.getActivity(), coordinator.getView()));
return coordinator;
}
@Before
public void setUp() throws Exception {
AutofillAssistantUiTestUtil.startOnBlankPage(mTestRule);
}
/** Tests assumptions about the initial state of the details. */
@Test
@MediumTest
......
......@@ -4,19 +4,20 @@
package org.chromium.chrome.browser.autofill_assistant;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.design.widget.CoordinatorLayout;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.TextView;
import org.junit.Before;
......@@ -24,126 +25,108 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.chrome.autofill_assistant.R;
import org.chromium.chrome.browser.ChromeSwitches;
import org.chromium.chrome.browser.autofill_assistant.infobox.AssistantInfoBox;
import org.chromium.chrome.browser.autofill_assistant.infobox.AssistantInfoBoxCoordinator;
import org.chromium.chrome.browser.autofill_assistant.infobox.AssistantInfoBoxModel;
import org.chromium.chrome.browser.customtabs.CustomTabActivity;
import org.chromium.chrome.browser.customtabs.CustomTabActivityTestRule;
import org.chromium.chrome.browser.customtabs.CustomTabsTestUtils;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
/**
* Tests for the Autofill Assistant infobox.
*/
@RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
@RunWith(ChromeJUnit4ClassRunner.class)
public class AutofillAssistantInfoBoxUiTest {
@Rule
public CustomTabActivityTestRule mCustomTabActivityTestRule = new CustomTabActivityTestRule();
@Before
public void setUp() throws Exception {
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(
CustomTabsTestUtils.createMinimalCustomTabIntent(
InstrumentationRegistry.getTargetContext(), "about:blank"));
}
private CustomTabActivity getActivity() {
return mCustomTabActivityTestRule.getActivity();
}
public CustomTabActivityTestRule mTestRule = new CustomTabActivityTestRule();
private TextView getExplanationView(AssistantInfoBoxCoordinator coordinator) {
return coordinator.getView().findViewById(R.id.info_box_explanation);
}
/** Creates a coordinator for use in UI tests, and adds it to the global view hierarchy. */
private AssistantInfoBoxCoordinator createCoordinator(AssistantInfoBoxModel model) {
ThreadUtils.assertOnUiThread();
Bitmap testImage =
BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.btn_close);
AssistantInfoBoxCoordinator coordinator =
new AssistantInfoBoxCoordinator(InstrumentationRegistry.getTargetContext(), model,
new AutofillAssistantUiTestUtil.MockImageFetcher(testImage, null));
CoordinatorLayout.LayoutParams lp = new CoordinatorLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.BOTTOM;
ViewGroup chromeCoordinatorView = getActivity().findViewById(R.id.coordinator);
chromeCoordinatorView.addView(coordinator.getView(), lp);
private AssistantInfoBoxCoordinator createCoordinator(AssistantInfoBoxModel model)
throws Exception {
AssistantInfoBoxCoordinator coordinator = TestThreadUtils.runOnUiThreadBlocking(() -> {
Bitmap testImage = BitmapFactory.decodeResource(
mTestRule.getActivity().getResources(), R.drawable.btn_close);
return new AssistantInfoBoxCoordinator(InstrumentationRegistry.getTargetContext(),
model, new AutofillAssistantUiTestUtil.MockImageFetcher(testImage, null));
});
TestThreadUtils.runOnUiThreadBlocking(
()
-> AutofillAssistantUiTestUtil.attachToCoordinator(
mTestRule.getActivity(), coordinator.getView()));
return coordinator;
}
@Before
public void setUp() throws Exception {
AutofillAssistantUiTestUtil.startOnBlankPage(mTestRule);
}
/** Tests assumptions about the initial state of the infobox. */
@Test
@MediumTest
public void testInitialState() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
public void testInitialState() throws Exception {
AssistantInfoBoxModel model = new AssistantInfoBoxModel();
AssistantInfoBoxCoordinator coordinator = createCoordinator(model);
assertNull(model.get(AssistantInfoBoxModel.INFO_BOX));
assertFalse(coordinator.getView().isShown());
});
assertThat(model.get(AssistantInfoBoxModel.INFO_BOX), nullValue());
onView(is(coordinator.getView())).check(matches(not(isDisplayed())));
}
/** Tests for an infobox with a message, but without an image. */
@Test
@MediumTest
public void testMessageNoImage() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
public void testMessageNoImage() throws Exception {
AssistantInfoBoxModel model = new AssistantInfoBoxModel();
AssistantInfoBoxCoordinator coordinator = createCoordinator(model);
AssistantInfoBox infoBox = new AssistantInfoBox("", "Message");
model.set(AssistantInfoBoxModel.INFO_BOX, infoBox);
assertTrue(getExplanationView(coordinator).isShown());
assertNull("Image should not be set",
getExplanationView(coordinator).getCompoundDrawables()[1]);
assertEquals(infoBox.getExplanation(), getExplanationView(coordinator).getText());
});
TestThreadUtils.runOnUiThreadBlocking(
() -> model.set(AssistantInfoBoxModel.INFO_BOX, infoBox));
onView(is(coordinator.getView())).check(matches(isDisplayed()));
// Image should not be set.
assertThat(getExplanationView(coordinator).getCompoundDrawables()[1], nullValue());
onView(is(getExplanationView(coordinator))).check(matches(withText("Message")));
}
/** Tests for an infobox with message and image. */
@Test
@MediumTest
public void testImage() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
public void testImage() throws Exception {
AssistantInfoBoxModel model = new AssistantInfoBoxModel();
AssistantInfoBoxCoordinator coordinator = createCoordinator(model);
AssistantInfoBox infoBox = new AssistantInfoBox("x", "Message");
model.set(AssistantInfoBoxModel.INFO_BOX, infoBox);
assertTrue(getExplanationView(coordinator).isShown());
assertNotNull("Image should be set",
getExplanationView(coordinator).getCompoundDrawables()[1]);
assertEquals(infoBox.getExplanation(), getExplanationView(coordinator).getText());
});
TestThreadUtils.runOnUiThreadBlocking(
() -> model.set(AssistantInfoBoxModel.INFO_BOX, infoBox));
onView(is(getExplanationView(coordinator))).check(matches(isDisplayed()));
// Image should be set.
assertThat(getExplanationView(coordinator).getCompoundDrawables()[1], not(nullValue()));
onView(is(getExplanationView(coordinator))).check(matches(withText("Message")));
}
@Test
@MediumTest
public void testVisibility() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
public void testVisibility() throws Exception {
AssistantInfoBoxModel model = new AssistantInfoBoxModel();
AssistantInfoBoxCoordinator coordinator = createCoordinator(model);
AssistantInfoBox infoBox = new AssistantInfoBox("", "");
model.set(AssistantInfoBoxModel.INFO_BOX, infoBox);
assertTrue(coordinator.getView().isShown());
model.set(AssistantInfoBoxModel.INFO_BOX, null);
assertFalse(coordinator.getView().isShown());
});
TestThreadUtils.runOnUiThreadBlocking(
() -> model.set(AssistantInfoBoxModel.INFO_BOX, infoBox));
onView(is(coordinator.getView())).check(matches(isDisplayed()));
TestThreadUtils.runOnUiThreadBlocking(
() -> model.set(AssistantInfoBoxModel.INFO_BOX, null));
onView(is(coordinator.getView())).check(matches(not(isDisplayed())));
}
}
......@@ -22,7 +22,6 @@ import android.support.test.filters.MediumTest;
import android.util.DisplayMetrics;
import org.json.JSONArray;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
......@@ -33,14 +32,12 @@ import org.chromium.chrome.browser.ChromeSwitches;
import org.chromium.chrome.browser.autofill_assistant.overlay.AssistantOverlayCoordinator;
import org.chromium.chrome.browser.autofill_assistant.overlay.AssistantOverlayModel;
import org.chromium.chrome.browser.autofill_assistant.overlay.AssistantOverlayState;
import org.chromium.chrome.browser.customtabs.CustomTabActivity;
import org.chromium.chrome.browser.customtabs.CustomTabActivityTestRule;
import org.chromium.chrome.browser.customtabs.CustomTabsTestUtils;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.content_public.browser.WebContents;
import org.chromium.content_public.browser.test.util.TestCallbackHelperContainer;
import org.chromium.content_public.browser.test.util.TestTouchUtils;
import org.chromium.net.test.EmbeddedTestServer;
import java.util.Collections;
import java.util.concurrent.ExecutionException;
......@@ -48,46 +45,33 @@ import java.util.concurrent.ExecutionException;
/**
* Tests for the Autofill Assistant overlay.
*/
@RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
@RunWith(ChromeJUnit4ClassRunner.class)
public class AutofillAssistantOverlayUiTest {
@Rule
public CustomTabActivityTestRule mTestRule = new CustomTabActivityTestRule();
// TODO(crbug.com/806868): Create a more specific test site for overlay testing.
private static final String TEST_PAGE =
"/components/test/data/autofill_assistant/autofill_assistant_target_website.html";
private EmbeddedTestServer mTestServer;
@Rule
public CustomTabActivityTestRule mCustomTabActivityTestRule = new CustomTabActivityTestRule();
@Before
public void setUp() throws Exception {
mTestServer = EmbeddedTestServer.createAndStartServer(
InstrumentationRegistry.getTargetContext().getApplicationContext());
String testPage = mTestServer.getURL(TEST_PAGE);
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(
CustomTabsTestUtils.createMinimalCustomTabIntent(
InstrumentationRegistry.getTargetContext(), testPage));
mCustomTabActivityTestRule.getActivity().getScrim().disableAnimationForTesting(true);
}
@After
public void tearDown() throws Exception {
mTestServer.stopAndDestroyServer();
}
private CustomTabActivity getActivity() {
return mCustomTabActivityTestRule.getActivity();
mTestRule.startCustomTabActivityWithIntent(CustomTabsTestUtils.createMinimalCustomTabIntent(
InstrumentationRegistry.getTargetContext(),
mTestRule.getTestServer().getURL(TEST_PAGE)));
mTestRule.getActivity().getScrim().disableAnimationForTesting(true);
}
private WebContents getWebContents() {
return mCustomTabActivityTestRule.getWebContents();
return mTestRule.getWebContents();
}
/** Creates a coordinator for use in UI tests. */
private AssistantOverlayCoordinator createCoordinator(AssistantOverlayModel model)
throws ExecutionException {
return runOnUiThreadBlocking(() -> new AssistantOverlayCoordinator(getActivity(), model));
return runOnUiThreadBlocking(
() -> new AssistantOverlayCoordinator(mTestRule.getActivity(), model));
}
/** Tests assumptions about the initial state of the infobox. */
......@@ -190,15 +174,15 @@ public class AutofillAssistantOverlayUiTest {
// The scrim view is only attached to the view hierarchy when needed, preventing us from
// using regular espresso facilities.
boolean scrimInHierarchy =
runOnUiThreadBlocking(() -> getActivity().getScrim().getParent() != null);
runOnUiThreadBlocking(() -> mTestRule.getActivity().getScrim().getParent() != null);
if (expected && !scrimInHierarchy) {
throw new Exception("Expected scrim view visible, but scrim was not in view hierarchy");
}
if (scrimInHierarchy) {
if (expected) {
onView(is(getActivity().getScrim())).check(matches(isDisplayed()));
onView(is(mTestRule.getActivity().getScrim())).check(matches(isDisplayed()));
} else {
onView(is(getActivity().getScrim())).check(matches(not(isDisplayed())));
onView(is(mTestRule.getActivity().getScrim())).check(matches(not(isDisplayed())));
}
}
}
......@@ -210,7 +194,7 @@ public class AutofillAssistantOverlayUiTest {
float y = coords.top + 0.5f * (coords.bottom - coords.top);
// Sanity check, can only click on coordinates on screen.
DisplayMetrics displayMetrics = getActivity().getResources().getDisplayMetrics();
DisplayMetrics displayMetrics = mTestRule.getActivity().getResources().getDisplayMetrics();
if (x < 0 || x > displayMetrics.widthPixels || y < 0 || y > displayMetrics.heightPixels) {
throw new IllegalArgumentException(elementId + " not on screen: tried to tap x=" + x
+ ", y=" + y + ", which is outside of display with w="
......@@ -230,13 +214,14 @@ public class AutofillAssistantOverlayUiTest {
* - Then, convert compositor space to screen space (add content offset).
*/
Rect viewport = getViewport();
float cssToPysicalPixels = (((float) getActivity().getCompositorViewHolder().getWidth()
float cssToPysicalPixels =
(((float) mTestRule.getActivity().getCompositorViewHolder().getWidth()
/ (float) viewport.width()));
int[] compositorLocation = new int[2];
getActivity().getCompositorViewHolder().getLocationOnScreen(compositorLocation);
int offsetY =
compositorLocation[1] + getActivity().getFullscreenManager().getContentOffset();
mTestRule.getActivity().getCompositorViewHolder().getLocationOnScreen(compositorLocation);
int offsetY = compositorLocation[1]
+ mTestRule.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),
......
......@@ -24,19 +24,14 @@ import static org.junit.Assert.assertThat;
import static org.chromium.chrome.browser.autofill_assistant.AssistantTagsForTesting.VERTICAL_EXPANDER_CHEVRON;
import android.support.design.widget.CoordinatorLayout;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.chrome.autofill_assistant.R;
import org.chromium.chrome.browser.ChromeSwitches;
......@@ -46,54 +41,37 @@ import org.chromium.chrome.browser.autofill_assistant.payment.AssistantChoiceLis
import org.chromium.chrome.browser.autofill_assistant.payment.AssistantPaymentRequestCoordinator;
import org.chromium.chrome.browser.autofill_assistant.payment.AssistantPaymentRequestModel;
import org.chromium.chrome.browser.autofill_assistant.payment.AssistantTermsAndConditionsState;
import org.chromium.chrome.browser.customtabs.CustomTabActivity;
import org.chromium.chrome.browser.customtabs.CustomTabActivityTestRule;
import org.chromium.chrome.browser.customtabs.CustomTabsTestUtils;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.content_public.browser.WebContents;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
/**
* Tests for the Autofill Assistant payment request UI.
*/
@RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
@RunWith(ChromeJUnit4ClassRunner.class)
public class AutofillAssistantPaymentRequestUiTest {
private AutofillAssistantPaymentRequestTestHelper mHelper;
@Rule
public CustomTabActivityTestRule mCustomTabActivityTestRule = new CustomTabActivityTestRule();
public CustomTabActivityTestRule mTestRule = new CustomTabActivityTestRule();
private AutofillAssistantPaymentRequestTestHelper mHelper;
@Before
public void setUp() throws Exception {
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(
CustomTabsTestUtils.createMinimalCustomTabIntent(
InstrumentationRegistry.getTargetContext(), "about:blank"));
AutofillAssistantUiTestUtil.startOnBlankPage(mTestRule);
mHelper = new AutofillAssistantPaymentRequestTestHelper();
}
private CustomTabActivity getActivity() {
return mCustomTabActivityTestRule.getActivity();
}
private WebContents getWebContents() {
return mCustomTabActivityTestRule.getWebContents();
}
/** Creates a coordinator for use in UI tests, and adds it to the global view hierarchy. */
private AssistantPaymentRequestCoordinator createPaymentRequestCoordinator(
AssistantPaymentRequestModel model) {
ThreadUtils.assertOnUiThread();
AssistantPaymentRequestCoordinator coordinator =
new AssistantPaymentRequestCoordinator(getActivity(), model);
CoordinatorLayout.LayoutParams lp = new CoordinatorLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.BOTTOM;
ViewGroup chromeCoordinatorView = getActivity().findViewById(R.id.coordinator);
chromeCoordinatorView.addView(coordinator.getView(), lp);
AssistantPaymentRequestModel model) throws Exception {
AssistantPaymentRequestCoordinator coordinator = TestThreadUtils.runOnUiThreadBlocking(
() -> new AssistantPaymentRequestCoordinator(mTestRule.getActivity(), model));
TestThreadUtils.runOnUiThreadBlocking(
()
-> AutofillAssistantUiTestUtil.attachToCoordinator(
mTestRule.getActivity(), coordinator.getView()));
return coordinator;
}
......@@ -104,8 +82,7 @@ public class AutofillAssistantPaymentRequestUiTest {
@MediumTest
public void testInitialState() throws Exception {
AssistantPaymentRequestModel model = new AssistantPaymentRequestModel();
AssistantPaymentRequestCoordinator coordinator =
TestThreadUtils.runOnUiThreadBlocking(() -> createPaymentRequestCoordinator(model));
AssistantPaymentRequestCoordinator coordinator = createPaymentRequestCoordinator(model);
/* Test initial model state. */
assertThat(model.get(AssistantPaymentRequestModel.VISIBLE), is(false));
......@@ -153,8 +130,7 @@ public class AutofillAssistantPaymentRequestUiTest {
@MediumTest
public void testSectionVisibility() throws Exception {
AssistantPaymentRequestModel model = new AssistantPaymentRequestModel();
AssistantPaymentRequestCoordinator coordinator =
TestThreadUtils.runOnUiThreadBlocking(() -> createPaymentRequestCoordinator(model));
AssistantPaymentRequestCoordinator coordinator = createPaymentRequestCoordinator(model);
AutofillAssistantPaymentRequestTestHelper
.ViewHolder viewHolder = TestThreadUtils.runOnUiThreadBlocking(
() -> new AutofillAssistantPaymentRequestTestHelper.ViewHolder(coordinator));
......@@ -221,8 +197,7 @@ public class AutofillAssistantPaymentRequestUiTest {
@MediumTest
public void testEmptyPaymentRequest() throws Exception {
AssistantPaymentRequestModel model = new AssistantPaymentRequestModel();
AssistantPaymentRequestCoordinator coordinator =
TestThreadUtils.runOnUiThreadBlocking(() -> createPaymentRequestCoordinator(model));
AssistantPaymentRequestCoordinator coordinator = createPaymentRequestCoordinator(model);
AutofillAssistantPaymentRequestTestHelper.MockDelegate delegate =
new AutofillAssistantPaymentRequestTestHelper.MockDelegate();
AutofillAssistantPaymentRequestTestHelper
......@@ -301,15 +276,14 @@ public class AutofillAssistantPaymentRequestUiTest {
@MediumTest
public void testContactDetailsLiveUpdate() throws Exception {
AssistantPaymentRequestModel model = new AssistantPaymentRequestModel();
AssistantPaymentRequestCoordinator coordinator =
TestThreadUtils.runOnUiThreadBlocking(() -> createPaymentRequestCoordinator(model));
AssistantPaymentRequestCoordinator coordinator = createPaymentRequestCoordinator(model);
AutofillAssistantPaymentRequestTestHelper
.ViewHolder viewHolder = TestThreadUtils.runOnUiThreadBlocking(
() -> new AutofillAssistantPaymentRequestTestHelper.ViewHolder(coordinator));
TestThreadUtils.runOnUiThreadBlocking(() -> {
// WEB_CONTENTS are necessary for the creation of the editors.
model.set(AssistantPaymentRequestModel.WEB_CONTENTS, getWebContents());
model.set(AssistantPaymentRequestModel.WEB_CONTENTS, mTestRule.getWebContents());
model.set(AssistantPaymentRequestModel.REQUEST_NAME, true);
model.set(AssistantPaymentRequestModel.REQUEST_EMAIL, true);
model.set(AssistantPaymentRequestModel.VISIBLE, true);
......@@ -357,15 +331,14 @@ public class AutofillAssistantPaymentRequestUiTest {
@MediumTest
public void testPaymentMethodsLiveUpdate() throws Exception {
AssistantPaymentRequestModel model = new AssistantPaymentRequestModel();
AssistantPaymentRequestCoordinator coordinator =
TestThreadUtils.runOnUiThreadBlocking(() -> createPaymentRequestCoordinator(model));
AssistantPaymentRequestCoordinator coordinator = createPaymentRequestCoordinator(model);
AutofillAssistantPaymentRequestTestHelper
.ViewHolder viewHolder = TestThreadUtils.runOnUiThreadBlocking(
() -> new AutofillAssistantPaymentRequestTestHelper.ViewHolder(coordinator));
TestThreadUtils.runOnUiThreadBlocking(() -> {
// WEB_CONTENTS are necessary for the creation of the editors.
model.set(AssistantPaymentRequestModel.WEB_CONTENTS, getWebContents());
model.set(AssistantPaymentRequestModel.WEB_CONTENTS, mTestRule.getWebContents());
model.set(AssistantPaymentRequestModel.REQUEST_PAYMENT, true);
model.set(AssistantPaymentRequestModel.VISIBLE, true);
});
......@@ -424,8 +397,7 @@ public class AutofillAssistantPaymentRequestUiTest {
mHelper.setCreditCard(creditCard);
AssistantPaymentRequestModel model = new AssistantPaymentRequestModel();
AssistantPaymentRequestCoordinator coordinator =
TestThreadUtils.runOnUiThreadBlocking(() -> createPaymentRequestCoordinator(model));
AssistantPaymentRequestCoordinator coordinator = createPaymentRequestCoordinator(model);
AutofillAssistantPaymentRequestTestHelper.MockDelegate delegate =
new AutofillAssistantPaymentRequestTestHelper.MockDelegate();
AutofillAssistantPaymentRequestTestHelper
......@@ -513,8 +485,7 @@ public class AutofillAssistantPaymentRequestUiTest {
@MediumTest
public void testRemoveLastItemImplicitSelection() throws Exception {
AssistantPaymentRequestModel model = new AssistantPaymentRequestModel();
AssistantPaymentRequestCoordinator coordinator =
TestThreadUtils.runOnUiThreadBlocking(() -> createPaymentRequestCoordinator(model));
AssistantPaymentRequestCoordinator coordinator = createPaymentRequestCoordinator(model);
AutofillAssistantPaymentRequestTestHelper.MockDelegate delegate =
new AutofillAssistantPaymentRequestTestHelper.MockDelegate();
AutofillAssistantPaymentRequestTestHelper
......
......@@ -6,13 +6,21 @@ package org.chromium.chrome.browser.autofill_assistant;
import android.graphics.Bitmap;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.test.InstrumentationRegistry;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.chromium.base.Callback;
import org.chromium.base.ThreadUtils;
import org.chromium.chrome.browser.customtabs.CustomTabActivity;
import org.chromium.chrome.browser.customtabs.CustomTabActivityTestRule;
import org.chromium.chrome.browser.customtabs.CustomTabsTestUtils;
import org.chromium.chrome.browser.image_fetcher.ImageFetcher;
import org.chromium.chrome.browser.image_fetcher.ImageFetcherConfig;
......@@ -69,4 +77,26 @@ class AutofillAssistantUiTestUtil {
}
};
}
/**
* Attaches the specified view to the Chrome coordinator. Must be called from the UI thread.
*/
public static void attachToCoordinator(CustomTabActivity activity, View view) {
ThreadUtils.assertOnUiThread();
ViewGroup chromeCoordinatorView =
activity.findViewById(org.chromium.chrome.autofill_assistant.R.id.coordinator);
CoordinatorLayout.LayoutParams lp = new CoordinatorLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.BOTTOM;
chromeCoordinatorView.addView(view, lp);
}
/**
* Starts the CCT test rule on a blank page.
*/
public static void startOnBlankPage(CustomTabActivityTestRule testRule)
throws InterruptedException {
testRule.startCustomTabActivityWithIntent(CustomTabsTestUtils.createMinimalCustomTabIntent(
InstrumentationRegistry.getTargetContext(), "about:blank"));
}
}
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