159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann/*
259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann * Copyright (C) 2016 The Android Open Source Project
359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann *
459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann *  Licensed under the Apache License, Version 2.0 (the "License");
559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann *  you may not use this file except in compliance with the License.
659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann *  You may obtain a copy of the License at
759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann *
859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann *       http://www.apache.org/licenses/LICENSE-2.0
959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann *
1059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann *  Unless required by applicable law or agreed to in writing, software
1159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann *  distributed under the License is distributed on an "AS IS" BASIS,
1259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann *  See the License for the specific language governing permissions and
1459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann *  limitations under the License.
1559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann */
1659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
17cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannpackage com.android.printspooler.outofprocess.tests;
1859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
1959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmannimport android.graphics.pdf.PdfDocument;
2059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmannimport android.os.Bundle;
2159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmannimport android.os.CancellationSignal;
2259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmannimport android.os.ParcelFileDescriptor;
23cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport android.print.PageRange;
24cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport android.print.PrintAttributes;
25cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport android.print.PrintDocumentAdapter;
26cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport android.print.PrintDocumentInfo;
27cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport android.print.PrinterCapabilitiesInfo;
28cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport android.print.PrinterId;
29cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport android.print.PrinterInfo;
30cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport com.android.printspooler.outofprocess.tests.mockservice.AddPrintersActivity;
31cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport com.android.printspooler.outofprocess.tests.mockservice.MockPrintService;
32cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport com.android.printspooler.outofprocess.tests.mockservice.PrinterDiscoverySessionCallbacks;
33cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport com.android.printspooler.outofprocess.tests.mockservice.StubbablePrinterDiscoverySession;
3459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmannimport android.print.pdf.PrintedPdfDocument;
3559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmannimport android.support.test.filters.LargeTest;
3686090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmannimport android.support.test.uiautomator.By;
3759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmannimport android.support.test.uiautomator.UiObject;
3859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmannimport android.support.test.uiautomator.UiObjectNotFoundException;
3959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmannimport android.support.test.uiautomator.UiSelector;
4086090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmannimport android.support.test.uiautomator.Until;
4159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmannimport android.util.Log;
4259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmannimport org.junit.AfterClass;
4359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmannimport org.junit.BeforeClass;
4459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmannimport org.junit.Test;
4586090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmannimport org.junit.runner.RunWith;
4686090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmannimport org.junit.runners.Parameterized;
4759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
4859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmannimport java.io.FileInputStream;
4959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmannimport java.io.FileOutputStream;
5059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmannimport java.io.IOException;
5159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmannimport java.util.ArrayList;
5286090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmannimport java.util.Collection;
5359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmannimport java.util.List;
5459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmannimport java.util.concurrent.TimeoutException;
5559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmannimport java.util.function.Supplier;
5659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
5786090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmannimport static org.junit.Assert.assertNotNull;
5859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
5959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann/**
6059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann * Tests for the basic printing workflows
6159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann */
6286090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann@RunWith(Parameterized.class)
6359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmannpublic class WorkflowTest extends BasePrintTest {
6459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    private static final String LOG_TAG = WorkflowTest.class.getSimpleName();
6559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
6659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    private static float sWindowAnimationScaleBefore;
6759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    private static float sTransitionAnimationScaleBefore;
6859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    private static float sAnimatiorDurationScaleBefore;
6959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
7086090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann    private PrintAttributes.MediaSize mFirst;
7186090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann    private boolean mSelectPrinter;
7286090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann    private PrintAttributes.MediaSize mSecond;
7386090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann
7486090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann    public WorkflowTest(PrintAttributes.MediaSize first, boolean selectPrinter,
7586090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            PrintAttributes.MediaSize second) {
7686090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        mFirst = first;
7786090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        mSelectPrinter = selectPrinter;
7886090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        mSecond = second;
7986090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann    }
8086090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann
8159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    interface InterruptableConsumer<T> {
8259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        void accept(T t) throws InterruptedException;
8359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    }
8459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
8559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    /**
8659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann     * Execute {@code waiter} until {@code condition} is met.
8759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann     *
8859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann     * @param condition Conditions to wait for
8959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann     * @param waiter    Code to execute while waiting
9059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann     */
9159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    private void waitWithTimeout(Supplier<Boolean> condition, InterruptableConsumer<Long> waiter)
9259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            throws TimeoutException, InterruptedException {
9359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        long startTime = System.currentTimeMillis();
9459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        while (condition.get()) {
9559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            long timeLeft = OPERATION_TIMEOUT - (System.currentTimeMillis() - startTime);
9659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            if (timeLeft < 0) {
9759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                throw new TimeoutException();
9859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            }
9959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
10059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            waiter.accept(timeLeft);
10159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        }
10259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    }
10359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
10459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    /**
10559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann     * Executes a shell command using shell user identity, and return the standard output in
10659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann     * string.
10759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann     *
10859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann     * @param cmd the command to run
10959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann     *
11059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann     * @return the standard output of the command
11159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann     */
11259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    private static String runShellCommand(String cmd) throws IOException {
11359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        try (FileInputStream is = new ParcelFileDescriptor.AutoCloseInputStream(
11459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                getInstrumentation().getUiAutomation().executeShellCommand(cmd))) {
11559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            byte[] buf = new byte[64];
11659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            int bytesRead;
11759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
11859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            StringBuilder stdout = new StringBuilder();
11959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            while ((bytesRead = is.read(buf)) != -1) {
12059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                stdout.append(new String(buf, 0, bytesRead));
12159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            }
12259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
12359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            return stdout.toString();
12459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        }
12559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    }
12659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
12759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    @BeforeClass
12859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    public static void disableAnimations() throws Exception {
12959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        try {
13059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            sWindowAnimationScaleBefore = Float.parseFloat(runShellCommand(
13159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                    "settings get global window_animation_scale"));
13259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
13359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            runShellCommand("settings put global window_animation_scale 0");
13459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        } catch (NumberFormatException e) {
13559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            sWindowAnimationScaleBefore = Float.NaN;
13659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        }
13759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        try {
13859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            sTransitionAnimationScaleBefore = Float.parseFloat(runShellCommand(
13959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                    "settings get global transition_animation_scale"));
14059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
14159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            runShellCommand("settings put global transition_animation_scale 0");
14259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        } catch (NumberFormatException e) {
14359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            sTransitionAnimationScaleBefore = Float.NaN;
14459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        }
14559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        try {
14659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            sAnimatiorDurationScaleBefore = Float.parseFloat(runShellCommand(
14759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                    "settings get global animator_duration_scale"));
14859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
14959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            runShellCommand("settings put global animator_duration_scale 0");
15059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        } catch (NumberFormatException e) {
15159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            sAnimatiorDurationScaleBefore = Float.NaN;
15259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        }
15359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    }
15459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
15559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    @AfterClass
15659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    public static void enableAnimations() throws Exception {
15759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        if (sWindowAnimationScaleBefore != Float.NaN) {
15859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            runShellCommand(
15959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                    "settings put global window_animation_scale " + sWindowAnimationScaleBefore);
16059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        }
16159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        if (sTransitionAnimationScaleBefore != Float.NaN) {
16259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            runShellCommand(
16359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                    "settings put global transition_animation_scale " +
16459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                            sTransitionAnimationScaleBefore);
16559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        }
16659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        if (sAnimatiorDurationScaleBefore != Float.NaN) {
16759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            runShellCommand(
16859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                    "settings put global animator_duration_scale " + sAnimatiorDurationScaleBefore);
16959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        }
17059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    }
17159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
17259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    /** Add a printer with a given name and supported mediasize to a session */
17359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    private void addPrinter(StubbablePrinterDiscoverySession session,
17459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            String name, PrintAttributes.MediaSize mediaSize) {
17559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        PrinterId printerId = session.getService().generatePrinterId(name);
17659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        List<PrinterInfo> printers = new ArrayList<>(1);
17759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
17859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        PrinterCapabilitiesInfo.Builder builder =
17959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                new PrinterCapabilitiesInfo.Builder(printerId);
18059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
18186090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        PrinterInfo printerInfo;
18286090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        if (mediaSize != null) {
18386090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            builder.setMinMargins(new PrintAttributes.Margins(0, 0, 0, 0))
18486090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                    .setColorModes(PrintAttributes.COLOR_MODE_COLOR,
18586090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                            PrintAttributes.COLOR_MODE_COLOR)
18686090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                    .addMediaSize(mediaSize, true)
18786090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                    .addResolution(new PrintAttributes.Resolution("300x300", "300x300", 300, 300),
18886090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                            true);
18986090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann
19086090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            printerInfo = new PrinterInfo.Builder(printerId, name,
19186090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                    PrinterInfo.STATUS_IDLE).setCapabilities(builder.build()).build();
19286090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        } else {
19386090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            printerInfo = (new PrinterInfo.Builder(printerId, name,
19486090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                    PrinterInfo.STATUS_IDLE)).build();
19586090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        }
19659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
19786090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        printers.add(printerInfo);
19859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        session.addPrinters(printers);
19959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    }
20059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
20159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    /** Find a certain element in the UI and click on it */
20259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    private void clickOn(UiSelector selector) throws UiObjectNotFoundException {
20359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        Log.i(LOG_TAG, "Click on " + selector);
20459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        UiObject view = getUiDevice().findObject(selector);
20559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        view.click();
20659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        getUiDevice().waitForIdle();
20759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    }
20859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
20959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    /** Find a certain text in the UI and click on it */
21059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    private void clickOnText(String text) throws UiObjectNotFoundException {
21159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        clickOn(new UiSelector().text(text));
21259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    }
21359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
21459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    /** Set the printer in the print activity */
21559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    private void setPrinter(String printerName) throws UiObjectNotFoundException {
21659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        clickOn(new UiSelector().resourceId("com.android.printspooler:id/destination_spinner"));
21759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
21859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        clickOnText(printerName);
21959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    }
22059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
22159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    /**
22259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann     * Init mock print servic that returns a single printer by default.
22359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann     *
22459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann     * @param sessionRef Where to store the reference to the session once started
22559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann     */
22686090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann    private void setMockPrintServiceCallbacks(StubbablePrinterDiscoverySession[] sessionRef,
22786090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            ArrayList<String> trackedPrinters, PrintAttributes.MediaSize mediaSize) {
22859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        MockPrintService.setCallbacks(createMockPrintServiceCallbacks(
22959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                inv -> createMockPrinterDiscoverySessionCallbacks(inv2 -> {
23059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                            synchronized (sessionRef) {
23159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                                sessionRef[0] = ((PrinterDiscoverySessionCallbacks) inv2.getMock())
23259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                                        .getSession();
23359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
23486090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                                addPrinter(sessionRef[0], "1st printer", mediaSize);
23559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
23659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                                sessionRef.notifyAll();
23759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                            }
23859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                            return null;
23959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                        },
24086090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                        null, null, inv2 -> {
24186090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                            synchronized (trackedPrinters) {
24286090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                                trackedPrinters
24386090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                                        .add(((PrinterId) inv2.getArguments()[0]).getLocalId());
24486090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                                trackedPrinters.notifyAll();
24586090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                            }
24686090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                            return null;
24786090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                        }, null, inv2 -> {
24886090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                            synchronized (trackedPrinters) {
24986090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                                trackedPrinters
25086090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                                        .remove(((PrinterId) inv2.getArguments()[0]).getLocalId());
25186090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                                trackedPrinters.notifyAll();
25286090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                            }
25386090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                            return null;
25486090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                        }, inv2 -> {
25559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                            synchronized (sessionRef) {
25659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                                sessionRef[0] = null;
25759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                                sessionRef.notifyAll();
25859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                            }
25959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                            return null;
26059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                        }
26186090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                ), null, null));
26259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    }
26359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
26459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    /**
26559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann     * Start print operation that just prints a single empty page
26659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann     *
26759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann     * @param printAttributesRef Where to store the reference to the print attributes once started
26859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann     */
26959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    private void print(PrintAttributes[] printAttributesRef) {
27059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        print(new PrintDocumentAdapter() {
27159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            @Override
27259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            public void onStart() {
27359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            }
27459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
27559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            @Override
27659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
27759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                    CancellationSignal cancellationSignal, LayoutResultCallback callback,
27859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                    Bundle extras) {
27959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                callback.onLayoutFinished((new PrintDocumentInfo.Builder("doc")).build(),
28059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                        !newAttributes.equals(printAttributesRef[0]));
28159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
28259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                synchronized (printAttributesRef) {
28359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                    printAttributesRef[0] = newAttributes;
28459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                    printAttributesRef.notifyAll();
28559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                }
28659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            }
28759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
28859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            @Override
28959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            public void onWrite(PageRange[] pages, ParcelFileDescriptor destination,
29059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                    CancellationSignal cancellationSignal, WriteResultCallback callback) {
29159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                try {
29259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                    try {
29359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                        PrintedPdfDocument document = new PrintedPdfDocument(getActivity(),
29459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                                printAttributesRef[0]);
29559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                        try {
29659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                            PdfDocument.Page page = document.startPage(0);
29759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                            document.finishPage(page);
29859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                            try (FileOutputStream os = new FileOutputStream(
29959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                                    destination.getFileDescriptor())) {
30059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                                document.writeTo(os);
30159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                                os.flush();
30259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                            }
30359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                        } finally {
30459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                            document.close();
30559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                        }
30659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                    } finally {
30759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                        destination.close();
30859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                    }
30959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
31059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                    callback.onWriteFinished(pages);
31159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                } catch (IOException e) {
31259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                    callback.onWriteFailed(e.getMessage());
31359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                }
31459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            }
31559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        }, null);
31659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    }
31759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
31886090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann    @Parameterized.Parameters
31986090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann    public static Collection<Object[]> getParameters() {
32086090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        ArrayList<Object[]> tests = new ArrayList<>();
32186090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann
32286090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        for (PrintAttributes.MediaSize first : new PrintAttributes.MediaSize[]{
32386090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                PrintAttributes.MediaSize.ISO_A0, null}) {
32486090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            for (Boolean selectPrinter : new Boolean[]{true, false}) {
32586090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                for (PrintAttributes.MediaSize second : new PrintAttributes.MediaSize[]{
32686090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                        PrintAttributes.MediaSize.ISO_A1, null}) {
32786090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                    // If we do not use the second printer, no need to try various options
32886090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                    if (!selectPrinter && second == null) {
32986090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                        continue;
33086090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                    }
33186090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                    tests.add(new Object[]{first, selectPrinter, second});
33286090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                }
33386090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            }
33459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        }
33559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
33686090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        return tests;
33759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    }
33859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
33959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    @Test
34059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    @LargeTest
34186090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann    public void addAndSelectPrinter() throws Exception {
34259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        final StubbablePrinterDiscoverySession session[] = new StubbablePrinterDiscoverySession[1];
34359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        final PrintAttributes printAttributes[] = new PrintAttributes[1];
34486090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        ArrayList<String> trackedPrinters = new ArrayList<>();
34586090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann
34686090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        Log.i(LOG_TAG, "Running " + mFirst + " " + mSelectPrinter + " " + mSecond);
34759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
34886090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        setMockPrintServiceCallbacks(session, trackedPrinters, mFirst);
34959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        print(printAttributes);
35059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
35159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        // We are now in the PrintActivity
35259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        Log.i(LOG_TAG, "Waiting for session");
35359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        synchronized (session) {
35459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            waitWithTimeout(() -> session[0] == null, session::wait);
35559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        }
35659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
35759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        setPrinter("1st printer");
35859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
35986090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        Log.i(LOG_TAG, "Waiting for 1st printer to be tracked");
36086090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        synchronized (trackedPrinters) {
36186090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            waitWithTimeout(() -> !trackedPrinters.contains("1st printer"), trackedPrinters::wait);
36286090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        }
36386090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann
36486090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        if (mFirst != null) {
36586090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            Log.i(LOG_TAG, "Waiting for print attributes to change");
36686090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            synchronized (printAttributes) {
36786090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                waitWithTimeout(
36886090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                        () -> printAttributes[0] == null ||
36986090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                                !printAttributes[0].getMediaSize().equals(
37086090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                                        mFirst), printAttributes::wait);
37186090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            }
37286090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        } else {
37386090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            Log.i(LOG_TAG, "Waiting for error message");
37486090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            assertNotNull(getUiDevice().wait(Until.findObject(
37586090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                    By.text("This printer isn't available right now.")), OPERATION_TIMEOUT));
37659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        }
37759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
37859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        setPrinter("All printers\u2026");
37959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
38059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        // We are now in the SelectPrinterActivity
38159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        clickOnText("Add printer");
38259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
38359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        // We are now in the AddPrinterActivity
38459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        AddPrintersActivity.addObserver(
38586090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                () -> addPrinter(session[0], "2nd printer", mSecond));
38659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
38759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        // This executes the observer registered above
38859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        clickOn(new UiSelector().text(MockPrintService.class.getCanonicalName())
38959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann                .resourceId("com.android.printspooler:id/title"));
39059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
39159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        getUiDevice().pressBack();
39259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        AddPrintersActivity.clearObservers();
39359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
39486090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        if (mSelectPrinter) {
39586090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            // We are now in the SelectPrinterActivity
39686090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            clickOnText("2nd printer");
39786090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        } else {
39886090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            getUiDevice().pressBack();
39986090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        }
40059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
40159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        // We are now in the PrintActivity
40286090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        if (mSelectPrinter) {
40386090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            if (mSecond != null) {
40486090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                Log.i(LOG_TAG, "Waiting for print attributes to change");
40586090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                synchronized (printAttributes) {
40686090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                    waitWithTimeout(
40786090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                            () -> printAttributes[0] == null ||
40886090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                                    !printAttributes[0].getMediaSize().equals(
40986090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                                            mSecond), printAttributes::wait);
41086090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                }
41186090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            } else {
41286090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                Log.i(LOG_TAG, "Waiting for error message");
41386090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                assertNotNull(getUiDevice().wait(Until.findObject(
41486090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                        By.text("This printer isn't available right now.")), OPERATION_TIMEOUT));
41586090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            }
41686090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann
41786090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            Log.i(LOG_TAG, "Waiting for 1st printer to be not tracked");
41886090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            synchronized (trackedPrinters) {
41986090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                waitWithTimeout(() -> trackedPrinters.contains("1st printer"),
42086090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                        trackedPrinters::wait);
42186090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            }
42286090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann
42386090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            Log.i(LOG_TAG, "Waiting for 2nd printer to be tracked");
42486090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            synchronized (trackedPrinters) {
42586090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                waitWithTimeout(() -> !trackedPrinters.contains("2nd printer"),
42686090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                        trackedPrinters::wait);
42786090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            }
42886090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        } else {
42986090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            Thread.sleep(100);
43086090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann
43186090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            if (mFirst != null) {
43286090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                Log.i(LOG_TAG, "Waiting for print attributes to change");
43386090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                synchronized (printAttributes) {
43486090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                    waitWithTimeout(
43586090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                            () -> printAttributes[0] == null ||
43686090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                                    !printAttributes[0].getMediaSize().equals(
43786090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                                            mFirst), printAttributes::wait);
43886090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                }
43986090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            } else {
44086090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                Log.i(LOG_TAG, "Waiting for error message");
44186090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                assertNotNull(getUiDevice().wait(Until.findObject(
44286090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                        By.text("This printer isn't available right now.")), OPERATION_TIMEOUT));
44386090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            }
44486090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann
44586090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            Log.i(LOG_TAG, "Waiting for 1st printer to be tracked");
44686090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            synchronized (trackedPrinters) {
44786090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                waitWithTimeout(() -> !trackedPrinters.contains("1st printer"),
44886090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann                        trackedPrinters::wait);
44986090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann            }
45086090a11b57a495d0be0fbe9de23681e9e4e9107Philip P. Moltmann        }
45159b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
45259b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        getUiDevice().pressBack();
45359b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann
45459b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        // We are back in the test activity
45559b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        Log.i(LOG_TAG, "Waiting for session to end");
45659b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        synchronized (session) {
45759b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann            waitWithTimeout(() -> session[0] != null, session::wait);
45859b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann        }
45959b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann    }
46059b0fbd1048521fed9180d592b689c4aa3d81a97Philip P. Moltmann}
461