Commit ddceedb2 authored by Clemens Arbesser's avatar Clemens Arbesser Committed by Commit Bot

[Autofill Assistant] Fix possible crash in OverlayImage.

Bug: b/150752324
Change-Id: Ida677ca8cd062856c053bcfc632b55da868ada1b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2087754
Commit-Queue: Clemens Arbesser <arbesser@google.com>
Reviewed-by: default avatarSandro Maggi <sandromaggi@google.com>
Cr-Commit-Position: refs/heads/master@{#746747}
parent b9bcc5a8
...@@ -86,8 +86,10 @@ public class AssistantOverlayCoordinator { ...@@ -86,8 +86,10 @@ public class AssistantOverlayCoordinator {
// TODO(b/143517837) Merge autofill assistant image fetcher UMA names. // TODO(b/143517837) Merge autofill assistant image fetcher UMA names.
mImageFetcher.fetchImage(image.mImageUrl, mImageFetcher.fetchImage(image.mImageUrl,
ImageFetcher.ASSISTANT_DETAILS_UMA_CLIENT_NAME, result -> { ImageFetcher.ASSISTANT_DETAILS_UMA_CLIENT_NAME, result -> {
image.mImageBitmap = Bitmap.createScaledBitmap(result, image.mImageBitmap = result != null ? Bitmap.createScaledBitmap(
image.mImageSizeInPixels, image.mImageSizeInPixels, true); result, image.mImageSizeInPixels,
image.mImageSizeInPixels, true)
: null;
mDrawable.setFullOverlayImage(image); mDrawable.setFullOverlayImage(image);
}); });
} else { } else {
......
...@@ -12,6 +12,7 @@ import static android.support.test.espresso.matcher.ViewMatchers.isRoot; ...@@ -12,6 +12,7 @@ import static android.support.test.espresso.matcher.ViewMatchers.isRoot;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.chromium.chrome.browser.autofill_assistant.AutofillAssistantUiTestUtil.checkElementExists; import static org.chromium.chrome.browser.autofill_assistant.AutofillAssistantUiTestUtil.checkElementExists;
import static org.chromium.chrome.browser.autofill_assistant.AutofillAssistantUiTestUtil.getBoundingRectForElement; import static org.chromium.chrome.browser.autofill_assistant.AutofillAssistantUiTestUtil.getBoundingRectForElement;
...@@ -23,6 +24,7 @@ import android.graphics.BitmapFactory; ...@@ -23,6 +24,7 @@ import android.graphics.BitmapFactory;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.RectF; import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.support.test.InstrumentationRegistry; import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest; import android.support.test.filters.MediumTest;
...@@ -71,16 +73,22 @@ public class AutofillAssistantOverlayUiTest { ...@@ -71,16 +73,22 @@ public class AutofillAssistantOverlayUiTest {
return mTestRule.getWebContents(); return mTestRule.getWebContents();
} }
/** Creates a coordinator for use in UI tests. */ /** Creates a coordinator for use in UI tests with a default, non-null overlay image. */
private AssistantOverlayCoordinator createCoordinator(AssistantOverlayModel model) private AssistantOverlayCoordinator createCoordinator(AssistantOverlayModel model)
throws ExecutionException { throws ExecutionException {
Bitmap testImage = BitmapFactory.decodeResource(mTestRule.getActivity().getResources(), return createCoordinator(model,
org.chromium.chrome.autofill_assistant.R.drawable.btn_close); BitmapFactory.decodeResource(mTestRule.getActivity().getResources(),
org.chromium.chrome.autofill_assistant.R.drawable.btn_close));
}
/** Creates a coordinator for use in UI tests with a custom overlay image. */
private AssistantOverlayCoordinator createCoordinator(
AssistantOverlayModel model, @Nullable Bitmap overlayImage) throws ExecutionException {
return runOnUiThreadBlocking( return runOnUiThreadBlocking(
() ()
-> new AssistantOverlayCoordinator(mTestRule.getActivity(), model, -> new AssistantOverlayCoordinator(mTestRule.getActivity(), model,
new AutofillAssistantUiTestUtil.MockImageFetcher(testImage, null))); new AutofillAssistantUiTestUtil.MockImageFetcher(
overlayImage, null)));
} }
/** Tests assumptions about the initial state of the infobox. */ /** Tests assumptions about the initial state of the infobox. */
...@@ -193,6 +201,48 @@ public class AutofillAssistantOverlayUiTest { ...@@ -193,6 +201,48 @@ public class AutofillAssistantOverlayUiTest {
assertThat(checkElementExists("touch_area_two", getWebContents()), is(false)); assertThat(checkElementExists("touch_area_two", getWebContents()), is(false));
} }
/**
* Regular overlay image test. Since there is no easy way to test whether the image is actually
* rendered, this is simply checking that nothing crashes.
*/
@Test
@MediumTest
public void testOverlayImageDoesNotCrashIfValid() throws Exception {
AssistantOverlayModel model = new AssistantOverlayModel();
Bitmap bitmap = BitmapFactory.decodeResource(mTestRule.getActivity().getResources(),
org.chromium.chrome.autofill_assistant.R.drawable.btn_close);
assertThat(bitmap, notNullValue());
AssistantOverlayCoordinator coordinator =
createCoordinator(model, /* overlayImage = */ bitmap);
runOnUiThreadBlocking(() -> {
model.set(AssistantOverlayModel.STATE, AssistantOverlayState.FULL);
model.set(AssistantOverlayModel.OVERLAY_IMAGE,
new AssistantOverlayImage("https://www.example.com/example.png", 32, 32, 12,
"Text", Color.RED, 20));
});
assertScrimDisplayed(true);
}
/** Simulates what would happen if the overlay image fetcher returned null. */
@Test
@MediumTest
public void testOverlayDoesNotCrashIfImageFailsToLoad() throws Exception {
AssistantOverlayModel model = new AssistantOverlayModel();
AssistantOverlayCoordinator coordinator =
createCoordinator(model, /* overlayImage = */ null);
runOnUiThreadBlocking(() -> {
model.set(AssistantOverlayModel.STATE, AssistantOverlayState.FULL);
model.set(AssistantOverlayModel.OVERLAY_IMAGE,
new AssistantOverlayImage("https://www.example.com/example.png", 32, 32, 12,
"Text", Color.RED, 20));
});
assertScrimDisplayed(true);
}
private void assertScrimDisplayed(boolean expected) throws Exception { private void assertScrimDisplayed(boolean expected) throws Exception {
// Wait for UI thread to be idle. // Wait for UI thread to be idle.
onView(isRoot()).check(matches(isDisplayed())); onView(isRoot()).check(matches(isDisplayed()));
......
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