Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 015deed8 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Make print core tests junit tests."

parents 0a08bc87 3ae33bd0
Loading
Loading
Loading
Loading
+49 −127
Original line number Diff line number Diff line
@@ -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 String[] getEnabledImes() throws IOException {
        List<String> imeList = new ArrayList<>();
    private static Instrumentation sInstrumentation;
    private static UiDevice sUiDevice;

        ParcelFileDescriptor pfd = getInstrumentation().getUiAutomation()
                .executeShellCommand(COMMAND_LIST_ENABLED_IME_COMPONENTS);
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(new FileInputStream(pfd.getFileDescriptor())))) {
    @Rule
    public ActivityTestRule<PrintTestActivity> mActivityRule =
            new ActivityTestRule<>(PrintTestActivity.class, false, true);

            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() {
        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);
    }
}
+158 −256

File changed.

Preview size limit exceeded, changes collapsed.

+0 −1
Original line number Diff line number Diff line
@@ -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);
+8 −6
Original line number Diff line number Diff line
@@ -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);
        }