From b85ab7e9799c9557123ab74052388c70579e6761 Mon Sep 17 00:00:00 2001 From: "Philip P. Moltmann" Date: Tue, 20 Mar 2018 09:00:30 -0700 Subject: [PATCH] Recover print job ID from intent extras If the fragment was started via ACTION_PRINT_SETTINGS the intent extra does not get transformed into a fragment argument. Hence need to check the intent extra. Fixes: 74456525 Test: Opened print job settings from a print notification, atest com.android.settings.print.PrintJobSettingsActivityTest Change-Id: I6c4605309c348456a862c4d7b48a11400b62fb70 --- res/values/strings.xml | 3 + .../print/PrintJobSettingsFragment.java | 20 ++- .../print/PrintJobSettingsActivityTest.java | 142 ++++++++++++++++++ 3 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 tests/unit/src/com/android/settings/print/PrintJobSettingsActivityTest.java diff --git a/res/values/strings.xml b/res/values/strings.xml index 01daa6892bb..33d9ed163ef 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -4699,6 +4699,9 @@ %1$s\n%2$s + + Configuring %1$s + Printing %1$s diff --git a/src/com/android/settings/print/PrintJobSettingsFragment.java b/src/com/android/settings/print/PrintJobSettingsFragment.java index 7142e4967d2..c16642a60e5 100644 --- a/src/com/android/settings/print/PrintJobSettingsFragment.java +++ b/src/com/android/settings/print/PrintJobSettingsFragment.java @@ -28,6 +28,7 @@ import android.print.PrintManager.PrintJobStateChangeListener; import android.support.v7.preference.Preference; import android.text.TextUtils; import android.text.format.DateUtils; +import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -45,6 +46,8 @@ import java.text.DateFormat; * Fragment for management of a print job. */ public class PrintJobSettingsFragment extends SettingsPreferenceFragment { + private static final String LOG_TAG = PrintJobSettingsFragment.class.getSimpleName(); + private static final int MENU_ITEM_ID_CANCEL = 1; private static final int MENU_ITEM_ID_RESTART = 2; @@ -164,10 +167,17 @@ public class PrintJobSettingsFragment extends SettingsPreferenceFragment { private void processArguments() { String printJobId = getArguments().getString(EXTRA_PRINT_JOB_ID); if (printJobId == null) { - finish(); - } else { - mPrintJobId = PrintJobId.unflattenFromString(printJobId); + printJobId = getIntent().getStringExtra(EXTRA_PRINT_JOB_ID); + + if (printJobId == null) { + Log.w(LOG_TAG, EXTRA_PRINT_JOB_ID + " not set"); + finish(); + return; + } } + + + mPrintJobId = PrintJobId.unflattenFromString(printJobId); } private PrintJob getPrintJob() { @@ -190,6 +200,10 @@ public class PrintJobSettingsFragment extends SettingsPreferenceFragment { PrintJobInfo info = printJob.getInfo(); switch (info.getState()) { + case PrintJobInfo.STATE_CREATED: { + mPrintJobPreference.setTitle(getString( + R.string.print_configuring_state_title_template, info.getLabel())); + } break; case PrintJobInfo.STATE_QUEUED: case PrintJobInfo.STATE_STARTED: { if (!printJob.getInfo().isCancelling()) { diff --git a/tests/unit/src/com/android/settings/print/PrintJobSettingsActivityTest.java b/tests/unit/src/com/android/settings/print/PrintJobSettingsActivityTest.java new file mode 100644 index 00000000000..da351e74c0e --- /dev/null +++ b/tests/unit/src/com/android/settings/print/PrintJobSettingsActivityTest.java @@ -0,0 +1,142 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.print; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assume.assumeTrue; + +import android.app.Activity; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.net.Uri; +import android.os.Bundle; +import android.os.CancellationSignal; +import android.os.ParcelFileDescriptor; +import android.print.PageRange; +import android.print.PrintAttributes; +import android.print.PrintDocumentAdapter; +import android.print.PrintDocumentInfo; +import android.print.PrintJob; +import android.print.PrintManager; +import android.support.annotation.NonNull; +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.LargeTest; +import android.support.test.rule.ActivityTestRule; +import android.support.test.runner.AndroidJUnit4; +import android.support.test.uiautomator.By; +import android.support.test.uiautomator.UiDevice; +import android.support.test.uiautomator.UiObject2; +import android.support.test.uiautomator.Until; +import android.util.Log; + +import com.android.settings.Settings; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.io.FileInputStream; +import java.io.IOException; +import java.util.UUID; + +@RunWith(AndroidJUnit4.class) +public class PrintJobSettingsActivityTest { + private static final String EXTRA_PRINT_JOB_ID = "EXTRA_PRINT_JOB_ID"; + private static final String LOG_TAG = PrintJobSettingsActivityTest.class.getSimpleName(); + + // Any activity is fine + @Rule + public final ActivityTestRule mActivityRule = + new ActivityTestRule<>(Settings.PrintSettingsActivity.class, true); + + public static void runShellCommand(@NonNull String cmd) throws IOException { + ParcelFileDescriptor stdOut = + InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand( + cmd); + + try (FileInputStream fis = new ParcelFileDescriptor.AutoCloseInputStream(stdOut)) { + byte[] buf = new byte[512]; + while (fis.read(buf) != -1) { + // keep reading + } + } + } + + @Before + public void requirePrintFeature() { + assumeTrue(InstrumentationRegistry.getTargetContext().getPackageManager().hasSystemFeature( + PackageManager.FEATURE_PRINTING)); + } + + @Before + public void wakeUpScreen() throws Exception { + runShellCommand("input keyevent KEYCODE_WAKEUP"); + } + + @Test + @LargeTest + public void viewPrintJobSettings() throws Exception { + UUID uuid = UUID.randomUUID(); + Object isWriteCalled = new Object(); + + // Create adapter that is good enough to start a print preview + PrintDocumentAdapter adapter = new PrintDocumentAdapter() { + @Override + public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, + CancellationSignal cancellationSignal, + LayoutResultCallback callback, Bundle extras) { + callback.onLayoutFinished(new PrintDocumentInfo.Builder(uuid.toString()).build(), + true); + } + + @Override + public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, + CancellationSignal cancellationSignal, + WriteResultCallback callback) { + synchronized (isWriteCalled) { + isWriteCalled.notify(); + } + callback.onWriteFailed(null); + } + }; + + Activity activity = mActivityRule.getActivity(); + PrintManager pm = mActivityRule.getActivity().getSystemService(PrintManager.class); + + // Start printing + PrintJob printJob = pm.print(uuid.toString(), adapter, null); + + // Wait until print preview is up + synchronized (isWriteCalled) { + isWriteCalled.wait(); + } + + // Start print job settings + Intent intent = new Intent(android.provider.Settings.ACTION_PRINT_SETTINGS); + intent.putExtra(EXTRA_PRINT_JOB_ID, printJob.getId().flattenToString()); + intent.setData(Uri.fromParts("printjob", printJob.getId().flattenToString(), null)); + activity.startActivity(intent); + + UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); + UiObject2 printPrefTitle = uiDevice.wait(Until.findObject(By.text("Configuring " + + uuid.toString())), 5000); + assertNotNull(printPrefTitle); + + Log.i(LOG_TAG, "Found " + printPrefTitle.getText()); + } +}