Merge "Make print core tests junit tests."

This commit is contained in:
TreeHugger Robot
2016-09-28 22:54:24 +00:00
committed by Android (Google) Code Review
4 changed files with 209 additions and 384 deletions

View File

@@ -16,6 +16,9 @@
package android.print;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doCallRealMethod;
@@ -26,173 +29,102 @@ import android.annotation.NonNull;
import android.app.Instrumentation;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.CancellationSignal;
import android.os.LocaleList;
import android.os.ParcelFileDescriptor;
import android.os.SystemClock;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintManager;
import android.print.PrinterId;
import android.print.mockservice.PrintServiceCallbacks;
import android.print.mockservice.PrinterDiscoverySessionCallbacks;
import android.print.mockservice.StubbablePrinterDiscoverySession;
import android.printservice.CustomPrinterIconCallback;
import android.printservice.PrintJob;
import android.printservice.PrintService;
import android.test.InstrumentationTestCase;
import android.util.DisplayMetrics;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.UiDevice;
import android.support.test.rule.ActivityTestRule;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.mockito.stubbing.Answer;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeoutException;
/**
* This is the base class for print tests.
*/
public abstract class BasePrintTest extends InstrumentationTestCase {
abstract class BasePrintTest {
private static final long OPERATION_TIMEOUT = 30000;
private static final String PM_CLEAR_SUCCESS_OUTPUT = "Success";
private static final String COMMAND_LIST_ENABLED_IME_COMPONENTS = "ime list -s";
private static final String COMMAND_PREFIX_ENABLE_IME = "ime enable ";
private static final String COMMAND_PREFIX_DISABLE_IME = "ime disable ";
private static final int CURRENT_USER_ID = -2; // Mirrors UserHandle.USER_CURRENT
private PrintTestActivity mActivity;
private android.print.PrintJob mPrintJob;
private LocaleList mOldLocale;
private CallCounter mStartCallCounter;
private CallCounter mStartSessionCallCounter;
private String[] mEnabledImes;
private static Instrumentation sInstrumentation;
private static UiDevice sUiDevice;
private String[] getEnabledImes() throws IOException {
List<String> imeList = new ArrayList<>();
@Rule
public ActivityTestRule<PrintTestActivity> mActivityRule =
new ActivityTestRule<>(PrintTestActivity.class, false, true);
ParcelFileDescriptor pfd = getInstrumentation().getUiAutomation()
.executeShellCommand(COMMAND_LIST_ENABLED_IME_COMPONENTS);
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream(pfd.getFileDescriptor())))) {
String line;
while ((line = reader.readLine()) != null) {
imeList.add(line);
}
}
String[] imeArray = new String[imeList.size()];
imeList.toArray(imeArray);
return imeArray;
/**
* Return the UI device
*
* @return the UI device
*/
public UiDevice getUiDevice() {
return sUiDevice;
}
private void disableImes() throws Exception {
mEnabledImes = getEnabledImes();
for (String ime : mEnabledImes) {
String disableImeCommand = COMMAND_PREFIX_DISABLE_IME + ime;
runShellCommand(getInstrumentation(), disableImeCommand);
}
protected static Instrumentation getInstrumentation() {
return sInstrumentation;
}
private void enableImes() throws Exception {
for (String ime : mEnabledImes) {
String enableImeCommand = COMMAND_PREFIX_ENABLE_IME + ime;
runShellCommand(getInstrumentation(), enableImeCommand);
}
mEnabledImes = null;
}
@BeforeClass
public static void setUpClass() throws Exception {
sInstrumentation = InstrumentationRegistry.getInstrumentation();
assumeTrue(sInstrumentation.getContext().getPackageManager().hasSystemFeature(
PackageManager.FEATURE_PRINTING));
@Override
protected void runTest() throws Throwable {
// Do nothing if the device does not support printing.
if (supportsPrinting()) {
super.runTest();
}
}
@Override
public void setUp() throws Exception {
super.setUp();
if (!supportsPrinting()) {
return;
}
sUiDevice = UiDevice.getInstance(sInstrumentation);
// Make sure we start with a clean slate.
clearPrintSpoolerData();
disableImes();
// Workaround for dexmaker bug: https://code.google.com/p/dexmaker/issues/detail?id=2
// Dexmaker is used by mockito.
System.setProperty("dexmaker.dexcache", getInstrumentation()
.getTargetContext().getCacheDir().getPath());
}
// Set to US locale.
Resources resources = getInstrumentation().getTargetContext().getResources();
Configuration oldConfiguration = resources.getConfiguration();
if (!oldConfiguration.getLocales().get(0).equals(Locale.US)) {
mOldLocale = oldConfiguration.getLocales();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
Configuration newConfiguration = new Configuration(oldConfiguration);
newConfiguration.setLocale(Locale.US);
resources.updateConfiguration(newConfiguration, displayMetrics);
}
@Before
public void setUp() throws Exception {
// Initialize the latches.
mStartCallCounter = new CallCounter();
mStartSessionCallCounter = new CallCounter();
// Create the activity for the right locale.
createActivity();
}
@Override
@After
public void tearDown() throws Exception {
if (!supportsPrinting()) {
return;
}
// Done with the activity.
getActivity().finish();
enableImes();
// Restore the locale if needed.
if (mOldLocale != null) {
Resources resources = getInstrumentation().getTargetContext().getResources();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
Configuration newConfiguration = new Configuration(resources.getConfiguration());
newConfiguration.setLocales(mOldLocale);
mOldLocale = null;
resources.updateConfiguration(newConfiguration, displayMetrics);
}
// Make sure the spooler is cleaned, this also un-approves all services
clearPrintSpoolerData();
super.tearDown();
// Exit print spooler
getUiDevice().pressBack();
getUiDevice().pressBack();
}
protected android.print.PrintJob print(@NonNull final PrintDocumentAdapter adapter,
final PrintAttributes attributes) {
// Initiate printing as if coming from the app.
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
PrintManager printManager = (PrintManager) getActivity()
.getSystemService(Context.PRINT_SERVICE);
mPrintJob = printManager.print("Print job", adapter, attributes);
}
getInstrumentation().runOnMainSync(() -> {
PrintManager printManager = (PrintManager) getActivity()
.getSystemService(Context.PRINT_SERVICE);
mPrintJob = printManager.print("Print job", adapter, attributes);
});
return mPrintJob;
@@ -215,7 +147,7 @@ public abstract class BasePrintTest extends InstrumentationTestCase {
waitForCallbackCallCount(mStartCallCounter, 1, "Did not get expected call to start.");
}
private void waitForCallbackCallCount(CallCounter counter, int count, String message) {
private static void waitForCallbackCallCount(CallCounter counter, int count, String message) {
try {
counter.waitForCount(count, OPERATION_TIMEOUT);
} catch (TimeoutException te) {
@@ -224,12 +156,7 @@ public abstract class BasePrintTest extends InstrumentationTestCase {
}
protected PrintTestActivity getActivity() {
return mActivity;
}
protected void createActivity() {
mActivity = launchActivity(getInstrumentation().getTargetContext().getPackageName(),
PrintTestActivity.class, null);
return mActivityRule.getActivity();
}
public static String runShellCommand(Instrumentation instrumentation, String cmd)
@@ -238,7 +165,7 @@ public abstract class BasePrintTest extends InstrumentationTestCase {
byte[] buf = new byte[512];
int bytesRead;
FileInputStream fis = new ParcelFileDescriptor.AutoCloseInputStream(pfd);
StringBuffer stdout = new StringBuffer();
StringBuilder stdout = new StringBuilder();
while ((bytesRead = fis.read(buf)) != -1) {
stdout.append(new String(buf, 0, bytesRead));
}
@@ -246,7 +173,7 @@ public abstract class BasePrintTest extends InstrumentationTestCase {
return stdout.toString();
}
protected void clearPrintSpoolerData() throws Exception {
protected static void clearPrintSpoolerData() throws Exception {
assertTrue("failed to clear print spooler data",
runShellCommand(getInstrumentation(), String.format(
"pm clear --user %d %s", CURRENT_USER_ID,
@@ -319,7 +246,7 @@ public abstract class BasePrintTest extends InstrumentationTestCase {
return service;
}
protected final class CallCounter {
private static final class CallCounter {
private final Object mLock = new Object();
private int mCallCount;
@@ -331,7 +258,7 @@ public abstract class BasePrintTest extends InstrumentationTestCase {
}
}
public int getCallCount() {
int getCallCount() {
synchronized (mLock) {
return mCallCount;
}
@@ -355,9 +282,4 @@ public abstract class BasePrintTest extends InstrumentationTestCase {
}
}
}
protected boolean supportsPrinting() {
return getInstrumentation().getContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_PRINTING);
}
}

View File

@@ -38,17 +38,22 @@ import android.print.mockservice.PrintServiceCallbacks;
import android.print.mockservice.PrinterDiscoverySessionCallbacks;
import android.print.mockservice.StubbablePrinterDiscoverySession;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.MediumTest;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import android.support.test.filters.LargeTest;
import android.support.test.filters.MediumTest;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* tests feeding all possible parameters to the IPrintManager Binder.
*/
@RunWith(AndroidJUnit4.class)
public class IPrintManagerParametersTest extends BasePrintTest {
private final int BAD_APP_ID = 0xffffffff;
@@ -58,9 +63,7 @@ public class IPrintManagerParametersTest extends BasePrintTest {
private final PrintJobId mBadPrintJobId;
private PrintJob mGoodPrintJob;
private PrinterId mBadPrinterId;
private PrinterId mGoodPrinterId;
private ComponentName mGoodComponentName;
private ComponentName mBadComponentName;
private IPrintManager mIPrintManager;
@@ -93,6 +96,7 @@ public class IPrintManagerParametersTest extends BasePrintTest {
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
CancellationSignal cancellationSignal, LayoutResultCallback callback,
Bundle extras) {
callback.onLayoutFailed("not implemented");
}
@Override
@@ -109,54 +113,46 @@ public class IPrintManagerParametersTest extends BasePrintTest {
*/
private PrintServiceCallbacks createMockCallbacks() {
return createMockPrintServiceCallbacks(
new Answer<PrinterDiscoverySessionCallbacks>() {
@Override
public PrinterDiscoverySessionCallbacks answer(InvocationOnMock invocation) {
return createMockPrinterDiscoverySessionCallbacks(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) {
// Get the session.
StubbablePrinterDiscoverySession session =
((PrinterDiscoverySessionCallbacks) invocation
.getMock()).getSession();
invocation -> createMockPrinterDiscoverySessionCallbacks(invocation1 -> {
// Get the session.
StubbablePrinterDiscoverySession session =
((PrinterDiscoverySessionCallbacks) invocation1
.getMock()).getSession();
if (session.getPrinters().isEmpty()) {
final String PRINTER_NAME = "good printer";
List<PrinterInfo> printers = new ArrayList<>();
if (session.getPrinters().isEmpty()) {
final String PRINTER_NAME = "good printer";
List<PrinterInfo> printers = new ArrayList<>();
// Add the printer.
mGoodPrinterId = session.getService()
.generatePrinterId(PRINTER_NAME);
// Add the printer.
mGoodPrinterId = session.getService()
.generatePrinterId(PRINTER_NAME);
PrinterCapabilitiesInfo capabilities =
new PrinterCapabilitiesInfo.Builder(mGoodPrinterId)
.setMinMargins(
new Margins(200, 200, 200, 200))
.addMediaSize(MediaSize.ISO_A4, true)
.addResolution(new Resolution("300x300",
"300x300", 300, 300),
true)
.setColorModes(
PrintAttributes.COLOR_MODE_COLOR,
PrintAttributes.COLOR_MODE_COLOR)
.build();
PrinterCapabilitiesInfo capabilities =
new PrinterCapabilitiesInfo.Builder(mGoodPrinterId)
.setMinMargins(
new Margins(200, 200, 200, 200))
.addMediaSize(MediaSize.ISO_A4, true)
.addResolution(new Resolution("300x300",
"300x300", 300, 300),
true)
.setColorModes(
PrintAttributes.COLOR_MODE_COLOR,
PrintAttributes.COLOR_MODE_COLOR)
.build();
PrinterInfo printer = new PrinterInfo.Builder(
mGoodPrinterId,
PRINTER_NAME,
PrinterInfo.STATUS_IDLE)
.setCapabilities(capabilities)
.build();
printers.add(printer);
PrinterInfo printer = new PrinterInfo.Builder(
mGoodPrinterId,
PRINTER_NAME,
PrinterInfo.STATUS_IDLE)
.setCapabilities(capabilities)
.build();
printers.add(printer);
session.addPrinters(printers);
}
onPrinterDiscoverySessionStartCalled();
return null;
}
}, null, null, null, null, null, null);
session.addPrinters(printers);
}
},
onPrinterDiscoverySessionStartCalled();
return null;
}, null, null, null, null, null, null),
null, null);
}
@@ -214,20 +210,23 @@ public class IPrintManagerParametersTest extends BasePrintTest {
waitForPrinterDiscoverySessionStartCallbackCalled();
}
/**
* Return a printer Id that is not from any print service
*
* @return The bad printer id.
*/
private PrinterId getBadPrinterId() {
return new PrinterId(getActivity().getComponentName(), "dummy printer");
}
@Override
public void setUp() throws Exception {
super.setUp();
MockPrintService.setCallbacks(createMockCallbacks());
mGoodComponentName = getActivity().getComponentName();
mIPrintManager = IPrintManager.Stub
.asInterface(ServiceManager.getService(Context.PRINT_SERVICE));
// Generate dummy printerId which is a valid PrinterId object, but does not correspond to a
// printer
mBadPrinterId = new PrinterId(mGoodComponentName, "dummy printer");
}
/**
@@ -268,6 +267,7 @@ public class IPrintManagerParametersTest extends BasePrintTest {
* test IPrintManager.getPrintJobInfo
*/
@LargeTest
@Test
public void testGetPrintJobInfo() throws Exception {
startPrinting();
@@ -276,12 +276,9 @@ public class IPrintManagerParametersTest extends BasePrintTest {
assertEquals(null, mIPrintManager.getPrintJobInfo(mBadPrintJobId, mAppId, mUserId));
assertEquals(null, mIPrintManager.getPrintJobInfo(null, mAppId, mUserId));
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.getPrintJobInfo(mGoodPrintJob.getId(), BAD_APP_ID, mUserId);
}
}, SecurityException.class);
assertException(
() -> mIPrintManager.getPrintJobInfo(mGoodPrintJob.getId(), BAD_APP_ID, mUserId),
SecurityException.class);
// Cannot test bad user Id as these tests are allowed to call across users
}
@@ -290,6 +287,7 @@ public class IPrintManagerParametersTest extends BasePrintTest {
* test IPrintManager.getPrintJobInfos
*/
@LargeTest
@Test
public void testGetPrintJobInfos() throws Exception {
startPrinting();
@@ -304,12 +302,8 @@ public class IPrintManagerParametersTest extends BasePrintTest {
}
assertTrue(foundPrintJob);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.getPrintJobInfos(BAD_APP_ID, mUserId);
}
}, SecurityException.class);
assertException(() -> mIPrintManager.getPrintJobInfos(BAD_APP_ID, mUserId),
SecurityException.class);
// Cannot test bad user Id as these tests are allowed to call across users
}
@@ -318,6 +312,7 @@ public class IPrintManagerParametersTest extends BasePrintTest {
* test IPrintManager.print
*/
@LargeTest
@Test
public void testPrint() throws Exception {
final String name = "dummy print job";
@@ -326,44 +321,23 @@ public class IPrintManagerParametersTest extends BasePrintTest {
startPrinting();
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.print(null, adapter, null, mGoodComponentName.getPackageName(),
mAppId, mUserId);
}
}, IllegalArgumentException.class);
assertException(() -> mIPrintManager.print(null, adapter, null,
getActivity().getPackageName(), mAppId, mUserId),
IllegalArgumentException.class);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.print(name, null, null, mGoodComponentName.getPackageName(),
mAppId, mUserId);
}
}, NullPointerException.class);
assertException(() -> mIPrintManager.print(name, null, null,
getActivity().getPackageName(), mAppId, mUserId),
NullPointerException.class);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.print(name, adapter, null, null, mAppId, mUserId);
}
}, IllegalArgumentException.class);
assertException(() -> mIPrintManager.print(name, adapter, null, null, mAppId, mUserId),
IllegalArgumentException.class);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.print(name, adapter, null, mBadComponentName.getPackageName(),
mAppId, mUserId);
}
}, IllegalArgumentException.class);
assertException(() -> mIPrintManager.print(name, adapter, null,
mBadComponentName.getPackageName(), mAppId, mUserId),
IllegalArgumentException.class);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.print(name, adapter, null, mGoodComponentName.getPackageName(),
BAD_APP_ID, mUserId);
}
}, SecurityException.class);
assertException(() -> mIPrintManager.print(name, adapter, null,
getActivity().getPackageName(), BAD_APP_ID, mUserId), SecurityException.class);
// Cannot test bad user Id as these tests are allowed to call across users
}
@@ -372,6 +346,7 @@ public class IPrintManagerParametersTest extends BasePrintTest {
* test IPrintManager.cancelPrintJob
*/
@LargeTest
@Test
public void testCancelPrintJob() throws Exception {
startPrinting();
@@ -379,12 +354,9 @@ public class IPrintManagerParametersTest extends BasePrintTest {
mIPrintManager.cancelPrintJob(mBadPrintJobId, mAppId, mUserId);
mIPrintManager.cancelPrintJob(null, mAppId, mUserId);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.cancelPrintJob(mGoodPrintJob.getId(), BAD_APP_ID, mUserId);
}
}, SecurityException.class);
assertException(
() -> mIPrintManager.cancelPrintJob(mGoodPrintJob.getId(), BAD_APP_ID, mUserId),
SecurityException.class);
// Cannot test bad user Id as these tests are allowed to call across users
@@ -396,6 +368,7 @@ public class IPrintManagerParametersTest extends BasePrintTest {
* test IPrintManager.restartPrintJob
*/
@LargeTest
@Test
public void testRestartPrintJob() throws Exception {
startPrinting();
@@ -405,12 +378,9 @@ public class IPrintManagerParametersTest extends BasePrintTest {
mIPrintManager.restartPrintJob(mBadPrintJobId, mAppId, mUserId);
mIPrintManager.restartPrintJob(null, mAppId, mUserId);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.restartPrintJob(mGoodPrintJob.getId(), BAD_APP_ID, mUserId);
}
}, SecurityException.class);
assertException(
() -> mIPrintManager.restartPrintJob(mGoodPrintJob.getId(), BAD_APP_ID, mUserId),
SecurityException.class);
// Cannot test bad user Id as these tests are allowed to call across users
}
@@ -419,24 +389,18 @@ public class IPrintManagerParametersTest extends BasePrintTest {
* test IPrintManager.addPrintJobStateChangeListener
*/
@MediumTest
@Test
public void testAddPrintJobStateChangeListener() throws Exception {
final IPrintJobStateChangeListener listener = createMockIPrintJobStateChangeListener();
mIPrintManager.addPrintJobStateChangeListener(listener, mAppId, mUserId);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.addPrintJobStateChangeListener(null, mAppId, mUserId);
}
}, NullPointerException.class);
assertException(() -> mIPrintManager.addPrintJobStateChangeListener(null, mAppId, mUserId),
NullPointerException.class);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.addPrintJobStateChangeListener(listener, BAD_APP_ID, mUserId);
}
}, SecurityException.class);
assertException(
() -> mIPrintManager.addPrintJobStateChangeListener(listener, BAD_APP_ID, mUserId),
SecurityException.class);
// Cannot test bad user Id as these tests are allowed to call across users
}
@@ -445,6 +409,7 @@ public class IPrintManagerParametersTest extends BasePrintTest {
* test IPrintManager.removePrintJobStateChangeListener
*/
@MediumTest
@Test
public void testRemovePrintJobStateChangeListener() throws Exception {
final IPrintJobStateChangeListener listener = createMockIPrintJobStateChangeListener();
@@ -455,12 +420,8 @@ public class IPrintManagerParametersTest extends BasePrintTest {
mIPrintManager.removePrintJobStateChangeListener(listener, mUserId);
mIPrintManager.addPrintJobStateChangeListener(listener, mAppId, mUserId);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.removePrintJobStateChangeListener(null, mUserId);
}
}, NullPointerException.class);
assertException(() -> mIPrintManager.removePrintJobStateChangeListener(null, mUserId),
NullPointerException.class);
// Cannot test bad user Id as these tests are allowed to call across users
}
@@ -469,17 +430,14 @@ public class IPrintManagerParametersTest extends BasePrintTest {
* test IPrintManager.addPrintServicesChangeListener
*/
@MediumTest
@Test
public void testAddPrintServicesChangeListener() throws Exception {
final IPrintServicesChangeListener listener = createMockIPrintServicesChangeListener();
mIPrintManager.addPrintServicesChangeListener(listener, mUserId);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.addPrintServicesChangeListener(null, mUserId);
}
}, NullPointerException.class);
assertException(() -> mIPrintManager.addPrintServicesChangeListener(null, mUserId),
NullPointerException.class);
// Cannot test bad user Id as these tests are allowed to call across users
}
@@ -488,6 +446,7 @@ public class IPrintManagerParametersTest extends BasePrintTest {
* test IPrintManager.removePrintServicesChangeListener
*/
@MediumTest
@Test
public void testRemovePrintServicesChangeListener() throws Exception {
final IPrintServicesChangeListener listener = createMockIPrintServicesChangeListener();
@@ -498,12 +457,8 @@ public class IPrintManagerParametersTest extends BasePrintTest {
mIPrintManager.removePrintServicesChangeListener(listener, mUserId);
mIPrintManager.addPrintServicesChangeListener(listener, mUserId);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.removePrintServicesChangeListener(null, mUserId);
}
}, NullPointerException.class);
assertException(() -> mIPrintManager.removePrintServicesChangeListener(null, mUserId),
NullPointerException.class);
// Cannot test bad user Id as these tests are allowed to call across users
}
@@ -512,6 +467,7 @@ public class IPrintManagerParametersTest extends BasePrintTest {
* test IPrintManager.getPrintServices
*/
@MediumTest
@Test
public void testGetPrintServices() throws Exception {
List<PrintServiceInfo> printServices = mIPrintManager.getPrintServices(
PrintManager.ALL_SERVICES, mUserId);
@@ -520,12 +476,8 @@ public class IPrintManagerParametersTest extends BasePrintTest {
printServices = mIPrintManager.getPrintServices(0, mUserId);
assertEquals(printServices, null);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.getPrintServices(~PrintManager.ALL_SERVICES, mUserId);
}
}, IllegalArgumentException.class);
assertException(() -> mIPrintManager.getPrintServices(~PrintManager.ALL_SERVICES, mUserId),
IllegalArgumentException.class);
// Cannot test bad user Id as these tests are allowed to call across users
}
@@ -534,38 +486,23 @@ public class IPrintManagerParametersTest extends BasePrintTest {
* test IPrintManager.setPrintServiceEnabled
*/
@MediumTest
@Test
public void testSetPrintServiceEnabled() throws Exception {
final ComponentName printService = mIPrintManager.getPrintServices(
PrintManager.ALL_SERVICES, mUserId).get(0).getComponentName();
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.setPrintServiceEnabled(printService, false, mUserId);
}
}, SecurityException.class);
assertException(() -> mIPrintManager.setPrintServiceEnabled(printService, false, mUserId),
SecurityException.class);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.setPrintServiceEnabled(printService, true, mUserId);
}
}, SecurityException.class);
assertException(() -> mIPrintManager.setPrintServiceEnabled(printService, true, mUserId),
SecurityException.class);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.setPrintServiceEnabled(new ComponentName("bad", "name"), true,
mUserId);
}
}, SecurityException.class);
assertException(
() -> mIPrintManager.setPrintServiceEnabled(new ComponentName("bad", "name"), true,
mUserId), SecurityException.class);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.setPrintServiceEnabled(null, true, mUserId);
}
}, SecurityException.class);
assertException(() -> mIPrintManager.setPrintServiceEnabled(null, true, mUserId),
SecurityException.class);
// Cannot test bad user Id as these tests are allowed to call across users
}
@@ -574,18 +511,16 @@ public class IPrintManagerParametersTest extends BasePrintTest {
* test IPrintManager.addPrintServiceRecommendationsChangeListener
*/
@MediumTest
@Test
public void testAddPrintServiceRecommendationsChangeListener() throws Exception {
final IRecommendationsChangeListener listener =
createMockIPrintServiceRecommendationsChangeListener();
mIPrintManager.addPrintServiceRecommendationsChangeListener(listener, mUserId);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.addPrintServiceRecommendationsChangeListener(null, mUserId);
}
}, NullPointerException.class);
assertException(
() -> mIPrintManager.addPrintServiceRecommendationsChangeListener(null, mUserId),
NullPointerException.class);
// Cannot test bad user Id as these tests are allowed to call across users
}
@@ -594,6 +529,7 @@ public class IPrintManagerParametersTest extends BasePrintTest {
* test IPrintManager.removePrintServicesChangeListener
*/
@MediumTest
@Test
public void testRemovePrintServiceRecommendationsChangeListener() throws Exception {
final IRecommendationsChangeListener listener =
createMockIPrintServiceRecommendationsChangeListener();
@@ -605,12 +541,9 @@ public class IPrintManagerParametersTest extends BasePrintTest {
mIPrintManager.removePrintServiceRecommendationsChangeListener(listener, mUserId);
mIPrintManager.addPrintServiceRecommendationsChangeListener(listener, mUserId);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.removePrintServiceRecommendationsChangeListener(null, mUserId);
}
}, NullPointerException.class);
assertException(
() -> mIPrintManager.removePrintServiceRecommendationsChangeListener(null, mUserId),
NullPointerException.class);
// Cannot test bad user Id as these tests are allowed to call across users
}
@@ -619,6 +552,7 @@ public class IPrintManagerParametersTest extends BasePrintTest {
* test IPrintManager.getPrintServiceRecommendations
*/
@MediumTest
@Test
public void testGetPrintServiceRecommendations() throws Exception {
mIPrintManager.getPrintServiceRecommendations(mUserId);
@@ -629,18 +563,15 @@ public class IPrintManagerParametersTest extends BasePrintTest {
* test IPrintManager.createPrinterDiscoverySession
*/
@MediumTest
@Test
public void testCreatePrinterDiscoverySession() throws Exception {
final IPrinterDiscoveryObserver listener = createMockIPrinterDiscoveryObserver();
mIPrintManager.createPrinterDiscoverySession(listener, mUserId);
try {
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.createPrinterDiscoverySession(null, mUserId);
}
}, NullPointerException.class);
assertException(() -> mIPrintManager.createPrinterDiscoverySession(null, mUserId),
NullPointerException.class);
// Cannot test bad user Id as these tests are allowed to call across users
} finally {
@@ -655,6 +586,7 @@ public class IPrintManagerParametersTest extends BasePrintTest {
* test IPrintManager.startPrinterDiscovery
*/
@LargeTest
@Test
public void testStartPrinterDiscovery() throws Exception {
startPrinting();
@@ -663,7 +595,7 @@ public class IPrintManagerParametersTest extends BasePrintTest {
goodPrinters.add(mGoodPrinterId);
final List<PrinterId> badPrinters = new ArrayList<>();
badPrinters.add(mBadPrinterId);
badPrinters.add(getBadPrinterId());
final List<PrinterId> emptyPrinters = new ArrayList<>();
@@ -677,19 +609,11 @@ public class IPrintManagerParametersTest extends BasePrintTest {
mIPrintManager.startPrinterDiscovery(listener, emptyPrinters, mUserId);
mIPrintManager.startPrinterDiscovery(listener, null, mUserId);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.startPrinterDiscovery(listener, nullPrinters, mUserId);
}
}, NullPointerException.class);
assertException(() -> mIPrintManager.startPrinterDiscovery(listener, nullPrinters, mUserId),
NullPointerException.class);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.startPrinterDiscovery(null, goodPrinters, mUserId);
}
}, NullPointerException.class);
assertException(() -> mIPrintManager.startPrinterDiscovery(null, goodPrinters, mUserId),
NullPointerException.class);
// Cannot test bad user Id as these tests are allowed to call across users
}
@@ -698,6 +622,7 @@ public class IPrintManagerParametersTest extends BasePrintTest {
* test IPrintManager.stopPrinterDiscovery
*/
@MediumTest
@Test
public void testStopPrinterDiscovery() throws Exception {
final IPrinterDiscoveryObserver listener = createMockIPrinterDiscoveryObserver();
@@ -708,12 +633,8 @@ public class IPrintManagerParametersTest extends BasePrintTest {
mIPrintManager.stopPrinterDiscovery(listener, mUserId);
mIPrintManager.startPrinterDiscovery(listener, null, mUserId);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.stopPrinterDiscovery(null, mUserId);
}
}, NullPointerException.class);
assertException(() -> mIPrintManager.stopPrinterDiscovery(null, mUserId),
NullPointerException.class);
// Cannot test bad user Id as these tests are allowed to call across users
}
@@ -722,6 +643,7 @@ public class IPrintManagerParametersTest extends BasePrintTest {
* test IPrintManager.validatePrinters
*/
@LargeTest
@Test
public void testValidatePrinters() throws Exception {
startPrinting();
@@ -729,7 +651,7 @@ public class IPrintManagerParametersTest extends BasePrintTest {
goodPrinters.add(mGoodPrinterId);
final List<PrinterId> badPrinters = new ArrayList<>();
badPrinters.add(mBadPrinterId);
badPrinters.add(getBadPrinterId());
final List<PrinterId> emptyPrinters = new ArrayList<>();
@@ -742,19 +664,11 @@ public class IPrintManagerParametersTest extends BasePrintTest {
mIPrintManager.validatePrinters(badPrinters, mUserId);
mIPrintManager.validatePrinters(emptyPrinters, mUserId);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.validatePrinters(null, mUserId);
}
}, NullPointerException.class);
assertException(() -> mIPrintManager.validatePrinters(null, mUserId),
NullPointerException.class);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.validatePrinters(nullPrinters, mUserId);
}
}, NullPointerException.class);
assertException(() -> mIPrintManager.validatePrinters(nullPrinters, mUserId),
NullPointerException.class);
// Cannot test bad user Id as these tests are allowed to call across users
}
@@ -763,20 +677,17 @@ public class IPrintManagerParametersTest extends BasePrintTest {
* test IPrintManager.startPrinterStateTracking
*/
@LargeTest
@Test
public void testStartPrinterStateTracking() throws Exception {
startPrinting();
mIPrintManager.startPrinterStateTracking(mGoodPrinterId, mUserId);
// Bad printers do no cause exceptions
mIPrintManager.startPrinterStateTracking(mBadPrinterId, mUserId);
mIPrintManager.startPrinterStateTracking(getBadPrinterId(), mUserId);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.startPrinterStateTracking(null, mUserId);
}
}, NullPointerException.class);
assertException(() -> mIPrintManager.startPrinterStateTracking(null, mUserId),
NullPointerException.class);
// Cannot test bad user Id as these tests are allowed to call across users
}
@@ -785,20 +696,17 @@ public class IPrintManagerParametersTest extends BasePrintTest {
* test IPrintManager.getCustomPrinterIcon
*/
@LargeTest
@Test
public void testGetCustomPrinterIcon() throws Exception {
startPrinting();
mIPrintManager.getCustomPrinterIcon(mGoodPrinterId, mUserId);
// Bad printers do no cause exceptions
mIPrintManager.getCustomPrinterIcon(mBadPrinterId, mUserId);
mIPrintManager.getCustomPrinterIcon(getBadPrinterId(), mUserId);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.getCustomPrinterIcon(null, mUserId);
}
}, NullPointerException.class);
assertException(() -> mIPrintManager.getCustomPrinterIcon(null, mUserId),
NullPointerException.class);
// Cannot test bad user Id as these tests are allowed to call across users
}
@@ -807,6 +715,7 @@ public class IPrintManagerParametersTest extends BasePrintTest {
* test IPrintManager.stopPrinterStateTracking
*/
@LargeTest
@Test
public void testStopPrinterStateTracking() throws Exception {
startPrinting();
@@ -817,15 +726,11 @@ public class IPrintManagerParametersTest extends BasePrintTest {
mIPrintManager.stopPrinterStateTracking(mGoodPrinterId, mUserId);
// Bad printers do no cause exceptions
mIPrintManager.startPrinterStateTracking(mBadPrinterId, mUserId);
mIPrintManager.stopPrinterStateTracking(mBadPrinterId, mUserId);
mIPrintManager.startPrinterStateTracking(getBadPrinterId(), mUserId);
mIPrintManager.stopPrinterStateTracking(getBadPrinterId(), mUserId);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.stopPrinterStateTracking(null, mUserId);
}
}, NullPointerException.class);
assertException(() -> mIPrintManager.stopPrinterStateTracking(null, mUserId),
NullPointerException.class);
// Cannot test bad user Id as these tests are allowed to call across users
}
@@ -834,6 +739,7 @@ public class IPrintManagerParametersTest extends BasePrintTest {
* test IPrintManager.destroyPrinterDiscoverySession
*/
@MediumTest
@Test
public void testDestroyPrinterDiscoverySession() throws Exception {
final IPrinterDiscoveryObserver listener = createMockIPrinterDiscoveryObserver();
@@ -843,12 +749,8 @@ public class IPrintManagerParametersTest extends BasePrintTest {
// Destroying already destroyed session is a no-op
mIPrintManager.destroyPrinterDiscoverySession(listener, mUserId);
assertException(new Invokable() {
@Override
public void run() throws Exception {
mIPrintManager.destroyPrinterDiscoverySession(null, mUserId);
}
}, NullPointerException.class);
assertException(() -> mIPrintManager.destroyPrinterDiscoverySession(null, mUserId),
NullPointerException.class);
// Cannot test bad user Id as these tests are allowed to call across users
}

View File

@@ -21,7 +21,6 @@ import android.os.Bundle;
import android.view.WindowManager;
public class PrintTestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

View File

@@ -16,6 +16,7 @@
package android.print.mockservice;
import android.support.annotation.NonNull;
import android.os.CancellationSignal;
import android.print.PrinterId;
import android.printservice.CustomPrinterIconCallback;
@@ -42,7 +43,7 @@ public class StubbablePrinterDiscoverySession extends PrinterDiscoverySession {
}
@Override
public void onStartPrinterDiscovery(List<PrinterId> priorityList) {
public void onStartPrinterDiscovery(@NonNull List<PrinterId> priorityList) {
if (mCallbacks != null) {
mCallbacks.onStartPrinterDiscovery(priorityList);
}
@@ -56,29 +57,30 @@ public class StubbablePrinterDiscoverySession extends PrinterDiscoverySession {
}
@Override
public void onValidatePrinters(List<PrinterId> printerIds) {
public void onValidatePrinters(@NonNull List<PrinterId> printerIds) {
if (mCallbacks != null) {
mCallbacks.onValidatePrinters(printerIds);
}
}
@Override
public void onStartPrinterStateTracking(PrinterId printerId) {
public void onStartPrinterStateTracking(@NonNull PrinterId printerId) {
if (mCallbacks != null) {
mCallbacks.onStartPrinterStateTracking(printerId);
}
}
@Override
public void onRequestCustomPrinterIcon(PrinterId printerId,
CancellationSignal cancellationSignal, CustomPrinterIconCallback callback) {
public void onRequestCustomPrinterIcon(@NonNull PrinterId printerId,
@NonNull CancellationSignal cancellationSignal,
@NonNull CustomPrinterIconCallback callback) {
if (mCallbacks != null) {
mCallbacks.onRequestCustomPrinterIcon(printerId, cancellationSignal, callback);
}
}
@Override
public void onStopPrinterStateTracking(PrinterId printerId) {
public void onStopPrinterStateTracking(@NonNull PrinterId printerId) {
if (mCallbacks != null) {
mCallbacks.onStopPrinterStateTracking(printerId);
}