1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.print; 18 19 import static org.junit.Assert.assertNotNull; 20 import static org.junit.Assume.assumeTrue; 21 22 import android.app.Activity; 23 import android.content.Intent; 24 import android.content.pm.PackageManager; 25 import android.net.Uri; 26 import android.os.Bundle; 27 import android.os.CancellationSignal; 28 import android.os.ParcelFileDescriptor; 29 import android.print.PageRange; 30 import android.print.PrintAttributes; 31 import android.print.PrintDocumentAdapter; 32 import android.print.PrintDocumentInfo; 33 import android.print.PrintJob; 34 import android.print.PrintManager; 35 import android.support.annotation.NonNull; 36 import android.support.test.InstrumentationRegistry; 37 import android.support.test.filters.LargeTest; 38 import android.support.test.rule.ActivityTestRule; 39 import android.support.test.runner.AndroidJUnit4; 40 import android.support.test.uiautomator.By; 41 import android.support.test.uiautomator.UiDevice; 42 import android.support.test.uiautomator.UiObject2; 43 import android.support.test.uiautomator.Until; 44 import android.util.Log; 45 46 import com.android.settings.Settings; 47 48 import org.junit.Before; 49 import org.junit.Rule; 50 import org.junit.Test; 51 import org.junit.runner.RunWith; 52 53 import java.io.FileInputStream; 54 import java.io.IOException; 55 import java.util.UUID; 56 57 @RunWith(AndroidJUnit4.class) 58 public class PrintJobSettingsActivityTest { 59 private static final String EXTRA_PRINT_JOB_ID = "EXTRA_PRINT_JOB_ID"; 60 private static final String LOG_TAG = PrintJobSettingsActivityTest.class.getSimpleName(); 61 62 // Any activity is fine 63 @Rule 64 public final ActivityTestRule<Settings.PrintSettingsActivity> mActivityRule = 65 new ActivityTestRule<>(Settings.PrintSettingsActivity.class, true); 66 runShellCommand(@onNull String cmd)67 public static void runShellCommand(@NonNull String cmd) throws IOException { 68 ParcelFileDescriptor stdOut = 69 InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand( 70 cmd); 71 72 try (FileInputStream fis = new ParcelFileDescriptor.AutoCloseInputStream(stdOut)) { 73 byte[] buf = new byte[512]; 74 while (fis.read(buf) != -1) { 75 // keep reading 76 } 77 } 78 } 79 80 @Before requirePrintFeature()81 public void requirePrintFeature() { 82 assumeTrue(InstrumentationRegistry.getTargetContext().getPackageManager().hasSystemFeature( 83 PackageManager.FEATURE_PRINTING)); 84 } 85 86 @Before wakeUpScreen()87 public void wakeUpScreen() throws Exception { 88 runShellCommand("input keyevent KEYCODE_WAKEUP"); 89 } 90 91 @Test 92 @LargeTest viewPrintJobSettings()93 public void viewPrintJobSettings() throws Exception { 94 UUID uuid = UUID.randomUUID(); 95 Object isWriteCalled = new Object(); 96 97 // Create adapter that is good enough to start a print preview 98 PrintDocumentAdapter adapter = new PrintDocumentAdapter() { 99 @Override 100 public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, 101 CancellationSignal cancellationSignal, 102 LayoutResultCallback callback, Bundle extras) { 103 callback.onLayoutFinished(new PrintDocumentInfo.Builder(uuid.toString()).build(), 104 true); 105 } 106 107 @Override 108 public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, 109 CancellationSignal cancellationSignal, 110 WriteResultCallback callback) { 111 synchronized (isWriteCalled) { 112 isWriteCalled.notify(); 113 } 114 callback.onWriteFailed(null); 115 } 116 }; 117 118 Activity activity = mActivityRule.getActivity(); 119 PrintManager pm = mActivityRule.getActivity().getSystemService(PrintManager.class); 120 121 // Start printing 122 PrintJob printJob = pm.print(uuid.toString(), adapter, null); 123 124 // Wait until print preview is up 125 synchronized (isWriteCalled) { 126 isWriteCalled.wait(); 127 } 128 129 // Start print job settings 130 Intent intent = new Intent(android.provider.Settings.ACTION_PRINT_SETTINGS); 131 intent.putExtra(EXTRA_PRINT_JOB_ID, printJob.getId().flattenToString()); 132 intent.setData(Uri.fromParts("printjob", printJob.getId().flattenToString(), null)); 133 activity.startActivity(intent); 134 135 UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 136 UiObject2 printPrefTitle = uiDevice.wait(Until.findObject(By.text("Configuring " 137 + uuid.toString())), 5000); 138 assertNotNull(printPrefTitle); 139 140 Log.i(LOG_TAG, "Found " + printPrefTitle.getText()); 141 } 142 } 143