Commit a5355cea authored by Tanya Gupta's avatar Tanya Gupta Committed by Chromium LUCI CQ

[LongScreenshots]Created tests for Entry and Generator logic.

Change-Id: If15c21b7cb2da83ec9c8b41bc77c525ef1dc1422
Bug: 1153969
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2630885Reviewed-by: default avatarKyle Milka <kmilka@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Commit-Queue: Tanya Gupta <tgupta@chromium.org>
Cr-Commit-Position: refs/heads/master@{#845360}
parent d45e4097
......@@ -9,6 +9,7 @@ import android.graphics.Bitmap;
import android.graphics.Rect;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.components.paint_preview.common.proto.PaintPreview.PaintPreviewProto;
......@@ -29,7 +30,7 @@ public class BitmapGenerator implements LongScreenshotsTabService.CaptureProcess
// Compositor delegate responsible for compositing the skia
private LongScreenshotsCompositor mCompositor;
private LongScreenshotsTabService mLongScreenshotsTabService;
private LongScreenshotsTabService mTabService;
private Tab mTab;
private static final String DIR_NAME = "long_screenshots_dir";
......@@ -69,16 +70,17 @@ public class BitmapGenerator implements LongScreenshotsTabService.CaptureProcess
mTab = tab;
mGeneratorCallback = callback;
mRect = rect;
mLongScreenshotsTabService = LongScreenshotsTabServiceFactory.getServiceInstance();
mLongScreenshotsTabService.setCaptureProcessor(this);
}
/**
* Starts the capture of the screenshot.
*/
public void captureScreenshot() {
mLongScreenshotsTabService.captureTab(mTab, mRect);
if (mTabService == null) {
mTabService = LongScreenshotsTabServiceFactory.getServiceInstance();
}
mTabService.setCaptureProcessor(this);
mTabService.captureTab(mTab, mRect);
}
/**
......@@ -91,22 +93,13 @@ public class BitmapGenerator implements LongScreenshotsTabService.CaptureProcess
public void processCapturedTab(PaintPreviewProto response, @Status int status) {
if (status != Status.OK) {
mGeneratorCallback.onCaptureError(status);
} else {
} else if (mCompositor == null) {
mCompositor = new LongScreenshotsCompositor(new GURL(response.getMetadata().getUrl()),
mLongScreenshotsTabService, DIR_NAME, response, mRect,
mGeneratorCallback::onBitmapGenerated, mGeneratorCallback::onCompositorError);
mTabService, DIR_NAME, response, mRect, mGeneratorCallback::onBitmapGenerated,
mGeneratorCallback::onCompositorError);
}
}
/**
* Called after the bitmap has been composited and can be shown to the user.
*
* @param result Bitmap to display in the dialog.
*/
private void onBitmapResult(Bitmap result) {
mGeneratorCallback.onBitmapGenerated(result);
}
/**
* Destroy and clean up any memory.
*/
......@@ -116,4 +109,11 @@ public class BitmapGenerator implements LongScreenshotsTabService.CaptureProcess
mCompositor = null;
}
}
@VisibleForTesting
public void setTabServiceAndCompositorForTest(
LongScreenshotsTabService tabService, LongScreenshotsCompositor compositor) {
mTabService = tabService;
mCompositor = compositor;
}
}
......@@ -50,21 +50,30 @@ public class BitmapGeneratorTest {
class TestListener implements BitmapGenerator.GeneratorCallBack {
boolean mOnBitmapGeneratedCalled;
boolean mSomeCallBackCalled;
@CompositorStatus
int mCompositorErrorStatus;
@Status
int mCaptureStatus;
@Override
public void onCompositorError(@CompositorStatus int status) {}
public void onCompositorError(@CompositorStatus int status) {
mCompositorErrorStatus = status;
mSomeCallBackCalled = true;
}
@Override
public void onBitmapGenerated(Bitmap bitmap) {
mOnBitmapGeneratedCalled = true;
mSomeCallBackCalled = true;
mGeneratedBitmap = bitmap;
}
@Override
public void onCaptureError(@Status int status) {}
boolean getOnBitmapGeneratedCalled() {
return mOnBitmapGeneratedCalled;
public void onCaptureError(@Status int status) {
mSomeCallBackCalled = true;
mCaptureStatus = status;
}
}
......@@ -102,10 +111,41 @@ public class BitmapGeneratorTest {
});
CriteriaHelper.pollUiThread(() -> {
Criteria.checkThat("Callback was not called",
mTestListener.getOnBitmapGeneratedCalled(), Matchers.is(true));
Criteria.checkThat("OnBitmapGenerated callback was not called",
mTestListener.mOnBitmapGeneratedCalled, Matchers.is(true));
}, 10000L, 50L);
Assert.assertNotNull(mGeneratedBitmap);
}
/**
* Verifies that a Tab's contents are captured.
* TODO(tgupta): Figure out how to mimic a low memory situation.
@Test
@MediumTest
@Feature({"LongScreenshots"})
public void testCapturedLowMemory() throws Exception {
EmbeddedTestServer testServer = mActivityTestRule.getTestServer();
final String url = testServer.getURL("/chrome/test/data/android/about.html");
TestThreadUtils.runOnUiThreadBlocking(() -> {
mTab.loadUrl(new LoadUrlParams(url));
MemoryPressureListener.notifyMemoryPressure(MemoryPressureLevel.CRITICAL);
mGenerator.captureScreenshot();
});
CriteriaHelper.pollUiThread(() -> {
Criteria.checkThat("No callback on the listener was called.",
mTestListener.mSomeCallBackCalled, Matchers.is(true));
}, 10000L, 50L);
Assert.assertNull(mGeneratedBitmap);
Assert.assertEquals(Status.LOW_MEMORY_DETECTED,
mTestListener.mCaptureStatus);
Assert.assertEquals(CompositorStatus.SKIPPED_DUE_TO_MEMORY_PRESSURE,
mTestListener.mCompositorErrorStatus);
}
*/
}
include_rules = [
"+components/paint_preview/player/android/java/src/org/chromium/components/paintpreview/player",
"+content/public/android/java/src/org/chromium/content_public/browser",
"+content/public/android/java/src/org/chromium/content/browser",
]
\ No newline at end of file
......@@ -4,7 +4,149 @@
package org.chromium.chrome.browser.share.long_screenshots;
/** Tests for the LongScreenshotsEntryTest. */
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.share.long_screenshots.LongScreenshotsEntry.EntryStatus;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.test.util.browser.Features;
import org.chromium.components.paintpreview.player.CompositorStatus;
import org.chromium.content.browser.RenderCoordinatesImpl;
import org.chromium.content.browser.webcontents.WebContentsImpl;
/** Tests for the LongScreenshotsEntry. */
@RunWith(BaseRobolectricTestRunner.class)
@Config(manifest = Config.NONE)
@Features.EnableFeatures(ChromeFeatureList.CHROME_SHARE_LONG_SCREENSHOT)
public class LongScreenshotsEntryTest {
// TODO(tgupta): Write tests for LongScreenshotsEntry.
@Mock
private Context mContext;
@Mock
private Tab mTab;
@Mock
private RenderCoordinatesImpl mRenderCoordinates;
@Mock
private WebContentsImpl mWebContents;
@Mock
private LongScreenshotsCompositor mCompositor;
@Mock
private LongScreenshotsTabService mTabService;
private Bitmap mTestBitmap = Bitmap.createBitmap(512, 1024, Bitmap.Config.ARGB_8888);
class TestEntryListener implements LongScreenshotsEntry.EntryListener {
@EntryStatus
int mReturnedStatus;
@Override
public void onResult(@EntryStatus int status) {
mReturnedStatus = status;
}
public @EntryStatus int getReturnedStatus() {
return mReturnedStatus;
}
}
class TestBitmapGenerator extends BitmapGenerator {
public TestBitmapGenerator(Rect rect, BitmapGenerator.GeneratorCallBack callback) {
super(mContext, mTab, rect, callback);
setTabServiceAndCompositorForTest(mTabService, mCompositor);
}
public void setCompositorStatus(@CompositorStatus int status) {
mGeneratorCallback.onCompositorError(status);
}
public void setCaptureStatus(@Status int status) {
mGeneratorCallback.onCaptureError(status);
}
public void setGeneratedBitmap(Bitmap bitmap) {
mGeneratorCallback.onBitmapGenerated(bitmap);
}
}
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mTab.getWebContents()).thenReturn(mWebContents);
when(mWebContents.getRenderCoordinates()).thenReturn(mRenderCoordinates);
when(mRenderCoordinates.getPageScaleFactorInt()).thenReturn(1);
when(mRenderCoordinates.getScrollYPixInt()).thenReturn(100);
when(mRenderCoordinates.getContentWidthPixInt()).thenReturn(200);
}
@Test
public void testSuccessfulEntry() {
LongScreenshotsEntry entry = new LongScreenshotsEntry(mContext, mTab, 0, 1000);
TestEntryListener entryListener = new TestEntryListener();
entry.setListener(entryListener);
TestBitmapGenerator testGenerator = new TestBitmapGenerator(
new Rect(0, 0, 200, 1000), entry.createBitmapGeneratorCallback());
entry.setBitmapGenerator(testGenerator);
testGenerator.setGeneratedBitmap(mTestBitmap);
assertEquals(EntryStatus.BITMAP_GENERATED, entryListener.getReturnedStatus());
}
@Test
public void testCompositorError() {
LongScreenshotsEntry entry = new LongScreenshotsEntry(mContext, mTab, 0, 1000);
TestEntryListener entryListener = new TestEntryListener();
entry.setListener(entryListener);
TestBitmapGenerator testGenerator = new TestBitmapGenerator(
new Rect(0, 0, 200, 1000), entry.createBitmapGeneratorCallback());
entry.setBitmapGenerator(testGenerator);
testGenerator.setCompositorStatus(CompositorStatus.COMPOSITOR_CLIENT_DISCONNECT);
assertEquals(EntryStatus.GENERATION_ERROR, entryListener.getReturnedStatus());
assertEquals(EntryStatus.GENERATION_ERROR, entry.getStatus());
testGenerator.setCompositorStatus(CompositorStatus.STOPPED_DUE_TO_MEMORY_PRESSURE);
assertEquals(EntryStatus.INSUFFICIENT_MEMORY, entryListener.getReturnedStatus());
assertEquals(EntryStatus.INSUFFICIENT_MEMORY, entry.getStatus());
}
@Test
public void testCaptureError() {
LongScreenshotsEntry entry = new LongScreenshotsEntry(mContext, mTab, 0, 1000);
TestEntryListener entryListener = new TestEntryListener();
entry.setListener(entryListener);
TestBitmapGenerator testGenerator = new TestBitmapGenerator(
new Rect(0, 0, 200, 1000), entry.createBitmapGeneratorCallback());
entry.setBitmapGenerator(testGenerator);
testGenerator.setCaptureStatus(Status.WEB_CONTENTS_GONE);
assertEquals(EntryStatus.GENERATION_ERROR, entryListener.getReturnedStatus());
assertEquals(EntryStatus.GENERATION_ERROR, entry.getStatus());
testGenerator.setCaptureStatus(Status.LOW_MEMORY_DETECTED);
assertEquals(EntryStatus.INSUFFICIENT_MEMORY, entryListener.getReturnedStatus());
assertEquals(EntryStatus.INSUFFICIENT_MEMORY, entry.getStatus());
}
}
......@@ -19,6 +19,7 @@ share_test_java_sources = [
share_junit_test_java_sources = [
"//chrome/browser/share/android/javatests/src/org/chromium/chrome/browser/share/link_to_text/LinkToTextCoordinatorTest.java",
"//chrome/browser/share/android/javatests/src/org/chromium/chrome/browser/share/long_screenshots/LongScreenshotsCompositorTest.java",
"//chrome/browser/share/android/javatests/src/org/chromium/chrome/browser/share/long_screenshots/LongScreenshotsEntryTest.java",
"//chrome/browser/share/android/javatests/src/org/chromium/chrome/browser/share/screenshot/ScreenshotCoordinatorTest.java",
"//chrome/browser/share/android/javatests/src/org/chromium/chrome/browser/share/screenshot/ScreenshotShareSheetMediatorUnitTest.java",
"//chrome/browser/share/android/javatests/src/org/chromium/chrome/browser/share/send_tab_to_self/NotificationSharedPrefManagerTest.java",
......
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