diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/contextmenu/ContextMenuTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/contextmenu/ContextMenuTest.java index 9f4b2a2651fb8..0e52e01fd46a0 100644 --- a/chrome/android/javatests/src/org/chromium/chrome/browser/contextmenu/ContextMenuTest.java +++ b/chrome/android/javatests/src/org/chromium/chrome/browser/contextmenu/ContextMenuTest.java @@ -95,6 +95,9 @@ import org.chromium.content_public.browser.test.util.DOMUtils; import org.chromium.content_public.browser.test.util.TestTouchUtils; import org.chromium.content_public.common.ContentFeatures; import org.chromium.net.test.EmbeddedTestServer; +import org.chromium.printing.Printable; +import org.chromium.printing.PrintingController; +import org.chromium.printing.PrintingControllerImpl; import org.chromium.ui.base.Clipboard; import org.chromium.ui.base.DeviceFormFactor; import org.chromium.ui.mojom.MenuSourceType; @@ -118,6 +121,7 @@ public class ContextMenuTest { @Mock private TabContextMenuItemDelegate mItemDelegate; @Mock private ShareDelegate mShareDelegate; + @Mock private PrintingController mPrintingController; @Mock private DataProtectionBridge.Natives mDataProtectionBridgeMock; @ClassRule @@ -1192,6 +1196,31 @@ public class ContextMenuTest { chromeExtrasCaptor.getValue().saveLastUsed()); } + @Test + @MediumTest + @Restriction(DeviceFormFactor.DESKTOP) + @EnableFeatures({ChromeFeatureList.CONTEXT_MENU_EMPTY_SPACE}) + public void testPrintPage() throws Exception { + Tab tab = sDownloadTestRule.getActivity().getActivityTab(); + ThreadUtils.runOnUiThreadBlocking( + // Set printing controller to use the mock instance. + () -> { + PrintingControllerImpl.setInstanceForTesting(mPrintingController); + }); + + ContextMenuUtils.selectContextMenuItemFromRightClick( + InstrumentationRegistry.getInstrumentation(), + sDownloadTestRule.getActivity(), + tab, + "testEmptySpace", + R.id.contextmenu_print_page); + + // Check that the started print job has the same title as the current tab. + ArgumentCaptor<Printable> printableCaptor = ArgumentCaptor.forClass(Printable.class); + verify(mPrintingController).startPrint(printableCaptor.capture(), any()); + Assert.assertEquals(tab.getTitle(), printableCaptor.getValue().getTitle()); + } + // TODO(benwgold): Add more test coverage for histogram recording of other context menu types. /** diff --git a/printing/android/java/src/org/chromium/printing/PrintingControllerImpl.java b/printing/android/java/src/org/chromium/printing/PrintingControllerImpl.java index 0e567b52132ea..2f9382105b74b 100644 --- a/printing/android/java/src/org/chromium/printing/PrintingControllerImpl.java +++ b/printing/android/java/src/org/chromium/printing/PrintingControllerImpl.java @@ -16,6 +16,7 @@ import android.print.PrintDocumentInfo; import androidx.annotation.VisibleForTesting; import org.chromium.base.Log; +import org.chromium.base.ResettersForTesting; import org.chromium.base.ThreadUtils; import org.chromium.build.annotations.NullMarked; import org.chromium.build.annotations.Nullable; @@ -58,6 +59,8 @@ public class PrintingControllerImpl implements PrintingController, PdfGenerator /** The singleton instance for this class. */ @VisibleForTesting protected static @Nullable PrintingController sInstance; + private static @Nullable PrintingController sInstanceForTesting; + private @Nullable String mErrorMessage; private int mRenderProcessId; @@ -103,6 +106,11 @@ public class PrintingControllerImpl implements PrintingController, PdfGenerator mPrintDocumentAdapterWrapper = new PrintDocumentAdapterWrapper(this); } + public static void setInstanceForTesting(PrintingController instanceForTesting) { + sInstanceForTesting = instanceForTesting; + ResettersForTesting.register(() -> sInstanceForTesting = null); + } + /** * Returns the singleton instance, lazily creating one if needed. * @@ -111,6 +119,8 @@ public class PrintingControllerImpl implements PrintingController, PdfGenerator public static PrintingController getInstance() { ThreadUtils.assertOnUiThread(); + if (sInstanceForTesting != null) return sInstanceForTesting; + if (sInstance == null) { sInstance = new PrintingControllerImpl(); } @@ -152,6 +162,11 @@ public class PrintingControllerImpl implements PrintingController, PdfGenerator return mIsBusy; } + @VisibleForTesting + public @Nullable Printable getPrintable() { + return mPrintable; + } + @Override public void setPendingPrint( final Printable printable,